feat: 三行简化为两行

refactor: 抽取出工具函数
This commit is contained in:
Zhicheng Wang 2018-03-03 11:01:57 +08:00
parent 19f4ad46ae
commit b8400fad7c
6 changed files with 74 additions and 67 deletions

View File

@ -1,7 +1,6 @@
import { expect } from 'chai';
import { DictEntry } from './dict-entry';
import { dirs } from './dirs';
import { gatherFromMarkdownFiles, gatherTranslations, listMarkdownFiles, splitAndTrim } from './extractor';
import { gatherFromDirectory, gatherTranslations, listMarkdownFiles, splitAndTrim, } from './extractor';
describe('从对照翻译文件中采集生成字典', () => {
it('空字符串应该拆分成空数组', function () {
@ -31,7 +30,7 @@ describe('从对照翻译文件中采集生成字典', () => {
expect(result).eql([{original: 'a', translation: '一'}]);
});
it('从真实的文件中采集', function () {
it('从真实的文件中采集(测试)', function () {
const fs = require('fs');
const content = fs.readFileSync(dirs.content + 'guide/forms.md', 'utf-8');
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;
}

View File

@ -1,8 +1,8 @@
import * as globby from 'globby';
import { DictEntry } from './dict-entry';
import { normalizeLines } from './translate';
import {
isNotCnPages,
normalizeLines,
originalIsNotChinese,
originalIsNotTag,
originalIsOnlyTag,
@ -26,8 +26,8 @@ export function gatherTranslations(text: string): DictEntry[] {
const result = [];
for (let i = 1; i < lines.length; ++i) {
const translation = purifyText(lines[i]);
const original = purifyText(lines[i - 1]);
if (isTranslation(translation)) {
const original = purifyText(lines[i - 1]);
result.push({original, translation});
}
}
@ -72,6 +72,10 @@ export function purifyEntry(entry: DictEntry): DictEntry {
};
}
const contentDirectory = process.argv[2];
gatherFromMarkdownFiles(contentDirectory);
export 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;
}

View File

@ -1,6 +1,6 @@
import { expect } from 'chai';
import { dirs } from './dirs';
import { kernelText, lookup, normalizeLines, translate } from './translate';
import { kernelText, lookup, translate } from './translate';
describe('根据字典进行翻译', () => {
@ -12,27 +12,6 @@ describe('根据字典进行翻译', () => {
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 () {
const fs = require('fs');
const content = fs.readFileSync(__dirname + '/../../../../content-en/' + 'guide/forms.md', 'utf-8');

View File

@ -1,5 +1,6 @@
import * as _ from 'lodash';
import { DictEntry } from './dict-entry';
import { indentOf, normalizeLines, repeat } from './utils';
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');
}

View File

@ -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 `);
});
});

View File

@ -36,3 +36,33 @@ export function isNotCnPages(entry: DictEntry): boolean {
export function isHead(line: string): boolean {
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;
}