-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathadoc-gen.js
67 lines (44 loc) · 1.78 KB
/
adoc-gen.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
const events = require('events');
const fs = require('fs')
const path = require('path')
const readline = require('readline');
const glob = require('glob')
const baseDir = `${__dirname}/../modules/`
const navFileName = 'content-nav.adoc'
const filePattern = `**/${navFileName}`
const xrefPattern = /xref:(.*)\.adoc\[(.*)\]/
glob(`${baseDir}${filePattern}`, async (err, matches) => {
console.log(`content-nav files to process: ${matches.length}`)
if (!matches.length) return
const objects = matches
.map(navFile => {
(async function processLineByLine() {
console.log(`Generating asciidoc files for ${navFile}`);
try {
const rl = readline.createInterface({
input: fs.createReadStream(navFile),
crlfDelay: Infinity
});
rl.on('line', (line) => {
const xrefMatch = line.match(xrefPattern)
if (!xrefMatch) return
const createPath = path.join(path.dirname(navFile),'pages',xrefMatch[1] + '.adoc');
if (fs.existsSync(createPath)) {
console.log(`${createPath} already exists`)
return
}
fs.mkdirSync(path.dirname(createPath), { recursive: true }, (err) => {
if (err) throw err;
});
const docTitle = xrefMatch[2] ? xrefMatch[2] : xrefMatch[1].replace(/-|\//g, ' ');
const fileContents = `// asciidoc file auto-generated by toc-gen\n= ${docTitle[0].toUpperCase() + docTitle.slice(1)}`
console.log(`Creating ${createPath} `)
fs.writeFileSync(createPath,fileContents,{ flag: 'w+' }, err => {})
});
await events.once(rl, 'close');
} catch (err) {
console.error(err);
}
})();
})
})