-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathmake_examples_readme.js
74 lines (60 loc) · 1.91 KB
/
make_examples_readme.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
const fs = require("fs");
const path = require("path");
const { examplesAlias, examplesLinks } = require("./examples");
function update(srcDir, outputFile) {
const files = fs.readdirSync(srcDir).filter(x => x.endsWith(".wy"));
files.sort((a, b) => a.slice(0, -3).localeCompare(b.slice(0, -3)));
const leftWidth = "--------------------------------------".length;
const rightWidth = "-------------------------".length;
let content = `<!-- GENERATED FILE, DO NOT MODIFY -->
<!-- "npm run build:docs" to regenerate -->
# Examples
| File | Remark |
| -------------------------------------- | ------------------------- |
`;
function ansiLength(str) {
return str.length + str.replace(/[\x00-\xff]+/g, "").length;
}
let links = [];
for (let fname of files) {
let basename = fname.replace(/\.wy$/, "");
let left = `[${fname}](${fname})`;
if (left.length < leftWidth) {
left = left.padEnd(leftWidth, " ");
} else {
left += " ";
}
let alias = examplesAlias[basename];
let link = examplesLinks[basename];
let linkTag = link && /^\[.+?\]:/.test(link) && link.match(/^\[.+?\]/)[0];
let right = link ? `[${alias || basename}]${linkTag}` : alias || "";
let rightLength = ansiLength(right);
let d = leftWidth + rightWidth - left.length - rightLength;
if (d >= 0) {
right += " ".repeat(d);
} else {
right += " ";
}
if (link) {
links.push(`${link}`);
}
content += `| ${left} | ${right} |` + "\n";
}
if (links.length) {
content += "\n";
content += links.join("\n");
content += "\n";
}
// console.log(content);
fs.writeFileSync(outputFile, content);
console.log("Examples list updated");
}
module.exports = {
update
};
if (require.main === module) {
update(
path.resolve(__dirname, "../examples/"),
path.resolve(__dirname, "../examples/README.md")
);
}