Skip to content

Commit 63f500d

Browse files
authored
Merge pull request #448 from cuixiping/master
Auto generate examples README.md by nodejs.
2 parents 1800505 + af28d2f commit 63f500d

File tree

5 files changed

+112
-71
lines changed

5 files changed

+112
-71
lines changed

examples/README.md

Lines changed: 41 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,44 @@
1-
1+
<!-- Auto generated. DO NOT edit this file humanly. -->
22
# Examples(範例)
33

4-
| File(文檔) | Remark(說明) |
5-
| -------------------------- | ----------------------- |
6-
| [beer.wy][3] | while |
7-
| [collatz.wy][4] | 角谷猜想 |
8-
| [collatz2.wy][5] | 考拉兹猜想(角谷猜想) |
9-
| [divination.wy][6] | 春秋古筮法 |
10-
| [eightqueens.wy][7] | 八皇后問題 |
11-
| [euclidean.wy][8] | 歐幾里得法 |
12-
| [factorial.wy][9] | 階乘 |
13-
| [fibonacci.wy][10] | 斐波那契 |
14-
| [fibonacci2.wy][11] | 斐氏列 |
15-
| [fizzbuzz.wy][12] | [Fizz buzz][1] |
16-
| [hanoi.wy][13] | 漢諾塔 |
17-
| [helloworld.wy][14] | 問天地好在 |
18-
| [helloworld+.wy][15] | 問天地好在+ |
19-
| [import.wy][16] | 導入 |
20-
| [mandelbrot.wy][17] | 曼德博集 |
21-
| [mergesort.wy][18] | 歸併排序 |
22-
| [misc.wy][19] | 雜項 |
23-
| [modinv.wy][20] | 大衍求一術 |
24-
| [multiplication_table.wy][21] | 乘算口訣 |
25-
| [nested_fun.wy][22] | 函数 |
26-
| [obj.wy][23] | 对象 |
27-
| [pi_leibniz.wy][24] | 萊布尼茲圓周率估算 |
28-
| [pi_liuhui.wy][25] | 劉徽割圓術求圓周率 |
29-
| [quicksort.wy][26] | 快速排序 |
30-
| [quicksort_inplace.wy][27] | 快速排序2 |
31-
| [quine.wy][28] | [自產生程式][2] |
32-
| [quine2.wy][29] | 自產生程式2 |
33-
| [selectionsort.wy][30] | 選擇排序 |
34-
| [sieve.wy][31] | 埃氏篩 |
35-
| [sqrt_newton.wy][32] | 牛頓求根法 |
36-
| [turing.wy][33] | 圖靈機 |
37-
| [draw_heart.wy][34] | 畫心 |
4+
| File(文檔) | Remark(說明) |
5+
| -------------------------------------- | ------------------------- |
6+
| [beer.wy](beer.wy) | 九十九瓶啤酒 |
7+
| [collatz.wy](collatz.wy) | 考拉兹猜想 |
8+
| [collatz2.wy](collatz2.wy) | 考拉兹猜想二 |
9+
| [divination.wy](divination.wy) | 春秋古筮法 |
10+
| [draw_heart.wy](draw_heart.wy) | 畫心 |
11+
| [eightqueens.wy](eightqueens.wy) | 八皇后問題 |
12+
| [euclidean.wy](euclidean.wy) | 歐幾里得法 |
13+
| [factorial.wy](factorial.wy) | 階乘 |
14+
| [fibonacci.wy](fibonacci.wy) | 斐氏列 |
15+
| [fibonacci2.wy](fibonacci2.wy) | 斐氏列二 |
16+
| [fizzbuzz.wy](fizzbuzz.wy) | [嘶嘶嗡嗡][1] |
17+
| [hanoi.wy](hanoi.wy) | 漢諾塔 |
18+
| [helloworld.wy](helloworld.wy) | 問天地好在 |
19+
| [helloworld+.wy](helloworld+.wy) | 問天地好在+ |
20+
| [import.wy](import.wy) | 導入示例 |
21+
| [macro.wy](macro.wy) | 宏示例 |
22+
| [mandelbrot.wy](mandelbrot.wy) | 曼德博集 |
23+
| [mergesort.wy](mergesort.wy) | 歸併排序 |
24+
| [misc.wy](misc.wy) | 雜項 |
25+
| [modinv.wy](modinv.wy) | 大衍求一術 |
26+
| [multiplication_table.wy](multiplication_table.wy) | 乘算口訣 |
27+
| [nested_fun.wy](nested_fun.wy) | 嵌套調用示例 |
28+
| [obj.wy](obj.wy) | 对象示例 |
29+
| [pi_leibniz.wy](pi_leibniz.wy) | 萊布尼茲圓周率估算 |
30+
| [pi_liuhui.wy](pi_liuhui.wy) | 劉徽割圓術求圓周率 |
31+
| [quicksort.wy](quicksort.wy) | 快速排序 |
32+
| [quicksort_inplace.wy](quicksort_inplace.wy) | 快速排序二 |
33+
| [quine.wy](quine.wy) | [自產生程式][2] |
34+
| [quine2.wy](quine2.wy) | 自產生程式二 |
35+
| [selectionsort.wy](selectionsort.wy) | 選擇排序 |
36+
| [sieve.wy](sieve.wy) | 埃氏篩 |
37+
| [sqrt_newton.wy](sqrt_newton.wy) | 牛頓求根法 |
38+
| [tree.wy](tree.wy) | 畫樹 |
39+
| [tree2.wy](tree2.wy) | 畫樹二 |
40+
| [try.wy](try.wy) | 異常處理示例 |
41+
| [turing.wy](turing.wy) | 圖靈機 |
3842

39-
[1]: https://en.wikipedia.org/wiki/Fizz_buzz "Fizz buzz"
40-
[2]: https://zh.wikipedia.org/wiki/自產生程式 "自產生程式"
41-
[3]: beer.wy
42-
[4]: collatz.wy
43-
[5]: collatz2.wy
44-
[6]: divination.wy
45-
[7]: eightqueens.wy
46-
[8]: euclidean.wy
47-
[9]: factorial.wy
48-
[10]: fibonacci.wy
49-
[11]: fibonacci2.wy
50-
[12]: fizzbuzz.wy
51-
[13]: hanoi.wy
52-
[14]: helloworld.wy
53-
[15]: helloworld+.wy
54-
[16]: import.wy
55-
[17]: mandelbrot.wy
56-
[18]: mergesort.wy
57-
[19]: misc.wy
58-
[20]: modinv.wy
59-
[21]: multiplication_table.wy
60-
[22]: nested_fun.wy
61-
[23]: obj.wy
62-
[24]: pi_leobniz.wy
63-
[25]: pi_liuhui.wy
64-
[26]: quicksort.wy
65-
[27]: quicksort_inplace.wy
66-
[28]: quine.wy
67-
[29]: quine2.wy
68-
[30]: selectionsort.wy
69-
[31]: sieve.wy
70-
[32]: sqrt_newton.wy
71-
[33]: turing.wy
72-
[34]: draw_heart.wy
43+
[1]: https://en.wikipedia.org/wiki/Fizz_buzz
44+
[2]: https://zh.wikipedia.org/wiki/自產生程式

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"publish:ci": "node ./tools/publish.js",
2727
"release": "bump --commit --tag && git push --follow-tags",
2828
"docs:update": "node ./tools/stdlib_doc.js",
29+
"make_examples_readme": "node ./tools/make_examples_readme.js",
2930
"make_ide": "node ./tools/make_ide.js",
3031
"make_site": "node ./tools/make_site.js",
3132
"ide:dev": "nodemon --ignore build,dist ./tools/make_ide.js",

tools/examples_alias.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = {
33
collatz: "考拉兹猜想",
44
collatz2: "考拉兹猜想二",
55
divination: "春秋古筮法",
6+
draw_heart: "畫心",
67
eightqueens: "八皇后問題",
78
euclidean: "歐幾里得法",
89
factorial: "階乘",
@@ -15,6 +16,7 @@ module.exports = {
1516
import: "導入示例",
1617
mandelbrot: "曼德博集",
1718
mergesort: "歸併排序",
19+
macro: "宏示例",
1820
misc: "雜項",
1921
modinv: "大衍求一術",
2022
multiplication_table: "乘算口訣",
@@ -30,6 +32,7 @@ module.exports = {
3032
sieve: "埃氏篩",
3133
sqrt_newton: "牛頓求根法",
3234
turing: "圖靈機",
33-
try: "異常處理示例",
34-
macro: "宏示例"
35+
tree: "畫樹",
36+
tree2: "畫樹二",
37+
try: "異常處理示例"
3538
};

tools/examples_links.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
fizzbuzz: "[1]: https://en.wikipedia.org/wiki/Fizz_buzz",
3+
quine: "[2]: https://zh.wikipedia.org/wiki/自產生程式"
4+
};

tools/make_examples_readme.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const fs = require("fs");
2+
const examplesAlias = require("./examples_alias");
3+
const examplesLinks = require("./examples_links");
4+
5+
const files = fs
6+
.readdirSync(__dirname + "/../examples/")
7+
.filter(x => x.endsWith(".wy"));
8+
files.sort((a, b) => a.slice(0, -3).localeCompare(b.slice(0, -3)));
9+
10+
const leftWidth = "--------------------------------------".length;
11+
const rightWidth = "-------------------------".length;
12+
13+
let content = `<!-- Auto generated. DO NOT edit this file humanly. -->
14+
# Examples(範例)
15+
16+
| File(文檔) | Remark(說明) |
17+
| -------------------------------------- | ------------------------- |
18+
`;
19+
20+
function ansiLength(str) {
21+
return str.length + str.replace(/[\x00-\xff]+/g, "").length;
22+
}
23+
24+
let links = [];
25+
26+
for (let fname of files) {
27+
let basename = fname.replace(/\.wy$/, "");
28+
let left = `[${fname}](${fname})`;
29+
if (left.length < leftWidth) {
30+
left = left.padEnd(leftWidth, " ");
31+
} else {
32+
left += " ";
33+
}
34+
let alias = examplesAlias[basename];
35+
let link = examplesLinks[basename];
36+
let linkTag = link && /^\[.+?\]:/.test(link) && link.match(/^\[.+?\]/)[0];
37+
let right = link ? `[${alias || basename}]${linkTag}` : alias || "";
38+
let rightLength = ansiLength(right);
39+
let d = leftWidth + rightWidth - left.length - rightLength;
40+
if (d >= 0) {
41+
right += " ".repeat(d);
42+
} else {
43+
right += " ";
44+
}
45+
if (link) {
46+
links.push(`${link}`);
47+
}
48+
content += `| ${left} | ${right} |` + "\n";
49+
}
50+
51+
if (links.length) {
52+
content += "\n";
53+
content += links.join("\n");
54+
content += "\n";
55+
}
56+
57+
console.log(content);
58+
59+
fs.writeFileSync(__dirname + "/../examples/README.md", content);
60+
61+
console.log('"examples/README.md" updated');

0 commit comments

Comments
 (0)