forked from AlexMubarakshin/linkerr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·245 lines (206 loc) · 18.6 KB
/
index.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
#!/usr/bin/env node
const program = require("commander");
const ora = require("ora");
const chalk = require("chalk");
const path = require("path");
const {parse} = require("./lib/parse");
const {saveFile, saveFileForce, isFileAlreadyExist, createDirIfNotExist} = require("./lib/file-system");
const {getCurrentDate} = require("./lib/time");
const URL_UTILS = require("./lib/url");
const main = async () => {
program
.version("0.0.2")
.option("-u, --url [Site URL*]", "Site URL")
.option("-o, --output [Output path]", "Output path")
.option("-f, --fileName [File name]", "File name")
.option("-m, --crawlingMode [Crawling mode]", "Crawling mode")
.parse(process.argv);
const PARSE_SITE_URL = program.url;
const BASE_URL = program.url;
if (PARSE_SITE_URL) {
if (URL_UTILS.isUrlValid(PARSE_SITE_URL)) {
const formatedURL = URL_UTILS.formatToStandard(PARSE_SITE_URL);
const baseURL = URL_UTILS.formatToStandard(BASE_URL);
const spinner = ora();
spinner.start(`Starting parse ${formatedURL}`);
try {
const siteName = URL_UTILS.splitURL(formatedURL).authority;
const fileNameBase = program.fileName || siteName;
const outputPath = program.output || path.join(__dirname, "/output/" + fileNameBase);
const crawlingMode = program.crawlingMode || 'linear';
let nav = [];
//если crawlingMode = linear , то formatedURL - это стартовая страница (например https://www.php.net/manual/en/copyright.php) , с которой мы начнем листать страницы
//node ./index.js -u https://www.php.net/manual/en/copyright.php
//node ./index.js -u https://www.php.net/manual/ru/function.preg-match.php
let pageHref = formatedURL;
while (1) {
let parsedData = await parse(pageHref, crawlingMode);
spinner.succeed(`Page ${chalk.green(pageHref)} have been parsed`);
if (parsedData.comments.length) {
let arUrlSplitted = parsedData.url.split("/");
let pageName = arUrlSplitted[arUrlSplitted.length - 1];
let currentFileName = fileNameBase + "_" + pageName + ".md";
for (let i = 0; i < parsedData.nav.length; i++) {
let arrayLength = i + 1;
if (arrayLength > 6) arrayLength = 6;
let val = new Array(arrayLength);
nav[parsedData.nav[i]] = val.fill('#').join('') + ' ';
}
nav["(/pages/" + currentFileName + ")"] = "[" + parsedData.title + "]";
/*if (isFileAlreadyExist(outputPath, currentFileName)) {
throw new Error(
chalk.red("File with name ") +
chalk.cyan(currentFileName) +
chalk.red(" already exist at ") +
chalk.cyan(outputPath));
}*/
/** @todo пусть переписывает пока , позже в настройку вынести */
let content = "# " + parsedData.title + "\n\n";
for (let i = 0; i < parsedData.comments.length; i++) {
let commentRowParsed = parsedData.comments[i];
commentRowParsed = commentRowParsed.replace(/\n/g, '');
commentRowParsed = commentRowParsed.replace(/<pre>/g, '');
commentRowParsed = commentRowParsed.replace(/<pre>/g, '');
commentRowParsed = commentRowParsed.replace(/<\/pre>/g, '');
commentRowParsed = commentRowParsed.replace(/~~~/g, '');
commentRowParsed = commentRowParsed.replace(/<br>/gm, '=br=');
commentRowParsed = commentRowParsed.replace(new RegExp('<<<EOT', 'g'), '#linkerr_tag_open__eot#');
commentRowParsed = commentRowParsed.replace(new RegExp('EOT;', 'g'), '#linkerr_tag_close__eot#');
let arEOTCode = commentRowParsed.match(/(#linkerr_tag_open__eot#(.*)#linkerr_tag_close__eot#)/g);
if (arEOTCode !== null) {
for (let i = 0; i < arEOTCode.length; i++) {
let arEOTCodeReplaced = arEOTCode[i].replace(new RegExp('<\\?php', 'g'), '#linkerr_tag_open__ignored#');
arEOTCodeReplaced = arEOTCodeReplaced.replace(new RegExp('\\?>', 'g'), '#linkerr_tag_close__ignored#');
arEOTCodeReplaced = arEOTCodeReplaced.replace(new RegExp('<\\?=', 'g'), '#linkerr_tag_open_type2__ignored#');
commentRowParsed = commentRowParsed.replace(arEOTCode[i], arEOTCodeReplaced);
}
}
commentRowParsed = commentRowParsed.replace(/OUTPUTS string\(21\) "<br>EachNew<br \/>Line"/gm, 'OUTPUTS string(21) "<br>EachNew<br />Line" ?>');
commentRowParsed = commentRowParsed.replace(/OUTPUTS string\(16\) "Each<br\/>NewLine"/gm, 'OUTPUTS string(16) "Each<br/>NewLine" ?>');
commentRowParsed = commentRowParsed.replace(/'<\?php'/gm, '#linkerr_tag_open___in_quote#');
commentRowParsed = commentRowParsed.replace(/"<\?php"/gm, '#linkerr_tag_open___in_quote#');
commentRowParsed = commentRowParsed.replace(/"<\?php "/gm, '#linkerr_tag_open___in_quote_type2#');
commentRowParsed = commentRowParsed.replace(/'\?>'/gm, '#linkerr_tag_close___in_quote#');
commentRowParsed = commentRowParsed.replace(/"\?>"/gm, '#linkerr_tag_close___in_quote#');
commentRowParsed = commentRowParsed.replace(/the <code> tag too/gm, '#linkerr_tag___code#');
commentRowParsed = commentRowParsed.replace(/\/\/\?>/gm, '#linkerr_tag_close___with_comment#');
commentRowParsed = commentRowParsed.replace(/contained \?>/gm, '#linkerr_tag_close___in_text1#');
commentRowParsed = commentRowParsed.replace(/the \?>/gm, '#linkerr_tag_close___in_text2#');
commentRowParsed = commentRowParsed.replace(/closing \?>/gm, '#linkerr_tag_close___in_text3#');
commentRowParsed = commentRowParsed.replace(new RegExp('<\\?xml version="1.0" encoding="UTF-8" \\?>', 'g'), '#linkerr_tag__xml_version_utf8#');
commentRowParsed = commentRowParsed.replace(new RegExp('<\\?xml version="1.0" encoding="UTF-8"\\?>', 'g'), '#linkerr_tag__xml_version_utf8#');
commentRowParsed = commentRowParsed.replace(new RegExp('<\\?xml version='1.0' encoding='UTF-8'\\?>', 'g'), '#linkerr_tag__xml_version_utf8#');
commentRowParsed = commentRowParsed.replace(new RegExp('<\\?xml version="1.0"\\?>', 'g'), '#linkerr_tag__xml_version#');
commentRowParsed = commentRowParsed.replace(new RegExp('<\\?xml version='1.0'\\?>', 'g'), '#linkerr_tag__xml_version#');
commentRowParsed = commentRowParsed.replace(new RegExp('<\\?xml version=\\\\"1.0\\\\" encoding=\\\\"utf-8\\\\" \\?>', 'g'), '#linkerr_tag__xml_version_utf8_lower_case#');
commentRowParsed = commentRowParsed.replace(new RegExp('!DOCTYPE.\\+\\?>', 'g'), '#linkerr_tag__doctype#');
commentRowParsed = commentRowParsed.replace(new RegExp('.\\*\\?', 'g'), '#linkerr_tag__regexp1#');
commentRowParsed = commentRowParsed.replace(new RegExp('\\(\/\\?', 'g'), '#linkerr_tag__regexp2#');
commentRowParsed = commentRowParsed.replace(new RegExp('\\?\\?>', 'g'), '#linkerr_tag__regexp3#');
commentRowParsed = commentRowParsed.replace(new RegExp('><\/span>=<', 'g'), '#linkerr_tag__regexp4#');
commentRowParsed = commentRowParsed.replace(new RegExp('\\(\\?>', 'g'), '#linkerr_tag__regexp5#');
commentRowParsed = commentRowParsed.replace(new RegExp('\\^\\(\\?>', 'g'), '#linkerr_tag__regexp6#');
commentRowParsed = commentRowParsed.replace(new RegExp('<\\?php echo 'something'; //comment \\?>', 'g'), '#linkerr_tag__php_type1#');
commentRowParsed = commentRowParsed.replace(new RegExp('<\\?php \\.\\.\\. \\?>', 'g'), '#linkerr_tag__php_type2#');
commentRowParsed = commentRowParsed.replace(new RegExp('<\\?php', 'g'), '#linkerr_tag_open#');
commentRowParsed = commentRowParsed.replace(new RegExp('\\?>', 'g'), '#linkerr_tag_close#');
commentRowParsed = commentRowParsed.replace(new RegExp('<\\?php', 'g'), '#linkerr_tag_open#');
commentRowParsed = commentRowParsed.replace(new RegExp('<\\? php', 'g'), '#linkerr_tag_open#');
commentRowParsed = commentRowParsed.replace(new RegExp('<php', 'g'), '#linkerr_tag_open#');
commentRowParsed = commentRowParsed.replace(new RegExp('<\\?PHP', 'g'), '#linkerr_tag_open#');
commentRowParsed = commentRowParsed.replace(new RegExp('<\\?Php', 'g'), '#linkerr_tag_open#');
commentRowParsed = commentRowParsed.replace(new RegExp('<\\?', 'g'), '#linkerr_tag_open#');
commentRowParsed = commentRowParsed.replace(new RegExp('<%', 'g'), '#linkerr_tag_open__percnt#');
commentRowParsed = commentRowParsed.replace(new RegExp('\\?>', 'g'), '#linkerr_tag_close#');
commentRowParsed = commentRowParsed.replace(new RegExp('<rss>', 'g'), '#linkerr_tag_open__rss#');
commentRowParsed = commentRowParsed.replace(new RegExp('</rss>', 'g'), '#linkerr_tag_close__rss#');
let countOpenPHPTags = (commentRowParsed.match(/#linkerr_tag_open#/g) || []).length;
let countClosePHPTags = (commentRowParsed.match(/#linkerr_tag_close#/g) || []).length;
if ((countOpenPHPTags !== 0 && countClosePHPTags === 0) || (countOpenPHPTags === countClosePHPTags + 1)) {
commentRowParsed = commentRowParsed.replace(new RegExp('</code>', 'g'), '#linkerr_tag_close#</code>');
}
commentRowParsed = commentRowParsed.replace(new RegExp('<code>', 'g'), '');
commentRowParsed = commentRowParsed.replace(new RegExp('</code>', 'g'), '');
commentRowParsed = commentRowParsed.replace(/(<([^>]+)>)/ig,"");
//content = content.replace(/[br]/gm, '\n'); - внутри [php ... php]
//content = content.replace(/[br]/gm, '<br>'); - везде
let arPHPCode = commentRowParsed.match(/(#linkerr_tag_open#(.*)#linkerr_tag_close#)/g);
if (arPHPCode !== null) {
for (let i = 0; i < arPHPCode.length; i++) {
let arPHPCodeReplaced = arPHPCode[i].replace(/=br=/gm, '\n');
arPHPCodeReplaced = arPHPCodeReplaced.replace(/->/gm, '->');
arPHPCodeReplaced = arPHPCodeReplaced.replace(/'/gm, '\'');
arPHPCodeReplaced = arPHPCodeReplaced.replace(/=>/gm, '=>');
arPHPCodeReplaced = arPHPCodeReplaced.replace(/</gm, '<');
arPHPCodeReplaced = arPHPCodeReplaced.replace(/>/gm, '>');
commentRowParsed = commentRowParsed.replace(arPHPCode[i], arPHPCodeReplaced);
}
}
commentRowParsed = commentRowParsed.replace(/=br=/gm, '<br>');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag_open#', 'g'), '\n\n```\n<?php');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag_close#', 'g'), '?>\n```\n');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag_open__percnt#', 'g'), '\n\n```\n<%');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag_open__rss#', 'g'), '<rss>');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag_close__rss#', 'g'), '</rss>');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag__xml_version_utf8#', 'g'), '``<?xml version="1.0" encoding="UTF-8" ?>``');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag__xml_version#', 'g'), '``<?xml version="1.0"?>``');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag__xml_version_utf8_lower_case#', 'g'), '``<?xml version=\\"1.0\\" encoding=\\"utf-8\\" ?>``');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag__doctype#', 'g'), '!DOCTYPE.+?>');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag__regexp1#', 'g'), '.*?');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag__regexp2#', 'g'), '(/?');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag__regexp3#', 'g'), '??>');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag__regexp4#', 'g'), '>=<');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag__regexp5#', 'g'), '(?>');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag__regexp6#', 'g'), '^(?>');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag__php_type1#', 'g'), '<?php echo \'something\'; //comment ?>');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag__php_type2#', 'g'), '``<?php ... ?>``');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag_open__eot#', 'g'), '<<<EOT');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag_close__eot#', 'g'), 'EOT;');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag_open__ignored#', 'g'), '<?php');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag_close__ignored#', 'g'), '?>');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag_open_type2__ignored#', 'g'), '<?=');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag_open___in_quote#', 'g'), '``<?php``');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag_open___in_quote_type2#', 'g'), '"<?php "');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag_close___in_quote#', 'g'), '``?>``');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag___code#', 'g'), 'the ``<code>`` tag too');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag_close___with_comment#', 'g'), '//?>');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag_close___in_text1#', 'g'), 'contained ?>');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag_close___in_text2#', 'g'), 'the ?>');
commentRowParsed = commentRowParsed.replace(new RegExp('#linkerr_tag_close___in_text3#', 'g'), 'closing ?>');
commentRowParsed = commentRowParsed.replace(new RegExp(' ', 'g'), ' ');
commentRowParsed = commentRowParsed.replace(new RegExp('"', 'g'), '"');
commentRowParsed = commentRowParsed.replace(new RegExp('§', 'g'), '§');
commentRowParsed = commentRowParsed.replace(new RegExp('<\/h1>', 'g'), '<\/h1>');
let contentRow = "\n\n" + commentRowParsed + "\n\n" + "---";
content = content + contentRow;
}
/** @todo нормальную обработку текста сделать */
content = content + "\n\n" + "[Official documentation page](" + parsedData.url + ")";
content = content + "\n\n" + "**[To root](/README.md)**";
await saveFile(outputPath + '/pages', currentFileName, content);
spinner.succeed(`Info saved at ${chalk.cyan(outputPath)}/${chalk.cyan(currentFileName)}`);
content = "";
for (var key in nav) {
let contentRow = nav[key] + key + "\n\n";
content = content + contentRow;
}
await saveFile(outputPath, 'README.md', content);
}
if (parsedData.nextPageHref) {
pageHref = parsedData.nextPageHref[0];
} else {
break;
}
}
} catch (e) {
spinner.fail(e.message);
}
} else {
console.log("URL is not valid!");
}
} else {
console.log("Please specify the URL for parsing\n");
program.help();
}
};
main();