/
build.js
873 lines (802 loc) · 33.1 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
// Copyright (c) 2018 Gordon Williams, Pur3 Ltd. See the file LICENSE for copying permission.
// Generate keywords.js, and convert Markdown into HTML
// needs marked + jsdoc + tern + acorn
console.log("Espruino documentation builder");
console.log("--------------------------------");
var OFFLINE = true;
var PROCESS_THUMBNAILS = true; // create thumbnails
var PROCESS_INFERENCE = true; // type inference
var PROCESS_IMAGE_RESIZE = true; // resize images
for (var i=2;i<process.argv.length;i++) {
var a = process.argv[i];
if (a=="--website") OFFLINE = false;
else if (a=="--no-thumbs") PROCESS_THUMBNAILS = false;
else if (a=="--no-infer") PROCESS_INFERENCE = false;
else if (a=="--no-resize") PROCESS_IMAGE_RESIZE = false;
else {
console.log("Usage:");
console.log(" build.js # Build for offline viewing");
console.log(" build.js --website # Build to be used on Espruino website");
console.log("");
console.log(" --no-thumbs Skip creating thumbnails");
console.log(" --no-infer Skip type inference on code (for reference links)");
console.log(" --no-resize Skip image resizing");
process.exit(1);
}
}
var fs = require('fs');
var path = require('path');
if (fs.existsSync==undefined) fs.existsSync = path.existsSync;
var common = require("./common.js");
var child_process = require('child_process');
var BASEDIR = path.resolve(__dirname, "..");
var HTML_DIR = BASEDIR+"/html/";
var IMAGE_DIR = "refimages/";
var THUMB_WIDTH = 180;
var THUMB_HEIGHT = 140;
var ESPRUINO_DIR = path.resolve(BASEDIR, "../Espruino");
var FUNCTION_KEYWORD_FILE = path.resolve(BASEDIR, "../Espruino/function_keywords.js");
var KEYWORD_JS_FILE = path.resolve(HTML_DIR, "keywords.js");
var warnings = [];
function WARNING(s) {
console.log("WARNING: "+s);
warnings.push(s);
}
var marked = require('marked');
// Set default options except highlight which has no default
var markedOptions = {
gfm: true, // github markdown
tables: true,
breaks: false,
pedantic: false,
sanitize: false,
smartLists: true,
smartypants: false,
langPrefix: 'lang-'
};
if (OFFLINE) {
var hljs;
try {
hljs = require('highlight.js')
markedOptions.highlight = function (code) {
return hljs.highlightAuto(code).value;
};
} catch(e) {
console.log("======================================================");
console.log(" highlight.js not installed - not syntax highlighting");
console.log("======================================================");
var d = Date.now()+2000;
while (Date.now()<d);
}
}
marked.setOptions(markedOptions);
// Work out file ordering
var fileOrdering = {};
if (fs.existsSync(BASEDIR+"/ordering.txt")) {
fs.readFileSync(BASEDIR+"/ordering.txt").toString().split("\n").map(function(line, linenumber) {
line = line.substr(line.indexOf(" ")+1);
fileOrdering[line] = linenumber;
});
} else {
WARNING("No 'ordering.txt' - not ordering tutorials");
console.log(' - Generate with: git ls-tree -r --name-only HEAD | xargs -I{} git log -1 --format="%at {}" -- {} | sort > ordering.txt');
}
// Get list of files that have been renamed
var renamedFiles = {};
if (fs.existsSync(BASEDIR+"/RENAMED.csv")) {
fs.readFileSync(BASEDIR+"/RENAMED.csv").toString().split("\n").map(function(line) {
if (line[0]=="#") return;
var csv = line.trim().split(",");
if (csv=="") return;
if (csv.length==2) {
renamedFiles[csv[0]] = csv[1];
} else WARNING("RENAMED.csv: Badly formatted line "+JSON.stringify(line));
});
} else {
console.log("ERROR: No 'RENAMED.csv'");
process.exit(1);
}
if (OFFLINE) {
// if offline, copy the offline.css file in
fs.createReadStream(path.resolve(BASEDIR, "bin/offline.css")).pipe(
fs.createWriteStream(path.resolve(HTML_DIR, "offline.css")));
}
function createSafeFilename(filename) {
filename = filename.replace(/\//g,"_");
filename = filename.replace(/\+/g,"_");
filename = filename.replace(/ /g,"_");
return filename;
}
function convertHTML(str) {
var entityPairs = [
{character: '&', html: '&'},
{character: '<', html: '<'},
{character: '>', html: '>'},
{character: "'", html: '''},
{character: '"', html: '"'},
];
entityPairs.forEach(function(pair){
var reg = new RegExp(pair.character, 'g');
str = str.replace(reg, pair.html);
});
return str;
}
var fileDates = {};
// MD files
var markdownFiles = common.getMarkdown(BASEDIR).map(function (file) {
var p = path.relative(BASEDIR, file);
fileDates[p] = new Date(fs.statSync(p).ctime).getTime();
return p;
});
// JS files
var exampleDir = path.resolve(BASEDIR,"examples");
var exampleFiles = fs.readdirSync(exampleDir).filter(function(file) {
return file.substr(-3) == ".js";
}).map(function(file) {
var p = path.relative(BASEDIR, path.resolve(exampleDir,file));
fileDates[p] = new Date(fs.statSync(p).ctime).getTime();
return p;
});
var preloadedFiles = {};
var fileTitles = {};
// Create thumbnail images
var markdownThumbs = {};
if (PROCESS_THUMBNAILS) markdownFiles.concat(exampleFiles).forEach(function(filename) {
//console.log(filename);
var baseName = filename.slice(0,-3);
var sourceImage;
if (fs.existsSync(baseName+".thumb.png"))
sourceImage = baseName+".thumb.png";
else if (fs.existsSync(baseName+".thumb.jpg"))
sourceImage = baseName+".thumb.jpg";
else if (fs.existsSync(baseName+".thumb.svg"))
sourceImage = baseName+".thumb.svg";
else if (fs.existsSync(baseName+".png"))
sourceImage = baseName+".png";
else if (fs.existsSync(baseName+".jpg"))
sourceImage = baseName+".jpg";
if (!sourceImage) {
var contents = fs.readFileSync(filename).toString();
var match = contents.match(/!\[[^\]]*\]\(([^\)]*)\)/);
if (match)
sourceImage = filename.substr(0,filename.lastIndexOf("/")+1)+match[1];
}
if (sourceImage) {
var dstImage = IMAGE_DIR+createSafeFilename(baseName)+"_thumb.png";
var radius = 6;
var roundcorners = `\\( +clone -crop ${radius}x${radius}+0+0 -fill white -colorize 100% -draw 'fill black circle ${radius-1},${radius-1} ${radius-1},0' -background White -alpha shape \\( +clone -flip \\) \\( +clone -flop \\) \\( +clone -flip \\) \\) -flatten`;
var cmd = `convert "${sourceImage}" -resize ${THUMB_WIDTH}x${THUMB_HEIGHT} +repage ${roundcorners} -gravity South -extent ${THUMB_WIDTH}x -strip -define png:include-chunk=none "${path.resolve(HTML_DIR, dstImage)}"`;
//console.log(cmd);
child_process.exec(cmd);
console.log("THUMBNAIL "+filename+" --> "+dstImage);
markdownThumbs[filename] = dstImage;
} else {
console.log("NO THUMBNAIL FOR "+filename);
}
});
// Add 'unknown' thumbnail
{
let dstImage = IMAGE_DIR+"no_thumb.png";
child_process.exec(`convert files/blank_thumb.png -resize ${THUMB_WIDTH}x${THUMB_HEIGHT} +repage -gravity Center -extent ${THUMB_WIDTH}x${THUMB_HEIGHT} -strip -define png:include-chunk=none "${path.resolve(HTML_DIR, dstImage)}"`);
markdownThumbs[""] = dstImage;
}
function addToList(keywords, k, fileInfo) {
k = k.toLowerCase();
//console.log(k);
if (keywords[k] != undefined) {
var contains = false;
for (i in keywords[k])
if (keywords[k][i]["title"]==fileInfo["title"])
return true;
if (!contains)
keywords[k].push(fileInfo);
} else {
keywords[k] = [fileInfo];
}
}
function grabInfo(markdownFiles, preloadedFiles) {
var keywords = {};
var parts = {};
if (fs.existsSync(FUNCTION_KEYWORD_FILE))
keywords = JSON.parse(fs.readFileSync(FUNCTION_KEYWORD_FILE));
markdownFiles.forEach(function (file) {
// get file info
var contents = preloadedFiles[file] ? preloadedFiles[file] : fs.readFileSync(BASEDIR+"/"+file).toString();
// console.log(file,contents.length);
var contentLines = contents.split("\n");
if (!contentLines[0].match(/<!--+ Copyright/)) WARNING(file+": No copyright line");
if (contentLines[1].trim()=="" || contentLines[2].substr(0,3)!="===") WARNING(file+": No title on the first line");
var fileInfo = {
path : file,
title : contentLines[1], // second line
};
fileTitles[fileInfo.path] = fileInfo.title;
// add keyword for directory
file.split("/").forEach(function (k) {
if (k.indexOf(".")>0) k = k.substr(0,k.indexOf(".")); // remove file extension
addToList(keywords, k, fileInfo);
});
// add keywords in file
var match;
match = contents.match(/\n\* KEYWORDS: (.*)/);
if (match!=null) {
match[1].split(",").forEach(function(k) {
addToList(keywords, k, fileInfo);
});
}
match = contents.match(/\n\* USES: (.*)/);
if (match!=null) {
match[1].split(",").forEach(function(k) {
addToList(parts, k, fileInfo);
});
}
});
// sort keywords
for (keyword in keywords)
keywords[keyword].sort(function(a,b){ return (a.title == b.title)?0:(a.title > b.title ? 1 : -1); });
return {keywords:keywords, parts:parts};
}
function grabWebsiteKeywords(keywords) {
common.getFiles(path.resolve(BASEDIR,"../espruinowebsite/cms")).forEach(function(f) {
if (f.substr(-5) != ".html") return;
var fileName = f.substring(f.lastIndexOf("/")+1,f.length-5);
var fileInfo = {
path : "/"+fileName,
title : fileName.replace(/\+/g, " ")
};
addToList(keywords, fileName.replace("+"," "), fileInfo); // add filename
console.log("Checking Website file "+fileName);
var contents = fs.readFileSync(f).toString();
var match = contents.match(/\* KEYWORDS: (.*)/);
if (match!=null) {
console.log(" found keywords "+match[1]);
match[1].split(",").forEach(function(k) {
addToList(keywords, k, fileInfo);
});
}
});
}
// Create a keywords structure that can be used for searching the website
function createKeywordsJS(keywords) {
var kw = {};
for (keyword in keywords) {
var keywordPages = keywords[keyword];
var kwd = [];
kw[keyword] = kwd;
for (idx in keywordPages) {
var data = keywordPages[idx];
var path = data["path"].split("#")[0];
var hash = data["path"].split("#")[1];
hash = hash?"#"+hash:"";
var f;
if (path.startsWith("http:") || path.startsWith("https:")) {
f = path;
} else {
f = (path.substr(0,1)=="/") ? path : htmlLinks[path];
if (f==undefined) {
WARNING("No file info for "+data["path"]);
process.exit(1);
}
}
kwd.push({ title : data["title"],
file : f+hash });
}
}
return kw;
}
function handleImages(file, contents) {
var directory = file;
if (directory.lastIndexOf("/")>0)
directory = directory.substr(0, directory.lastIndexOf("/"));
var tagStart = contents.indexOf("![");
while (tagStart>=0) {
var tagMid = contents.indexOf("](", tagStart);
var tagEnd = contents.indexOf(")", tagMid);
if (tagMid>=0 && tagEnd>=0) {
// we've found a tag - do stuff
var imageName = contents.substring(tagMid+2, tagEnd);
if (imageName.substr(0,5)!="data:") {
var imagePath = directory+"/"+imageName;
if (fs.existsSync(imagePath)) {
/* console.log("IMAGE -----------------------------");
console.log(imageName);
console.log(imagePath);*/
console.log(imageName);
var newPath = IMAGE_DIR + createSafeFilename(/*htmlLinks[file]+"_"+*/imageName);
var finalImagePath = path.resolve(HTML_DIR, newPath);
//console.log("Copying "+imagePath+" to "+finalImagePath);
// copy gifs - so we don't break anything on optimised animations
if (imagePath.substr(-4)==".gif" || !PROCESS_IMAGE_RESIZE)
fs.createReadStream(imagePath).pipe(fs.createWriteStream(finalImagePath));
else
child_process.exec(`convert "${imagePath}" -resize "600x800>" +repage -strip -define png:include-chunk=none "${finalImagePath}"`);
// now rename the image in the tag
contents = contents.substr(0,tagMid+2)+newPath+contents.substr(tagEnd);
} else {
WARNING(file+": Image '"+imagePath+"' does not exist ("+tagStart+","+tagMid+","+tagEnd+")");
}
}
}
tagStart = contents.indexOf("![", tagStart+1);
}
return contents;
}
// Now handle our simple 'example' files
exampleFiles.forEach(function(exampleFile) {
console.log("Example File "+exampleFile);
var contents = fs.readFileSync(exampleFile).toString();
var slashStar = contents.indexOf("/*");
var starSlash = contents.indexOf("*/",slashStar);
if (slashStar>=0 && starSlash>=0) {
var markdown = contents.substr(slashStar+2, starSlash-(3+slashStar)).trim();
var newFile = "";
if (markdown.indexOf("Copyright")<0)
newFile += "<!--- Copyright (c) 2018 Gordon Williams, Pur3 Ltd. See the file LICENSE for copying permission. -->\n";
// newFile += exampleFile+"\n";
// newFile += "====================================\n";
// newFile += "\n";
newFile += markdown + "\n";
newFile += "\n\n";
newFile += "Source Code\n";
newFile += "-----------\n\n";
newFile += "```\n";
newFile += contents.substr(starSlash+2).trim()+"\n";
newFile += "```\n";
// add file
markdownFiles.push(exampleFile);
preloadedFiles[exampleFile] = newFile;
} else WARNING(exampleFile+": no comment block at the start");
});
//console.log(markdownFiles);
var fileInfo = grabInfo(markdownFiles, preloadedFiles);
//console.log(fileInfo.keywords);
grabWebsiteKeywords(fileInfo.keywords);
htmlFiles = {}; // filename -> file on disk with full path (for copy/etc)
htmlLinks = {}; // filename -> actual URL link
knownPageLinks = []; // pages that we know exist on this site
// Go through all referenced files on the main Espruino site
// TODO: in offline mode we should detect these links and go direct
"Order Search Reference Download binaries".split(" ").forEach(function(file) {
var htmlLink = file + (OFFLINE?".html":"");
knownPageLinks.push((OFFLINE?"":"/")+htmlLink);
});
// Add JS files to our known files list
["devices","modules","boards"].forEach(function(moduledir) {
fs.readdirSync(path.resolve(BASEDIR,moduledir)).filter(function(file) {
return file.substr(-3) == ".js";
}).forEach(function(file) {
knownPageLinks.push("/modules/"+file);
});
});
// Go through all Markdown files
markdownFiles.forEach(function (file) {
var htmlFile = file.substring(file.lastIndexOf("/")+1);
htmlFile = htmlFile.replace(/ /g,"+");
htmlFile = htmlFile.substring(0,htmlFile.lastIndexOf("."));
htmlFiles[file] = HTML_DIR+htmlFile+".html";
var htmlLink = htmlFile + (OFFLINE?".html":"");
htmlLinks[file] = htmlLink;
knownPageLinks.push((OFFLINE?"":"/")+htmlLink);
});
//console.log(JSON.stringify(knownPageLinks,null,2));process.exit(1);
// For files that have been renamed, create placeholder files that redirect
Object.keys(renamedFiles).forEach(function(originalFile) {
var newFile = renamedFiles[originalFile];
var htmlFile = HTML_DIR+originalFile.replace(/ /g,"+")+".html";
var html = `<!---REDIRECT ${newFile}--->
<p>This page has moved to <a href="${newFile}">${newFile}</a></p>
<script>window.location = "${newFile}";</script>`;
fs.writeFileSync(htmlFile, html);
});
// ---------------------------------------------- Inference code
/* Works out which of Espruino's built-in commands are used in which
files */
//Load tern inference thingybob
var infer = require("tern/lib/infer");
var defs = [JSON.parse(fs.readFileSync(path.resolve(BASEDIR, "bin/espruino.json")))];
// Our list of Reference URLs that are used
var urls = {};
function inferFile(filename, fileContents, baseLineNumber) {
function addLink(url, node) {
if (!(url in urls)) urls[url] = [];
var lineNumber = baseLineNumber + require("acorn").getLineInfo(fileContents, node.start).line;
var link = "/"+htmlLinks[filename]+"#line=";
var found = false;
for (var i in urls[url]) {
if (urls[url][i].url.indexOf(link)==0) {
var lineNumbers = urls[url][i].url.substr(link.length).split(",");
if (lineNumbers.indexOf(lineNumber)<0)
urls[url][i].url += ","+lineNumber;
found = true;
}
}
if (!found)
urls[url].push({ url : link+lineNumber, title : fileTitles[filename]});
}
var cx;
try {
cx = new infer.Context(defs, null);
} catch (e) {
console.log(`ERROR: Tern parse of bin/espruino.json failed`);
console.log(e);
process.exit(1);
}
infer.withContext(cx, function() {
//console.log(JSON.stringify(filename));
var ast = infer.parse(fileContents, {}, {});
infer.analyze(ast, "file:"+filename /* just an id */);
// find all calls
require("acorn-walk").simple(ast, { "CallExpression" : function(n) {
var expr = infer.findExpressionAt(n.callee);
var type;
try {
type = infer.expressionType(expr);
} catch (e) {
WARNING(filename+": TERN ERRORED ON FILE, "+e.toString());
return;
}
// Try and handle 'require(...).foo) - this doesn't work for
// var fs = require("fs") though.
if (n.callee.type=="MemberExpression" &&
n.callee.object.type=="CallExpression" &&
n.callee.object.callee.type=="Identifier" &&
n.callee.object.callee.name=="require" &&
n.callee.object.arguments.length==1 &&
n.callee.object.arguments[0].type=="Literal") {
var lib = n.callee.object.arguments[0].value;
var name = n.callee.property.name;
if (defs[0][lib] && defs[0][lib][name] && defs[0][lib][name]["!url"]) {
// console.log(">>>>>>>>>>>>>>>"+lib+":"+name);
// console.log(defs[0][lib][name], defs[0][lib][name]["!url"]);
addLink(defs[0][lib][name]["!url"], n);
}
}
// search prototype chain of type to try and find our call
if (type.forward && type.types) {
var name = type.forward[0].propName;
type.types.forEach(function(t) {
var def = undefined;
while (t && !def) {
if (t.props && name in t.props)
def = t.props[name];
t = t.proto;
}
if (def) type = def;
});
}
if (type && type.types && type.types.length>0) {
var url = type.types[0].url;
if (url) addLink(url, n);
}
}});
});
}
function inferMarkdownFile(filename, fileContents) {
var baseLineNumber = 0;
var codeBlocks = fileContents.match( /```[^`]+```|[ \n\t]`[^`]+`/g );
if (codeBlocks)
codeBlocks.forEach(function(code) {
if (code.indexOf("```\n")==0 || code.indexOf("```JavaScript\n")==0) {
code = code.slice(code.indexOf("\n")+1,-3);
//inferFile(filename, code, baseLineNumber);
} else {
//console.log("Ignoring code block because first line is "+JSON.stringify(code.split("\n")[0]));
}
// increment line counter
// ... multi-line code has ``` at the end which takes up a line
var lines = code.split("\n").length;
if (lines>1) lines--;
//console.log(code, "--->",lines);
baseLineNumber += lines;
});
}
function getThumbLinkHTML(a) {
var thumb = (a.path in markdownThumbs) ? markdownThumbs[a.path] : markdownThumbs[""];
return `<a class="thumblink" href="${htmlLinks[a.path]}" title="${convertHTML(a.title)}"><img src="${thumb}" alt="${convertHTML(a.title)}" title="${convertHTML(a.title)}"></img><span>${convertHTML(a.title)}</span></a>`
}
// -------------------------------------------------------------
markdownFiles.forEach(function (file) {
var contents = preloadedFiles[file] ? preloadedFiles[file] : fs.readFileSync(file).toString();
if (file.substr(-3)==".md") {
var contentLines = contents.split("\n");
if (contentLines[3].trim()!="" || contentLines[4].trim().substr(0,6)!="<span " || contentLines[5].trim()!="") {
console.log("=============");
console.log(JSON.stringify(contentLines[3]));
console.log(JSON.stringify(contentLines[4]));
console.log(JSON.stringify(contentLines[5]));
console.log("=============");
throw new Error("Expecting to find warning comment in "+file+", but didn't.");
}
contentLines.splice(4,2); // remove comment line
contents = contentLines.join("\n");
}
//console.log(file,contents.length);
// Check over images... ![Image Title](foo.png)
contents = handleImages(file, contents);
// replace simple links
contents = contents.replace(/\[\[http[s]?:\/\/youtu.be\/([a-zA-Z0-9\-_ ]+)\]\]/g,
'<iframe allowfullscreen="" frameborder="0" height="450" src="https://www.youtube.com/embed/$1" width="800"></iframe>'); // youtube
contents = contents.replace(/\[\[([a-zA-Z0-9_\- ]+).js\]\]/g,"[$1](/modules/$1.js) ([About Modules](/Modules))");
contents = contents.replace(/\[\[([a-zA-Z0-9_\- ]+)\]\]/g,"[$1](/$1)");
contents = contents.replace(/!\[\]\((Font[a-zA-Z0-9_]+\.js)\)/g, function(match, filename) {
console.log("Font Preview: "+JSON.stringify(filename));
var js = "";
if (filename.startsWith("Font") && filename.endsWith(".js")) {
var fontName = filename.slice(4,-3);
js = '<br /> `require("Font'+fontName+'").add(Graphics);` <br /> `g.setFont("'+fontName+'");`';
}
return "!["+filename+"]("+require("./fontpreview.js").getFontPreview("../modules/"+filename)+")"+js;
});
for (var i=0;i<3;i++) // cope with multiple spaces in links (nasty!)
contents = contents.replace(/(\[.+\]\([^) ]+) ([^)]+\))/g,"$1+$2"); // spaces in links
// Hacks for 'broken' markdown parsing
contents = contents.replace(/\n\n```([^\n]+)```\n\n/g,"\n\n```\n$1\n```\n\n"); // turn in-line code on its own into separate paragraph
contents = contents.replace(/```([^ \n][^\n]+)```/g,"``` $1 ```"); // need spaces after ```
// Hide keywords
contents = contents.replace(/\n(\* KEYWORDS: .*)/g, "<!---\n$1\n--->");
contents = contents.replace(/\n(\* USES: .*)/g, "<!---\n$1\n--->");
// TODO - 'Tutorial 2' -> 'Tutorial+2', recognize pages that are references in docs themselves
var contentLines = contents.split("\n");
var appendMatching = function(kwName, infoList, ifNone) {
var regex = new RegExp("<ul>\n<li>"+kwName+": (.*)<\/li>\n<\/ul>");
var match = regex.exec(html,"g");
while (match!=null) {
var kws = match[1].toLowerCase().split(",").map(function(x){return x.trim();});
var kw = kws[0];
var links = [ ];
//console.log("============================== appendMatching "+kwName,kws);
if (infoList[kw]!=undefined) {
// deep copy
var pages = {};
// add keywords
for (var k=0;k<kws.length;k++) {
if (kws[k][0]!="-" && infoList[kws[k]]!=undefined) {
for (var attr in infoList[kws[k]]) {
let page = infoList[kws[k]][attr];
pages[page.path] = page; // stop duplicates
}
}
}
// remove any keywords
for (j in pages) {
var a = pages[j];
if (a["path"]!=file && htmlLinks[a.path]!=undefined) { // if we don't have links it is probably in the reference
var pageOk = true;
// if extra keywords specified, they may be to reject certain pages... check
for (var k=1;k<kws.length;k++) {
if (kws[k][0]=="-") {
var notkw = kws[k].substr(1);
if (infoList[notkw]!=undefined)
for (var notpg in infoList[notkw])
if (infoList[notkw][notpg]["path"]==a.path) {
console.log("REJECTED "+a.path+" from "+file+" because of '-"+notkw+"' keyword");
pageOk = false;
}
}
}
// add page link if ok
if (pageOk) {
console.log(a);
links.push(a);
}
}
}
}
var content = "";
if (links.length>0) {
var contentThumbs = [];
// Put ones with thumbnails first, otherwise base on fileOrdering
links = links.sort(function(a,b) {
var d = ((b.path in markdownThumbs)?1:0) - ((a.path in markdownThumbs)?1:0);
if (d) return d;
var at = 0|fileOrdering[a.path];
var bt = 0|fileOrdering[b.path];
return bt-at;
/*if (a.title > b.title) return 1;
if (a.title < b.title) return -1;
return 0;*/
});
// Output images
links.forEach(function(a) {
contentThumbs.push(getThumbLinkHTML(a));
});
content = '<div class="thumblinklist thumblinklist-thumbnails">\n '+contentThumbs.join("\n ")+'\n</div>';
} else {
WARNING(file+": "+kwName+" for '"+kw+"' found nothing");
content = ifNone;
}
html = html.substr(0,match.index)+content+html.substr(match.index+match[0].length);
match = regex.exec(html);
}
};
for (i in contentLines) {
var match;
// try and handle module documentation
match = contentLines[i].match(/^\* APPEND_JSDOC: (.*)/);
if (match!=null) {
var jsfilename = file.substr(0, file.lastIndexOf("/")+1) + match[1];
var js = fs.readFileSync(jsfilename).toString();
console.log("APPEND_JSDOC "+jsfilename);
var doc = common.getJSDocumentation(js);
/* setting the language to Java lets it be highlighted correctly
while not adding the 'send to Espruino' icon */
contentLines[i] = "```Java\n"+doc+"```\n";
}
// handle buy links
match = contentLines[i].match(/^\* BUYFROM:(.*)/);
if (match!=null) {
var info = match[1].trim().split(",");
var html = `<div style="float:right;z-index:-1" class="panel panel-info" >
<div class="panel-heading"><h4 style="text-align:center;margin:0px;">Buy Now</h4></div>
<div class="panel-body center-block">
<h1 style="margin-top:0px"><a name="buy"></a><small>From</small> ${info[0]}</h1>
`;
if (info[1]) html += `<p><small>Or ${info[1]} in volume</small></p>`;
for (var j=2;j<info.length;j++) {
var link = info[j].split("|");
var name = link[0].includes("shop.espruino.com") ? "Espruino Shop" : "🌎 Distributors";
var btnType = link[0].includes("shop.espruino.com") ? "btn-primary" : "btn-default";
if (link[1]) name += " ("+link[1]+")";
html += ` <a role="button" class="btn ${btnType}" style="width:100%" href="${link[0]}">${name}</a>${(j<info.length-1)?"<br/>":""}\n`;
}
html += ` </div>
</div>`;
contentLines[i] = html;
}
}
// Check for LIST_LINKS_AS_KEYWORDS and add any links in lists `* [...](...)
var listLinksAsKeywordsIdx = contentLines.indexOf("* LIST_LINKS_AS_KEYWORDS");
if (listLinksAsKeywordsIdx>=0) {
contentLines.splice(listLinksAsKeywordsIdx,1); // remove first line (copyright)
for (i in contentLines) {
var match;
match = contentLines[i].match(/^\* \[(.*)\]\((.*)\)$/);
if (match!=null) {
var description = match[1];
var link = match[2];
var pageInfo = {
path : link,
title : description,
};
fileTitles[pageInfo.path] = pageInfo.title;
htmlLinks[link] = link;
addToList(fileInfo.keywords, description, pageInfo);
}
}
}
contentLines.splice(0,1); // remove first line (copyright)
var title = contentLines[0];
contents = contentLines.join("\n");
// ========================================================================== Get Markdown
if (PROCESS_INFERENCE) inferMarkdownFile(file, contents);
html = marked(contents).replace(/lang-JavaScript/g, 'sh_javascript');
// ==========================================================================
// Append tutorial links
appendMatching("APPEND_KEYWORD", fileInfo.keywords, "");
appendMatching("APPEND_USES", fileInfo.parts, "<p>(No tutorials are available yet)</p>");
// Check for Pinouts
var regex = /<ul>\n<li>APPEND_PINOUT: (.*)<\/li>\n<\/ul>/;
var match = html.match(regex);
if (match!=null) {
var htmlfilename = HTML_DIR+"boards/" + match[1] + ".html";
console.log("APPEND_PINOUT "+htmlfilename);
var pinout = fs.readFileSync(htmlfilename).toString();
html = html.replace(regex, pinout);
}
// Check for TOC and create it if needed
var tocTag = "<li>APPEND_TOC</li>";
var tocIdx = html.indexOf(tocTag);
if (tocIdx>=0) {
// parse out all headings, and create lists based on them
regex = /<([hH])([123]) id="([a-z\-]*)"[^>]*>/g;
var toc = "";
var currentLevel = 1;
var m = regex.exec(html);
while (m) {
if (m.index > tocIdx) { // only add headings from AFTER this TOC tag
var heading = {level:0|m[2], hash:m[3]};
var endIdx = html.indexOf("</"+m[1]+m[2], m.index);
heading.title = html.substring(m.index+m[0].length, endIdx);
heading.title = heading.title.replace(/<\/?a[^>]*>/g,""); // remove anchors
while (currentLevel<heading.level) { toc+="<ul>";currentLevel++; }
while (currentLevel>heading.level) { toc+="</ul>";currentLevel--; }
toc += '<li><a href="#'+heading.hash+'">'+heading.title+"</a></li>\n";
// add keywords
var pageInfo = {
path : file+"#"+heading.hash,
title : title+": "+heading.title,
};
fileTitles[pageInfo.path] = pageInfo.title;
addToList(fileInfo.keywords, heading.title, pageInfo);
}
m = regex.exec(html);
}
while (currentLevel>1) { toc+="</ul>";currentLevel--; }
html = html.substr(0,tocIdx) + toc + html.substr(tocIdx+tocTag.length);
}
// If compiling for offline, quick hack to modify most links to go direct
if (OFFLINE) // FIXME - move this into the code below, do it properly with knownPageLinks
html = html.replace(/(<[aA] .*href=")\/([^"#]*)/g, '$1$2.html');
// Scan all links in HTML to find possible replaced files
{
regex = /href="([^"]+)"/g;
var m = regex.exec(html);
while (m) {
var url = m[1];
var baseURL = url.replace(/#.*$/,"");
if (baseURL!="") {
if (baseURL in renamedFiles) {
WARNING(`${file}: ${JSON.stringify(baseURL)} was renamed to ${JSON.stringify(renamedFiles[baseURL])}`);
} else {
if (baseURL.startsWith("http:") || baseURL.startsWith("https:")) {
// we're ok...
} else { // TODO: check if we know the page link
/*if (knownPageLinks.indexOf(baseURL)<0)
WARNING(`${file}: ${JSON.stringify(baseURL)} is not found`);*/
}
}
}
m = regex.exec(html);
}
}
// work out of we have any images that might be at the top of the page
var hasImageContent = html.indexOf("<iframe ")>=0; // do we have a video?
// do we have an image in the first few lines of markdown?
for (var i=0;i<15 && i<contentLines.length;i++)
if (contentLines[i].match(/!\[[^\]]*\]\(([^\)]*)\)/)) // find an image tag
hasImageContent = true;
// if there's no image/video then we add the thumbnail in the top-right
var thumbnail = "";
if (!hasImageContent && file in markdownThumbs)
thumbnail = `<img class="topthumbnail" src="${markdownThumbs[file]}">`;
// GitHub link
var github_url = "https://github.com/espruino/EspruinoDocs/blob/master/"+path.relative(BASEDIR, file);
// assemble final page
html = '<div style="min-height:700px;">' +
thumbnail +
html + '</div>'+
'<p style="text-align:right;font-size:75%;">This page is auto-generated from <a href="'+github_url+'">GitHub</a>. If you see any mistakes or have suggestions, please <a href="https://github.com/espruino/EspruinoDocs/issues/new?title='+file+'">let us know</a>.</p>';
if (OFFLINE) {
html = `<html><head>
<title>${title}</title>
<link rel="stylesheet" href="offline.css" type="text/css" />
</head>
<body style="background-image:none;">
<div id="wrap">${html}</div>
</body>
</html>
`;
}
fs.writeFileSync(htmlFiles[file], html);
});
// -----------------------------------------------------------
// write out the references
var refPath = path.resolve(BASEDIR, "references.json");
console.log("---------------------");
console.log("Writing references to "+refPath);
fs.writeFileSync(refPath, JSON.stringify(urls,null,1));
// Write out keywords
fs.writeFileSync(KEYWORD_JS_FILE, "var keywords = "+JSON.stringify(createKeywordsJS(fileInfo.keywords),null,1)+";");
console.log("====================================================== WARNINGS");
console.log("====================================================== WARNINGS");
console.log("====================================================== WARNINGS");
warnings.forEach(w=>console.log(w));
console.log("====================================================== WARNINGS");
console.log("====================================================== WARNINGS");
console.log("====================================================== WARNINGS");
// -----------------------------------------------------------
// Newest tutorials
/*child_process.exec(
'git log --name-status | grep "^A" | cut -c3- | grep -e ".*\.md$" -e "examples/.*\.js$" | grep -v "^boards" | head -20',
function(error, stdout, stderr){
var newTutorials = stdout.split("\n").map(x=>x.trim());
newTutorials.forEach(tutorial=>{
var p = path.relative(BASEDIR, tutorial);
if (!p) return;
var thumb = getThumbLinkHTML({
path: p,
title: fileTitles[p]
});
console.log(thumb);
});
});*/