-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstart.html
More file actions
397 lines (357 loc) · 14.5 KB
/
start.html
File metadata and controls
397 lines (357 loc) · 14.5 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hugging Face 博客翻译辅助工具</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
background-color: #f0f0f0;
}
.container {
max-width: 600px;
margin: auto;
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input[type="text"], input[type="url"] {
width: 96%;
padding: 8px;
margin: 4px 0 8px;
display: block;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
margin-right: 10px;
}
button:hover {
background-color: #45a049;
}
.blog-details {
margin-top: 20px;
border: 1px solid #ddd;
border-radius: 5px;
padding: 10px;
background-color: #f9f9f9;
}
.detail-item {
margin-bottom: 10px;
}
.detail-item strong {
font-weight: bold;
}
.hidden {
display: none;
}
.tool-usage {
margin-top: 30px;
padding: 20px;
background-color: #f0f8ff;
border-radius: 5px;
}
.tool-usage h3 {
color: #333;
}
.tool-usage ol {
line-height: 1.6;
}
.tool-usage a {
color: #007bff;
text-decoration: none;
}
.tool-usage a:hover {
text-decoration: underline;
}
</style>
<script src="https://cdn.staticfile.net/axios/1.6.5/axios.min.js"></script>
<script src="https://cdn.staticfile.net/js-yaml/4.1.0/js-yaml.min.js"></script>
<script src="https://cdn.staticfile.net/jszip/3.10.1/jszip.min.js"></script>
<script src="https://cdn.staticfile.net/FileSaver.js/2.0.5/FileSaver.min.js"></script>
</head>
<body>
<div class="container">
<h2>Hugging Face 博客翻译辅助工具</h2>
<div class="form-group">
<label for="blogUrl">要翻译的博客链接:</label>
<input type="url" id="blogUrl" name="blogUrl" placeholder="https://huggingface.co/blog/your-blog-id">
<button type="button" id="fetchBlogInfo">获取博客信息</button>
</div>
<div id="blogDetails" class="blog-details hidden">
<!-- 博客详情将在这里显示 -->
</div>
<div id="translationInputs" class="hidden">
<div class="form-group">
<label for="chineseTitle">博客中文标题:</label>
<input type="text" id="chineseTitle" name="chineseTitle" placeholder="输入翻译后的中文标题">
</div>
<div class="form-group">
<label for="hfUsername">HF 用户名:</label>
<input type="text" id="hfUsername" name="hfUsername" placeholder="你的 Hugging Face 用户名">
</div>
<button type="button" id="downloadFile">下载待翻译的文件</button>
</div>
<!-- 工具使用方法 -->
<div class="tool-usage">
<h3>工具使用方法:</h3>
<ol>
<li>输入 HF 博客链接(如: https://huggingface.co/blog/ml-for-games-3)</li>
<li>点击"获取博客信息"查看原文信息</li>
<li>输入翻译后的中文标题</li>
<li>输入自己的 HF 用户名</li>
<li>点击"下载待翻译的文件"获取 ZIP 文件</li>
<li>在自己 fork 的 repo 里将 zh/ 文件夹的内容复制到 zh/ 目录下</li>
<li>翻译 .md 文件的内容部分</li>
<li>提交 PR 给 <a href="https://github.com/huggingface-cn/hf-blog-translation" target="_blank">https://github.com/huggingface-cn/hf-blog-translation</a></li>
</ol>
<p>本页面地址: <a href="https://hf.link/start">https://hf.link/start</a>,<a href="https://github.com/huggingface-cn/tools/issues" target="_blank">提交 issue</a></p>
</div>
</div>
<script>
// 全局变量存储当前博客信息
let currentBlogData = {};
// 提取 Markdown 文件的 frontmatter
function extractFrontmatter(mdContent) {
let lines = mdContent.split('\n');
let inFrontmatter = false;
let frontmatterLines = [];
for (let line of lines) {
if (line === '---') {
if (!inFrontmatter) {
inFrontmatter = true;
} else {
break;
}
} else if (inFrontmatter) {
frontmatterLines.push(line);
}
}
if (frontmatterLines.length > 0) {
let frontmatterText = frontmatterLines.join('\n');
try {
let frontmatter = jsyaml.load(frontmatterText);
return frontmatter;
} catch (e) {
console.error('Error parsing frontmatter:', e);
return null;
}
}
return null;
}
// 显示博客详情
function displayBlogDetails(frontmatter, blogId, originalBlogMeta) {
currentBlogData = {
frontmatter: frontmatter,
blogId: blogId,
originalMeta: originalBlogMeta
};
let blogDetails = document.getElementById("blogDetails");
if (blogDetails) {
blogDetails.classList.remove("hidden");
// 处理作者信息
let authorInfo = '未找到作者';
if (frontmatter.authors && Array.isArray(frontmatter.authors)) {
authorInfo = frontmatter.authors.map(a => a.user || a).join(', ');
} else if (frontmatter.author) {
authorInfo = frontmatter.author;
}
// 处理日期信息
let dateInfo = originalBlogMeta?.date || '未找到日期';
// 处理标签信息
let tagsInfo = '无';
if (originalBlogMeta?.tags && originalBlogMeta.tags.length > 0) {
tagsInfo = originalBlogMeta.tags.join(', ');
}
blogDetails.innerHTML = `
<div class="detail-item"><strong>标题:</strong> ${frontmatter.title || '未找到标题'}</div>
<div class="detail-item"><strong>作者:</strong> ${authorInfo}</div>
<div class="detail-item"><strong>日期:</strong> ${dateInfo}</div>
<div class="detail-item"><strong>标签:</strong> ${tagsInfo}</div>
<div class="detail-item"><strong>文件名:</strong> ${blogId}.md</div>
`;
document.getElementById("translationInputs").classList.remove("hidden");
}
}
// 显示未找到消息
function showNotFoundMessage() {
let blogDetails = document.getElementById("blogDetails");
blogDetails.classList.remove("hidden");
blogDetails.innerHTML = `
<div class="detail-item" style="color: red;">请检查博客链接,如果无误,请在群里反馈。</div>
`;
document.getElementById("translationInputs").classList.add("hidden");
}
// 显示已翻译消息
function showAlreadyTranslated() {
let blogDetails = document.getElementById("blogDetails");
blogDetails.classList.remove("hidden");
blogDetails.innerHTML = `
<div class="detail-item" style="color: green;">本篇博客已完成中文翻译,请选择其他博客内容。</div>
`;
document.getElementById("translationInputs").classList.add("hidden");
}
// 获取博客信息
document.getElementById("fetchBlogInfo").addEventListener("click", async function() {
let blogUrl = document.getElementById("blogUrl").value.trim();
if (!blogUrl) {
alert("请输入博客链接");
return;
}
// 提取博客 ID
let blogId = blogUrl.split("/").pop();
if (!blogId) {
alert("无法从链接中提取博客 ID");
return;
}
try {
// 先检查是否已翻译
let zhResponse = await axios.get("https://raw.githubusercontent.com/huggingface-cn/hf-blog-translation/main/zh/_blog.yml");
let zhBlogs = jsyaml.load(zhResponse.data);
if (zhBlogs.find(blog => blog.local === blogId)) {
showAlreadyTranslated();
return;
}
// 获取原始英文 _blog.yml 以获取 date 和 tags
let enBlogResponse = await axios.get("https://raw.githubusercontent.com/huggingface/blog/main/_blog.yml");
let enBlogs = jsyaml.load(enBlogResponse.data);
let originalBlogMeta = enBlogs.find(blog => blog.local === blogId);
// 获取原始博客内容
let mdResponse = await axios.get(`https://raw.githubusercontent.com/huggingface/blog/main/${blogId}.md`);
let frontmatter = extractFrontmatter(mdResponse.data);
if (frontmatter) {
displayBlogDetails(frontmatter, blogId, originalBlogMeta);
} else {
showNotFoundMessage();
}
} catch (error) {
console.error("Error fetching blog info:", error);
if (error.response && error.response.status === 404) {
showNotFoundMessage();
} else {
alert("获取博客信息时发生错误,请检查网络连接或博客链接是否正确。");
}
}
});
// 修改 YAML 文件,保持与上游格式完全一致
function modifyYmlFile(ymlContent, blogId, originalMeta) {
let blogs = jsyaml.load(ymlContent);
// 检查是否已存在
let existingIndex = blogs.findIndex(b => b.local === blogId);
if (existingIndex === -1) {
// 创建新条目,使用原始的 date 和 tags
let newEntry = {
local: blogId
};
// 添加 date(如果存在)
if (originalMeta?.date) {
newEntry.date = originalMeta.date;
}
// 添加 tags(如果存在)
if (originalMeta?.tags && originalMeta.tags.length > 0) {
newEntry.tags = originalMeta.tags;
}
blogs.push(newEntry);
}
// 生成 YAML,保持格式整洁
let modifiedYml = jsyaml.dump(blogs, {
lineWidth: -1,
noRefs: true,
flowLevel: -1 // 确保 tags 使用列表格式
});
// 在每个条目之间添加空行(匹配上游格式)
modifiedYml = modifiedYml.replace(/^- local:/gm, '\n- local:');
// 确保文件开头没有多余的空行
return modifiedYml.trim();
}
// 修改 Markdown 文件
function modifyMdFile(mdContent, chineseTitle, hfUsername) {
let lines = mdContent.split('\n');
let inFrontmatter = false;
let frontmatterEndIndex = -1;
let hasTranslators = false;
for (let i = 0; i < lines.length; i++) {
if (lines[i] === '---') {
if (!inFrontmatter) {
inFrontmatter = true;
} else {
frontmatterEndIndex = i;
break;
}
} else if (inFrontmatter) {
// 替换标题
if (lines[i].startsWith('title:')) {
lines[i] = `title: "${chineseTitle}"`;
}
// 检查是否已有 translators
if (lines[i].startsWith('translators:')) {
hasTranslators = true;
}
}
}
// 如果没有 translators,添加它
if (!hasTranslators && frontmatterEndIndex > 0) {
lines.splice(frontmatterEndIndex, 0,
'translators:',
`- user: ${hfUsername}`
);
}
return lines.join('\n');
}
// 下载文件
document.getElementById("downloadFile").addEventListener("click", async function() {
let chineseTitle = document.getElementById("chineseTitle").value.trim();
let hfUsername = document.getElementById("hfUsername").value.trim();
if (!chineseTitle) {
alert("请输入中文标题");
return;
}
if (!hfUsername) {
alert("请输入 HF 用户名");
return;
}
if (!currentBlogData.blogId) {
alert("请先获取博客信息");
return;
}
let blogId = currentBlogData.blogId;
let originalMeta = currentBlogData.originalMeta;
let zip = new JSZip();
let zhFolder = zip.folder("zh");
try {
// 获取并修改 yml 文件
let ymlResponse = await axios.get("https://raw.githubusercontent.com/huggingface-cn/hf-blog-translation/main/zh/_blog.yml");
let modifiedYml = modifyYmlFile(ymlResponse.data, blogId, originalMeta);
zhFolder.file("_blog.yml", modifiedYml);
// 获取并修改 MD 文件
let mdResponse = await axios.get(`https://raw.githubusercontent.com/huggingface/blog/main/${blogId}.md`);
let modifiedMd = modifyMdFile(mdResponse.data, chineseTitle, hfUsername);
zhFolder.file(`${blogId}.md`, modifiedMd);
// 生成并下载 ZIP
let content = await zip.generateAsync({ type: "blob" });
saveAs(content, `${blogId}_translation.zip`);
} catch (error) {
console.error("Error generating the zip file:", error);
alert("生成文件时出错,请检查网络连接或稍后重试。");
}
});
</script>
</body>
</html>