This repository was archived by the owner on Mar 22, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathscaffold.mjs
195 lines (159 loc) · 4.79 KB
/
scaffold.mjs
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
import { get } from 'https';
import { createWriteStream } from 'fs';
import { Transform } from 'stream';
import path from 'path';
import chalk from 'chalk';
import { parse } from '@textlint/markdown-to-ast';
import { __ } from './translate.mjs';
function main() {
const [URL, DATE] = process.argv.slice(2);
const POST_PATH = path.resolve('source', '_posts');
if (!URL) {
console.error(`🚨 번역할 nodejs.org의 블로그 글 URL이 필요합니다.`);
printGuide();
return;
}
const rawURL = URL.replace(/^https:\/\/nodejs.org(\/en\/blog\/.+?)\/?$/, "https://raw.githubusercontent.com/nodejs/nodejs.org/master/locale$1.md")
if (isWeeklyUpdate(rawURL) && !DATE) {
console.error(`🚨 주간 뉴스 외에는 url 뒤에 발행 일자를 전달해야 합니다.`);
printGuide();
return;
}
const fileName = getFileName(rawURL, DATE);
if (!fileName) {
console.error(`🚨 지원하지 않는 형식의 URL입니다.`);
printGuide();
return;
}
if (!process.env.NODE_TRANSLATOR) {
console.warn(`ℹ️ 번역자의 사용자명이 설정되어 있지 않습니다. ${chalk.italic('NODE_TRANSLATOR')} 환경 변수의 값을 설정하세요.`);
console.warn('e.g. NODE_TRANSLATOR=username npm run scaffold ...\n');
}
const filePath = `${POST_PATH}/${fileName}`;
get(rawURL, resp => {
resp
.pipe(transformer(rawURL))
.pipe(createWriteStream(filePath));
console.log(`✅ 번역 파일이 생성되었습니다: ${filePath}`);
});
}
main();
function printGuide() {
console.error('자세한 내용은 https://nodejs.github.io/nodejs-ko/CONTRIBUTING.html 을 참고하세요.\n');
}
function isWeeklyUpdate(url) {
return url.includes('weekly-updates');
}
function getFileName(url, date) {
try {
if (isWeeklyUpdate(url)) {
return `${url.match(/\d{4}-\d{2}-\d{2}/)[0]}-weekly.md`;
}
return url.match(/blog\/.+\.md/)[0].replace(/\//g, '-').replace('blog', date);
} catch(e) {
return;
}
}
function transformer(url) {
let buffer = '';
function transform(chunk, _, cb) {
buffer += chunk.toString();
cb();
}
function flush() {
const ast = parse(buffer);
const result = processAst(ast, url);
this.push(result);
buffer = '';
}
return new Transform({
transform,
flush,
});
}
function processAst(ast, url) {
let result = [];
for(let i = 0; i < ast.children.length; i++) {
const cur = ast.children[i];
switch(cur.type) {
case 'Yaml': // header
result.push(processMetaInfo(cur, url));
break;
case 'Header':
case 'Paragraph':
const idx = findNextHeader(ast.children, i);
const endIdx = idx > 0 ? idx : ast.children.length;
const blocks = ast.children.slice(i, endIdx);
result.push(processContent(blocks));
i = endIdx - 1;
break;
default: // Do not translate unknown types
result.push(cur.value || cur.raw);
break;
}
}
return result.join('\n\n');
}
function processMetaInfo(block, url) {
const meta = parseMeta(block.value);
const articlePath = (/blog\/(.+)\.md/.exec(url) || ['', ''])[1];
return `
---
category: ${isWeeklyUpdate(url) ? 'weekly' : 'articles'}
title: ${translateTitle(meta.title)}
author: ${meta.author || ''}
ref: ${meta.title || ''}
refurl: https://nodejs.org/en/blog/${articlePath}
translator: ${process.env.NODE_TRANSLATOR || ''}
---
`.trim();
}
function parseMeta(text) {
const regex = /^([\w-]+):\s*(.+)$/gm;
const infoSet = {};
let match = null;
while(match = regex.exec(text)) {
infoSet[match[1]] = match[2];
}
return infoSet;
}
function translateTitle(title) {
let match = null;
// Node.js releases
if (match = /^Node v(?<version>[\d\.]+) \((?<type>Current|LTS|Maintenance|Stable)\)$/.exec(title)) {
const type = __(match.groups.type);
return `Node v${match.groups.version}(${type})`;
}
// Security releases
if (match = /^(?<date>[A-Za-z]+ \d+) Security Releases$/.exec(title)) {
const date = new Date(`1 ${match.groups.date}`);
return `${date.getFullYear()}년 ${date.getMonth() + 1}월 보안 릴리스`;
}
return '';
}
function findNextHeader(blocks, from) {
let i = from;
while(++i < blocks.length) {
if (blocks[i].type === 'Header') return i;
}
return -1;
}
function processContent(blocks) {
const rawContent = blocks.map(b => b.raw).join('\n\n');
if (blocks[0].type === 'Header') {
const header = blocks[0].raw.trim();
// Do not translate these blocks
if (/\b[Cc]ommits|SHASUMS$/.test(header)) {
return rawContent;
}
blocks[0].raw = header.replace(/^(#+)\s+(.+)$/, (_, symbols, text) => {
return `${symbols} ${__(text)}`;
});
}
return `
<!--
${rawContent}
-->
${blocks.map(b => __(b.raw)).join('\n\n')}
`.trim();
}