-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
103 lines (89 loc) · 2.63 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
//
// index.js creates the database of all the examples
// and tutorials and exports a function used in the distribution
// to load the files via a json file as a dictionary
//
const fs = require('fs-extra');
const fg = require('fast-glob');
const path = require('path');
fs.ensureDirSync('./data');
fs.ensureDirSync('./data/md');
//toMarkdown();
// load examples and store in json
let examples = getFiles('./data/examples/**/*.txt');
fs.writeJSONSync('./data/examples.json', examples, { spaces : 2 });
// load tutorials and store in json
let tuts = getFiles('./data/tutorials/**/*.txt');
fs.writeJSONSync('./data/tutorials.json', tuts, { spaces : 2 });
// return a list of files in a json format
// with key: filename and value: path
function getFiles(glob){
const fold = fg.sync(glob);
console.log('folder files', fold);
let files = {};
for (let f in fold){
let file = path.parse(fold[f]);
// read the file and store the text in the dictionary
let txt = fs.readFileSync(fold[f], 'utf-8');
files[file.name] = txt;
}
return files;
}
function toMarkdown(folder){
let files = getFiles('./data/tutorials/**/*.txt');
let chapter = 0;
let md = '';
Object.keys(files).forEach((f) => {
let text = fs.readFileSync(files[f], 'utf-8');
md += markdownFromText(text);
});
fs.writeFileSync('./data/md/tutorials.md', md, 'utf-8');
}
// parse the code text files to a markdown file for the docs
function markdownFromText(text){
// buffer for markdown txt
let md = '\n';
// all the lines separated
let lines = text.split('\n');
// when inside a block of code
let codeblock = false;
// variables for storage
let chapter;
let title;
try {
lines.forEach((l) => {
// console.log(l);
if (l.match(/^\/\/ === (.+) ===$/g)){
let full = l.match(/^\/\/ === (.+) ===$/)[1];
title = full.match(/\: (.+)$/)[1];
chapter = full.match(/(\d).(\d+)/);
md += `### ${chapter[1]}.${chapter[2]}: ${title}\n\n`;
console.log('title', title, chapter[1], chapter[2]);
} else if (l.match(/^\/\//g)){
if (codeblock){
codeblock = false;
md += '```\n'
}
// console.log('comment', l);
md += l.match(/[^/]+/g)[0]; //.trim();
} else if (l === ''){
// console.log('empty');
md += '\n';
} else {
// console.log('code', l)
if (!codeblock){
codeblock = true;
md += '\n```js\n';
}
md += l + '\n';
}
});
// when last line is done but it is still in a codeblock
md += (codeblock)? '```\n' : '\n';
} catch (e) {
console.log('error converting', e);
}
return md;
// console.log(md);
// fs.writeFileSync(`./data/md/${chapter[1]}.${String(chapter[2]).padStart(2, "0")}.md`, md, 'utf-8');
}