-
Notifications
You must be signed in to change notification settings - Fork 30
/
generate-language-block.js
76 lines (71 loc) · 3.01 KB
/
generate-language-block.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
const fs = require("fs-extra");
const path = require("path");
const args = process.argv.slice(2);
const lang = args[0].split(":")[1];
// List of files to process
let filesToProcess = require("./generate-language-block-url.js");
const filesToProcessDealed = filesToProcess?.map(
(file) => `docs/${lang}/${file}`
);
// Regular expressions for English and Chinese blocks
const EN_BLOCK_REGEX = /<!-- #ifendef -->\s*([\s\S]*?)\s*<!-- #endendef -->/g;
const CN_BLOCK_REGEX = /<!-- #ifcndef -->\s*([\s\S]*?)\s*<!-- #endcndef -->/g;
async function processFiles(language, action) {
for (const file of filesToProcessDealed) {
const filePath = path.resolve(file);
if (fs.existsSync(filePath)) {
let fileContent = await fs.readFile(filePath, "utf-8");
if (language === "en") {
if (action === "block_before") {
// Comment out Chinese blocks and keep English blocks
fileContent = fileContent.replace(CN_BLOCK_REGEX, (match, p1) => {
return `<!-- #ifcndef -->\n<!--\n${p1}\n-->\n<!-- #endcndef -->`; // Comment out and ensure line breaks
});
} else if (action === "block_after") {
// Restore Chinese blocks by removing comments and cleaning up extra line breaks
fileContent = fileContent.replace(
/<!-- #ifcndef -->\n<!--\n([\s\S]*?)\n-->\n<!-- #endcndef -->/g,
(match, p1) => {
// Restore content directly and ensure no extra line breaks
return `<!-- #ifcndef -->\n${p1.trim()}\n<!-- #endcndef -->`;
}
);
}
} else if (language === "cn") {
if (action === "block_before") {
// Comment out English blocks and keep Chinese blocks
fileContent = fileContent.replace(EN_BLOCK_REGEX, (match, p1) => {
return `<!-- #ifendef -->\n<!--\n${p1}\n-->\n<!-- #endendef -->`; // Comment out and ensure line breaks
});
} else if (action === "block_after") {
// Restore English blocks by removing comments and cleaning up extra line breaks
fileContent = fileContent.replace(
/<!-- #ifendef -->\n<!--\n([\s\S]*?)\n-->\n<!-- #endendef -->/g,
(match, p1) => {
// Restore content directly and ensure no extra line breaks
return `<!-- #ifendef -->\n${p1.trim()}\n<!-- #endendef -->`;
}
);
}
}
// Write back to the file
await fs.writeFile(filePath, fileContent, "utf-8");
} else {
console.log(`File not found: ${filePath}`);
}
}
}
// Run the appropriate function based on command line arguments
if (args[0] === "block_before:en") {
processFiles("en", "block_before");
} else if (args[0] === "block_after:en") {
processFiles("en", "block_after");
} else if (args[0] === "block_before:cn") {
processFiles("cn", "block_before");
} else if (args[0] === "block_after:cn") {
processFiles("cn", "block_after");
} else {
console.log(
"Please use block_before:en, block_after:en, block_before:cn, or block_after:cn"
);
}