-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.js
1180 lines (1033 loc) · 37.4 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint no-undef: "error" */
/*
This entire file is one giant hack and really needs to be cleaned up!
*/
'use strict';
const requiredNodeVersion = 16;
if (parseInt((/^v(\d+)\./).exec(process.version)[1]) < requiredNodeVersion) {
throw Error(`requires at least node: ${requiredNodeVersion}`);
}
let fetch = function() {
// we'll replace this later, below.
debugger; // eslint-disable-line no-debugger
};
function slugify(s) {
return s
.toLowerCase()
.trim()
.replace(/<[!/a-z].*?>/ig, '')
.replace(/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g, '')
.replace(/\s/g, '-');
}
module.exports = function(settings) { // wrapper in case we're in module_context mode
const hackyProcessSelectFiles = settings.filenames !== undefined;
const cache = new (require('inmemfilecache'))();
const Feed = require('feed').Feed;
const fs = settings.fs || require('fs');
const Handlebars = require('handlebars');
const hanson = require('hanson');
const md = require('markdown-it')({
html: true,
langPrefix: 'lang-',
typographer: true,
quotes: '“”‘’',
})
.use(require('markdown-it-anchor'), {
tabIndex: false,
slugify,
})
.use(require('markdown-it-footnote'));
const path = require('path');
const sitemap = require('sitemap');
const utils = require('./lib/utils');
const url = require('url');
const colors = require('ansi-colors');
const colorSupport = require('color-support');
const sizeOfImage = require('image-size');
const ThumbnailGenerator = require('./thumbnail');
const {
extractHandlebars,
insertHandlebars,
} = require('./lib/extractors.js');
const {
dateFromGitLog,
} = require('./lib/git.js');
const g_cacheid = Date.now();
const packageJSON = JSON.parse(fs.readFileSync('package.json', {encoding: 'utf8'}));
const {URL} = url;
colors.enabled = colorSupport.hasBasic;
const g_errors = [];
function error(...args) {
g_errors.push([...args].join(' '));
console.error(colors.red(...args)); // eslint-disable-line no-console
}
const g_warnings = [];
function warn(...args) {
g_warnings.push([...args].join(' '));
console.warn(colors.yellow(...args)); // eslint-disable-line no-console
}
function log(...args) {
console.log(...args); // eslint-disable-line no-console
}
let numErrors = 0;
function failError(...args) {
++numErrors;
error(...args);
}
function applyObject(src, dst) {
Object.keys(src).forEach(function(key) {
dst[key] = src[key];
});
return dst;
}
function mergeObjects() {
const merged = {};
Array.prototype.slice.call(arguments).forEach(function(src) {
applyObject(src, merged);
});
return merged;
}
function readFile(fileName) {
try {
return cache.readFileSync(fileName, 'utf-8');
} catch (e) {
error('could not read:', fileName);
throw e;
}
}
function readHANSON(fileName) {
const text = readFile(fileName);
try {
return hanson.parse(text);
} catch (e) {
throw new Error(`can not parse: ${fileName}: ${e}`);
}
}
function writeFileIfChanged(fileName, content) {
if (fs.existsSync(fileName)) {
const old = readFile(fileName);
if (content === old) {
return;
}
}
const dirname = path.dirname(fileName);
if (!fs.existsSync(dirname)) {
fs.mkdirSync(dirname, {recursive: true});
}
fs.writeFileSync(fileName, content);
console.log('Wrote: ' + fileName); // eslint-disable-line
}
function copyFile(src, dst) {
writeFileIfChanged(dst, readFile(src));
}
function replaceParams(str, params) {
const template = Handlebars.compile(str);
if (Array.isArray(params)) {
params = mergeObjects.apply(null, params.slice().reverse());
}
return template(params);
}
function encodeParams(params) {
const values = Object.values(params).filter(v => v);
if (!values.length) {
return '';
}
return '&' + Object.entries(params).map((kv) => {
return `${encodeURIComponent(kv[0])}=${encodeURIComponent(kv[1])}`;
}).join('&');
}
function encodeQuery(query) {
if (!query) {
return '';
}
return '?' + query.split('&').map(function(pair) {
return pair.split('=').map(function(kv) {
return encodeURIComponent(decodeURIComponent(kv));
}).join('=');
}).join('&');
}
function encodeUrl(src) {
const u = url.parse(src);
u.search = encodeQuery(u.query);
return url.format(u);
}
function TemplateManager() {
const templates = {};
this.apply = function(filename, params) {
let template = templates[filename];
if (!template) {
template = Handlebars.compile(readFile(filename));
templates[filename] = template;
}
if (Array.isArray(params)) {
params = mergeObjects.apply(null, params.slice().reverse());
}
return template(params);
};
this.applyString = function(string, params) {
let template = templates[string];
if (!template) {
template = Handlebars.compile(string);
templates[string] = template;
}
if (Array.isArray(params)) {
params = mergeObjects.apply(null, params.slice().reverse());
}
return template(params);
};
}
const templateManager = new TemplateManager();
Handlebars.registerHelper('include', function(filename, options) {
let context;
if (options && options.hash && options.hash.filename) {
const varName = options.hash.filename;
filename = options.data.root[varName];
if (filename === undefined) {
throw new Error(`no value for key "${varName}" in include macro`);
}
context = {...options.data.root, ...options.hash};
} else {
context = options.data.root;
}
return templateManager.apply(filename, context);
});
Handlebars.registerHelper('ifexists', function(options) {
const filename = path.join(process.cwd(), replaceParams(options.hash.filename, options.hash));
const exists = fs.existsSync(filename);
if (exists) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
Handlebars.registerHelper('example', function(options) {
options.hash.width = options.hash.width ? 'width: ' + options.hash.width + 'px;' : '';
options.hash.height = options.hash.height ? 'height: ' + options.hash.height + 'px;' : '';
options.hash.caption = options.hash.caption || options.data.root.defaultExampleCaption;
options.hash.examplePath = options.data.root.examplePath;
options.hash.encodedUrl = encodeURIComponent(encodeUrl(options.hash.url));
options.hash.url = encodeUrl(options.hash.url);
options.hash.cacheid = g_cacheid;
options.hash.params = encodeParams({
startPane: options.hash.startPane,
});
return templateManager.apply(path.join(settings.templatePath, 'example.template'), options.hash);
});
function getTranslation(root, msgId) {
const lang = root.lang;
const translations = root.translations;
const origTranslations = root.origLangInfo.translations;
let str = translations[msgId];
if (!str) {
warn(`missing ${lang} translation for msgId: ${msgId}`);
str = origTranslations[msgId];
translations[msgId] = str;
}
return str;
}
Handlebars.registerHelper('warning', function(options) {
const root = options.data.root;
const link = options.hash.link || `${root.packageJSON.homepage}}/blob/master/{$root.contentFileName}`;
const translation = getTranslation(root, options.hash.msgId);
const msg = templateManager.applyString(translation, {...root, link});
const data = {
...root,
msg,
};
const str = templateManager.apply(path.join(settings.templatePath, 'warning.template'), data);
return str;
});
function getProperty(obj, propSpec) {
const parts = propSpec.split('.');
let data = obj;
for (const part of parts) {
if (!Object.prototype.hasOwnProperty.call(data, part)) {
return undefined;
}
data = data[part];
}
return data;
}
function getPropertyOr(obj, propSpecs) {
const specs = propSpecs.split(',');
for (const spec of specs) {
const value = getProperty(obj, spec);
if (value !== undefined) {
return value;
}
}
return undefined;
}
// lets you insert build time data but stringified
// const foo = {{{stringify names="langInfo"}}};
// const title = {{{stringify names="langInfo.title"}}};
// const title = {{{stringify names="langInfo.title,originalLangInfo.title"}}};
Handlebars.registerHelper('stringify', function(options) {
const data = getPropertyOr(options.data.root, options.hash.names);
if (data === undefined) {
throw new Error(`no property '${options.hash.names}' for {{{stringify}}}`);
}
return JSON.stringify(data, null, 2);
});
/*
This won't work because handlebars is going to try to parse
the content. For example
{{{escapeHTML content="<div class="foo"></div>"}}}
Won't work because of the nested quotes and having to manually
escape the quotes defeats the purpose.
*/
Handlebars.registerHelper('escapehtml', function(options) {
return Handlebars.escapeExpression(options.fn(this));
});
Handlebars.registerHelper('diagram', function(options) {
options.hash.width = options.hash.width || '400';
options.hash.height = options.hash.height || '300';
options.hash.examplePath = options.data.root.examplePath;
options.hash.className = options.hash.className || '';
options.hash.url = encodeUrl(options.hash.url);
return templateManager.apply(path.join(settings.templatePath, 'diagram.template'), options.hash);
});
Handlebars.registerHelper('image', function(options) {
options.hash.examplePath = options.data.root.examplePath;
options.hash.className = options.hash.className || '';
options.hash.caption = options.hash.caption || undefined;
if (options.hash.url.substring(0, 4) === 'http') {
options.hash.examplePath = '';
}
return templateManager.apply(path.join(settings.templatePath, 'image.template'), options.hash);
});
Handlebars.registerHelper('selected', function(options) {
const key = options.hash.key;
const value = options.hash.value;
const re = options.hash.re;
const sub = options.hash.sub;
const a = this[key];
let b = options.data.root[value];
if (re) {
const r = new RegExp(re);
b = b.replace(r, sub);
}
return a === b ? 'selected' : '';
});
function slashify(s) {
return s.replace(/\\/g, '/');
}
function articleFilter(f) {
if (hackyProcessSelectFiles) {
if (!settings.filenames.has(f)) {
return false;
}
}
return !process.env['ARTICLE_FILTER'] || f.indexOf(process.env['ARTICLE_FILTER']) >= 0;
}
const readDirs = function(dirPath) {
const dirsOnly = function(filename) {
const stat = fs.statSync(filename);
return stat.isDirectory();
};
const addPath = function(filename) {
return path.join(dirPath, filename);
};
return fs.readdirSync(`${settings.rootFolder}/lessons`)
.map(addPath)
.filter(dirsOnly);
};
const isLangFolder = function(dirname) {
const filename = path.join(dirname, 'langinfo.hanson');
return fs.existsSync(filename);
};
const pathToLang = function(filename) {
const lang = path.basename(filename);
const lessonBase = `${settings.rootFolder}/lessons`;
const lessons = `${lessonBase}/${lang}`;
return {
lang,
toc: `${settings.rootFolder}/lessons/${lang}/toc.html`,
lessons: `${lessonBase}/${lang}`,
template: path.join(settings.templatePath, 'lesson.template'),
examplePath: `/${lessonBase}/`,
home: `/${lessons}/`,
};
};
let g_langs = [
// English is special (sorry it's where I started)
{
template: path.join(settings.templatePath, 'lesson.template'),
lessons: `${settings.rootFolder}/lessons`,
lang: 'en',
toc: `${settings.rootFolder}/lessons/toc.html`,
examplePath: `/${settings.rootFolder}/lessons/`,
home: '/',
},
];
g_langs = g_langs.concat(readDirs(`${settings.rootFolder}/lessons`)
.filter(isLangFolder)
.map(pathToLang));
const extractHeader = (function() {
const headerRE = /([A-Z0-9_-]+): (.*?)$/i;
return function(content) {
const metaData = { };
const lines = content.split('\n');
for (;;) {
const line = lines[0].trim();
const m = headerRE.exec(line);
if (!m) {
break;
}
metaData[m[1].toLowerCase()] = m[2];
lines.shift();
}
return {
content: lines.join('\n'),
headers: metaData,
};
};
}());
const parseMD = function(content) {
return extractHeader(content);
};
const loadMD = function(contentFileName) {
const content = cache.readFileSync(contentFileName, 'utf-8');
const data = parseMD(content);
data.link = contentFileName.replace(/\\/g, '/').replace(/\.md$/, '.html');
return data;
};
function loadFiles(filenames) {
const byFilename = {};
filenames.forEach((fileName) => {
const data = loadMD(fileName);
byFilename[path.basename(fileName)] = data;
});
return byFilename;
}
const Builder = function(outBaseDir, options) {
const g_articlesByLang = {};
let g_articles = [];
let g_langInfo;
let g_originalLangInfo;
let g_thumbailGen;
const g_langDB = {};
const g_outBaseDir = outBaseDir;
const g_origPath = options.origPath;
const toc = readHANSON(settings.tocHanson || 'toc.hanson');
const g_siteThumbnailFilename = path.join(settings.rootFolder, 'lessons', 'resources', settings.thumbnailOptions.thumbnailBackground || settings.siteThumbnail);
const g_siteThumbnail = {
url: `${settings.baseUrl}/${settings.rootFolder}/lessons/resources/${settings.siteThumbnail}`,
size: sizeOfImage(g_siteThumbnailFilename),
};
// These are the english articles.
const g_allOriginalArticlesFullPath = utils.glob(path.join(g_origPath, '*.md'))
.filter(a => !a.endsWith('index.md'));
const g_origArticles = g_allOriginalArticlesFullPath
.map(a => path.basename(a))
.filter(articleFilter);
const g_originalByFileName = loadFiles(g_allOriginalArticlesFullPath);
function isSameDomain(url, pageUrl) {
const fdq1 = new URL(pageUrl);
const fdq2 = new URL(url, pageUrl);
return fdq1.origin === fdq2.origin;
}
function getUrlPath(url) {
// yes, this is a hack
const q = url.indexOf('?');
return q >= 0 ? url.substring(0, q) : url;
}
function isRelative(url) {
return !url.startsWith('#') &&
!url.startsWith('/') &&
!url.includes('://');
}
function isArticleLink(url) {
return !url.includes('/') && url.endsWith('.html');
}
// Try top fix relative links. This *should* only
// happen in translations
const iframeLinkRE = /(<iframe[\s\S]*?\s+src=")(.*?)(")/g;
const imgLinkRE = /(<img[\s\S]*?\s+src=")(.*?)(")/g;
const videoLinkRE = /(<video[\s\S]*?\s+src=")(.*?)(")/g;
const aLinkRE = /(<a[^>]*?\s+href=")(.*?)(")/g;
const mdLinkRE = /(\[[\s\S]*?\]\()(.*?)(\))/g;
const handlebarLinkRE = /({{{.*?\s+url=")(.*?)(")/g;
const scriptLinkRE = /(<script[\s\S]*?\s+src=")(.*?)(")/g;
const linkRE = /(<link[\s\S]*?\s+href=")(.*?)(")/g;
const linkREs = [
iframeLinkRE,
imgLinkRE,
videoLinkRE,
aLinkRE,
mdLinkRE,
handlebarLinkRE,
scriptLinkRE,
linkRE,
];
function hackRelLinks(content, pageUrl/*, contentFileName*/) {
//const basedir = path.dirname(contentFileName);
// console.log('---> pageUrl:', pageUrl);
function fixRelLink(m, prefix, url, suffix) {
if (isSameDomain(url, pageUrl) && isRelative(url) && !isArticleLink(url)) {
// a link that starts with "../" should be "../../" if it's in a translation
// a link that starts with "resources" should be "../resources" if it's in a translation
//const testName = path.join(basedir, url);
//if (!fs.existsSync(testName)) {
// this needs to be fixed! It should just do all local links?
// console.log(' url:', url);
return `${prefix}../${url}${suffix}`;
//}
}
return m;
}
return content
.replace(imgLinkRE, fixRelLink)
.replace(videoLinkRE, fixRelLink)
.replace(aLinkRE, fixRelLink)
.replace(iframeLinkRE, fixRelLink)
.replace(scriptLinkRE, fixRelLink)
.replace(linkRE, fixRelLink);
}
/**
* Get all the local urls based on a regex that has <prefix><url><suffix>
*/
function getUrls(regex, str) {
const links = new Set();
let m;
do {
m = regex.exec(str);
if (m && m[2][0] !== '#' && isSameDomain(m[2], 'http://example.com/a/b/c/d')) {
links.add(getUrlPath(m[2]));
}
} while (m);
return links;
}
/**
* Get all the local links in content
*/
function getLinks(content) {
return new Set(linkREs.map(re => [...getUrls(re, content)]).flat());
}
function fixUrls(regex, content, origLinks) {
return content.replace(regex, (m, prefix, url, suffix) => {
const q = url.indexOf('?');
const urlPath = q >= 0 ? url.substring(0, q) : url;
const urlQuery = q >= 0 ? url.substring(q) : '';
if (!origLinks.has(urlPath) &&
isSameDomain(urlPath, 'https://foo.com/a/b/c/d.html') &&
!(/\/..\/^/.test(urlPath)) && // hacky test for link to main page. Example /webgl/lessons/ja/
urlPath[0] !== '#') { // test for same page anchor -- bad test :(
for (const origLink of origLinks) {
if (urlPath.endsWith(origLink)) {
const newUrl = `${origLink}${urlQuery}`;
log(' fixing:', url, 'to', newUrl);
return `${prefix}${newUrl}${suffix}`;
}
}
failError('could not fix:', url);
}
return m;
});
}
const showContent = process.env['SHOW_CONTENT']
? (label, content) => console.log(`======================= [ ${label} ] ==============================================\n${content}`)
: _ => _;
const applyTemplateToContent = async function(templatePath, contentFileName, outFileName, opt_extra, data) {
// Call prep's Content which parses the HTML. This helps us find missing tags
// should probably call something else.
//Convert(md_content)
const relativeOutName = slashify(outFileName).substring(g_outBaseDir.length);
const pageUrl = `${settings.baseUrl}${relativeOutName}`;
const metaData = data.headers;
const content = data.content;
//console.log(JSON.stringify(metaData, undefined, ' '));
showContent('orig', content);
const {handlebars, content: noHandlebarsContent} = extractHandlebars(content);
showContent('no handlebars', noHandlebarsContent);
let html = md.render(noHandlebarsContent);
showContent('HTML', html);
// HACK! :-(
// There's probably a way to do this in marked
html = html.replace(/<pre><code/g, '<pre class="prettyprint notranslate" translate="no"><code');
html = html.replace(/<code>/g, '<code class="notranslate" translate="no">');
showContent('with snippets', html);
// HACK! :-(
if (opt_extra && opt_extra.home && opt_extra.home.length > 1) {
html = hackRelLinks(html, pageUrl, contentFileName);
}
html = insertHandlebars(handlebars, html);
showContent('with handlebars', html);
html = replaceParams(html, [
opt_extra,
g_langInfo,
{
origLangInfo: g_originalLangInfo,
contentFileName,
packageJSON,
},
]);
const pathRE = new RegExp(`^\\/${settings.rootFolder}\\/lessons\\/$`);
const langs = Object.keys(g_langDB).map((name) => {
const lang = g_langDB[name];
const url = slashify(path.join(lang.basePath, path.basename(outFileName)))
.replace('index.html', '')
.replace(pathRE, '/');
return {
lang: lang.lang,
language: lang.language,
url: url,
};
});
metaData['content'] = html;
metaData['langs'] = langs;
metaData['src_file_name'] = slashify(contentFileName);
metaData['dst_file_name'] = relativeOutName;
metaData['basedir'] = '';
metaData['toc'] = opt_extra.toc;
metaData['tocHtml'] = g_langInfo.tocHtml;
metaData['templateOptions'] = opt_extra.templateOptions;
metaData['langInfo'] = g_langInfo;
metaData['originalLangInfo'] = g_originalLangInfo;
metaData['url'] = pageUrl;
metaData['settings'] = settings;
metaData['relUrl'] = relativeOutName;
metaData['screenshot'] = opt_extra.screenshot || g_siteThumbnail.url;
metaData['screenshotSize'] = opt_extra.screenshotSize || g_siteThumbnail.size;
const basename = path.basename(contentFileName, '.md');
for (const ext of ['.jpg', '.png']) {
const filename = path.join(settings.rootFolder, 'lessons', 'screenshots', basename + ext);
if (fs.existsSync(filename)) {
metaData['screenshot'] = `${settings.baseUrl}/${settings.rootFolder}/lessons/screenshots/${basename}${ext}`;
const size = sizeOfImage(filename);
metaData['screenshotSize'] = size;
break;
}
}
let output = templateManager.apply(templatePath, metaData);
if (settings.postHTMLFn) {
output = settings.postHTMLFn(output);
}
writeFileIfChanged(outFileName, output);
return metaData;
};
const applyTemplateToFile = async function(templatePath, contentFileName, outFileName, opt_extra) {
console.log('processing: ', contentFileName); // eslint-disable-line
opt_extra = opt_extra || {};
const data = loadMD(contentFileName);
const metaData = await applyTemplateToContent(templatePath, contentFileName, outFileName, opt_extra, data);
g_articles.push(metaData);
};
const applyTemplateToFiles = async function(templatePath, filesSpec, extra) {
const allFiles = utils.glob(filesSpec).sort();
const files = allFiles.filter(articleFilter);
const byFilename = loadFiles(allFiles);
function getLocalizedCategory(category) {
const localizedCategory = g_langInfo.categoryMapping[category];
if (localizedCategory) {
return localizedCategory;
}
warn(`no localization for category: ${category} in langinfo.hanson file for ${extra.lang}`);
const categoryName = g_originalLangInfo.categoryMapping[category];
if (!categoryName) {
throw new Error(`no English mapping for category: ${category} in langinfo.hanson file for english`);
}
return categoryName;
}
function addLangToLink(link) {
return extra.lang === 'en'
? link
: `${path.dirname(link)}/${extra.lang}/${path.basename(link)}`;
}
function tocLink(fileName) {
let data = byFilename[fileName];
let link;
if (data) {
link = data.headers.link || data.link;
} else {
data = g_originalByFileName[fileName];
if (!data) {
throw new Error(`original ${fileName} from 'toc.hanson' does not exist`);
}
link = data.headers.link || addLangToLink(data.link);
}
const toc = data.headers.toc || data.headers.title;
if (toc === '#') {
return [...data.content.matchAll(/<a\s*id="(.*?)"\s*data-toc="(.*?)"\s*><\/a>/g)].map(([, id, title]) => {
const hashLink = `${link}#${id}`;
return `<li><a href="/${hashLink}">${title}</a></li>`;
}).join('\n');
}
return `<li><a href="/${link}">${toc}</a></li>`;
}
/*
{
'section1': [
'article1.md',
'article2.md',
],
'section2': [
'article3.md',
{
subSection: [
'article4.md',
],
},
],
},
*/
function makeToc(toc) {
function makeTocImpl(entry) {
if (Array.isArray(entry)) {
return entry.map(makeTocImpl).join('\n');
} else if (typeof entry === 'object') {
return Object.entries(entry).map(([category, subEntry]) => ` <li>${getLocalizedCategory(category)}</li>
<ul>
${makeTocImpl(subEntry)}
</ul>`).join('\n');
} else {
return tocLink(entry);
}
}
return makeTocImpl(toc);
}
g_langInfo.tocHtml = `<ul>${makeToc(toc)}</ul>`;
g_langInfo.carousel = JSON.stringify([...g_langInfo.tocHtml.matchAll(/<a href="(.*?)"/g)]
.filter(m => !m[1].includes('#'))
.map((m, ndx) => {
return {
'@type': 'ListItem',
'position': ndx + 1,
'url': `${settings.baseUrl}${m[1]}`,
};
}), null, 2);
for (const fileName of files) {
const ext = path.extname(fileName);
if (!byFilename[path.basename(fileName)]) {
if (!hackyProcessSelectFiles) {
throw new Error(`${fileName} is not in toc.hanson`);
}
warn(fileName, 'is not in toc.hanson');
}
const baseName = fileName.substr(0, fileName.length - ext.length);
const outFileName = path.join(outBaseDir, baseName + '.html');
{
const data = loadMD(fileName);
settings.thumbnailOptions.text[0].text = data.headers.toc || data.headers.title;
const thumb = await g_thumbailGen.generate({
backgroundFilename: g_siteThumbnailFilename,
...settings.thumbnailOptions,
});
const basename = path.basename(baseName);
const filename = path.join(settings.outDir, settings.rootFolder, 'lessons', 'screenshots', `${basename}_${g_langInfo.langCode}.jpg`);
const size = sizeOfImage(thumb);
extra['screenshot'] = `${settings.baseUrl}/${settings.rootFolder}/lessons/screenshots/${path.basename(filename)}`;
extra['screenshotSize'] = { width: size.width, height: size.height };
writeFileIfChanged(filename, thumb);
}
await applyTemplateToFile(templatePath, fileName, outFileName, extra);
}
};
const addArticleByLang = function(article, lang) {
const filename = path.basename(article.dst_file_name);
let articleInfo = g_articlesByLang[filename];
const url = article.link
? `${settings.baseUrl}/${article.link}`
: `${settings.baseUrl}${article.dst_file_name}`;
if (!articleInfo) {
articleInfo = {
url: url,
changefreq: 'monthly',
links: [],
};
g_articlesByLang[filename] = articleInfo;
}
articleInfo.links.push({
url: url,
lang: lang,
});
};
const getLanguageSelection = function(lang) {
const lessons = lang.lessons;
const langInfo = readHANSON(path.join(lessons, 'langinfo.hanson'));
langInfo.langCode = langInfo.langCode || lang.lang;
langInfo.baseDirname = lang.lang;
langInfo.home = lang.home;
langInfo.translations = langInfo.translations || {};
g_langDB[lang.lang] = {
lang: lang.lang,
language: langInfo.language,
basePath: '/' + lessons,
langInfo: langInfo,
};
};
this.preProcess = async function(langs) {
g_thumbailGen = new ThumbnailGenerator();
langs.forEach(getLanguageSelection);
g_originalLangInfo = g_langDB['en'].langInfo;
};
this.close = async function() {
await g_thumbailGen.close();
};
this.process = async function(options) {
console.log('Processing Lang: ' + options.lang); // eslint-disable-line
g_articles = [];
g_langInfo = g_langDB[options.lang].langInfo;
await applyTemplateToFiles(options.template, path.join(options.lessons, settings.lessonGrep), options);
const articlesFilenames = g_articles.map(a => path.basename(a.src_file_name));
function linkIsIndex(link) {
const base = `/${settings.rootFolder}/lessons/`;
const index = `${base}${options.lang}`;
const indexSlash = `${index}/`;
//console.log(index, indexSlash);
return link === index || link === indexSlash || link === base;
}
// should do this first was easier to add here
if (options.lang !== 'en') {
const existing = g_origArticles.filter(name => articlesFilenames.indexOf(name) >= 0);
existing.forEach((name) => {
//const origMdFilename = path.join(g_origPath, name);
const transMdFilename = path.join(g_origPath, options.lang, name);
const ext = path.extname(name);
const baseName = name.substr(0, name.length - ext.length);
const originHTMLFilename = path.join(outBaseDir, g_langDB.en.basePath, baseName + '.html');
const transHTMLFilename = path.join(outBaseDir, options.lessons, baseName + '.html');
const origLinks = getLinks(readFile(originHTMLFilename));
const transLinks = getLinks(fs.readFileSync(transHTMLFilename, {encoding: 'utf8'}));
if (process.env['ARTICLE_VERBOSE']) {
log('---[', transMdFilename, ']---');
log('origLinks: ---\n ', [...origLinks].join('\n '));
log('transLinks: ---\n ', [...transLinks].join('\n '));
}
let show = true;
transLinks.forEach((link) => {
link = link
.replace(`/${options.lang}/`, '/')
.replace(/^..\//, '');
if (!origLinks.has(link)) {
if (linkIsIndex(link)) {
return;
}
if (show) {
show = false;
failError('---[', transMdFilename, ']---');
}
failError(` link:[${link}] not found in English file`);
}
});
if (!show && process.env['ARTICLE_FIX']) {
// there was an error, try to auto-fix
let fixedMd = fs.readFileSync(transMdFilename, {encoding: 'utf8'});
linkREs.forEach((re) => {
fixedMd = fixUrls(re, fixedMd, origLinks);
});
fs.writeFileSync(transMdFilename, fixedMd);
}
});
}
if (hackyProcessSelectFiles) {
return;
}
// generate place holders for non-translated files
const missing = g_origArticles.filter(name => articlesFilenames.indexOf(name) < 0);
for (const name of missing) {
const ext = path.extname(name);
const baseName = name.substr(0, name.length - ext.length);
const outFileName = path.join(outBaseDir, options.lessons, baseName + '.html');
const data = {...loadMD(path.join(g_origPath, name))};
data.content = g_langInfo.missing;
const extra = {
origLink: '/' + slashify(path.join(g_origPath, baseName + '.html')),
toc: options.toc,
};
console.log(' generating missing:', outFileName); // eslint-disable-line
await applyTemplateToContent(
path.join(settings.templatePath, 'missing.template'),
path.join(options.lessons, 'langinfo.hanson'),
outFileName,
extra,
data);
}
for (const article of g_articles) {
article.dateAdded = await dateFromGitLog(fs, article.src_file_name, 'ctimeMs');
article.dateModified = await dateFromGitLog(fs, article.src_file_name, 'mtimeMs');
}
let articles = g_articles.filter(function(article) {
return article.dateAdded !== undefined;
});
articles = articles.sort(function(a, b) {
return b.dateAdded - a.dateAdded;
});
if (articles.length) {
const feed = new Feed({
title: g_langInfo.title,
description: g_langInfo.description,
link: g_langInfo.link,
image: `${settings.baseUrl}/${settings.rootFolder}/lessons/resources/${settings.siteThumbnail}`,
date: settings.feedDate || articles[0].dateModified,
published: settings.feedDate || articles[0].dateModified,
updated: settings.feedDate || articles[0].dateModified,
author: {
name: `${settings.siteName} Contributors`,
link: `${settings.baseUrl}/contributors.html`,
},
});
articles.forEach(function(article) {
feed.addItem({