Skip to content

Commit

Permalink
refactory
Browse files Browse the repository at this point in the history
  • Loading branch information
dhammena committed Mar 9, 2022
1 parent 0f46295 commit 715afff
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 92 deletions.
18 changes: 9 additions & 9 deletions abbr/index.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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}`;
}
}
Binary file modified assets/dppn/dict.mdx
Binary file not shown.
65 changes: 32 additions & 33 deletions common/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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 = <IEntry>entry;
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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}`;
}
}
6 changes: 2 additions & 4 deletions common/description.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<p>name: {{fullName}}</p>
<p>name: {{ fullName }}</p>

<p>entries: {{entries}}</p>

<p>json data: {{jsonUrl}}</p>
<p>entries: {{ entries }}</p>

<p>github: https://github.com/dhammena/pali-mdict</p>
20 changes: 10 additions & 10 deletions common/interfaces.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -41,5 +41,5 @@ export interface IAbbr {
export type IEntry = IPts | INcped | IDppn | IAbbr;

export interface IJsonFileGenerator {
generate(destinationPath: string): Promise<void>;
generate(jsonFile: string): Promise<void>;
}
10 changes: 7 additions & 3 deletions common/jsonFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ import { IDictConf, IJsonFileGenerator } from "./interfaces";

export class GeneratorByDownload implements IJsonFileGenerator {
constructor(private conf: IDictConf) {}
async generate(destinationPath: string): Promise<void> {
async generate(jsonFile: string): Promise<void> {
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<void> {
async generate(jsonFile: string): Promise<void> {
console.info("json file generated!");
}
}
2 changes: 1 addition & 1 deletion common/title.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<p>{{title}}</p>
<p>{{ title }}</p>
5 changes: 2 additions & 3 deletions config.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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}`,
Expand Down
18 changes: 9 additions & 9 deletions dppn/index.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
}

Expand Down Expand Up @@ -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}`;
}
}
4 changes: 2 additions & 2 deletions factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
18 changes: 9 additions & 9 deletions ncped/index.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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}`;
}
}
Loading

0 comments on commit 715afff

Please sign in to comment.