Skip to content

Commit

Permalink
Merge branch 'master' into more-spanish-transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
djahandarie authored Feb 9, 2025
2 parents 6163fcc + 84fe55a commit 35d2e21
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- uses: actions/checkout@v4

- name: Install CJK fonts
uses: awalsh128/cache-apt-pkgs-action@a6c3917cc929dd0345bfb2d3feaf9101823370ad # v1.4.2
uses: awalsh128/cache-apt-pkgs-action@5902b33ae29014e6ca012c5d8025d4346556bd40 # v1.4.3
with:
packages: fonts-ipafont-mincho
execute_install_scripts: true
Expand Down
6 changes: 6 additions & 0 deletions ext/css/display.css
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,12 @@ button.footer-notification-close-button {
padding-left: 1.5em;
list-style: disc;
}
.anki-note-error-info {
color: var(--danger-color);
}
.anki-note-error-header {
font-weight: bold;
}


/* Conditional styles */
Expand Down
47 changes: 44 additions & 3 deletions ext/data/recommended-dictionaries.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@
"frequency": [],
"grammar": [],
"kanji": [],
"pronunciation": [],
"pronunciation": [
{
"name": "kty-aii-en-ipa",
"description": "Assyrian Neo-Aramaic IPA dictionary created from Wiktionary data.",
"homepage": "https://github.com/yomidevs/kaikki-to-yomitan/blob/master/downloads.md",
"downloadUrl": "https://github.com/yomidevs/kaikki-to-yomitan/releases/latest/download/kty-aii-en-ipa.zip"
}
],
"terms": [
{
"name": "kty-aii-en",
Expand Down Expand Up @@ -45,7 +52,14 @@
"frequency": [],
"grammar": [],
"kanji": [],
"pronunciation": [],
"pronunciation": [
{
"name": "kty-ar-en-ipa",
"description": "Arabic IPA dictionary created from Wiktionary data.",
"homepage": "https://github.com/yomidevs/kaikki-to-yomitan/blob/master/downloads.md",
"downloadUrl": "https://github.com/yomidevs/kaikki-to-yomitan/releases/latest/download/kty-ar-en-ipa.zip"
}
],
"terms": [
{
"name": "kty-ar-en",
Expand Down Expand Up @@ -387,7 +401,14 @@
"frequency": [],
"grammar": [],
"kanji": [],
"pronunciation": [],
"pronunciation": [
{
"name": "kty-mt-en-ipa",
"description": "Maltese IPA dictionary created from Wiktionary data.",
"homepage": "https://github.com/yomidevs/kaikki-to-yomitan/blob/master/downloads.md",
"downloadUrl": "https://github.com/yomidevs/kaikki-to-yomitan/releases/latest/download/kty-mt-en-ipa.zip"
}
],
"terms": [
{
"name": "kty-mt-en",
Expand All @@ -411,6 +432,26 @@
}
]
},
"no": {
"frequency": [],
"grammar": [],
"kanji": [],
"pronunciation": [],
"terms": [
{
"name": "kty-nb-en",
"description": "Norwegian Bokmål to English dictionary created from Wiktionary data.",
"homepage": "https://github.com/yomidevs/kaikki-to-yomitan/blob/master/downloads.md",
"downloadUrl": "https://github.com/yomidevs/kaikki-to-yomitan/releases/latest/download/kty-nb-en.zip"
},
{
"name": "kty-nn-en",
"description": "Norwegian Nynorsk to English dictionary created from Wiktionary data.",
"homepage": "https://github.com/yomidevs/kaikki-to-yomitan/blob/master/downloads.md",
"downloadUrl": "https://github.com/yomidevs/kaikki-to-yomitan/releases/latest/download/kty-nn-en.zip"
}
]
},
"pl": {
"frequency": [],
"grammar": [],
Expand Down
26 changes: 26 additions & 0 deletions ext/data/recommended-settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,32 @@
"description": "Turn off Yomitan's internal parser for languages with spaces."
}
],
"no": [
{
"modification": {
"action": "set",
"path": "scanning.scanResolution",
"value": "word"
},
"description": "Scan text one word at a time (as opposed to one character)."
},
{
"modification": {
"action": "set",
"path": "translation.searchResolution",
"value": "word"
},
"description": "Lookup whole words in the dictionary."
},
{
"modification": {
"action": "set",
"path": "parsing.enableScanningParser",
"value": false
},
"description": "Turn off Yomitan's internal parser for languages with spaces."
}
],
"pl": [
{
"modification": {
Expand Down
12 changes: 10 additions & 2 deletions ext/js/data/anki-template-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,16 @@ export function getStandardFieldMarkers(type) {

/**
* @param {import('settings').ProfileOptions} options
* @param {import('dictionary-importer').Summary[]} dictionaryInfo
* @returns {string}
*/
export function getDynamicTemplates(options) {
export function getDynamicTemplates(options, dictionaryInfo) {
let dynamicTemplates = '\n';
for (const dictionary of options.dictionaries) {
const currentDictionaryInfo = dictionaryInfo.find(({title}) => title === dictionary.name);
if (!dictionary.enabled) { continue; }
const totalTerms = currentDictionaryInfo?.counts?.terms?.total;
if (!totalTerms || totalTerms === 0) { continue; }
dynamicTemplates += `
{{#*inline "single-glossary-${getKebabCase(dictionary.name)}"}}
{{~> glossary selectedDictionary='${escapeDictName(dictionary.name)}'}}
Expand All @@ -127,12 +131,16 @@ export function getDynamicTemplates(options) {

/**
* @param {import('settings').DictionariesOptions} dictionaries
* @param {import('dictionary-importer').Summary[]} dictionaryInfo
* @returns {string[]} The list of field markers.
*/
export function getDynamicFieldMarkers(dictionaries) {
export function getDynamicFieldMarkers(dictionaries, dictionaryInfo) {
const markers = [];
for (const dictionary of dictionaries) {
const currentDictionaryInfo = dictionaryInfo.find(({title}) => title === dictionary.name);
if (!dictionary.enabled) { continue; }
const totalTerms = currentDictionaryInfo?.counts?.terms?.total;
if (!totalTerms || totalTerms === 0) { continue; }
markers.push(`single-glossary-${getKebabCase(dictionary.name)}`);
}
return markers;
Expand Down
9 changes: 8 additions & 1 deletion ext/js/display/display-anki.js
Original file line number Diff line number Diff line change
Expand Up @@ -833,8 +833,9 @@ export class DisplayAnki {
* @returns {Promise<string>}
*/
async _getAnkiFieldTemplates(options) {
const dictionaryInfo = await this._display.application.api.getDictionaryInfo();
const staticTemplates = await this._getStaticAnkiFieldTemplates(options);
const dynamicTemplates = getDynamicTemplates(options);
const dynamicTemplates = getDynamicTemplates(options, dictionaryInfo);
return staticTemplates + dynamicTemplates;
}

Expand Down Expand Up @@ -935,6 +936,12 @@ export class DisplayAnki {
if (context === null) { throw new Error('Note context not initialized'); }
const modeOptions = this._modeOptions.get(mode);
if (typeof modeOptions === 'undefined') { throw new Error(`Unsupported note type: ${mode}`); }
if (!this._ankiFieldTemplates) {
const options = this._display.getOptions();
if (options) {
await this._updateAnkiFieldTemplates(options);
}
}
const template = this._ankiFieldTemplates;
if (typeof template !== 'string') { throw new Error('Invalid template'); }
const {deck: deckName, model: modelName} = modeOptions;
Expand Down
4 changes: 2 additions & 2 deletions ext/js/display/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {ExtensionError} from '../core/extension-error.js';
import {log} from '../core/log.js';
import {safePerformance} from '../core/safe-performance.js';
import {toError} from '../core/to-error.js';
import {addScopeToCss, clone, deepEqual, promiseTimeout} from '../core/utilities.js';
import {addScopeToCssLegacy, clone, deepEqual, promiseTimeout} from '../core/utilities.js';
import {setProfile} from '../data/profiles-util.js';
import {PopupMenu} from '../dom/popup-menu.js';
import {querySelectorNotNull} from '../dom/query-selector.js';
Expand Down Expand Up @@ -1255,7 +1255,7 @@ export class Display extends EventDispatcher {
for (const {name, enabled, styles = ''} of dictionaries) {
if (enabled) {
const escapedTitle = name.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
customCss += '\n' + addScopeToCss(styles, `[data-dictionary="${escapedTitle}"]`);
customCss += '\n' + addScopeToCssLegacy(styles, `[data-dictionary="${escapedTitle}"]`);
}
}
this.setCustomCss(customCss);
Expand Down
9 changes: 9 additions & 0 deletions ext/js/language/language-descriptors.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,15 @@ const languageDescriptors = [
exampleText: 'lezen',
textPreprocessors: capitalizationPreprocessors,
},
{
iso: 'no',
iso639_3: 'nor',
name: 'Norwegian',
exampleText: 'lese',
textPreprocessors: {
...capitalizationPreprocessors,
},
},
{
iso: 'pl',
iso639_3: 'pol',
Expand Down
12 changes: 8 additions & 4 deletions ext/js/pages/settings/anki-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ import {ObjectPropertyAccessor} from '../../general/object-property-accessor.js'
export class AnkiController {
/**
* @param {import('./settings-controller.js').SettingsController} settingsController
* @param {import('../../application.js').Application} application
*/
constructor(settingsController) {
constructor(settingsController, application) {
/** @type {import('../../application.js').Application} */
this._application = application;
/** @type {import('./settings-controller.js').SettingsController} */
this._settingsController = settingsController;
/** @type {AnkiConnect} */
Expand Down Expand Up @@ -181,7 +184,7 @@ export class AnkiController {

this._updateDuplicateOverwriteWarning(anki.duplicateBehavior);

this._setupFieldMenus(dictionaries);
void this._setupFieldMenus(dictionaries);
}

/** */
Expand Down Expand Up @@ -309,7 +312,7 @@ export class AnkiController {
/**
* @param {import('settings').DictionariesOptions} dictionaries
*/
_setupFieldMenus(dictionaries) {
async _setupFieldMenus(dictionaries) {
/** @type {[types: import('dictionary').DictionaryEntryType[], templateName: string][]} */
const fieldMenuTargets = [
[['term'], 'anki-card-terms-field-menu'],
Expand Down Expand Up @@ -339,7 +342,8 @@ export class AnkiController {
markers.push(...getStandardFieldMarkers(type));
}
if (types.includes('term')) {
markers.push(...getDynamicFieldMarkers(dictionaries));
const dictionaryInfo = await this._application.api.getDictionaryInfo();
markers.push(...getDynamicFieldMarkers(dictionaries, dictionaryInfo));
}
markers = [...new Set(markers.sort())];

Expand Down
9 changes: 5 additions & 4 deletions ext/js/pages/settings/anki-deck-generator-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ export class AnkiDeckGeneratorController {
query: sentenceText,
fullQuery: sentenceText,
};
const template = this._getAnkiTemplate(options);
const template = await this._getAnkiTemplate(options);
const deckOptionsFields = options.anki.terms.fields;
const {general: {resultOutputMode, glossaryLayoutMode, compactTags}} = options;
const fields = [];
Expand Down Expand Up @@ -512,12 +512,13 @@ export class AnkiDeckGeneratorController {

/**
* @param {import('settings').ProfileOptions} options
* @returns {string}
* @returns {Promise<string>}
*/
_getAnkiTemplate(options) {
async _getAnkiTemplate(options) {
let staticTemplates = options.anki.fieldTemplates;
if (typeof staticTemplates !== 'string') { staticTemplates = this._defaultFieldTemplates; }
const dynamicTemplates = getDynamicTemplates(options);
const dictionaryInfo = await this._application.api.getDictionaryInfo();
const dynamicTemplates = getDynamicTemplates(options, dictionaryInfo);
return staticTemplates + '\n' + dynamicTemplates;
}

Expand Down
9 changes: 5 additions & 4 deletions ext/js/pages/settings/anki-templates-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export class AnkiTemplatesController {
query: sentenceText,
fullQuery: sentenceText,
};
const template = this._getAnkiTemplate(options);
const template = await this._getAnkiTemplate(options);
const {general: {resultOutputMode, glossaryLayoutMode, compactTags}} = options;
const {note, errors} = await this._ankiNoteBuilder.createNote(/** @type {import('anki-note-builder').CreateNoteDetails} */ ({
dictionaryEntry,
Expand Down Expand Up @@ -315,12 +315,13 @@ export class AnkiTemplatesController {

/**
* @param {import('settings').ProfileOptions} options
* @returns {string}
* @returns {Promise<string>}
*/
_getAnkiTemplate(options) {
async _getAnkiTemplate(options) {
let staticTemplates = options.anki.fieldTemplates;
if (typeof staticTemplates !== 'string') { staticTemplates = this._defaultFieldTemplates; }
const dynamicTemplates = getDynamicTemplates(options);
const dictionaryInfo = await this._application.api.getDictionaryInfo();
const dynamicTemplates = getDynamicTemplates(options, dictionaryInfo);
return staticTemplates + '\n' + dynamicTemplates;
}
}
2 changes: 1 addition & 1 deletion ext/js/pages/settings/settings-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ await Application.main(true, async (application) => {
const settingsBackup = new BackupController(settingsController, modalController);
preparePromises.push(settingsBackup.prepare());

const ankiController = new AnkiController(settingsController);
const ankiController = new AnkiController(settingsController, application);
preparePromises.push(ankiController.prepare());

const ankiDeckGeneratorController = new AnkiDeckGeneratorController(application, settingsController, modalController, ankiController);
Expand Down
3 changes: 3 additions & 0 deletions types/ext/language-descriptors.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ type AllTextProcessors = {
nl: {
pre: CapitalizationPreprocessors;
};
no: {
pre: CapitalizationPreprocessors;
};
pl: {
pre: CapitalizationPreprocessors;
};
Expand Down

0 comments on commit 35d2e21

Please sign in to comment.