diff --git a/abbr/index.ts b/abbr/index.ts index b413ea6..431f221 100644 --- a/abbr/index.ts +++ b/abbr/index.ts @@ -1,12 +1,12 @@ import fs from "fs"; import { render } from "template-file"; -import { BaseMaker } from "../common/classes"; +import { MakerBase } from "../common/classes"; import { IAbbr, IDictConf } from "../common/interfaces"; import { GeneratorByHand } from "../common/jsonFileGenerator"; -import { FILENAME_MAP } from "../config"; +import { FILENAME } from "../config"; -export class Maker extends BaseMaker { +export class Maker extends MakerBase { constructor(conf: IDictConf) { let jsonFileGenerator = new GeneratorByHand(conf); super(conf, jsonFileGenerator); @@ -16,18 +16,18 @@ export class Maker extends BaseMaker { const data = { entry: entry.abbr, type: entry.type, - cssFileName: FILENAME_MAP.css, + cssFileName: FILENAME.css, text: entry.text, }; - const template = fs.readFileSync(this.entryTemplateFile, "utf8"); + const template = fs.readFileSync(this._entryTemplateFile, "utf8"); return render(template, data); } - protected get jsonFile(): string { - return `${__dirname}/${FILENAME_MAP.json}`; + protected get _jsonFile(): string { + return `${__dirname}/${FILENAME.json}`; } - protected get entryTemplateFile(): string { - return `${__dirname}/${FILENAME_MAP.entryTemplate}`; + protected get _entryTemplateFile(): string { + return `${__dirname}/${FILENAME.entryTemplate}`; } } diff --git a/assets/dppn/dict.mdx b/assets/dppn/dict.mdx index 2c02765..d517221 100644 Binary files a/assets/dppn/dict.mdx and b/assets/dppn/dict.mdx differ diff --git a/common/classes.ts b/common/classes.ts index f8a1bdc..561fd2a 100644 --- a/common/classes.ts +++ b/common/classes.ts @@ -3,24 +3,24 @@ import execSh from "exec-sh"; import { render } from "template-file"; import { IEntry, IDictConf, IJsonFileGenerator } from "./interfaces"; -import { FILENAME_MAP } from "../config" +import { FILENAME } from "../config" -export abstract class BaseMaker { +export abstract class MakerBase { constructor( private conf: IDictConf, private jsonFileGenerator: IJsonFileGenerator ) {} - public clean(): void { + clean(): void { console.log("remove temporary files ...\n"); - if (fs.existsSync(this.txtOutputFile)) { - fs.unlinkSync(this.txtOutputFile); + if (fs.existsSync(this._txtOutputFile)) { + fs.unlinkSync(this._txtOutputFile); } - if (fs.existsSync(this.titleOutputFile)) { - fs.unlinkSync(this.titleOutputFile); + if (fs.existsSync(this._titleOutputFile)) { + fs.unlinkSync(this._titleOutputFile); } - if (fs.existsSync(this.descriptionOutputFile)) { - fs.unlinkSync(this.descriptionOutputFile); + if (fs.existsSync(this._descriptionOutputFile)) { + fs.unlinkSync(this._descriptionOutputFile); } } @@ -41,16 +41,16 @@ export abstract class BaseMaker { fs.mkdirSync(this.conf.outputDir, { recursive: true }); } if (download) { - await this.jsonFileGenerator.generate(this.jsonFile); + await this.jsonFileGenerator.generate(this._jsonFile); } - if (!fs.existsSync(this.jsonFile)) { - await this.jsonFileGenerator.generate(this.jsonFile); + if (!fs.existsSync(this._jsonFile)) { + await this.jsonFileGenerator.generate(this._jsonFile); } } private _generateTxtStr(): string { let result: string = ""; - const jsonData = fs.readFileSync(this.jsonFile); + const jsonData = fs.readFileSync(this._jsonFile); let json = JSON.parse(jsonData.toString()); for (let entry of json) { entry = entry; @@ -65,7 +65,7 @@ export abstract class BaseMaker { private _makeTxtFile(htmlStr: string): void { // Replace LF with CRLF for bug fixing of mdx builder. // htmlStr = htmlStr.replace(/[^\r]\n/g, "\r\n"); - fs.writeFileSync(this.txtOutputFile, htmlStr, "utf-8"); + fs.writeFileSync(this._txtOutputFile, htmlStr, "utf-8"); } private _makeTitleFile(forEudic: boolean) { @@ -76,27 +76,26 @@ export abstract class BaseMaker { const data = { title: this.conf.shortName.toLocaleUpperCase(), }; - const template = fs.readFileSync(this.titleTemplateFile, "utf8"); + const template = fs.readFileSync(this._titleTemplateFile, "utf8"); htmlStr = render(template, data); } - fs.writeFileSync(this.titleOutputFile, htmlStr, "utf-8"); + fs.writeFileSync(this._titleOutputFile, htmlStr, "utf-8"); } private _makeDescriptionFile() { - const jsonData = fs.readFileSync(this.jsonFile); + const jsonData = fs.readFileSync(this._jsonFile); let json = JSON.parse(jsonData.toString()); const data = { fullName: this.conf.fullName, - jsonUrl: this.conf.jsonUrl, entries: json.length, }; - const template = fs.readFileSync(this.descriptionTemplateFile, "utf8"); + const template = fs.readFileSync(this._descriptionTemplateFile, "utf8"); const htmlStr = render(template, data); - fs.writeFileSync(this.descriptionOutputFile, htmlStr, "utf-8"); + fs.writeFileSync(this._descriptionOutputFile, htmlStr, "utf-8"); } private async _makeMdxFile() { - const command = `mdict --title ${FILENAME_MAP.title} --description ${FILENAME_MAP.description} -a ${FILENAME_MAP.txt} ${FILENAME_MAP.mdx}`; + const command = `mdict --title ${FILENAME.title} --description ${FILENAME.description} -a ${FILENAME.txt} ${FILENAME.mdx}`; try { let result = await execSh.promise(command, { cwd: this.conf.outputDir }); console.info(result.stdout); @@ -106,27 +105,27 @@ export abstract class BaseMaker { } } - protected abstract get jsonFile(): string; + protected abstract get _jsonFile(): string; - protected abstract get entryTemplateFile(): string; + protected abstract get _entryTemplateFile(): string; - private get txtOutputFile(): string { - return `${this.conf.outputDir}/${FILENAME_MAP.txt}`; + private get _txtOutputFile(): string { + return `${this.conf.outputDir}/${FILENAME.txt}`; } - private get titleTemplateFile(): string { - return `${__dirname}/${FILENAME_MAP.title}`; + private get _titleTemplateFile(): string { + return `${__dirname}/${FILENAME.title}`; } - private get titleOutputFile(): string { - return `${this.conf.outputDir}/${FILENAME_MAP.title}`; + private get _titleOutputFile(): string { + return `${this.conf.outputDir}/${FILENAME.title}`; } - private get descriptionTemplateFile(): string { - return `${__dirname}/${FILENAME_MAP.description}`; + private get _descriptionTemplateFile(): string { + return `${__dirname}/${FILENAME.description}`; } - private get descriptionOutputFile(): string { - return `${this.conf.outputDir}/${FILENAME_MAP.description}`; + private get _descriptionOutputFile(): string { + return `${this.conf.outputDir}/${FILENAME.description}`; } } diff --git a/common/description.html b/common/description.html index 3fe6c90..9b246fa 100644 --- a/common/description.html +++ b/common/description.html @@ -1,7 +1,5 @@ -

name: {{fullName}}

+

name: {{ fullName }}

-

entries: {{entries}}

- -

json data: {{jsonUrl}}

+

entries: {{ entries }}

github: https://github.com/dhammena/pali-mdict

\ No newline at end of file diff --git a/common/interfaces.ts b/common/interfaces.ts index a0f0568..4a47cc4 100644 --- a/common/interfaces.ts +++ b/common/interfaces.ts @@ -1,18 +1,18 @@ export interface IDictConf { - jsonUrl: string; + jsonUrl?: string; fullName: string; shortName: string; outputDir: string; } -export interface IFileNameMap { - entryTemplate: string; // layout must compliance with the mdict standard - json: string; // name of dict's json file - css: string; // dict stylesheet - txt: string; // for building mdx with mdx-builder - title: string; // for building mdx with mdx-builder - description: string; // for building mdx with mdx-builder - mdx: string; // result mdict file +export interface IFileName { + entryTemplate: string; // layout must compliance with the mdict standard + json: string; // name of dict's json file + css: string; // dict stylesheet + txt: string; // for building mdx with mdx-builder + title: string; // for building mdx with mdx-builder + description: string; // for building mdx with mdx-builder + mdx: string; // result mdict file } export interface IPts { @@ -41,5 +41,5 @@ export interface IAbbr { export type IEntry = IPts | INcped | IDppn | IAbbr; export interface IJsonFileGenerator { - generate(destinationPath: string): Promise; + generate(jsonFile: string): Promise; } diff --git a/common/jsonFileGenerator.ts b/common/jsonFileGenerator.ts index 2b2e1bc..05ab975 100644 --- a/common/jsonFileGenerator.ts +++ b/common/jsonFileGenerator.ts @@ -4,16 +4,20 @@ import { IDictConf, IJsonFileGenerator } from "./interfaces"; export class GeneratorByDownload implements IJsonFileGenerator { constructor(private conf: IDictConf) {} - async generate(destinationPath: string): Promise { + async generate(jsonFile: string): Promise { console.info("downloading json file..."); - fs.writeFileSync(destinationPath, await download(this.conf.jsonUrl)); + if (this.conf.jsonUrl) { + fs.writeFileSync(jsonFile, await download(this.conf.jsonUrl)); + } else { + throw Error("invalid json url!") + } console.info("download finished!"); } } export class GeneratorByHand implements IJsonFileGenerator { constructor(private conf: IDictConf) {} - async generate(destinationPath: string): Promise { + async generate(jsonFile: string): Promise { console.info("json file generated!"); } } diff --git a/common/title.html b/common/title.html index 53ccc03..26e41cc 100644 --- a/common/title.html +++ b/common/title.html @@ -1 +1 @@ -

{{title}}

\ No newline at end of file +

{{ title }}

\ No newline at end of file diff --git a/config.ts b/config.ts index b79c467..1eac41b 100644 --- a/config.ts +++ b/config.ts @@ -1,6 +1,6 @@ -import { IDictConf, IFileNameMap } from "./common/interfaces"; +import { IDictConf, IFileName as IFileName } from "./common/interfaces"; -export const FILENAME_MAP: IFileNameMap = { +export const FILENAME: IFileName = { entryTemplate: 'entry.html', json: "dict.json", css: "dict.css", @@ -44,7 +44,6 @@ export const DICTIONARY: dictionary = { outputDir: `${ASSETS_DIR}/${DictEnum.NCPED}`, }, [DictEnum.ABBR]: { - jsonUrl:`${__dirname}/${DictEnum.ABBR}/dict.json`, fullName: "New Concise Pali-English Dictionary", shortName: DictEnum.ABBR, outputDir: `${ASSETS_DIR}/${DictEnum.ABBR}`, diff --git a/dppn/index.ts b/dppn/index.ts index e27ff10..3e154c9 100644 --- a/dppn/index.ts +++ b/dppn/index.ts @@ -1,12 +1,12 @@ import fs from "fs"; import { render } from "template-file"; -import { BaseMaker } from "../common/classes"; +import { MakerBase } from "../common/classes"; import { IDppn, IDictConf } from "../common/interfaces"; import { GeneratorByDownload } from "../common/jsonFileGenerator"; -import { FILENAME_MAP } from "../config"; +import { FILENAME } from "../config"; -export class Maker extends BaseMaker { +export class Maker extends MakerBase { constructor(conf: IDictConf) { let jsonFileGenerator = new GeneratorByDownload(conf); super(conf, jsonFileGenerator); @@ -17,9 +17,9 @@ export class Maker extends BaseMaker { entry: entry.word, type: this._getEntryType(entry.text), textHtml: this._rmRedundanceDtTag(entry.text), - cssFileName: FILENAME_MAP.css, + cssFileName: FILENAME.css, }; - const template = fs.readFileSync(this.entryTemplateFile, "utf8"); + const template = fs.readFileSync(this._entryTemplateFile, "utf8"); return render(template, data); } @@ -57,11 +57,11 @@ export class Maker extends BaseMaker { return result; } - protected get jsonFile(): string { - return `${__dirname}/${FILENAME_MAP.json}`; + protected get _jsonFile(): string { + return `${__dirname}/${FILENAME.json}`; } - protected get entryTemplateFile(): string { - return `${__dirname}/${FILENAME_MAP.entryTemplate}`; + protected get _entryTemplateFile(): string { + return `${__dirname}/${FILENAME.entryTemplate}`; } } diff --git a/factory.ts b/factory.ts index 821a836..16b1dbd 100644 --- a/factory.ts +++ b/factory.ts @@ -2,11 +2,11 @@ import { Maker as NcpedMaker } from "./ncped"; import { Maker as DppnMaker } from "./dppn"; import { Maker as PtsMaker } from "./pts"; import { Maker as AbbrMaker } from "./abbr"; -import { BaseMaker } from "./common/classes"; +import { MakerBase } from "./common/classes"; import { DictEnum, DICTIONARY } from "./config"; export class MakerFactory { - static create(dict: DictEnum): BaseMaker { + static create(dict: DictEnum): MakerBase { switch (dict) { case DictEnum.PTS: return new PtsMaker(DICTIONARY[dict]); diff --git a/ncped/index.ts b/ncped/index.ts index c248a3b..ee100b4 100644 --- a/ncped/index.ts +++ b/ncped/index.ts @@ -1,12 +1,12 @@ import fs from "fs"; import { render } from "template-file"; -import { BaseMaker } from "../common/classes"; +import { MakerBase } from "../common/classes"; import { INcped, IDictConf } from "../common/interfaces"; import { GeneratorByDownload } from "../common/jsonFileGenerator"; -import { FILENAME_MAP } from "../config"; +import { FILENAME } from "../config"; -export class Maker extends BaseMaker { +export class Maker extends MakerBase { constructor(conf: IDictConf) { let jsonFileGenerator = new GeneratorByDownload(conf); super(conf, jsonFileGenerator); @@ -52,19 +52,19 @@ export class Maker extends BaseMaker { const data = { entry: entry.entry, grammarHtml: grammarHtml, - cssFileName: FILENAME_MAP.css, + cssFileName: FILENAME.css, definitionHtml: definitionHtml, xfHtml: xfHtml, }; - const template = fs.readFileSync(this.entryTemplateFile, "utf8"); + const template = fs.readFileSync(this._entryTemplateFile, "utf8"); return render(template, data); } - protected get jsonFile(): string { - return `${__dirname}/${FILENAME_MAP.json}`; + protected get _jsonFile(): string { + return `${__dirname}/${FILENAME.json}`; } - protected get entryTemplateFile(): string { - return `${__dirname}/${FILENAME_MAP.entryTemplate}`; + protected get _entryTemplateFile(): string { + return `${__dirname}/${FILENAME.entryTemplate}`; } } diff --git a/pts/index.ts b/pts/index.ts index e7fd853..edcb300 100644 --- a/pts/index.ts +++ b/pts/index.ts @@ -1,12 +1,12 @@ import fs from "fs"; import { render } from "template-file"; -import { BaseMaker } from "../common/classes"; +import { MakerBase } from "../common/classes"; import { IPts, IDictConf } from "../common/interfaces"; import { GeneratorByDownload } from "../common/jsonFileGenerator"; -import { FILENAME_MAP } from "../config"; +import { FILENAME } from "../config"; -export class Maker extends BaseMaker { +export class Maker extends MakerBase { constructor(conf: IDictConf) { let jsonFileGenerator = new GeneratorByDownload(conf); super(conf, jsonFileGenerator); @@ -22,9 +22,9 @@ export class Maker extends BaseMaker { entry: entry.word, entryGrammarHtml, textHtml: this._generateTextHtml(entry, isSingleDdEntry), - cssFileName: FILENAME_MAP.css, + cssFileName: FILENAME.css, }; - const template = fs.readFileSync(this.entryTemplateFile, "utf8"); + const template = fs.readFileSync(this._entryTemplateFile, "utf8"); return render(template, data); } @@ -96,11 +96,11 @@ export class Maker extends BaseMaker { return text.replace(regexp, ""); } - protected get jsonFile(): string { - return `${__dirname}/${FILENAME_MAP.json}`; + protected get _jsonFile(): string { + return `${__dirname}/${FILENAME.json}`; } - protected get entryTemplateFile(): string { - return `${__dirname}/${FILENAME_MAP.entryTemplate}`; + protected get _entryTemplateFile(): string { + return `${__dirname}/${FILENAME.entryTemplate}`; } }