-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordpress-readme.js
264 lines (225 loc) · 8.81 KB
/
wordpress-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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// eslint-disable-next-line no-undef
const marked = require('marked');
// eslint-disable-next-line no-undef
const fs = require('fs');
const debugTokens = false;
// Conversion options ---------------------------------------------------------]
const options = {
contributors: 'dmitryrudakov',
tags: 'gutenberg, ajax, contact form, feedback, email, feedback form, contact',
tested: '5.8.3',
license: 'GPLv2 or later',
pluginfile: 'zu-contact.php',
fromfile: 'README.md',
tofile: 'readme.txt',
logfile: 'CHANGES.md',
limitRecordrs: 15,
};
// Skip tokens
const skipTokens = [
// if 'contains' is true then skip all tokens of this 'type'
{ type:'html', contains: true },
{ type:'blockquote', contains: '🎃' },
{ type:'blockquote', contains: '❗' },
{ type:'paragraph', contains: 'img.shields.io' },
{ type:'paragraph', contains: 'user-images.githubusercontent.com' },
{ type:'paragraph', contains: 'ps.w.org' },
// custom tokens
{ type:'paragraph', contains: 'Unfortunately WordPress does not come' },
// { type:'heading', contains: 'Download' },
// { type:'list', contains: 'archive/master.zip' },
// { type:'list', contains: 'downloads.wordpress.org' },
];
// Skip sections
const skipSections = [
'Download',
'Extensions',
'Support',
];
// Skip log record if it ends with these strings
const skipLogs = [
'(-)',
'[-]',
];
// Processing Сore ------------------------------------------------------------]
function readFileAndSaveResult() {
const buffer = fs.readFileSync(options.fromfile);
const readme = parseReadme(buffer.toString());
const buffer2 = fs.readFileSync(options.logfile);
const log = parseChangelog(buffer2.toString(), options.limitRecordrs);
const result = String(readme + '\n\n== Changelog ==\n\n' + log).replace(/\n{3,}/g, '\n\n');
fs.writeFileSync(options.tofile, result);
}
function parseReadme(md) {
const tokens = marked.lexer(md);
// debug('Tokens:', tokens); // more compact because without logging each array item
// debug({ context: 'Tokens:'}, tokens);
// debug({ context: 'List tokens:', type: 'list', deep: true}, tokens);
// debug({ context: 'List tokens:', type: 'list', contains: 'With custom'}, tokens);
// debug({ context: 'Headings 2:', type: 'heading', depth: 2, deep: true }, tokens);
// debug({ context: 'Paragraph:', type: 'paragraph'}, tokens);
let content = [];
let skipSection = { enabled: false, depth: 0 };
let screenshots = 0;
tokens.forEach(token => {
// skip single tokens
const shouldSkip = skipTokens.reduce((acc, value) => {
if(acc === true || value === undefined) return acc;
if(token.type === value.type &&
(value.contains === true || token.raw.includes(value.contains))
) return true;
else return false;
}, false);
// skip whole sections
if(token.type === 'heading') {
if(skipSection.enabled) {
if(token.depth <= skipSection.depth) skipSection.enabled = false;
}
if(!skipSection.enabled) {
skipSections.forEach(heading => {
if(token.raw.includes(heading)) {
skipSection.enabled = true;
skipSection.depth = token.depth;
}
});
}
}
if(shouldSkip || skipSection.enabled) return debugToken(token, true);
if(token.type === 'heading') {
// main heading
if(token.depth === 1) {
const heading = token.text.split(':')[0] || token.text;
content.push(`=== ${heading.trim()} ===${parseHeader(options)}\n\n`);
return debugToken(token);
}
// regular headings & screenshots
if(token.depth === 2) {
if(token.raw.includes('[![')) {
let screenshotName = null;
try {
screenshotName = token.tokens[0].tokens[0].text;
} catch(e) { /* ignore the error */ }
if(screenshotName) content.push(`${++screenshots}. ${screenshotName}\n`);
} else {
content.push(`== ${token.text} ==\n\n`);
}
return debugToken(token);
}
}
// remove all HTML comments
let cleanRaw = token.raw.replace( /<!--.*?-->\n?/gm, '');
// add newline after 'paragraph'
if(token.type === 'paragraph') cleanRaw = `${cleanRaw}\n`;
// remove one newline because was already added to 'paragraph'
if(token.type === 'space') cleanRaw = cleanRaw.replace(/\n{1}/m, '');
content.push(cleanRaw);
debugToken(token);
});
return content.join('');
}
function parseChangelog(md, limit) {
const tokens = marked.lexer(md);
// debug('Changelog Tokens:', tokens);
let content = [];
let count = 0;
tokens.forEach(token => {
if(count > limit) return;
if(token.type === 'heading') {
if(token.depth === 4) {
const heading = token.text.split('/')[0] || token.text;
content.push(`\n### ${heading.trim()} ###\n`);
return;
}
}
if(token.type === 'list') {
token.items.forEach(item => {
// skip single log record
const shouldSkip = skipLogs.reduce((acc, value) => {
if(acc === true || value === undefined) return acc;
if(item.text.endsWith(value)) return true;
else return false;
}, false);
if(shouldSkip) return;
else content.push(item.raw);
});
count++;
}
});
return content.join('');
}
function parseHeader(data) {
const buffer = fs.readFileSync(data.pluginfile);
const string = buffer.toString();
const version = /Version\s*:\s*([^\s]+)/gm.exec(string);
data.stable = version !== null ? version[1] : '1.0.0';
const requires = /Requires at least\s*:\s*([^\s]+)/gm.exec(string);
data.requires = requires !== null ? requires[1] : '5.1';
const php = /Requires PHP\s*:\s*([^\s]+)/gm.exec(string);
data.php = php !== null ? php[1] : '7.0';
return `
Contributors: ${data.contributors}
Tags: ${data.tags}
Requires at least: ${data.requires}
Tested up to: ${data.tested}
Stable tag: ${data.stable}
License: ${data.license}
Requires PHP: ${data.php}`;
}
// Debug functions ------------------------------------------------------------]
function isString(value) {
return typeof value === 'string' || value instanceof String;
}
function debugToken(token, shouldSkip = false) {
if(debugTokens) {
let outputToken = { type: token.type, raw: token.raw };
if(token.depth) outputToken.depth = token.depth;
if(token.text) outputToken.text = token.text;
debug(`* ${token.type.toUpperCase()}:${shouldSkip ? ' SKIPPED!' : ''}`, outputToken);
}
}
function debug(params, value) {
const simpleOutput = isString(params);
const { context, type, depth, deep, contains } = simpleOutput ? { context: params } : params;
const deepOutput = (token, p) => {
const level = `${p} > ${token.type.toUpperCase()}`;
console.log(level);
console.log(token);
if(Array.isArray(token.tokens)) {
token.tokens.forEach(t => deepOutput(t, level));
}
else if(Array.isArray(token.items)) {
token.items.forEach(t => deepOutput(t, level));
}
};
const output = val => {
const allowed = contains ? val.raw.includes(contains) : true;
const allowedType = type === undefined ? val.type : type;
const allowedDepth = depth === undefined ? val.depth : depth;
if(allowed && val.type === allowedType && val.depth === allowedDepth) {
console.log(val);
if(deep === true) {
const collection = Array.isArray(val.tokens) ? val.tokens : (
Array.isArray(val.items) ? val.items : null
);
if(collection) {
const level = val.type.toUpperCase();
console.log(`>>>>>>>>>> (${level})`);
collection.forEach(t => deepOutput(t, level));
console.log(`<<<<<<<<<< (${level})\n`);
}
}
}
};
if(simpleOutput) console.log(context, value, '\n');
else {
console.log(`\n\n### ${context} ###\n`);
if(Array.isArray(value)) {
value.forEach(output);
} else {
output(value);
}
}
}
// Start! ---------------------------------------------------------------------]
// read file & process it
readFileAndSaveResult();