feat: 中英字典收集工具

This commit is contained in:
Zhicheng Wang 2018-03-01 20:11:12 +08:00
parent 6120300d4a
commit 1b786e0a72
5 changed files with 60302 additions and 188313 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -31,19 +31,19 @@ describe('gather to dictionary', () => {
it('should gather from real file', function () { it('should gather from real file', function () {
const fs = require('fs'); const fs = require('fs');
const content = fs.readFileSync(__dirname + '/../../content/guide/forms.md', 'utf-8'); const content = fs.readFileSync(__dirname + '/../guide/forms.md', 'utf-8');
const result = gatherTranslations(content); const result = gatherTranslations(content);
expect(result[0]).eql({original: '# Forms', translation: '# 表单'}); expect(result[0]).eql({original: '# Forms', translation: '# 表单'});
}); });
it('should list files recursive', function () { it('should list files recursive', function () {
expect(listMarkdownFiles().length).greaterThan(10); expect(listMarkdownFiles(__dirname + '/../').length).greaterThan(10);
}); });
it('should gather from directory', () => { it('should gather from directory', () => {
const entries = gatherFromMarkdownFiles(); const entries = gatherFromMarkdownFiles(__dirname + '/../');
const dict = JSON.stringify(entries, null, 2); const dict = JSON.stringify(entries, null, 2);
const fs = require('fs'); const fs = require('fs');
fs.writeFileSync(__dirname + '/../../content/dict.json', dict, 'utf-8'); fs.writeFileSync(__dirname + '/../dict/dict-2.json', dict, 'utf-8');
expect(entries.length).greaterThan(100); expect(entries.length).greaterThan(100);
}); });
}); });

View File

@ -29,8 +29,8 @@ export function gatherTranslations(text: string): DictEntry[] {
return result; return result;
} }
export function listMarkdownFiles(): string[] { export function listMarkdownFiles(directory: string): string[] {
return globby.sync(__dirname + '/../**/*.md'); return globby.sync(directory + '**/*.md');
} }
export function gatherFromMarkdownFile(fileName: string): DictEntry[] { export function gatherFromMarkdownFile(fileName: string): DictEntry[] {
@ -41,13 +41,12 @@ export function gatherFromMarkdownFile(fileName: string): DictEntry[] {
return entries; return entries;
} }
export function gatherFromMarkdownFiles(): DictEntry[] { export function gatherFromMarkdownFiles(directory: string): DictEntry[] {
const files = listMarkdownFiles(); const files = listMarkdownFiles(directory);
const entries = files.map(gatherFromMarkdownFile); const entries = files.map(gatherFromMarkdownFile);
return entries.reduce((result, value) => result.concat(value), []); return entries.reduce((result, value) => result.concat(value), []);
} }
const entries = gatherFromMarkdownFiles(); const contentDirectory = process.argv[2];
const dict = JSON.stringify(entries, null, 2);
const fs = require('fs'); gatherFromMarkdownFiles(contentDirectory);
fs.writeFileSync(__dirname + '/dict-current.json', dict, 'utf-8');