-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakedoc.ts
87 lines (65 loc) · 2.18 KB
/
makedoc.ts
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
import fs from 'fs'
interface Doc {
title: string
chapters: {
name: string
title: string
desc?: string
complexity?: string
sections: {
name: string
title: string
desc?: string
hideTitle?: boolean
complexity?: string
entries: {
name: string
code: string
desc: string
complexity?: string
indexEntry?: string
}[]
}[]
}[]
}
function fmtComplexity (comp?: string): string {
return comp ? comp.split('\n').map(line => '> ' + line).join('\n') + '\n\n' : ''
}
function generate (inputPath: string, outputPath: string): void {
const data: Doc = JSON.parse(fs.readFileSync(inputPath, 'utf8'))
let head = `# ${data.title}\n\n## Table of contents\n`
let t = ''
const indexEntries: { name: string, title: string }[] = []
for (const chapter of data.chapters) {
head += `- <a href="#${chapter.name}">${chapter.title}</a>\n`
t += `## <a name="${chapter.name}"></a>${chapter.title}\n\n`
if (chapter.desc) {
t += chapter.desc + '\n\n'
}
t += fmtComplexity(chapter.complexity)
for (const section of chapter.sections) {
if (!section.hideTitle) {
head += ` - <a href="#${chapter.name}-${section.name}">${section.title}</a>\n`
t += `### <a name="${chapter.name}-${section.name}"></a>${section.title}\n\n`
}
if (section.desc) {
t += section.desc + '\n\n'
}
t += fmtComplexity(section.complexity)
for (const entry of section.entries) {
if (entry.indexEntry) {
indexEntries.push({ name: `${chapter.name}-${section.name}-${entry.name}`, title: entry.indexEntry })
}
t += `<a name="${chapter.name}-${section.name}-${entry.name}"></a>\n`
t += `\`\`\`ocaml\n${entry.code}\n\`\`\`\n`
t += `${entry.desc}\n\n`
t += fmtComplexity(entry.complexity)
}
}
}
const index = indexEntries.sort(({ title: a }, { title: b }) => a > b ? 1 : -1).map(({ name, title }) => `- <a href="#${name}">${title}</a>`).join('\n')
const doc = head + '\n## Index\n' + index + '\n' + t
console.log(doc)
fs.writeFileSync(outputPath, doc)
}
generate('doc.json', 'README.md')