Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add {sentence-furigana-plain} handlebar #1834

Merged
merged 8 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -571,6 +571,7 @@ export class OptionsUtil {
this._updateVersion57,
this._updateVersion58,
this._updateVersion59,
this._updateVersion60,
];
/* eslint-enable @typescript-eslint/unbound-method */
if (typeof targetVersion === 'number' && targetVersion < result.length) {
Expand Down Expand Up @@ -1594,6 +1595,14 @@ export class OptionsUtil {
}
}

/**
* - Added sentence-furigana-plain handlebar
* @type {import('options-util').UpdateFunction}
*/
async _updateVersion60(options) {
await this._applyAnkiFieldTemplatesPatch(options, '/data/templates/anki-field-templates-upgrade-v60.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