diff --git a/package.json b/package.json index 84348fc..efb867f 100644 --- a/package.json +++ b/package.json @@ -12,11 +12,13 @@ "dependencies": { "axios": "^0.26.1", "axios-rate-limit": "^1.3.0", + "fs-extra": "^10.1.0", "js-yaml": "^4.1.0", "nanoid": "^3.3.2", "wanakana": "^4.0.2" }, "devDependencies": { + "@types/fs-extra": "^9.0.13", "@types/js-yaml": "^4.0.5", "@types/node": "^17.0.23", "@types/wanakana": "^4.0.3", diff --git a/scripts/anki-template.ts b/scripts/anki-template.ts new file mode 100644 index 0000000..1f0e352 --- /dev/null +++ b/scripts/anki-template.ts @@ -0,0 +1,82 @@ +import path from 'path'; + +import { AnkiConnect, IAnkiConnectActions } from '@/ankiconnect'; +import { + ensureDirSync, + readFileSync, + readdirSync, + writeFileSync, +} from 'fs-extra'; + +export class AnkiTemplateEditor { + $connect = new AnkiConnect(); + + templateDir = 'template'; + rootDir = path.resolve(this.templateDir, this.modelName); + styleFilename = 'style.css'; + + constructor(public modelName: string) { + ensureDirSync(this.rootDir); + } + + async get() { + const d = await this.$connect.api('modelTemplates', { + modelName: this.modelName, + }); + + Object.entries(d).map(([cardName, sideDict]) => { + const folderDir = path.resolve(this.rootDir, cardName); + ensureDirSync(folderDir); + Object.entries(sideDict).map(([side, html]) => { + writeFileSync(path.resolve(folderDir, side + '.html'), html); + }); + }); + + const s = await this.$connect.api('modelStyling', { + modelName: this.modelName, + }); + + writeFileSync(path.resolve(this.rootDir, this.styleFilename), s.css); + } + + async set() { + const templates: IAnkiConnectActions['updateModelTemplates']['params']['model']['templates'] = + {}; + + for (const p of readdirSync(this.rootDir)) { + if (p === this.styleFilename) { + await this.$connect.api('updateModelStyling', { + model: { + name: this.modelName, + css: readFileSync( + path.resolve(this.rootDir, this.styleFilename), + 'utf-8', + ), + }, + }); + continue; + } + + readdirSync(path.resolve(this.rootDir, p)).map((p1) => { + const side = p1.replace(/\.[^\.]+$/, '') as 'Front' | 'Back'; + const sideMap = templates[p] || {}; + sideMap[side] = readFileSync( + path.resolve(this.rootDir, p, p1), + 'utf-8', + ); + templates[p] = sideMap; + }); + } + + await this.$connect.api('updateModelTemplates', { + model: { + name: this.modelName, + templates, + }, + }); + } +} + +if (require.main === module) { + new AnkiTemplateEditor('jp.takoboto').set(); +} diff --git a/template/jp.takoboto/EJ/Back.html b/template/jp.takoboto/EJ/Back.html new file mode 100644 index 0000000..4c06113 --- /dev/null +++ b/template/jp.takoboto/EJ/Back.html @@ -0,0 +1,209 @@ +{{FrontSide}} + +
{{furigana:Japanese}}
+ +{{#JapaneseAlt}} +{{JapaneseAlt}}
+{{/JapaneseAlt}} + +
+ {{#Pitch}}
+ {{Pitch}}
+ {{/Pitch}}
+
+ {{^Pitch}}
+ {{Reading}}
+ {{/Pitch}}
+
+ {{^JapaneseAudio}}
+
+
{{Mnemonic}}
+{{Meaning}}
+{{/MeaningQuiz}} + +{{#MeaningQuiz}} +{{MeaningQuiz}}
+{{/MeaningQuiz}} \ No newline at end of file diff --git a/template/jp.takoboto/JE/Back.html b/template/jp.takoboto/JE/Back.html new file mode 100644 index 0000000..2895b1b --- /dev/null +++ b/template/jp.takoboto/JE/Back.html @@ -0,0 +1,197 @@ +{{FrontSide}} + +{{JapaneseAlt}}
+ ++ {{#Pitch}} + {{Pitch}} + {{/Pitch}} + + {{^Pitch}} + {{Reading}} + {{/Pitch}} +
+ +{{Meaning}}
+ +{{#Sentence}} ++
{{Mnemonic}}
+{{furigana:Japanese}}
diff --git a/template/jp.takoboto/style.css b/template/jp.takoboto/style.css new file mode 100644 index 0000000..f40653d --- /dev/null +++ b/template/jp.takoboto/style.css @@ -0,0 +1,31 @@ +@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+JP&display=swap'); + +.card { + font-family: 'Noto Sans JP'; + font-size: 20px; + text-align: center; + color: black; + background-color: white; +} + +.hidden { + display: none; +} + +.low-pitch { + text-decoration: none; + border-bottom: 3px dashed #512da8; +} + +.high-pitch { + font-weight: normal; + border-left: 3px dashed #512da8; + border-top: 3px dashed #512da8; + border-right: 3px dashed #512da8; +} + +.high-pitch-unterminated { + font-weight: normal; + border-left: 3px dashed #512da8; + border-top: 3px dashed #512da8; +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index e36a5ee..96c61ba 100644 --- a/yarn.lock +++ b/yarn.lock @@ -936,6 +936,15 @@ fs-extra@^10.0.1: jsonfile "^6.0.1" universalify "^2.0.0" +fs-extra@^10.1.0: + version "10.1.0" + resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-10.1.0.tgz#02873cfbc4084dde127eaa5f9905eef2325d1abf" + integrity sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ== + dependencies: + graceful-fs "^4.2.0" + jsonfile "^6.0.1" + universalify "^2.0.0" + fsevents@~2.3.2: version "2.3.2" resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.2.tgz#8a526f78b8fdf4623b709e0b975c52c24c02fd1a"