-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.js
executable file
·201 lines (188 loc) · 6.54 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
const path = require('path');
const repo = path.dirname(process.argv[1]);
const marked = require(path.join(repo, 'builder', 'marked.0.3.6', 'marked.js'));
const KaTeX = require(path.join(repo, 'builder', 'katex.0.11.1', 'katex.js'));
const htmlEntityMap = { '"': '"', '\'': '&39;', '&': '&', '<': '<', '>': '>', '/': '/', '\\': '\', '\r': ' ', '\n': ' ' };
const entityMap = function (x) { return htmlEntityMap[x]; };
const sanitizeForAttribute = function (text) { return text.replace(/["'&<>\/\\\r\n]/g, entityMap); };
/*
\s*
1 2 3
(\\@nobreak{([^{]*?){([\s\S]*?)}\2}\s*)?
4 5
\\@arialabel{([^{]*?){([\s\S]*?)}\4}
6
([\s\S]*?)
7 8 9
(\\@nobreak{([^{]*?){([\s\S]*?)}\8}\s*)?
*/
const regexKaTeX = /^\s*(\\@nobreak{([^{]*?){([\s\S]*?)}\2}\s*)?\\@arialabel{([^{]*?){([\s\S]*?)}\4}([\s\S]*?)(\\@nobreak{([^{]*?){([\s\S]*?)}\8}\s*)?$/;
const presenceInitialPunct = 1;
const contentInitialPunct = 3;
const contentAriaLabel = 5;
const contentBody = 6;
const presenceTrailingPunct = 7;
const contentTrailingPunct = 9;
function RenderKaTeX(part, isDisplay)
{
const renderOptions = {
displayMode: isDisplay,
throwOnError: false,
errorColor: '#FF0000',
macros: {},
GLarialabel: undefined,
GLinitPunct: undefined,
GLtrailPunct: undefined
};
wasRendering = part;
const matchKaTeX = regexKaTeX.exec(part);
var failure = null;
if (matchKaTeX === null)
{
failure = '\\@arialabel is required but absent';
}
else if (matchKaTeX[contentAriaLabel].trim() === '')
{
failure = '\\@arialabel is required but empty';
}
else if (matchKaTeX[contentBody].trim() === '')
{
failure = 'body is required but empty';
}
else if (isDisplay && matchKaTeX[presenceInitialPunct] !== undefined)
{
failure = 'display equation cannot have initial \\@nobreak';
}
else if (isDisplay && matchKaTeX[presenceTrailingPunct] !== undefined)
{
failure = 'display equation cannot have trailing \\@nobreak';
}
else if (matchKaTeX[presenceInitialPunct] !== undefined
&& matchKaTeX[contentInitialPunct].trim() === '')
{
failure = 'initial \\@nobreak is present but empty';
}
else if (matchKaTeX[presenceTrailingPunct] !== undefined
&& matchKaTeX[presenceTrailingPunct].trim() === '')
{
failure = 'trailing \\@nobreak is present but empty';
}
else
{
renderOptions.GLarialabel = matchKaTeX[contentAriaLabel];
renderOptions.GLinitPunct = matchKaTeX[contentInitialPunct];
renderOptions.GLtrailPunct = matchKaTeX[contentTrailingPunct];
return KaTeX.__renderToHTMLTree(
matchKaTeX[contentBody], renderOptions).toMarkup();
}
return '<span class="gl-katex-fails' +
(isDisplay ? ' gl-katex-display' : '') +
'"><span>' + failure + ': </span><code>' +
sanitizeForAttribute(part) + '</code></span>';
};
const fs = require('fs');
const indexmd = path.join(repo, 'index.md');
const content = fs.readFileSync(indexmd, 'utf8');
const rendered = marked(content);
const months = ['January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'];
const now = new Date();
const dateModified = (function (now)
{
const year = now.getUTCFullYear();
const month = now.getUTCMonth() + 1;
const day = now.getUTCDate();
if (year < 1000 || year > 5000)
{
throw new Error('Unsupported date: ' + now);
}
return year + '-' +
(month < 10 ? '0' + month : month) + '-' +
(day < 10 ? '0' + day : day);
})(now);
const dateModifiedLong = (function (now)
{
return now.getUTCDate() + ' ' +
months[now.getUTCMonth()] + ' ' +
now.getUTCFullYear();
})(now);
let indexhtml = fs.readFileSync(
path.join(repo, 'builder', 'template.html'),
'utf8');
indexhtml = indexhtml.replace(/\r\n|\r|\n/g, '\n');
indexhtml = indexhtml.replace(/\n[ \t\v\f\n]*\n/g, '\n');
indexhtml = indexhtml.replace(
/<!--\[bio\]\[body\]([\u0000-\uffff]*?)\[bio\]-->/g,
rendered);
let meta = undefined;
indexhtml = indexhtml.replace(
/<!--\[bio\]\[meta\]([\u0000-\uffff]*?)\[bio\]-->/g,
function (match, metastring)
{
meta = JSON.parse(metastring);
return '';
});
indexhtml = indexhtml.replace(
/<!--\[bio\]\[date-modified\]([\u0000-\uffff]*?)\[bio\]-->/g,
dateModified);
indexhtml = indexhtml.replace(
/<!--\[bio\]\[date-modified-long\]([\u0000-\uffff]*?)\[bio\]-->/g,
dateModifiedLong);
indexhtml = indexhtml.replace(
/<!--\[bio\]\[name\]([\u0000-\uffff]*?)\[bio\]-->/g,
meta.name);
indexhtml = indexhtml.replace(
/<!--\[bio\]\[title\]([\u0000-\uffff]*?)\[bio\]-->/g,
meta.title);
indexhtml = indexhtml.replace(
/<!--\[bio\]\[description\]([\u0000-\uffff]*?)\[bio\]-->/g,
meta.description);
indexhtml = indexhtml.replace(
/<!--\[bio\]\[url\]([\u0000-\uffff]*?)\[bio\]-->/g,
meta.url);
indexhtml = indexhtml.replace(
/<!--\[bio\]\[assets\]([\u0000-\uffff]*?)\[bio\]-->/g,
meta.assets);
indexhtml = indexhtml.replace(
/<!--\[bio\]\[date-created\]([\u0000-\uffff]*?)\[bio\]-->/g,
meta['date-created']);
indexhtml = indexhtml.replace(
/<!--\[bio\]\[tilecolor\]([\u0000-\uffff]*?)\[bio\]-->/g,
meta.tilecolor);
indexhtml = indexhtml.replace(
/<!--\[bio\]\[repo\]([\u0000-\uffff]*?)\[bio\]-->/g,
meta.repo);
let headline = undefined;
indexhtml = indexhtml.replace(
/<!--\[bio\]\[set-headline\]([\u0000-\uffff]*?)\[bio\]-->/g,
function (match, headlinestring)
{
headline = headlinestring;
return '';
});
indexhtml = indexhtml.replace(
/<!--\[bio\]\[get-headline\]([\u0000-\uffff]*?)\[bio\]-->/g,
headline);
indexhtml = indexhtml.replace(
/<!--\[blog\]\[katex(-display)?\]([\u0000-\uffff]*?)\[blog\]-->/g,
function (match, display, part)
{
return RenderKaTeX(part, !!display);
});
indexhtml = indexhtml.replace(
/<!--\[bio\]\[protect\][ \t\v\f\n]*([\u0000-\uffff]*?)[ \t\v\f\n]*\[bio\]-->/g,
'$1');
indexhtml = indexhtml.replace(
/<!--\[bio\]\[remove\]([\u0000-\uffff]*?)\[bio\]-->/g,
'');
fs.writeFileSync(
path.join(repo, 'index.html'),
indexhtml);
fs.writeFileSync(
path.join(repo, '404.html'),
fs.readFileSync(path.join(repo, 'builder', '404.template.html'), 'utf8').
replace(/\r\n|\r|\n/g, '\n').
replace(/\n[ \t\v\f\n]*\n/g, '\n').
replace(/<!--\[bio\]\[url\]([\u0000-\uffff]*?)\[bio\]-->/g, meta.url)
);