-
Notifications
You must be signed in to change notification settings - Fork 418
/
locco.js
201 lines (169 loc) · 6 KB
/
locco.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
/*
locco: less docco
Converts JavaScript source files containing documentation blocks into Jekyll
source. Pretty much the bare minimum subset of docco needed to generate
Jasmine's side-by-side tutorials.
Comments with jsdoc style delimiters (opening with slash star star on a
line by itself, ending with star slash on a line by itself) will be treated
as documentation. Their contents are assumed to be Markdown. Alternating
documentation and code blocks are fed to _layouts/tutorail_docco.html as
a list of {docs, code} pairs.
Redirects can be specified in this file. They will be omitted
(i.e. inferred by Jekyll) if not specified.
*/
const fs = require('fs');
const path = require('path');
const mustache = require('mustache');
const {marked} = require('marked');
// Per-file configuration.
// Redirects are because the original tutorials were part of the versioned doc
// set, which is now limited to API reference only.
// See commits a3bd33a793de5ff6df4a2f9cdd80162715201f91 and ed3cdb3b70d5aaff6ef5ed5ad97a43e16338c7ca.
const configs = [
{
src: '_tutorials/src/your_first_suite.js',
dest: '_tutorials/your_first_suite.md',
redirectFrom: [
{path: '/edge/your_first_suite.html'},
{path: '/edge/introduction.html'},
],
},
{
src: '_tutorials/src/custom_matchers.js',
dest: '_tutorials/custom_matchers.md',
redirectFrom: [
{
path: '/edge/custom_matcher.html',
path: '/tutorials/custom_matcher',
},
],
},
{
src: '_tutorials/src/mocking_ajax.js',
dest: '_tutorials/mocking_ajax.md',
template: '_layouts/mocking_ajax.yml.mustache',
redirectFrom: [
{path: '/edge/mocking_ajax.html'},
],
}
]
for (const config of configs) {
const src = fs.readFileSync(config.src, {encoding:'utf8'});
const chunks = chunkify(src);
processText(chunks);
const sections = makeSections(chunks);
const rendered = render(sections, config);
fs.writeFileSync(config.dest, rendered, {encoding: 'utf8'});
}
function chunkify(src) {
const lines = src.split('\n');
// Doc blocks start with /** and end with */. Each of those needs to be on a
// line by itself, except for optional whitespace.
const docStartRe = /^\s*\/\*\*\s*$/;
const docEndRe = /^\s*\*\/\s*$/;
let inDoc = false;
let currentChunk = {type: 'code', lines: []}
const chunks = [currentChunk];
function currentChunkType() {
return inDoc ? 'docs' : 'code';
}
for (let i = 0; i < lines.length; i++) {
const delimRe = inDoc ? docEndRe : docStartRe;
if (lines[i].match(delimRe)) {
// Switch between docs and code
inDoc = !inDoc;
currentChunk = {type: currentChunkType(), lines: []}
chunks.push(currentChunk)
} else {
currentChunk.lines.push(lines[i]);
}
}
if (inDoc) {
throw new Error('Unclosed doc comment');
}
return chunks;
}
function processText(chunks) {
for (const chunk of chunks) {
if (chunk.type === 'docs') {
// Generate an ID from the first line, by removing anything outside
// a subset of characters we know the template can handle
chunk.id = chunk.lines[0]
?.replace(/[^a-zA-Z0-9\-_:', ]/g, '')
?.replace(/^ */, '')
?.replace(/ /g, '_');
// Do markdown processing ourselves. Jekyll's embedded markdown
// processing is brittle and hard to debug when it goes wrong,
// which is often.
chunk.text = marked.parse(unindent(chunk.lines).join('\n'));
} else {
chunk.text = chunk.lines.join('\n');
}
delete chunk.lines;
}
}
// Doc comments are often indented, but markdown is sensitive to leading
// whitespace. Find the shortest number of spaces prefixing any line and
// strip that from every line.
function unindent(lines) {
let minIndent;
for (line of lines) {
if (line === '') {
// Could be an entirely-blank line between indented markdown blocks
continue;
}
const prefix = line.match(/^ */);
if (minIndent === undefined || prefix[0].length < minIndent) {
minIndent = prefix[0].length;
}
}
const re = new RegExp(`^ {${minIndent}}`);
return lines.map(line => line.replace(re, ''));
}
function makeSections(chunks) {
// Group chunks into sections, each consisting of a docs block and the
// following code block. If the first chunk is a non-empty code block, it
// goes in its own section. So does the last chunk if there's an odd number.
if (chunks[0].text === '') {
chunks.shift();
}
const sections = [];
if (chunks[0].type === 'code') {
sections.push({docs: chunks.text});
}
while (chunks.length > 0) {
const docs = chunks.shift();
const code = chunks.shift();
if (docs.type !== 'docs') {
throw new Error('Expected a docs block but got ' + docs.type);
}
if (code && code.type !== 'code') {
throw new Error('Expected a code block but got ' + docs.type);
}
sections.push({
id: docs.id,
docs: docs.text,
code: code?.text,
});
}
return sections;
}
function render(sections, config) {
const tmplPath = config.template || '_layouts/tutorial_docco.yml.mustache';
const template = fs.readFileSync(tmplPath, {encoding: 'utf8'});
return mustache.render(template, {
...config,
sections,
filename: path.basename(config.src).replace(/\.js$/, ''),
src_path: config.src
});
}
function findMatchingConfig(srcPath) {
srcPath = path.resolve(srcPath);
for (const k of Object.keys(configuredSources)) {
if (path.resolve(k) === srcPath) {
return configuredSources[k];
}
}
return {};
}