parent
19f4ad46ae
commit
b8400fad7c
|
@ -1,7 +1,6 @@
|
||||||
import { expect } from 'chai';
|
import { expect } from 'chai';
|
||||||
import { DictEntry } from './dict-entry';
|
|
||||||
import { dirs } from './dirs';
|
import { dirs } from './dirs';
|
||||||
import { gatherFromMarkdownFiles, gatherTranslations, listMarkdownFiles, splitAndTrim } from './extractor';
|
import { gatherFromDirectory, gatherTranslations, listMarkdownFiles, splitAndTrim, } from './extractor';
|
||||||
|
|
||||||
describe('从对照翻译文件中采集生成字典', () => {
|
describe('从对照翻译文件中采集生成字典', () => {
|
||||||
it('空字符串应该拆分成空数组', function () {
|
it('空字符串应该拆分成空数组', function () {
|
||||||
|
@ -31,7 +30,7 @@ describe('从对照翻译文件中采集生成字典', () => {
|
||||||
expect(result).eql([{original: 'a', translation: '一'}]);
|
expect(result).eql([{original: 'a', translation: '一'}]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('从真实的文件中采集', function () {
|
it('从真实的文件中采集(测试)', function () {
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const content = fs.readFileSync(dirs.content + 'guide/forms.md', 'utf-8');
|
const content = fs.readFileSync(dirs.content + 'guide/forms.md', 'utf-8');
|
||||||
const result = gatherTranslations(content);
|
const result = gatherTranslations(content);
|
||||||
|
@ -49,11 +48,3 @@ describe('从对照翻译文件中采集生成字典', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
function gatherFromDirectory(directory: string, dictFile: string): DictEntry[] {
|
|
||||||
const entries = gatherFromMarkdownFiles(directory);
|
|
||||||
const dict = JSON.stringify(entries, null, 2);
|
|
||||||
const fs = require('fs');
|
|
||||||
fs.writeFileSync(dictFile, dict, 'utf-8');
|
|
||||||
return entries;
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import * as globby from 'globby';
|
import * as globby from 'globby';
|
||||||
import { DictEntry } from './dict-entry';
|
import { DictEntry } from './dict-entry';
|
||||||
import { normalizeLines } from './translate';
|
|
||||||
import {
|
import {
|
||||||
isNotCnPages,
|
isNotCnPages,
|
||||||
|
normalizeLines,
|
||||||
originalIsNotChinese,
|
originalIsNotChinese,
|
||||||
originalIsNotTag,
|
originalIsNotTag,
|
||||||
originalIsOnlyTag,
|
originalIsOnlyTag,
|
||||||
|
@ -26,8 +26,8 @@ export function gatherTranslations(text: string): DictEntry[] {
|
||||||
const result = [];
|
const result = [];
|
||||||
for (let i = 1; i < lines.length; ++i) {
|
for (let i = 1; i < lines.length; ++i) {
|
||||||
const translation = purifyText(lines[i]);
|
const translation = purifyText(lines[i]);
|
||||||
const original = purifyText(lines[i - 1]);
|
|
||||||
if (isTranslation(translation)) {
|
if (isTranslation(translation)) {
|
||||||
|
const original = purifyText(lines[i - 1]);
|
||||||
result.push({original, translation});
|
result.push({original, translation});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,6 +72,10 @@ export function purifyEntry(entry: DictEntry): DictEntry {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const contentDirectory = process.argv[2];
|
export function gatherFromDirectory(directory: string, dictFile: string): DictEntry[] {
|
||||||
|
const entries = gatherFromMarkdownFiles(directory);
|
||||||
gatherFromMarkdownFiles(contentDirectory);
|
const dict = JSON.stringify(entries, null, 2);
|
||||||
|
const fs = require('fs');
|
||||||
|
fs.writeFileSync(dictFile, dict, 'utf-8');
|
||||||
|
return entries;
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { expect } from 'chai';
|
import { expect } from 'chai';
|
||||||
import { dirs } from './dirs';
|
import { dirs } from './dirs';
|
||||||
import { kernelText, lookup, normalizeLines, translate } from './translate';
|
import { kernelText, lookup, translate } from './translate';
|
||||||
|
|
||||||
|
|
||||||
describe('根据字典进行翻译', () => {
|
describe('根据字典进行翻译', () => {
|
||||||
|
@ -12,27 +12,6 @@ describe('根据字典进行翻译', () => {
|
||||||
expect(lookup('# Forms')[0].translation).eql('# 表单');
|
expect(lookup('# Forms')[0].translation).eql('# 表单');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('把“- * 1. #”等处理成空行分隔的格式,以便处理', function () {
|
|
||||||
const lines = normalizeLines('1. abc\n11. def\n');
|
|
||||||
expect(lines).eql('1. abc\n\n11. def\n');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('把 html tag 拆解开', function () {
|
|
||||||
const lines = normalizeLines(`
|
|
||||||
<header>
|
|
||||||
Angular forms don't require a style library
|
|
||||||
</header>
|
|
||||||
`);
|
|
||||||
expect(lines).eq(`
|
|
||||||
|
|
||||||
<header>
|
|
||||||
|
|
||||||
Angular forms don't require a style library
|
|
||||||
|
|
||||||
</header>
|
|
||||||
|
|
||||||
`);
|
|
||||||
});
|
|
||||||
it('自动根据字典翻译单个文件', function () {
|
it('自动根据字典翻译单个文件', function () {
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const content = fs.readFileSync(__dirname + '/../../../../content-en/' + 'guide/forms.md', 'utf-8');
|
const content = fs.readFileSync(__dirname + '/../../../../content-en/' + 'guide/forms.md', 'utf-8');
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
import { DictEntry } from './dict-entry';
|
import { DictEntry } from './dict-entry';
|
||||||
|
import { indentOf, normalizeLines, repeat } from './utils';
|
||||||
|
|
||||||
export const dict = require('./dict-3.json') as DictEntry[];
|
export const dict = require('./dict-3.json') as DictEntry[];
|
||||||
|
|
||||||
|
@ -34,32 +35,3 @@ export function translate(content: string): string[] {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function indentOf(line): number {
|
|
||||||
let pattern = /^( *)[\s\S]*/;
|
|
||||||
if (!pattern.test(line)) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
const leadSpaces = line.replace(pattern, '$1').length;
|
|
||||||
if (/^ *(\d+\.|-|\*) /.test(line)) {
|
|
||||||
return leadSpaces + 3;
|
|
||||||
} else {
|
|
||||||
return leadSpaces;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function repeat(indent: number): string {
|
|
||||||
let result = '';
|
|
||||||
for (let i = 0; i < indent; ++i) {
|
|
||||||
result = result + ' ';
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function normalizeLines(text: string): string {
|
|
||||||
// 列表、标题等自带换行含义的markdown
|
|
||||||
const blockElementPattern = /(?=\n *(\d+\.|-|\*|#|<) )\n/g;
|
|
||||||
const htmlTagPattern = /\n(\s*<.*?>\s*)\n/g;
|
|
||||||
return text.replace(blockElementPattern, '\n\n')
|
|
||||||
.replace(htmlTagPattern, '\n\n$1\n\n');
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
import { expect } from 'chai';
|
||||||
|
import { normalizeLines } from './utils';
|
||||||
|
|
||||||
|
describe(' 工具函数', () => {
|
||||||
|
it('把“- * 1. #”等处理成空行分隔的格式,以便处理', function () {
|
||||||
|
const lines = normalizeLines('1. abc\n11. def\n');
|
||||||
|
expect(lines).eql('1. abc\n\n11. def\n');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('把 html tag 拆解开', function () {
|
||||||
|
const lines = normalizeLines(`
|
||||||
|
<header>
|
||||||
|
Angular forms don't require a style library
|
||||||
|
</header>
|
||||||
|
`);
|
||||||
|
expect(lines).eq(`
|
||||||
|
|
||||||
|
<header>
|
||||||
|
|
||||||
|
Angular forms don't require a style library
|
||||||
|
|
||||||
|
</header>
|
||||||
|
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('把连续的三行及以上空行简化为两个空行', function () {
|
||||||
|
const lines = normalizeLines(`\n a \n\n\n b `);
|
||||||
|
expect(lines).eql(`\n a \n\n b `);
|
||||||
|
});
|
||||||
|
});
|
|
@ -36,3 +36,33 @@ export function isNotCnPages(entry: DictEntry): boolean {
|
||||||
export function isHead(line: string): boolean {
|
export function isHead(line: string): boolean {
|
||||||
return /^#/.test(line);
|
return /^#/.test(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function normalizeLines(text: string): string {
|
||||||
|
// 列表、标题等自带换行含义的markdown
|
||||||
|
const blockElementPattern = /(?=\n *(\d+\.|-|\*|#|<) )\n/g;
|
||||||
|
const htmlTagPattern = /\n(\s*<.*?>\s*?)\n/g;
|
||||||
|
return text.replace(blockElementPattern, '\n\n')
|
||||||
|
.replace(htmlTagPattern, '\n\n$1\n\n')
|
||||||
|
.replace(/\n\n+/, '\n\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
export function indentOf(line): number {
|
||||||
|
let pattern = /^( *)[\s\S]*/;
|
||||||
|
if (!pattern.test(line)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
const leadSpaces = line.replace(pattern, '$1').length;
|
||||||
|
if (/^ *(\d+\.|-|\*) /.test(line)) {
|
||||||
|
return leadSpaces + 3;
|
||||||
|
} else {
|
||||||
|
return leadSpaces;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function repeat(indent: number): string {
|
||||||
|
let result = '';
|
||||||
|
for (let i = 0; i < indent; ++i) {
|
||||||
|
result = result + ' ';
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue