Skip to content

Commit

Permalink
support css file in dictionaries (#1080)
Browse files Browse the repository at this point in the history
* get styles in db

* get styles in settings

* use styles

* fix test

* scope

* fix comma separated

* escape dict name in css selector

* g regex

* get styles in anki

* fix tests

* more specificity

* whitespace

* test importing

* test handlebars

* add styles to glossary-first
  • Loading branch information
StefanVukovic99 authored Jun 20, 2024
1 parent 1a866b3 commit d2c930a
Show file tree
Hide file tree
Showing 26 changed files with 1,255 additions and 664 deletions.
2 changes: 1 addition & 1 deletion benches/translator.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {createFindKanjiOptions, createFindTermsOptions} from '../test/utilities/

const dirname = path.dirname(fileURLToPath(import.meta.url));
const dictionaryName = 'Test Dictionary 2';
const translator = await createTranslatorContext(path.join(dirname, '..', 'test', 'data/dictionaries/valid-dictionary1'), dictionaryName);
const {translator} = await createTranslatorContext(path.join(dirname, '..', 'test', 'data/dictionaries/valid-dictionary1'), dictionaryName);

describe('Translator', () => {
const testInputsFilePath = path.join(dirname, '..', 'test', 'data/translator-test-inputs.json');
Expand Down
129 changes: 129 additions & 0 deletions ext/data/templates/anki-field-templates-upgrade-v41.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{{<<<<<<<}}
{{~#*inline "glossary"~}}
<div style="text-align: left;">
{{~#scope~}}
{{~#if (op "===" definition.type "term")~}}
{{~#unless (op "&&" selectedDictionary (op "!=" selectedDictionary definition.dictionary))~}}
{{~> glossary-single definition brief=brief noDictionaryTag=noDictionaryTag ~}}
{{~/unless~}}
{{~else if (op "||" (op "===" definition.type "termGrouped") (op "===" definition.type "termMerged"))~}}
{{~#if (op ">" definition.definitions.length 1)~}}
<ol>
{{~#each definition.definitions~}}
{{~#unless (op "&&" ../selectedDictionary (op "!=" ../selectedDictionary dictionary))~}}
<li>
{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag ~}}
</li>
{{~/unless~}}
{{~/each~}}
</ol>
{{~else~}}
{{~#each definition.definitions~}}
{{~#unless (op "&&" ../selectedDictionary (op "!=" ../selectedDictionary dictionary))~}}
{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag ~}}
{{~/unless~}}
{{~/each~}}
{{~/if~}}
{{~else if (op "===" definition.type "kanji")~}}
{{~#if (op ">" definition.glossary.length 1)~}}
<ol>{{#each definition.glossary}}<li>{{.}}</li>{{/each}}</ol>
{{~else~}}
{{~#each definition.glossary~}}{{.}}{{~/each~}}
{{~/if~}}
{{~/if~}}
{{~/scope~}}
</div>
{{~/inline~}}
{{=======}}
{{~#*inline "glossary"~}}
<div style="text-align: left;" class="yomitan-glossary">
{{~#scope~}}
{{~#if (op "===" definition.type "term")~}}
{{~#unless (op "&&" selectedDictionary (op "!=" selectedDictionary definition.dictionary))~}}
{{~> glossary-single definition brief=brief noDictionaryTag=noDictionaryTag ~}}
{{~#if definition.glossaryScopedStyles~}}
<style>{{{definition.glossaryScopedStyles}}}</style>
{{~/if~}}
{{~/unless~}}
{{~else if (op "||" (op "===" definition.type "termGrouped") (op "===" definition.type "termMerged"))~}}
{{~#if (op ">" definition.definitions.length 1)~}}
<ol>
{{~#each definition.definitions~}}
{{~#unless (op "&&" ../selectedDictionary (op "!=" ../selectedDictionary dictionary))~}}
<li data-dictionary="{{dictionary}}">
{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag ~}}
</li>
{{~#if dictScopedStyles~}}
<style>{{{dictScopedStyles}}}</style>
{{~/if~}}
{{~/unless~}}
{{~/each~}}
</ol>
{{~else~}}
{{~#each definition.definitions~}}
{{~#unless (op "&&" ../selectedDictionary (op "!=" ../selectedDictionary dictionary))~}}
{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag ~}}
{{~#if glossaryScopedStyles~}}
<style>{{{glossaryScopedStyles}}}</style>
{{~/if~}}
{{~/unless~}}
{{~/each~}}
{{~/if~}}
{{~else if (op "===" definition.type "kanji")~}}
{{~#if (op ">" definition.glossary.length 1)~}}
<ol>{{#each definition.glossary}}<li>{{.}}</li>{{/each}}</ol>
{{~else~}}
{{~#each definition.glossary~}}{{.}}{{~/each~}}
{{~/if~}}
{{~/if~}}
{{~/scope~}}
</div>
{{~/inline~}}
{{>>>>>>>}}

{{<<<<<<<}}
{{~#*inline "glossary-first"~}}
<div style="text-align: left;">
{{~#scope~}}
{{~#if (op "===" definition.type "term")~}}
{{~> glossary-single definition brief=brief noDictionaryTag=noDictionaryTag ~}}
{{~else if (op "||" (op "===" definition.type "termGrouped") (op "===" definition.type "termMerged"))~}}
{{~#if (op ">" definition.definitions.length 1)~}}
{{~#with definition.definitions.[0]~}}{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag ~}}{{~/with~}}
{{~else~}}
{{~#with definition.definitions.[0]~}}{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag ~}}{{~/with~}}
{{~/if~}}
{{~/if~}}
{{~/scope~}}
</div>
{{~/inline~}}
{{=======}}
{{~#*inline "glossary-first"~}}
<div style="text-align: left;" class="yomitan-glossary">
{{~#scope~}}
{{~#if (op "===" definition.type "term")~}}
{{~> glossary-single definition brief=brief noDictionaryTag=noDictionaryTag ~}}
{{~#if definition.glossaryScopedStyles~}}
<style>{{{definition.glossaryScopedStyles}}}</style>
{{~/if~}}
{{~else if (op "||" (op "===" definition.type "termGrouped") (op "===" definition.type "termMerged"))~}}
{{~#if (op ">" definition.definitions.length 1)~}}
{{~#with definition.definitions.[0]~}}
{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag ~}}
{{~#if glossaryScopedStyles~}}
<style>{{{glossaryScopedStyles}}}</style>
{{~/if~}}
{{~/with~}}
{{~else~}}
{{~#with definition.definitions.[0]~}}
{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag ~}}
{{~#if glossaryScopedStyles~}}
<style>{{{glossaryScopedStyles}}}</style>
{{~/if~}}
{{~/with~}}
{{~/if~}}
{{~/if~}}
{{~/scope~}}
</div>
{{~/inline~}}
{{>>>>>>>}}
32 changes: 27 additions & 5 deletions ext/data/templates/default-anki-field-templates.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,36 @@
{{/inline}}

{{~#*inline "glossary"~}}
<div style="text-align: left;">
<div style="text-align: left;" class="yomitan-glossary">
{{~#scope~}}
{{~#if (op "===" definition.type "term")~}}
{{~#unless (op "&&" selectedDictionary (op "!=" selectedDictionary definition.dictionary))~}}
{{~> glossary-single definition brief=brief noDictionaryTag=noDictionaryTag ~}}
{{~#if definition.glossaryScopedStyles~}}
<style>{{{definition.glossaryScopedStyles}}}</style>
{{~/if~}}
{{~/unless~}}
{{~else if (op "||" (op "===" definition.type "termGrouped") (op "===" definition.type "termMerged"))~}}
{{~#if (op ">" definition.definitions.length 1)~}}
<ol>
{{~#each definition.definitions~}}
{{~#unless (op "&&" ../selectedDictionary (op "!=" ../selectedDictionary dictionary))~}}
<li>
<li data-dictionary="{{dictionary}}">
{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag ~}}
</li>
{{~#if dictScopedStyles~}}
<style>{{{dictScopedStyles}}}</style>
{{~/if~}}
{{~/unless~}}
{{~/each~}}
</ol>
{{~else~}}
{{~#each definition.definitions~}}
{{~#unless (op "&&" ../selectedDictionary (op "!=" ../selectedDictionary dictionary))~}}
{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag ~}}
{{~#if glossaryScopedStyles~}}
<style>{{{glossaryScopedStyles}}}</style>
{{~/if~}}
{{~/unless~}}
{{~/each~}}
{{~/if~}}
Expand All @@ -142,15 +151,28 @@
{{/inline}}

{{~#*inline "glossary-first"~}}
<div style="text-align: left;">
<div style="text-align: left;" class="yomitan-glossary">
{{~#scope~}}
{{~#if (op "===" definition.type "term")~}}
{{~> glossary-single definition brief=brief noDictionaryTag=noDictionaryTag ~}}
{{~#if definition.glossaryScopedStyles~}}
<style>{{{definition.glossaryScopedStyles}}}</style>
{{~/if~}}
{{~else if (op "||" (op "===" definition.type "termGrouped") (op "===" definition.type "termMerged"))~}}
{{~#if (op ">" definition.definitions.length 1)~}}
{{~#with definition.definitions.[0]~}}{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag ~}}{{~/with~}}
{{~#with definition.definitions.[0]~}}
{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag ~}}
{{~#if glossaryScopedStyles~}}
<style>{{{glossaryScopedStyles}}}</style>
{{~/if~}}
{{~/with~}}
{{~else~}}
{{~#with definition.definitions.[0]~}}{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag ~}}{{~/with~}}
{{~#with definition.definitions.[0]~}}
{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag ~}}
{{~#if glossaryScopedStyles~}}
<style>{{{glossaryScopedStyles}}}</style>
{{~/if~}}
{{~/with~}}
{{~/if~}}
{{~/if~}}
{{~/scope~}}
Expand Down
10 changes: 7 additions & 3 deletions ext/js/data/anki-note-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class AnkiNoteBuilder {
glossaryLayoutMode = 'default',
compactTags = false,
mediaOptions = null,
dictionaryStylesMap,
}) {
let duplicateScopeDeckName = null;
let duplicateScopeCheckChildren = false;
Expand All @@ -80,7 +81,7 @@ export class AnkiNoteBuilder {
}
}

const commonData = this._createData(dictionaryEntry, mode, context, resultOutputMode, glossaryLayoutMode, compactTags, media);
const commonData = this._createData(dictionaryEntry, mode, context, resultOutputMode, glossaryLayoutMode, compactTags, media, dictionaryStylesMap);
const formattedFieldValuePromises = [];
for (const [, fieldValue] of fields) {
const formattedFieldValuePromise = this._formatField(fieldValue, commonData, template);
Expand Down Expand Up @@ -135,8 +136,9 @@ export class AnkiNoteBuilder {
glossaryLayoutMode = 'default',
compactTags = false,
marker,
dictionaryStylesMap,
}) {
const commonData = this._createData(dictionaryEntry, mode, context, resultOutputMode, glossaryLayoutMode, compactTags, void 0);
const commonData = this._createData(dictionaryEntry, mode, context, resultOutputMode, glossaryLayoutMode, compactTags, void 0, dictionaryStylesMap);
return await this._templateRenderer.getModifiedData({marker, commonData}, 'ankiNote');
}

Expand Down Expand Up @@ -181,9 +183,10 @@ export class AnkiNoteBuilder {
* @param {import('settings').GlossaryLayoutMode} glossaryLayoutMode
* @param {boolean} compactTags
* @param {import('anki-templates').Media|undefined} media
* @param {Map<string, string>} dictionaryStylesMap
* @returns {import('anki-note-builder').CommonData}
*/
_createData(dictionaryEntry, mode, context, resultOutputMode, glossaryLayoutMode, compactTags, media) {
_createData(dictionaryEntry, mode, context, resultOutputMode, glossaryLayoutMode, compactTags, media, dictionaryStylesMap) {
return {
dictionaryEntry,
mode,
Expand All @@ -192,6 +195,7 @@ export class AnkiNoteBuilder {
glossaryLayoutMode,
compactTags,
media,
dictionaryStylesMap,
};
}

Expand Down
Loading

0 comments on commit d2c930a

Please sign in to comment.