-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
269 lines (256 loc) · 10.1 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Note: 读取 public 目录下的 docs/ref 下的 index.html,解析出其中的 article 内容,翻译其中的中文,写回 index.html
const fs = require('fs');
const path = require('path');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
const glob = require('glob');
const { Semaphore } = require('async-mutex');
const { translate } = require('./utils.js');
// 使用信号量控制并发
const MAX_CONCURRENT = 2;
// Note: 配置各个文件需要翻译的节点选择器,如果不存在,则默认翻译页面首个 article 中的全部 p 标签内容
const defaultConfig = (more) => {
return [
{
container: 'head',
selector: 'title',
},
{
container: '#page-wrapper #content',
selector: 'p, h1, h2, h3, h4, li',
},
];
};
const config = {
// 'website/public/index.html': [...defaultConfig()],
// 'website/public/examples/index.html': [...defaultConfig('h2, h3')],
// 'website/public/examples/basic/index.html': [...defaultConfig('ul li, h1')],
// 'website/public/examples/markdown/index.html': [
// ...defaultConfig('textarea#content'),
// ],
// 'website/public/docs/ref/index.html': [
// ...defaultConfig('ul li, #part_top h2'),
// ],
// 'website/public/docs/index.html': [...defaultConfig('h2, h3')],
};
const propertyMap = {
title: 'Rust 中文',
description: 'Rust 中文文档',
url: 'https://rust.xheldon.com',
};
const semaphore = new Semaphore(MAX_CONCURRENT);
// 使用 glob 模块来匹配文件
const files = glob.sync(path.resolve(__dirname, 'book_src/book/**/*.html'), {
ignore: ['**/print.html'], // 跳过 print 页面 ,因为它是全部页面的集合!
});
Promise.all(
files.map((file) => {
return new Promise((rootResolve) => {
const rawString = fs.readFileSync(file, 'utf8');
if (!rawString) {
console.error('文件不存在');
process.exit(1);
}
const key = path.relative(__dirname, file);
const dom = new JSDOM(rawString, { url: 'https://rust.xheldon.com' });
const document = dom.window.document;
// Note: 修改 head 部分的 meta 信息
document.querySelector('html').setAttribute('lang', 'zh-CN');
const head = document.querySelector('head');
if (head) {
const metas = head.querySelectorAll('meta');
if (metas.length) {
for (const meta of metas) {
const property = meta.getAttribute('property');
if (property && propertyMap[property]) {
meta.setAttribute('content', propertyMap[property]);
}
}
}
for (const property of Object.keys(propertyMap)) {
const meta = document.createElement('meta');
meta.setAttribute('name', property);
meta.setAttribute('content', propertyMap[property]);
head.appendChild(meta);
const meta2 = document.createElement('meta');
meta2.setAttribute('property', `og:${property}`);
meta2.setAttribute('content', propertyMap[property]);
head.appendChild(meta2);
}
// Note: 增加 google 统计/广告(要吃饭呀)
// Note: 谷歌统计
const script = document.createElement('script');
script.async = true;
script.src = 'https://www.googletagmanager.com/gtag/js?id=G-V7NE1X37EX';
head.appendChild(script);
const script2 = document.createElement('script');
script2.innerHTML = `window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments);}gtag('js', new Date());gtag('config', 'G-V7NE1X37EX')`;
head.appendChild(script2);
// Note: 谷歌广告
const script3 = document.createElement('script');
script3.async = true;
script3.crossorigin = 'anonymous';
script3.src =
'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5486286026923411';
head.appendChild(script3);
const style = document.createElement('style');
style.innerHTML = fs.readFileSync(
path.resolve(__dirname, 'style.css'),
'utf8'
);
head.appendChild(style);
}
// Note: 修改 nav 部分,增加译者地址
const nav = document.querySelector('#menu-bar .right-buttons');
if (nav) {
const a = document.createElement('a');
a.href = 'https://github.com/Xheldon/rust-book-cn';
a.target = '_blank';
const i = document.createElement('i');
i.className = 'fa fa-language';
a.appendChild(i);
const a2 = document.createElement('a');
a2.href = 'https://www.xheldon.com';
a2.target = '_blank';
const i2 = document.createElement('i');
i2.className = 'fa fa-id-card';
a2.appendChild(i2);
nav.appendChild(a);
nav.appendChild(a2);
}
const banner = document.createElement('div');
banner.id = 'banner-info';
// Note: 整站通知
const p = key.replace('book_src/book', '').replace('.html', '');
banner.innerHTML = `本文档为 AI + 人工翻译,hover 可以显示原文。当前页翻译有问题?<a style="cursor: pointer;" href="https://github.com/Xheldon/rust-book-cn/blob/main/dict${p}.json" target="_blank">我来翻译!</a>`;
const bodyContainer = document.querySelector('#menu-bar');
if (bodyContainer) {
bodyContainer.insertBefore(banner, bodyContainer.firstChild);
}
// Note: 添加 hover 显示原文的样式信息
// Note: 如果 config 中的路径文件不存在,则使用默认,否则使用 config 配置文件
let list = [];
list = (config[key] || defaultConfig())
.flatMap((c) => {
const container = document.querySelector(c.container);
if (!container) {
console.log(`${file} 未找到 ${c.container} 标签`);
return;
}
return [
...container.querySelectorAll(`${c.selector}:not([data-x-en])`),
];
})
.filter(Boolean);
if (!list.length) {
console.log(`${file} 不存在可翻译内容,中断`);
rootResolve();
return;
}
const dictPath = file
.replace('.html', '.json')
.replace('book_src/book', 'dict');
if (!fs.existsSync(dictPath)) {
// Note: 如果不存在,则递归创建
fs.mkdirSync(path.dirname(dictPath), { recursive: true });
fs.writeFileSync(dictPath, '{}');
}
let dict = {};
try {
dict = JSON.parse(fs.readFileSync(dictPath, 'utf8'));
} catch (error) {
console.error(`${dictPath} 解析失败, 跳过`);
rootResolve();
return;
}
Promise.all(
Array.from(list).map((item, key) => {
return new Promise((resolve) => {
const text = item.innerHTML.trim();
// Note: 移除换行符
const pureText = item.textContent.trim().replace(/\s+/gm, ' ');
if (!pureText) {
resolve();
return;
}
if (dict[pureText]) {
if (dict[pureText]._translate) {
item.innerHTML = dict[pureText]._translate;
// Note: hover 显示原文,不需要 html 标签
if (item.tagName === 'P' || item.tagName === 'LI') {
item.setAttribute('data-x-en', pureText);
}
}
// Note: 如果有注释,则插入到当前 p 后面
if (dict[pureText]._note) {
const note = document.createElement('div');
note.setAttribute('type', 'comment');
note.innerHTML = dict[pureText]._note;
// Note: 如果 p 本身在 a 标签里面,而 p 内又有 a 标签,那么实际渲染的时候会发生意外当的情况
if (item.parentNode.tagName === 'A') {
item.parentNode?.parentNode?.insertBefore(
note,
item.parentNode?.nextSibling
);
} else {
item.parentNode.insertBefore(note, item.nextSibling);
}
}
resolve();
} else {
return semaphore.acquire().then(() => {
return translate(text, { key: file })
.then((translate) => {
console.log('文件:', file, '--------');
dict[pureText] = {
_translate: translate,
_note: '',
};
console.log('AI 翻译:', `${pureText} -> ${translate}`);
// Note: 跟之前一样替换
item.innerHTML = dict[pureText]._translate;
if (item.tagName !== 'TITLE') {
item.setAttribute('data-x-en', pureText);
}
})
.finally(() => {
// Note: 随机增加延迟
setTimeout(() => {
semaphore.release();
resolve();
}, Math.random() * 500);
});
});
}
});
})
).finally(() => {
// 在文件处理完成后(不管有没有错误,保存更新后的字典
console.log(`${file} 翻译完成`);
// Note: 修改 DOM 地址,如果本手册没有,就跳到原始地址
for (const a of document.querySelectorAll('a')) {
try {
const href = a.href;
const url = new URL(href);
if (
url.origin === 'https://rust.xheldon.com' &&
url.pathname?.split('/').length > 2
) {
const newHref = href.replace(
'https://rust.xheldon.com',
'https://doc.rust-lang.org'
);
a.setAttribute('target', '_blank');
a.setAttribute('href', newHref);
}
} catch (e) {}
}
fs.writeFileSync(dictPath, JSON.stringify(dict, null, 2));
fs.writeFileSync(file, dom.serialize());
rootResolve();
});
});
})
).finally(() => {
console.log('文件全部翻译完成');
});