-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateSummary.js
156 lines (128 loc) · 3.42 KB
/
generateSummary.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const IGNORE_FILES = [
/* gitignore style */
'SUMMARY.md',
];
const DEFAULT_README_TITLE = 'Introduction';
////////////////////////////////////////////////
const fs = require('fs');
const path = require('path');
const isNotIgnored = (() => {
const ignoredPatterns = fs.readFileSync('./.gitignore', 'utf8')
.split('\n')
.map(f => f.trim())
.filter(f => f.length > 0)
.filter(f => f[0] !== '#')
.concat(IGNORE_FILES)
.map(f => new RegExp(f.replace(/\*/g, '(.*?)')));
return (f) => {
if(!(f instanceof File)) throw "Error: expecting File";
return ignoredPatterns.reduce((bool, p) => bool && !p.test(f.path, true));
};
})();
const indent = (s, n=4) => {
const spaces = (new Array(n + 1)).join(' ');
return spaces + s.replace(/\n/g, '\n' + spaces);
};
class File{
constructor(path){
this.path = path;
}
get filename(){
return this.path.substring(this.path.lastIndexOf('/') + 1);
}
get name(){
const p = this.filename;
if(p.lastIndexOf('.') === -1) return p;
return p.substring(0, p.lastIndexOf('.'));
}
get prettyname(){
const capitalize = (s) => {
if(s.length === 0) return '';
return [s[0].toUpperCase(), ...s.substring(1).toLowerCase()].join('');
};
const sepWords = (p) => p.replace(/[-_]/g, ' ');
return capitalize(sepWords(this.name));
}
get ext(){
return this.path.substring(this.path.lastIndexOf('.'));
}
}
class Dir extends File{
ls(){
const isVisible = (f) => f[0] !== '.';
return fs.readdirSync(this.path)
.filter(isVisible)
.map((f) => path.join(this.path, f))
.map(p => new (fs.statSync(p).isDirectory()? Dir: File)(p));
}
lsdirs(){
return this.ls().filter(f => f instanceof Dir);
}
}
class Root extends Dir{
/* directories are viewed as Parts
* files are Articles */
ls(){
return super.ls()
.filter(isNotIgnored)
.map(f => new (f instanceof Dir? Part: Article)(f.path))
.filter(f => f instanceof Article? f.ext === '.md': f);
}
getHeader(){
return `# Summary`;
}
articlesSummary(){
return this.ls()
.filter(f => f instanceof Article)
.map(f => f.toSummary())
.join('\n');
}
directoriesSummary(){
return this.lsdirs()
.map(part => part.toSummary())
.join('\n');
}
getSummary(){
const artSum = this.articlesSummary()
const dirsSum = this.directoriesSummary();
return [artSum, dirsSum].filter(s => s.trim() !== '').join('\n\n');
}
toSummary(){
return this.getHeader() + '\n' + this.getSummary() + '\n';
}
}
class Part extends Root{
/* directories are viewed as Chapters
* files are Articles */
ls(){
return super.ls()
.map(f => f instanceof Part? new Chapter(f.path): f)
}
getHeader(){
return `## ${this.prettyname}`;
}
}
class Chapter extends Part{
ls(){
return super.ls().filter(f => f.filename !== 'README.md');
}
getHeader(){
const readme_path = path.join(this.path, 'README.md');
return `* [${this.prettyname}](${readme_path})`
}
getSummary(){
return indent(super.getSummary());
}
}
class Article extends File{
get prettyname(){
const original_prettyname = super.prettyname;
if(original_prettyname !== 'Readme') return original_prettyname;
return DEFAULT_README_TITLE;
}
toSummary(){
return `* [${this.prettyname}](${this.path})`;
}
}
const summary = new Root('.').toSummary();
fs.writeFileSync('SUMMARY.md', summary);