Skip to content

Commit

Permalink
Add {sentence-furigana-plain} handlebar (#1834)
Browse files Browse the repository at this point in the history
* Initial implementation

* Add handlebars update

* Fix distribution of furigana when rendered

* Deduplicate code

* Add to docs and dropdown

* Update tests

* Consolidate textFurigana and textFuriganaPlain
  • Loading branch information
Kuuuube authored Feb 24, 2025
1 parent 4f109a9 commit 85af213
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{#*inline "sentence-furigana-plain"}}
{{~#if definition.cloze~}}
{{~#if (hasMedia "textFuriganaPlain" definition.cloze.sentence)~}}
{{{getMedia "textFuriganaPlain" definition.cloze.sentence escape=false}}}
{{~else~}}
{{{definition.cloze.sentence}}}
{{~/if~}}
{{~/if~}}
{{/inline}}
10 changes: 10 additions & 0 deletions ext/data/templates/default-anki-field-templates.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -472,4 +472,14 @@
{{~/if~}}
{{/inline}}

{{#*inline "sentence-furigana-plain"}}
{{~#if definition.cloze~}}
{{~#if (hasMedia "textFuriganaPlain" definition.cloze.sentence)~}}
{{{getMedia "textFuriganaPlain" definition.cloze.sentence escape=false}}}
{{~else~}}
{{{definition.cloze.sentence}}}
{{~/if~}}
{{~/if~}}
{{/inline}}

{{~> (lookup . "marker") ~}}
26 changes: 24 additions & 2 deletions ext/js/data/anki-note-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,9 @@ export class AnkiNoteBuilder {
break;
}
if (data !== null) {
const value = this._createFuriganaHtml(data, readingMode);
results.push({text, readingMode, details: {value}});
const valueHtml = this._createFuriganaHtml(data, readingMode);
const valuePlain = this._createFuriganaPlain(data, readingMode);
results.push({text, readingMode, detailsHtml: {value: valueHtml}, detailsPlain: {value: valuePlain}});
}
}
return results;
Expand Down Expand Up @@ -551,6 +552,27 @@ export class AnkiNoteBuilder {
return result;
}

/**
* @param {import('api').ParseTextLine[]} data
* @param {?import('anki-templates').TextFuriganaReadingMode} readingMode
* @returns {string}
*/
_createFuriganaPlain(data, readingMode) {
let result = '';
for (const term of data) {
for (const {text, reading} of term) {
if (reading.length > 0) {
const reading2 = this._convertReading(reading, readingMode);
result += ` ${text}[${reading2}]`;
} else {
result += text;
}
}
}
result = result.substring(1);
return result;
}

/**
* @param {string} reading
* @param {?import('anki-templates').TextFuriganaReadingMode} readingMode
Expand Down
2 changes: 2 additions & 0 deletions ext/js/data/anki-template-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export function getStandardFieldMarkers(type) {
'popup-selection-text',
'sentence',
'sentence-furigana',
'sentence-furigana-plain',
'tags',
'url',
];
Expand Down Expand Up @@ -91,6 +92,7 @@ export function getStandardFieldMarkers(type) {
'popup-selection-text',
'sentence',
'sentence-furigana',
'sentence-furigana-plain',
'stroke-count',
'tags',
'url',
Expand Down
9 changes: 9 additions & 0 deletions ext/js/data/options-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ export class OptionsUtil {
this._updateVersion58,
this._updateVersion59,
this._updateVersion60,
this._updateVersion61,
];
/* eslint-enable @typescript-eslint/unbound-method */
if (typeof targetVersion === 'number' && targetVersion < result.length) {
Expand Down Expand Up @@ -1606,6 +1607,14 @@ export class OptionsUtil {
}
}

/**
* - Added sentence-furigana-plain handlebar
* @type {import('options-util').UpdateFunction}
*/
async _updateVersion61(options) {
await this._applyAnkiFieldTemplatesPatch(options, '/data/templates/anki-field-templates-upgrade-v61.handlebars');
}

/**
* @param {string} url
* @returns {Promise<chrome.tabs.Tab>}
Expand Down
13 changes: 10 additions & 3 deletions ext/js/templates/template-renderer-media-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ export class TemplateRendererMediaProvider {
case 'clipboardImage': return this._getSimpleMediaData(media, 'clipboardImage');
case 'clipboardText': return this._getSimpleMediaData(media, 'clipboardText');
case 'popupSelectionText': return this._getSimpleMediaData(media, 'popupSelectionText');
case 'textFurigana': return this._getTextFurigana(media, args[1], namedArgs);
case 'textFurigana': return this._getTextFurigana(media, args[1], namedArgs, 'furiganaHtml');
case 'textFuriganaPlain': return this._getTextFurigana(media, args[1], namedArgs, 'furiganaPlain');
case 'dictionaryMedia': return this._getDictionaryMedia(media, args[1], namedArgs);
default: return null;
}
Expand Down Expand Up @@ -155,16 +156,22 @@ export class TemplateRendererMediaProvider {
* @param {import('anki-templates').Media} media
* @param {unknown} text
* @param {import('core').SerializableObject} namedArgs
* @param {import('anki-note-builder').TextFuriganaFormats} furiganaFormat
* @returns {?import('anki-templates').MediaObject}
*/
_getTextFurigana(media, text, namedArgs) {
_getTextFurigana(media, text, namedArgs, furiganaFormat) {
if (typeof text !== 'string') { return null; }
const readingMode = this._normalizeReadingMode(namedArgs.readingMode);
const {textFurigana} = media;
if (Array.isArray(textFurigana)) {
for (const entry of textFurigana) {
if (entry.text !== text || entry.readingMode !== readingMode) { continue; }
return entry.details;
switch (furiganaFormat) {
case 'furiganaHtml':
return entry.detailsHtml;
case 'furiganaPlain':
return entry.detailsPlain;
}
}
}
this._addRequirement({
Expand Down
4 changes: 4 additions & 0 deletions ext/templates-modals.html
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,10 @@ <h1 class="modal-title">Pronunciation Dictionaries</h1>
<td><code class="anki-field-marker">{sentence-furigana}</code></td>
<td>Sentence, quote, or phrase that the term or kanji appears in from the source content, with furigana added.</td>
</tr>
<tr>
<td><code class="anki-field-marker">{sentence-furigana-plain}</code></td>
<td>Sentence, quote, or phrase that the term or kanji appears in from the source content, with furigana added in brackets.</td>
</tr>
<tr>
<td><code class="anki-field-marker">{url}</code></td>
<td>Address of the web page in which the term or kanji appeared in.</td>
Expand Down
Loading

0 comments on commit 85af213

Please sign in to comment.