fix: header 的拆解

This commit is contained in:
Zhicheng Wang 2018-03-06 17:08:10 +08:00
parent 206f6ecc4f
commit 73e4efa80d
7 changed files with 761 additions and 429 deletions

View File

@ -0,0 +1,8 @@
#!/usr/bin/env ts-node
import { dirs } from '../dirs';
import { gatherFromDirectory } from '../extractor';
import { translateDirectory } from '../translate';
gatherFromDirectory(dirs.aio + '../../content-1/', dirs.here + 'dict-1.json');
gatherFromDirectory(dirs.aio + '../../content-2/', dirs.here + 'dict-2.json');
gatherFromDirectory(dirs.aio + '../../content-3/', dirs.here + 'dict-3.json');

View File

@ -3,4 +3,5 @@
import { dirs } from '../dirs';
import { translateFile } from '../translate';
translateFile(__dirname + '/../../../../../content-en/' + 'guide/http.md', dirs.content + 'guide/http.md');
const filename = 'aot-compiler.md';
translateFile(__dirname + '/../../../../../content-en/' + 'guide/' + filename, dirs.content + 'guide/' + filename);

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
import { expect } from 'chai';
import { dirs } from './dirs';
import { gatherFromDirectory, gatherTranslations, listMarkdownFiles, splitAndTrim, } from './extractor';
import { gatherTranslations, listMarkdownFiles, splitAndTrim, } from './extractor';
describe('从对照翻译文件中采集生成字典', () => {
it('空字符串应该拆分成空数组', function () {
@ -47,19 +47,12 @@ describe('从对照翻译文件中采集生成字典', () => {
it('从真实的文件中采集(测试)', function () {
const fs = require('fs');
const content = fs.readFileSync(dirs.content + 'guide/forms.md', 'utf-8');
const content = fs.readFileSync(dirs.content + 'guide/ajs-quick-reference.md', 'utf-8');
const result = gatherTranslations(content);
expect(result[0]).eql({original: '# Forms', translation: '# 表单'});
expect(result).eql({original: '# Forms', translation: '# 表单'});
});
it('递归查找所有 markdown 文件', function () {
expect(listMarkdownFiles(dirs.content).length).greaterThan(10);
});
it('从对照文本的文件夹中采集生成字典(非测试)', () => {
gatherFromDirectory(dirs.aio + '../../content-1/', dirs.here + 'dict-1.json');
gatherFromDirectory(dirs.aio + '../../content-2/', dirs.here + 'dict-2.json');
gatherFromDirectory(dirs.aio + '../../content-3/', dirs.here + 'dict-3.json');
});
});

View File

@ -13,6 +13,5 @@ describe('根据字典进行翻译', () => {
});
it('对包裹在 a 标签中的内容查字典', () => {
expect(lookup('<a href=""># Forms</a>')[0].translation).eql('# 表单');
});
});

View File

@ -23,7 +23,21 @@ def`);
expect(lines).eql('\n\n# abc\n\ndef');
});
it('把 html tag 拆解开', function () {
it('拆解单行的成对 tag', function () {
const lines = normalizeLines(`
a
<div class="abc">DEF</div>
b
`);
expect(lines).eql(`
a
<div class="abc">DEF</div>
b
`);
});
it('拆解多行的成对 tag', function () {
const lines = normalizeLines(`
<header>
Angular forms don't require a style library

View File

@ -53,15 +53,12 @@ export function normalizeLines(text: string): string {
text = text.replace(hxPattern, '\n$1\n\n');
const leadHxPattern = /^( *#.*)\n/g;
text = text.replace(leadHxPattern, '$1\n\n');
const htmlTagPattern = /(\s*)<(\/?\w+)(.*?)>(\s*?)\n/g;
text = text.replace(htmlTagPattern, (line, _1, _2, _3, _4) => {
if (_2 === 'a') {
return line;
} else {
return `\n\n${line}\n\n`;
}
});
text = text.replace(/\n\s*\n+/g, '\n\n');
const oneLinePairedTagPattern = /(\n *<div +[^> \n]*>[^<\n]*<\/div> *\n)/g;
text = text.replace(oneLinePairedTagPattern, '\n\n$1\n\n');
const multiLinePairedTagPattern = /\n( *)<(header)( *[^> \n]*)>\n?(.*?)\n?( *)<\/\2>( *)\n/g;
text = text.replace(multiLinePairedTagPattern, '\n\n$1<$2$3>\n\n$4\n\n$5</$2>$6\n\n');
const multipleBlankLinePattern = /\n\s*\n+/g;
text = text.replace(multipleBlankLinePattern, '\n\n');
return text;
}