| 
									
										
										
										
											2018-03-01 14:07:51 +08:00
										 |  |  | import * as globby from 'globby'; | 
					
						
							| 
									
										
										
										
											2018-03-01 22:19:59 +08:00
										 |  |  | import { DictEntry } from './dict-entry'; | 
					
						
							| 
									
										
										
										
											2018-03-03 08:13:52 +08:00
										 |  |  | import { | 
					
						
							| 
									
										
										
										
											2018-03-24 16:33:17 +08:00
										 |  |  |   extractOriginalContent, | 
					
						
							| 
									
										
										
										
											2018-03-03 08:13:52 +08:00
										 |  |  |   isNotCnPages, | 
					
						
							| 
									
										
										
										
											2018-03-24 16:33:17 +08:00
										 |  |  |   isOnlyBeginTag, hasInlineText, kernelText, | 
					
						
							| 
									
										
										
										
											2018-03-03 11:01:57 +08:00
										 |  |  |   normalizeLines, | 
					
						
							| 
									
										
										
										
											2018-03-03 08:13:52 +08:00
										 |  |  |   originalIsNotChinese, | 
					
						
							| 
									
										
										
										
											2018-03-07 14:19:47 +08:00
										 |  |  |   originalIsNotCodeExampleTag, | 
					
						
							| 
									
										
										
										
											2018-03-07 09:08:44 +08:00
										 |  |  |   originalIsNotOnlyBeginTag, | 
					
						
							| 
									
										
										
										
											2018-03-07 14:19:47 +08:00
										 |  |  |   originalIsNotPureCloseTag, | 
					
						
							|  |  |  |   originalIsNotSpecialDivTag, | 
					
						
							| 
									
										
										
										
											2018-03-03 08:13:52 +08:00
										 |  |  | } from './utils'; | 
					
						
							| 
									
										
										
										
											2018-03-01 14:07:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function splitAndTrim(text = ''): string[] { | 
					
						
							|  |  |  |   return text.split(/\n+\s*\n+/).map(line => line.trim()).filter(line => !!line); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-03 08:13:52 +08:00
										 |  |  | // tslint:disable:max-line-length
 | 
					
						
							|  |  |  | const pattern = /[\u2E80-\u2EFF\u2F00-\u2FDF\u3000-\u303F\u31C0-\u31EF\u3200-\u32FF\u3300-\u33FF\u3400-\u4DBF\u4DC0-\u4DFF\u4E00-\u9FBF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF]/; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-01 14:07:51 +08:00
										 |  |  | export function isTranslation(text) { | 
					
						
							| 
									
										
										
										
											2018-03-03 08:13:52 +08:00
										 |  |  |   return text && pattern.test(text); | 
					
						
							| 
									
										
										
										
											2018-03-01 14:07:51 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function gatherTranslations(text: string): DictEntry[] { | 
					
						
							| 
									
										
										
										
											2018-03-03 08:13:52 +08:00
										 |  |  |   const lines = splitAndTrim(normalizeLines(text)); | 
					
						
							| 
									
										
										
										
											2018-03-01 14:07:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-27 16:34:19 +08:00
										 |  |  |   const result: any[] = []; | 
					
						
							| 
									
										
										
										
											2018-03-01 14:07:51 +08:00
										 |  |  |   for (let i = 1; i < lines.length; ++i) { | 
					
						
							| 
									
										
										
										
											2018-03-03 08:13:52 +08:00
										 |  |  |     const translation = purifyText(lines[i]); | 
					
						
							| 
									
										
										
										
											2018-03-24 16:33:17 +08:00
										 |  |  |     if (hasInlineText(translation)) { | 
					
						
							|  |  |  |       const originalContent = extractOriginalContent(translation); | 
					
						
							|  |  |  |       result.push({ | 
					
						
							|  |  |  |         key: kernelText(originalContent), | 
					
						
							|  |  |  |         original: originalContent, | 
					
						
							|  |  |  |         translation | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } else if (isTranslation(translation)) { | 
					
						
							| 
									
										
										
										
											2018-03-03 11:01:57 +08:00
										 |  |  |       const original = purifyText(lines[i - 1]); | 
					
						
							| 
									
										
										
										
											2018-03-03 11:45:03 +08:00
										 |  |  |       // 对于包裹在 html tag 中的翻译文本进行特殊处理
 | 
					
						
							| 
									
										
										
										
											2018-03-07 09:08:44 +08:00
										 |  |  |       if (isOnlyBeginTag(original)) { | 
					
						
							|  |  |  |         const prevBeginTag = lines[i - 4].trim(); | 
					
						
							| 
									
										
										
										
											2018-03-03 11:45:03 +08:00
										 |  |  |         const prevEndTag = lines[i - 2].trim(); | 
					
						
							|  |  |  |         const thisEndTag = lines[i + 1].trim(); | 
					
						
							| 
									
										
										
										
											2018-03-07 09:08:44 +08:00
										 |  |  |         if (original === prevBeginTag && prevEndTag === thisEndTag) { | 
					
						
							| 
									
										
										
										
											2018-03-03 11:45:03 +08:00
										 |  |  |           result.push({ | 
					
						
							| 
									
										
										
										
											2018-03-24 16:33:17 +08:00
										 |  |  |             key: kernelText(lines[i - 3]), | 
					
						
							| 
									
										
										
										
											2018-03-03 11:45:03 +08:00
										 |  |  |             original: lines[i - 3], | 
					
						
							|  |  |  |             translation: lines[i], | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2018-03-24 16:33:17 +08:00
										 |  |  |         result.push({key: kernelText(original), original, translation}); | 
					
						
							| 
									
										
										
										
											2018-03-03 11:45:03 +08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-03-01 14:07:51 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-03-03 08:13:52 +08:00
										 |  |  |   return result | 
					
						
							|  |  |  |     .filter(isNotCnPages) | 
					
						
							|  |  |  |     .filter(originalIsNotChinese) | 
					
						
							| 
									
										
										
										
											2018-03-07 14:19:47 +08:00
										 |  |  |     .filter(originalIsNotSpecialDivTag) | 
					
						
							|  |  |  |     .filter(originalIsNotCodeExampleTag) | 
					
						
							|  |  |  |     .filter(originalIsNotPureCloseTag) | 
					
						
							| 
									
										
										
										
											2018-03-07 09:08:44 +08:00
										 |  |  |     .filter(originalIsNotOnlyBeginTag) | 
					
						
							| 
									
										
										
										
											2018-03-03 08:13:52 +08:00
										 |  |  |     .map(purifyEntry); | 
					
						
							| 
									
										
										
										
											2018-03-01 14:07:51 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-01 20:11:12 +08:00
										 |  |  | export function listMarkdownFiles(directory: string): string[] { | 
					
						
							|  |  |  |   return globby.sync(directory + '**/*.md'); | 
					
						
							| 
									
										
										
										
											2018-03-01 14:07:51 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function gatherFromMarkdownFile(fileName: string): DictEntry[] { | 
					
						
							|  |  |  |   const fs = require('fs'); | 
					
						
							|  |  |  |   const content = fs.readFileSync(fileName, 'utf-8'); | 
					
						
							|  |  |  |   const entries = gatherTranslations(content); | 
					
						
							|  |  |  |   entries.forEach(entry => entry.sourceFile = fileName); | 
					
						
							|  |  |  |   return entries; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-01 20:11:12 +08:00
										 |  |  | export function gatherFromMarkdownFiles(directory: string): DictEntry[] { | 
					
						
							|  |  |  |   const files = listMarkdownFiles(directory); | 
					
						
							| 
									
										
										
										
											2018-03-01 14:07:51 +08:00
										 |  |  |   const entries = files.map(gatherFromMarkdownFile); | 
					
						
							|  |  |  |   return entries.reduce((result, value) => result.concat(value), []); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-03 08:13:52 +08:00
										 |  |  | export function purifyText(text): string { | 
					
						
							|  |  |  |   return text | 
					
						
							|  |  |  |     .replace(/^(.*)<code-example .*$/, '$1') | 
					
						
							|  |  |  |     .trim(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function purifyEntry(entry: DictEntry): DictEntry { | 
					
						
							|  |  |  |   return { | 
					
						
							| 
									
										
										
										
											2018-03-24 16:33:17 +08:00
										 |  |  |     key: entry.key, | 
					
						
							| 
									
										
										
										
											2018-03-03 08:13:52 +08:00
										 |  |  |     original: purifyText(entry.original), | 
					
						
							|  |  |  |     translation: purifyText(entry.translation), | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-03 11:01:57 +08:00
										 |  |  | 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; | 
					
						
							|  |  |  | } |