-
Notifications
You must be signed in to change notification settings - Fork 0
/
maker.js
1028 lines (929 loc) · 41.1 KB
/
maker.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
//First, we get the list of all the pages we need to make, in order, in pages.list, and
//we also get all the pages that are defined inside the pages.js module, in pages.pages.
var pages = require("./pages/pages.js");
var filenameFormatter = require('./formatters/filename-formatter.js');
var codeFormatter = require('./formatters/code-formatter.js');
var fs = require("fs");
var mathFormatter = require('./formatters/math-formatter.js');
var dateFormatter = require("./formatters/date-formatter.js");
mathFormatter.start();
var verbose = (process.argv[2] == "-v") ? true : false;
var audioBoxTemplate = [
`<div class="audio-container"><div class="audio-box">
<audio>
<source src="`,
`">
</audio>
<div class="middle">
<div class="play-button-zone">
<div class="play-button">
<div class="btn">
</div>
</div>
</div>
</div>
<div class="info-and-controls">
<div class="info">
`,
`</div>
<div class="controls">
<div class="progress-bar">
</div>
<div class="progress-bar-overlay">
</div>
<div class="progress-bar-bottom">
</div>
<div class="current-progress-text">0:00
</div>
<div class="track-duration-text">9:00
</div>
</div>
</div>
</div>
</div>`
];
//-------Function Calls-----------------------------------------------------------------------------//
removeDirectory("./en");
removeDirectory("./fr");
makeFolder("./en");
makeFolder("./fr");
//second argument is the prefix
//third argument is the number of steps away from root.
//fourth argument is the parent of the page.
makeIndex(pages, "en", 0);
makeIndex(pages, "fr", 1);
makePage(seekPage("about", "./pages/"), "fr", 1, false);
makePage(seekPage("about", "./pages/"), "en", 1, false);
makeFolder("./en/projects");
makeFolder("./fr/projets");
for (var i = 0; i < pages.list.length; i++) {
let page = seekPage(pages.list[i], "./pages/");
if (!page.link && page.en.title != "About") {
makePage(page, "en", 2, "projects/", "projets/");
makePage(page, "fr", 2, "projets/", "projects/");
}
}
makeFolder("./fr/blog");
makeFolder("./en/blog");
makeBlog("fr");
makeBlog("en");
//-------Function Definitions----------------------------------------------------------------------//
function makeBlog(language) {
var stepsFromRoot = 2;
var blog = require('./blog/blog.js');
var currentPost = 0;
var postsToPrint = blog.posts.length;
//Determine the amount of pages that needs to be created...
var pagesToCreate = Math.ceil(blog.posts.length / blog.config.itemsPerPage);
var rssFeedName = (language == "fr") ? "Flux RSS" : "RSS Feed";
var rssFeedLink = `
<div id="rss-link">
<a href="https://pelletierauger.com/${language}/blog/rss.xml">${rssFeedName}</a>
</div>
`;
for (var k = 0; k < pagesToCreate; k++) {
var root;
var suffix;
if (k == 0) {
suffix = "";
root = true;
} else {
suffix = " - Page " + (k + 1);
root = false;
}
var oppositeLang = (language == "fr") ? "en" : "fr";
var page = {
fr: {
title: "page-" + (k + 1)
},
en: {
title: "page-" + (k + 1)
}
};
var parent = filenameFormatter(blog.config[language].title) + "/";
var oppositeParent = filenameFormatter(blog.config[oppositeLang].title) + "/";
var navigation = makeNavigation(page, language, stepsFromRoot, parent, oppositeParent, root);
// var blogNameLanguage = (language == "fr") ? "Blog" : "Blog";
var blogNameLanguage = "";
if (k > 0) {
// blogNameLanguage += " - page " + (k + 1);
blogNameLanguage = `
<div id="blog-page-number">
<h2>Page ${(k + 1)}</h2>
</div>`;
}
var content = `${blogNameLanguage}`;
content += `<div class="blog-posts">`;
var postsOnThisPage = Math.min(postsToPrint, blog.config.itemsPerPage);
var scripts = [];
var styles = [];
//This runs once for every blog post to build
for (var l = 0; l < postsOnThisPage; l++) {
// var post = require('./blog/posts/' + blog.posts[currentPost]);
var post = seekPage(blog.posts[currentPost], './blog/posts/');
let itemscope = "";
if (post.itemscope) {
itemscope = ` itemscope itemtype="https://schema.org/${post.itemscope}"`;
};
// console.log("How many times?");
if (post.sketch) {
// console.log("Sketch!" + post.sketch);
for (let i = 0; i < post.sketch.length; i++) {
scripts.push(post.sketch[i]);
}
}
if (post.style) {
styles.push(post.style);
}
// console.log(post.date);
var date = dateFormatter(post.date, language);
//We start to build the individual page
var blogPrefix = parent + post.date.year + "/" + post.date.month + "/";
var oppositePrefix = oppositeParent + post.date.year + "/" + post.date.month + "/";
var filenameIndividual = filenameFormatter(post[language].title);
var linkIndividual = blogPrefix + filenameIndividual;
//We create the content of a post within the blog
let title = post[language].HTMLTitle || post[language].title;
let description = (post[language].description) ? `<div class="description">
${post[language].description}
</div>` : "";
//We decide on what excerpt to display within the blog
let languageJump = (language == "fr") ? "Continuer à lire…" : "Read more…";
let blogPostContent;
if (post[language].content.match(/<!-- read-more -->/)) {
// console.log(post[language].title + " has an excerpt to be extracted!");
blogPostContent = post[language].content.match(/([\S\s]*?)(<!-- read-more -->)/)[1];
blogPostContent = `${blogPostContent}
<div class="languageJump">
<a href="../${linkIndividual}.html">${languageJump}</a>
</div>
`;
} else {
blogPostContent = (post[language].excerpt) ?
`${post[language].excerpt}
<div class="languageJump">
<a href="../${linkIndividual}.html">${languageJump}</a>
</div>
` : post[language].content;
}
let itempropURL = (post.itemscope) ? ` itemprop="url"` : "";
let itempropName = (post.itemscope) ? ` itemprop="name"` : "";
let itempropAbstract = (post.itemscope) ? `<span itemprop="abstract">` : "";
let itempropAbstractEnd = (post.itemscope) ? `</span>` : "";
content += `
<div class="blog-post"${itemscope}>
<div class="blog-post-header">
<a href="../${linkIndividual}.html"${itempropURL}><div class="date">${date}</div>
<div class="blog-title-box"><h2 class="header"${itempropName}>${title}</h2></div>
${itempropAbstract}${description}${itempropAbstractEnd}</a>
</div>
<div class="blog-post-content">
${blogPostContent}
</div>
</div>`;
currentPost++;
postsToPrint--;
//We make the directories before making the files.
makeFolder("./" + language + "/" + parent + post.date.year);
makeFolder("./" + language + "/" + parent + post.date.year + "/" + post.date.month);
//We make the file for an individual blog page
// console.log("Individual sketch before sending :" + post.sketch);
var individualHeader = makeHeader(post, language, stepsFromRoot + 2, post.sketch);
var individualNavigation = makeNavigation(post, language, stepsFromRoot + 2, blogPrefix, oppositePrefix, false);
var individualContent = individualHeader + individualNavigation;
individualContent += `
<article${itemscope}>
<h2 class="with-date"${itempropName}>${title}</h2>${itempropAbstract}${description}${itempropAbstractEnd}
<div class="date">${date}</div>
${post[language].content}
</article>
`;
individualContent += rssFeedLink;
individualContent += makeFooter(post, language);
//Change the image links for the individual blog page
var re = /src="..\/..\/..\//gi;
individualContent = individualContent.replace(re, `src="../../../../`);
makeFile(language, blogPrefix + filenameIndividual, individualContent);
}
content += `
</div>`;
// console.log("scripts before sending :" + scripts + ", " + language);
// content += `
// </div>`;
content += `
<div id = "blog-pagination">`;
var pagination = {
fr: {
prev: "Page précédente",
next: "Page suivante"
},
en: {
prev: "Previous page",
next: "Next page"
}
};
let paginationLink = "";
if (k == 0 || postsToPrint == 0) {
paginationLink = "pagination-link-single";
} else {
paginationLink = "pagination-link";
}
if (k > 0) {
var prevLink;
if (k == 1) {
prevLink = "./";
} else {
prevLink = `./page-${k}.html`;
}
content += `
<div class="${paginationLink}">
<a href="${prevLink}">
${pagination[language].prev}
</a>
</div>
`;
}
if (postsToPrint > 0) {
content += `
<div class="${paginationLink}">
<a href="./page-${k+2}.html">
${pagination[language].next}
</a>
</div>
`;
}
content += `
</div>`;
content += rssFeedLink;
page = {
fr: {
title: blog.config.fr.title + suffix,
content: content
},
en: {
title: blog.config.en.title + suffix,
content: content
},
style: styles
};
var header = makeHeader(page, language, stepsFromRoot, scripts);
var footer = makeFooter(page, language);
var fileName = (k == 0) ? "index" : "page-" + (k + 1);
makeFile(language, parent + fileName, header + navigation + content + footer);
}
}
function makePage(page, language, stepsFromRoot, parent, oppositeParent) {
parent = parent || "";
// page = require('./pages/' + page);
var fileName = filenameFormatter(page[language].title);
var header = makeHeader(page, language, stepsFromRoot, page.sketch);
var navigation = makeNavigation(page, language, stepsFromRoot, parent, oppositeParent, false);
var content = makeContent(page, language);
var footer = makeFooter(page, language);
// console.log(header + navigation + content + footer);
makeFile(language, parent + fileName, header + navigation + content + footer);
}
function makeIndex(pages, language, stepsFromRoot) {
var header = makeHeader(null, language, stepsFromRoot);
var navigation = makeNavigation({
fr: { title: "index" },
en: { title: "index" }
}, language, stepsFromRoot, null, null, true);
var mosaic = makeMosaic(pages, language, stepsFromRoot);
var footer = makeFooter(language);
// console.log(header + navigation + mosaic + footer);
// if (prefix == "./en/") {
// makeFile(null, "index", header + navigation + mosaic + footer);
// } else {
makeFile(language, "index", header + navigation + mosaic + footer);
// }
}
function makeMosaic(pages, language, stepsFromRoot) {
var innerLink = (stepsFromRoot == 0) ? "./en/projects/" : "../fr/projets/";
var navPrefix = (stepsFromRoot == 0) ? "./" : "../";
var introduction;
if (language == "fr") {
introduction = `
<p class="introduction">
<span class="drop-caps">B</span><span class="small-caps">onjour !</span>
Je suis un artiste et un cinéaste d’animation vivant à Montréal, Canada. J’expérimente avec les
mathématiques à l’aide de logiciels en code source libre.
</p>
`;
} else {
introduction = `
<p class="introduction">
<span class="drop-caps">H</span><span class="small-caps">ello !</span>
I’m an artist and animation filmmaker living in Montréal, Canada.
I create mathematical and algorithmic art using open source software.
</p>
`;
}
introduction = "";
var mosaic = `${introduction}<div class="mosaic">`;
for (var i = 0; i < pages.list.length; i++) {
let page = seekPage(pages.list[i], "./pages/");
//We exclude the "About" page from building an element in the mosaic.
if (page["en"].title == "About") {
continue;
}
let title = page[language].HTMLTitle || page[language].title;
var filename = filenameFormatter(page[language].title);
var thumbnailName = pages.list[i];
var description = page[language].description;
var link = (page[language].link || page.link || innerLink + filename + ".html");
let itemscope = "";
if (page.itemscope) {
itemscope = ` itemscope itemtype="https://schema.org/${page.itemscope}"`;
};
let itempropURL = (page.itemscope) ? ` itemprop="url"` : "";
let itempropName = (page.itemscope) ? ` itemprop="name"` : "";
let itempropThumbnail = (page.itemscope) ? ` itemprop="thumbnail"` : "";
let itempropAbstract = (page.itemscope) ? `<span itemprop="abstract">` : "";
let itempropAbstractEnd = (page.itemscope) ? `</span>` : "";
var itemDiv = `
<div class="portfolio-item"${itemscope}>
<a href="${link}"${itempropURL}>
<div class="thumbnail">
<img src="${navPrefix}images/thumbnails/${thumbnailName}.jpg"${itempropThumbnail}>
</div>
<div class="portfolio-banner">
<div class="portfolio-description">
<h2${itempropName}>${title}</h2>
${itempropAbstract}${description}${itempropAbstractEnd}
</div>
</div>
</a>
</div>
`;
mosaic = mosaic + itemDiv;
}
mosaic = mosaic + `</div>`;
// mosaic = `${mosaic}
// <div id="ornament-003">
// <img src="${navPrefix}style/ornaments/terminal-ornament-003.png">
// </div>
// `;
return mosaic;
}
function makeHeader(page, language, stepsFromRoot, sketches) {
//If its a page and it has content, test the content with this regular expression.
//If there is code embedded in the HTML content,
//add the code.css stylesheet to the head of the HTML file.
//Also, the font Inconsolata gets added to the head if there is embedded code.
var prefixToRoot = "";
for (var i = 0; i < stepsFromRoot; i++) {
prefixToRoot += "../";
}
// console.log(page);
var codeCSS = "";
var codeFont = "";
if (page && page[language].content) {
var r = /(<code>|<code class="supercollider">|<code class="glsl">|<code class="nosyntax">)([\S\s]*?)(<\/code>)/g;
var inlineCodeTest = /(<span class="inline-code8">)([\S\s]*?)(<\/span>)/g;
if (page[language].content.match(inlineCodeTest)) {
codeFont = "Inconsolata:400,700|";
}
if (page[language].content.match(r)) {
codeCSS = `<link href="${prefixToRoot}style/code.css" rel="stylesheet" type="text/css">`;
if (!codeFont) {
codeFont = "Inconsolata|";
}
}
}
//If page exists, use its title in the HTML.
var title = (page) ? " - " + page[language].title : "";
var prefix = "";
if (stepsFromRoot == 0) {
prefix = "./";
} else if (stepsFromRoot > 0) {
for (var i = 0; i < stepsFromRoot; i++) {
prefix += "../";
}
}
let style = ``;
if (page && page.style) {
for (let i = 0; i < page.style.length; i++) {
style += `
<link href="${prefix}style/${page.style[i]}.css" rel="stylesheet" type="text/css">
`;
}
}
var scripts = ``;
if (sketches && sketches.length > 0) {
scripts += `
<script src="${prefix}libraries/p5.min.js" type="text/javascript"></script>
`;
scripts += `<script src="${prefix}libraries/p5.dom.min.js" type="text/javascript"></script>
`;
for (let i = 0; i < sketches.length; i++) {
scripts += `<script src="${prefix}sketches`;
scripts += sketches[i];
scripts += `" type="text/javascript"></script>
`;
}
}
if (page !== null) {
let audioBoxes = page[language].content.match(/<div class="audio-box">/);
if (audioBoxes) {
console.log("Audio boxes were detected!");
style += `
<link href="${prefix}style/audio-box.css" rel="stylesheet" type="text/css">
`;
scripts += `<script src="${prefix}audio-boxes/audio-boxes.js" type="text/javascript"></script>
`;
}
}
// console.log(scripts);
return `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Guillaume Pelletier-Auger${title}</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=4, user-scalable=yes" />${scripts}
<link href="${prefix}style/style.css" rel="stylesheet" type="text/css">${style}
<link href="https://fonts.googleapis.com/css?family=${codeFont}EB+Garamond" rel="stylesheet">
${codeCSS}
</head>`;
}
function makeNavigation(page, language, stepsFromRoot, parent, oppositeParent, root) {
oppositeParent = oppositeParent || "";
parent = parent || "";
var prefix = "";
if (stepsFromRoot == 0) {
prefix = "./";
} else if (stepsFromRoot > 0) {
for (var i = 0; i < stepsFromRoot; i++) {
prefix += "../";
}
}
var nav1 = (language == "fr") ? "Projets" : "Projects";
var nav15 = (language == "fr") ? "Blog" : "Blog";
var nav2 = (language == "fr") ? "À propos" : "About";
var nav3 = (language == "fr") ? "Fr → En" : "En → Fr";
var liProjects;
var rPEN = /projects\//g;
var rPFR = /projets\//g;
if (page["en"].title == "index" || parent.match(rPEN) || parent.match(rPFR)) {
liProjects = `<li class = "selected">`;
} else {
liProjects = "<li>";
}
var liBlog;
var r = /blog\//g;
var r2 = /blog\//g;
if (parent.match(r) || parent.match(r2)) {
liBlog = `<li class = "selected">`;
} else {
liBlog = "<li>";
}
var liAbout = (page["en"].title == "About") ? `<li class = "selected">` : "<li>";
var oppositeLanguage = (language == "fr") ? "en" : "fr";
var navProjects = (language == "fr") ? "projets" : "projects";
var navBlog = (language == "fr") ? "blog" : "blog";
var navAbout = (language == "fr") ? "a-propos" : "about";
var navLang;
//if the file isn't not at the root of a directory, its opposite link is its opposite name
//and its opposite link ends with .html
if (root == false || root == null) {
navLang = "/";
navLang += oppositeParent;
navLang += (language == "fr") ? filenameFormatter(page["en"].title) : filenameFormatter(page["fr"].title);
navLang += ".html";
} else {
//if it is at the root of a directory, its opposite link is its oppositeParent
navLang = "/";
navLang += oppositeParent;
}
navLang = (page["en"].title == "index") ? "" : navLang;
//this is used if the oppositeLanguage page is the absolute root of the website.
//this is good. Don't change this.
if (page["en"].title == "index" && language == "en") {
navLang = "/";
}
if (page["en"].title == "index" && language == "fr") {
oppositeLanguage = "";
}
var titleLink;
if (language == "en") {
titleLink = prefix;
} else if (language == "fr") {
titleLink = prefix + "fr/";
}
return `<body>
<nav>
<h1><a href="${titleLink}">Guillaume <span class="nobreak">Pelletier-Auger</span></a></h1>
<ul>
${liProjects}<a href="${titleLink}">${nav1}</a></li>
${liBlog}<a href="${prefix}${language}/${navBlog}/">${nav15}</a></li>
${liAbout}<a href="${prefix}${language}/${navAbout}.html">${nav2}</a></li>
<li><a href="${prefix}${oppositeLanguage}${navLang}">${nav3}</a></li>
</ul>
</nav>
`;
}
function makeContent(page, language) {
if (page.en.title == "About") {
return `
<article>
${page[language].content}
</article>`;
} else {
let title = page[language].HTMLTitle || page[language].title;
let itemscope = "";
if (page.itemscope) {
itemscope = ` itemscope itemtype="https://schema.org/${page.itemscope}"`;
};
let itempropName = (page.itemscope) ? ` itemprop="name"` : "";
let itempropAbstract = (page.itemscope) ? ` itemprop="abstract"` : "";
let description = `
<div class="description"${itempropAbstract}>
${page[language].description}
</div>
` || "";
if (page[language].date) {
return `
<article${itemscope}>
<h2 class="with-date"${itempropName}>${title}</h2>${description}
<div class="date">${page[language].date}</div>
${page[language].content}
</article>`;
} else {
return `
<article${itemscope}>
<h2${itempropName}>${title}</h2>${description}
${page[language].content}
<article>`;
}
}
}
function makeFooter(language) {
return `<div id="footer">Guillaume Pelletier-Auger — 2016-2024
</div>
</body>
</html>`;
}
function makeFile(language, fileName, htmlContent, prefix) {
//First, we test if htmlContent contains code. We formatted it if it exists.
//We modify htmlContent with code-formatter, which is synchronous.
htmlContent = codeFormatter(htmlContent, fileName);
//Then, we test if htmlContent contains LaTeX math,
//whether it is displayed mathematics or in-line mathematics.
var r = /\\\[|\\\(/g;
if (htmlContent.match(r)) {
//If it does, we send the htmlContent to math-formatter.js which will write the file itself.
//(The MathJax-node module is asynchronous, so it's much simpler if it writes the file itself.)
verbalize(fileName + " contains math.");
mathFormatter.typeset(language, fileName, htmlContent, verbose);
} else {
if (prefix == null) { prefix = ""; }
if (language == "en" && fileName == "index") {
prefix = "./";
} else {
prefix = './' + language + '/' + prefix;
}
// console.log(prefix);
fs.writeFile(prefix + fileName + '.html', htmlContent, function(err) {
if (err) {
return console.error(err);
} else {
verbalize(prefix + fileName + '.html written successfully.');
}
});
}
}
function removeDirectory(dirPath) {
try {
var files = fs.readdirSync(dirPath);
} catch (e) {
return;
}
if (files.length > 0)
for (var i = 0; i < files.length; i++) {
var filePath = dirPath + '/' + files[i];
if (fs.statSync(filePath).isFile())
fs.unlinkSync(filePath);
else
removeDirectory(filePath);
}
fs.rmdirSync(dirPath);
};
function makeFolder(folder) {
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder);
verbalize("Make folder : " + folder);
}
}
function verbalize(message) {
if (verbose) {
console.log(message);
}
}
function seekPage(name, folder) {
let page;
if (folder == './blog/posts/') {
// We are seeking a blog post
// All blog posts are defined with the HTML template
if (fs.existsSync(folder + name + ".html")) {
page = parseHTMLTemplate(folder + name + ".html");
return page;
}
}
//First, we test if the page is not simply defined in the pages.js file.
if (pages.pages[name]) {
page = pages.pages[name];
//Else, we check if an .html version of the wanted page exists.
} else if (fs.existsSync(folder + name + ".html")) {
//If this condition is met, we parse the HTML file.
page = parseHTMLTemplate(folder + name + ".html");
//If this condition fails too, then we simply have a page defined in the "traditional" way.
//Inside a .js template.
} else {
page = require(folder + name);
}
return page;
}
function parseHTMLTemplate(s) {
let page = {
fr: {},
en: {}
};
var data = fs.readFileSync(s, { encoding: "utf8" });
let year = data.match(/(<!-- year -->)([\S\s]*?)(<!--)/);
if (year) {
let month = data.match(/(<!-- month -->)([\S\s]*?)(<!--)/);
let day = data.match(/(<!-- day -->)([\S\s]*?)(<!--)/);
page.date = {
year: parseInt(year[2]),
month: parseInt(month[2]),
day: parseInt(day[2])
};
// console.log(page.date);
}
let style = data.match(/(<!-- style -->)([\S\s]*?)([\n\r])/);
if (style) {
page.style = [style[2]];
}
let sketches = data.match(/(<!-- sketch -->)([\S\s]*?)(<!--)/);
if (sketches) {
let sketchesToCatch = [];
let runThroughSketches = data.replace(/(<!-- sketch -->)([\S\s]*?)([\n\r])/g, function(a, b, c) {
// console.log("A catch!!!");
sketchesToCatch.push(c);
});
page.sketch = sketchesToCatch;
// console.log(sketchesToCatch);
}
page.fr.title = data.match(/(<!-- fr-title -->)([\S\s]*?)(<!--)/)[2];
page.fr.title = page.fr.title.replace(/(?:\r\n|\r|\n)/g, "");
// page.fr.description = data.match(/(<!-- fr-description -->)([\S\s]*?)(<!--)/)[2];
let frDescription = data.match(/(<!-- fr-description -->)([\S\s]*?)(<!--)/);
if (frDescription) {
page.fr.description = frDescription[2];
page.fr.description = page.fr.description.replace(/([a-zA-ZÀ-ú])(\')([a-zA-ZÀ-ú])/g, function(a, b, c, d) {
return "" + b + "’" + d;
});
}
let itemscope = data.match(/(<!-- itemscope -->)([\S\s]*?)(<!--)/);
if (itemscope) {
page.itemscope = itemscope[2];
page.itemscope = page.itemscope.replace(/(?:\r\n|\r|\n)/g, "");
}
let frExcerpt = data.match(/(<!-- fr-excerpt -->)([\S\s]*?)(<!--)/);
if (frExcerpt) {
page.fr.excerpt = frExcerpt[2];
page.fr.excerpt = page.fr.excerpt.replace(/([a-zA-ZÀ-ú])(\')([a-zA-ZÀ-ú])/g, function(a, b, c, d) {
return "" + b + "’" + d;
});
}
page.fr.content = data.match(/(<!-- fr-content -->)([\S\s]*?)(<!-- en-)/)[2];
page.fr.content = page.fr.content.replace(/([a-zA-ZÀ-ú])(\')([a-zA-ZÀ-ú])/g, function(a, b, c, d) {
return "" + b + "’" + d;
});
// Add a non-breaking space after French opening quotes and before French closing quotes.
page.fr.content = page.fr.content.replace(/([a-zA-ZÀ-ú\.\?])(\s?)(»)/g, function(a, b, c, d) {
return "" + b + " " + d;
});
page.fr.content = page.fr.content.replace(/(«)(\s?)([a-zA-ZÀ-ú])/g, function(a, b, c, d) {
return "" + b + " " + d;
});
page.fr.content = page.fr.content.replace(/<i>/g, `<span class="italic">`);
page.fr.content = page.fr.content.replace(/<\/i>/g, `</span>`);
page.fr.content = page.fr.content.replace(/<i8>/g, `<span class="italic8">`);
page.fr.content = page.fr.content.replace(/<\/i8>/g, `</span>`);
page.fr.content = page.fr.content.replace(/<ic8>/g, `<span class="inline-code8">`);
page.fr.content = page.fr.content.replace(/<\/ic8>/g, `</span>`);
page.fr.content = page.fr.content.replace(/<dc>/g, `<span class="drop-caps">`);
page.fr.content = page.fr.content.replace(/<\/dc>/g, `</span>`);
page.fr.content = page.fr.content.replace(/<sc>/g, `<span class="small-caps">`);
page.fr.content = page.fr.content.replace(/<\/sc>/g, `</span>`);
page.fr.content = page.fr.content.replace(/<sc8>/g, `<span class="small-caps8">`);
page.fr.content = page.fr.content.replace(/<\/sc8>/g, `</span>`);
page.fr.content = page.fr.content.replace(/<math>/g, String.raw `<div class="math">\[`);
page.fr.content = page.fr.content.replace(/<\/math>/g, String.raw `\]</div>`);
page.fr.content = page.fr.content.replace(/<im>/g, String.raw `\(`);
page.fr.content = page.fr.content.replace(/<\/im>/g, String.raw `\)`);
// Put a non-breaking space between any letter followed immediately by an exclamation point
// or an interrogation mark. (There can also be a space between the letter and the punctuation.)
page.fr.content = page.fr.content.replace(/([a-zA-ZÀ-ú]+)(\s?)([\?\!])/g, function(a, b, c, d) {
// console.log(b);
if (b == "watch") {
return b + c + d;
} else {
return "" + b + " " + d;
}
});
// page.fr.content = page.fr.content.replace(/([a-zA-ZÀ-ú])(\s)([:;])/g, function(a, b, c, d) {
// return "" + b + " " + d;
// });
// Replace the snl and snr pseudo-HTML tags (used for sidenotes)
page.fr.content = page.fr.content.replace(/&nbsn;/g, String.raw `<span class="nobreak">`);
// page.fr.content = page.fr.content.replace(/(\s)(?!span)([0-9a-zA-ZÀ-ú\.<>":;&\/\?\-=]+?|<span\sclass=)(<sn[lr])/g, function(a, b, c, d) {
// console.log(page.fr.title + "--->b" + b + "--->c" + c + "--->d" + d);
// return b + '<span class="nobreak">' + c + d;
// });
page.fr.content = page.fr.content.replace(/(<sn)([lr])( label=")([0-9a-zA-ZÀ-ú\-]*)(">)/g, function(a, b, c, d, e) {
let typeOfSidenote = (c == "l") ? "sidenote-left" : "sidenote";
let response = `
<label for="${e}" class="margin-toggle sidenote-number">
</label></span>
<input type="checkbox" id="${e}" class="margin-toggle"/>
<span class="${typeOfSidenote}">`;
return response;
});
page.fr.content = page.fr.content.replace(/<\/sn[lr]>/g, `</span>`);
page.fr.content = page.fr.content.replace(/(<mn)([lr])( label=")([0-9a-zA-ZÀ-ú\-]*)(">)/g, function(a, b, c, d, e) {
let typeOfMarginnote = (c == "l") ? "marginnote-left" : "marginnote";
let response = `
<label for="${e}" class="margin-toggle">—
</label>
<input type="checkbox" id="${e}" class="margin-toggle" />
<span class="${typeOfMarginnote}">`;
return response;
});
page.fr.content = page.fr.content.replace(/<\/mn[lr]>/g, `</span>`);
page.fr.content = page.fr.content.replace(/<\/a>,/g,
String.raw `</a><span class="cleardescenders">,</span>`);
page.fr.content = page.fr.content.replace(/<li>\s\s+/g, "<li>");
let frDate = data.match(/(<!-- fr-date -->)([\S\s]*?)(<!--)/);
if (frDate) {
page.fr.date = frDate[2];
}
let frHTMLTitle = data.match(/(<!-- fr-html-title -->)([\S\s]*?)(<!--)/);
if (frHTMLTitle) {
page.fr.HTMLTitle = frHTMLTitle[2];
}
page.fr.content = page.fr.content.replace(/<code>/g, `<pre><code>`);
page.fr.content = page.fr.content.replace(/<\/code>/g, `</code></pre>`);
page.fr.content = page.fr.content.replace(/<javascript>/g, `<pre><code>`);
page.fr.content = page.fr.content.replace(/<\/javascript>/g, `</code></pre>`);
page.fr.content = page.fr.content.replace(/<glsl>/g, `<pre><code class="glsl">`);
page.fr.content = page.fr.content.replace(/<\/glsl>/g, `</code></pre>`);
page.fr.content = page.fr.content.replace(/<supercollider>/g, `<pre><code class="supercollider">`);
page.fr.content = page.fr.content.replace(/<\/supercollider>/g, `</code></pre>`);
page.fr.content = page.fr.content.replace(/<nosyntax>/g, `<pre><code class="nosyntax">`);
page.fr.content = page.fr.content.replace(/<\/nosyntax>/g, `</code></pre>`);
page.fr.content = page.fr.content.replace(/<pre>/g, `<div class="codebox"><pre>`);
page.fr.content = page.fr.content.replace(/<\/pre>/g, `</pre></div>`);
page.fr.content = page.fr.content.replace(/<ic>/g, `<span class="inline-code">`);
page.fr.content = page.fr.content.replace(/<\/ic>/g, `</span>`);
page.fr.content = page.fr.content.replace(/\\'/g, `'`);
page.fr.content = page.fr.content.replace(/\\!/g, `!`);
page.fr.content = page.fr.content.replace(/\\\?/g, `?`);
page.fr.content = page.fr.content.replace(/<li>/g, `<li>— `);
page.fr.content = page.fr.content.replace(/\s:/g, ` :`);
page.fr.content = page.fr.content.replace(/\s;/g, ` ;`);
page.fr.content = page.fr.content.replace(/<lnum>/g, `<span class="lnum">`);
page.fr.content = page.fr.content.replace(/<\/lnum>/g, `</span>`);
page.fr.content = page.fr.content.replace(/<nb>/g, `<span class="nobreak">`);
page.fr.content = page.fr.content.replace(/<\/nb>/g, `</span>`);
page.fr.content = page.fr.content.replace(/<pn>/g, `<p class="noindent">`);
page.fr.content = page.fr.content.replace(/<\/pn>/g, `</p>`);
page.fr.content = page.fr.content.replace(/(<code>)(\s)([a-zA-ZÀ-ú\d])/g,
function(m, a, b, c) {
return (a + c);
});
page.en.title = data.match(/(<!-- en-title -->)([\S\s]*?)(<!--)/)[2];
page.en.title = page.en.title.replace(/(?:\r\n|\r|\n)/g, "");
// page.en.description = data.match(/(<!-- en-description -->)([\S\s]*?)(<!--)/)[2];
// page.en.description = page.en.description.replace(/([a-zA-ZÀ-ú])(\')([a-zA-ZÀ-ú])/g, function(a, b, c, d) {
// return "" + b + "’" + d
// });
let enDescription = data.match(/(<!-- en-description -->)([\S\s]*?)(<!--)/);
if (enDescription) {
page.en.description = enDescription[2];
page.en.description = page.en.description.replace(/([a-zA-ZÀ-ú])(\')([a-zA-ZÀ-ú])/g, function(a, b, c, d) {
return "" + b + "’" + d
});
}
let enExcerpt = data.match(/(<!-- en-excerpt -->)([\S\s]*?)(<!--)/);
if (enExcerpt) {
page.en.excerpt = enExcerpt[2];
page.en.excerpt = page.en.excerpt.replace(/([a-zA-ZÀ-ú])(\')([a-zA-ZÀ-ú])/g, function(a, b, c, d) {
return "" + b + "’" + d;
});
}
page.en.content = data.match(/(<!-- en-content -->)([\S\s]*)/)[2];
page.en.content = page.en.content.replace(/([a-zA-ZÀ-ú])(\')([a-zA-ZÀ-ú])/g, function(a, b, c, d) {
return "" + b + "’" + d
});
page.en.content = page.en.content.replace(/<i>/g, `<span class="italic">`);
page.en.content = page.en.content.replace(/<\/i>/g, `</span>`);
page.en.content = page.en.content.replace(/<i8>/g, `<span class="italic8">`);
page.en.content = page.en.content.replace(/<\/i8>/g, `</span>`);
page.en.content = page.en.content.replace(/<ic8>/g, `<span class="inline-code8">`);
page.en.content = page.en.content.replace(/<\/ic8>/g, `</span>`);
page.en.content = page.en.content.replace(/<dc>/g, `<span class="drop-caps">`);
page.en.content = page.en.content.replace(/<\/dc>/g, `</span>`);
page.en.content = page.en.content.replace(/<sc>/g, `<span class="small-caps">`);
page.en.content = page.en.content.replace(/<\/sc>/g, `</span>`);
page.en.content = page.en.content.replace(/<sc8>/g, `<span class="small-caps8">`);
page.en.content = page.en.content.replace(/<\/sc8>/g, `</span>`);
page.en.content = page.en.content.replace(/<math>/g, String.raw `<div class="math">\[`);
page.en.content = page.en.content.replace(/<\/math>/g, String.raw `\]</div>`);
page.en.content = page.en.content.replace(/<im>/g, String.raw `\(`);
page.en.content = page.en.content.replace(/<\/im>/g, String.raw `\)`);
// Put a non-breaking space between any letter followed immediately by an exclamation point
// or an interrogation mark. (There can also be a space between the letter and the punctuation.)
// page.en.content = page.en.content.replace(/([a-zA-ZÀ-ú])( *[\?\!])/g, function(a, b, c, d) {
// return "" + b + " " + c;
// });
// Replace the snl and snr pseudo-HTML tags (used for sidenotes)
page.en.content = page.en.content.replace(/&nbsn;/g, String.raw `<span class="nobreak">`);
// page.en.content = page.en.content.replace(/(\s)([0-9a-zA-ZÀ-ú\.<>":;&\/\?]*)(<sn[lr])/g, function(a, b, c, d) {
// console.log(page.en.title + ", " + b + c + d);
// return b + '<span class="nobreak">' + c + d;
// });
page.en.content = page.en.content.replace(/(<sn)([lr])( label=")([0-9a-zA-ZÀ-ú\-]*)(">)/g, function(a, b, c, d, e) {
let typeOfSidenote = (c == "l") ? "sidenote-left" : "sidenote";
let response = `
<label for="${e}" class="margin-toggle sidenote-number"></label></span>
<input type="checkbox" id="${e}" class="margin-toggle"/>
<span class="${typeOfSidenote}">`;
return response;
});
page.en.content = page.en.content.replace(/<\/sn[lr]>/g, `</span>`);
page.en.content = page.en.content.replace(/(<mn)([lr])( label=")([0-9a-zA-ZÀ-ú\-]*)(">)/g, function(a, b, c, d, e) {
let typeOfMarginnote = (c == "l") ? "marginnote-left" : "marginnote";
let response = `
<label for="${e}" class="margin-toggle">—
</label>
<input type="checkbox" id="${e}" class="margin-toggle" />
<span class="${typeOfMarginnote}">`;
return response;
});
page.en.content = page.en.content.replace(/<\/mn[lr]>/g, `</span>`);
page.en.content = page.en.content.replace(/<\/a>,/g,
String.raw `</a><span class="cleardescenders">,</span>`);
page.en.content = page.en.content.replace(/<li>\s\s+/g, "<li>");
let enDate = data.match(/(<!-- en-date -->)([\S\s]*?)(<!--)/);
if (enDate) {
page.en.date = enDate[2];
}
let enHTMLTitle = data.match(/(<!-- en-html-title -->)([\S\s]*?)(<!--)/);
if (enHTMLTitle) {
page.en.HTMLTitle = enHTMLTitle[2];
}
page.en.content = page.en.content.replace(/<code>/g, `<pre><code>`);
page.en.content = page.en.content.replace(/<\/code>/g, `</code></pre>`);
page.en.content = page.en.content.replace(/<javascript>/g, `<pre><code>`);
page.en.content = page.en.content.replace(/<\/javascript>/g, `</code></pre>`);
page.en.content = page.en.content.replace(/<glsl>/g, `<pre><code class="glsl">`);
page.en.content = page.en.content.replace(/<\/glsl>/g, `</code></pre>`);
page.en.content = page.en.content.replace(/<supercollider>/g, `<pre><code class="supercollider">`);
page.en.content = page.en.content.replace(/<\/supercollider>/g, `</code></pre>`);
page.en.content = page.en.content.replace(/<nosyntax>/g, `<pre><code class="nosyntax">`);
page.en.content = page.en.content.replace(/<\/nosyntax>/g, `</code></pre>`);
page.en.content = page.en.content.replace(/<pre>/g, `<div class="codebox"><pre>`);
page.en.content = page.en.content.replace(/<\/pre>/g, `</pre></div>`);
page.en.content = page.en.content.replace(/<ic>/g, `<span class="inline-code">`);
page.en.content = page.en.content.replace(/<\/ic>/g, `</span>`);
page.en.content = page.en.content.replace(/\\'/g, `'`);
page.en.content = page.en.content.replace(/\\!/g, `!`);
page.en.content = page.en.content.replace(/\\\?/g, `?`);
page.en.content = page.en.content.replace(/<li>/g, `<li>— `);