summaryrefslogtreecommitdiff
path: root/ext/js/templates/sandbox/anki-template-renderer.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/js/templates/sandbox/anki-template-renderer.js')
-rw-r--r--ext/js/templates/sandbox/anki-template-renderer.js347
1 files changed, 258 insertions, 89 deletions
diff --git a/ext/js/templates/sandbox/anki-template-renderer.js b/ext/js/templates/sandbox/anki-template-renderer.js
index 10f69745..ce8e3200 100644
--- a/ext/js/templates/sandbox/anki-template-renderer.js
+++ b/ext/js/templates/sandbox/anki-template-renderer.js
@@ -36,17 +36,29 @@ export class AnkiTemplateRenderer {
* Creates a new instance of the class.
*/
constructor() {
+ /** @type {CssStyleApplier} */
this._structuredContentStyleApplier = new CssStyleApplier('/data/structured-content-style.json');
+ /** @type {CssStyleApplier} */
this._pronunciationStyleApplier = new CssStyleApplier('/data/pronunciation-style.json');
+ /** @type {RegExp} */
this._structuredContentDatasetKeyIgnorePattern = /^sc([^a-z]|$)/;
+ /** @type {JapaneseUtil} */
this._japaneseUtil = new JapaneseUtil(null);
+ /** @type {TemplateRenderer} */
this._templateRenderer = new TemplateRenderer();
+ /** @type {AnkiNoteDataCreator} */
this._ankiNoteDataCreator = new AnkiNoteDataCreator(this._japaneseUtil);
+ /** @type {TemplateRendererMediaProvider} */
this._mediaProvider = new TemplateRendererMediaProvider();
+ /** @type {PronunciationGenerator} */
this._pronunciationGenerator = new PronunciationGenerator(this._japaneseUtil);
+ /** @type {?(Map<string, unknown>[])} */
this._stateStack = null;
+ /** @type {?import('anki-note-builder').Requirement[]} */
this._requirements = null;
- this._cleanupCallbacks = null;
+ /** @type {(() => void)[]} */
+ this._cleanupCallbacks = [];
+ /** @type {?HTMLElement} */
this._temporaryElement = null;
}
@@ -93,7 +105,7 @@ export class AnkiTemplateRenderer {
]);
this._templateRenderer.registerDataType('ankiNote', {
modifier: ({marker, commonData}) => this._ankiNoteDataCreator.create(marker, commonData),
- composeData: (marker, commonData) => ({marker, commonData})
+ composeData: ({marker}, commonData) => ({marker, commonData})
});
this._templateRenderer.setRenderCallbacks(
this._onRenderSetup.bind(this),
@@ -107,40 +119,56 @@ export class AnkiTemplateRenderer {
// Private
+ /**
+ * @returns {{requirements: import('anki-note-builder').Requirement[]}}
+ */
_onRenderSetup() {
+ /** @type {import('anki-note-builder').Requirement[]} */
const requirements = [];
this._stateStack = [new Map()];
this._requirements = requirements;
this._mediaProvider.requirements = requirements;
- this._cleanupCallbacks = [];
return {requirements};
}
+ /**
+ * @returns {void}
+ */
_onRenderCleanup() {
for (const callback of this._cleanupCallbacks) { callback(); }
this._stateStack = null;
this._requirements = null;
this._mediaProvider.requirements = null;
- this._cleanupCallbacks = null;
+ this._cleanupCallbacks.length = 0;
}
+ /**
+ * @param {string} text
+ * @returns {string}
+ */
_escape(text) {
return Handlebars.Utils.escapeExpression(text);
}
+ /**
+ *
+ * @param text
+ */
_safeString(text) {
return new Handlebars.SafeString(text);
}
// Template helpers
- _dumpObject(context, object) {
+ /** @type {import('template-renderer').HelperFunction<string>} */
+ _dumpObject(object) {
const dump = JSON.stringify(object, null, 4);
return this._escape(dump);
}
- _furigana(context, ...args) {
- const {expression, reading} = this._getFuriganaExpressionAndReading(context, ...args);
+ /** @type {import('template-renderer').HelperFunction<string>} */
+ _furigana(args, context, options) {
+ const {expression, reading} = this._getFuriganaExpressionAndReading(args, context, options);
const segs = this._japaneseUtil.distributeFurigana(expression, reading);
let result = '';
@@ -157,8 +185,9 @@ export class AnkiTemplateRenderer {
return this._safeString(result);
}
- _furiganaPlain(context, ...args) {
- const {expression, reading} = this._getFuriganaExpressionAndReading(context, ...args);
+ /** @type {import('template-renderer').HelperFunction<string>} */
+ _furiganaPlain(args, context, options) {
+ const {expression, reading} = this._getFuriganaExpressionAndReading(args, context, options);
const segs = this._japaneseUtil.distributeFurigana(expression, reading);
let result = '';
@@ -174,43 +203,56 @@ export class AnkiTemplateRenderer {
return result;
}
- _getFuriganaExpressionAndReading(context, ...args) {
- if (args.length >= 3) {
- return {expression: args[0], reading: args[1]};
- } else if (args.length === 2) {
- const {expression, reading} = args[0];
- return {expression, reading};
+ /**
+ * @type {import('template-renderer').HelperFunction<{expression: string, reading: string}>}
+ */
+ _getFuriganaExpressionAndReading(args) {
+ let expression;
+ let reading;
+ if (args.length >= 2) {
+ [expression, reading] = /** @type {[expression?: string, reading?: string]} */ (args);
} else {
- return void 0;
+ ({expression, reading} = /** @type {import('core').SerializableObject} */ (args[0]));
}
+ return {
+ expression: typeof expression === 'string' ? expression : '',
+ reading: typeof reading === 'string' ? reading : ''
+ };
}
+ /**
+ *
+ * @param string
+ */
_stringToMultiLineHtml(string) {
return string.split('\n').join('<br>');
}
- _multiLine(context, options) {
- return this._stringToMultiLineHtml(options.fn(context));
+ /** @type {import('template-renderer').HelperFunction<string>} */
+ _multiLine(_args, context, options) {
+ return this._stringToMultiLineHtml(this._asString(options.fn(context)));
}
- _regexReplace(context, ...args) {
+ /** @type {import('template-renderer').HelperFunction<string>} */
+ _regexReplace(args, context, options) {
// Usage:
// {{#regexReplace regex string [flags] [content]...}}content{{/regexReplace}}
// regex: regular expression string
// string: string to replace
// flags: optional flags for regular expression
// e.g. "i" for case-insensitive, "g" for replace all
- const argCount = args.length - 1;
- const options = args[argCount];
- let value = typeof options.fn === 'function' ? options.fn(context) : '';
+ const argCount = args.length;
+ let value = this._asString(options.fn(context));
if (argCount > 3) {
value = `${args.slice(3, -1).join('')}${value}`;
}
if (argCount > 1) {
try {
- const flags = argCount > 2 ? args[2] : 'g';
- const regex = new RegExp(args[0], flags);
- value = value.replace(regex, args[1]);
+ const [pattern, replacement, flags] = args;
+ if (typeof pattern !== 'string') { throw new Error('Invalid pattern'); }
+ if (typeof replacement !== 'string') { throw new Error('Invalid replacement'); }
+ const regex = new RegExp(pattern, typeof flags === 'string' ? flags : 'g');
+ value = value.replace(regex, replacement);
} catch (e) {
return `${e}`;
}
@@ -218,24 +260,26 @@ export class AnkiTemplateRenderer {
return value;
}
- _regexMatch(context, ...args) {
+ /** @type {import('template-renderer').HelperFunction<string>} */
+ _regexMatch(args, context, options) {
// Usage:
// {{#regexMatch regex [flags] [content]...}}content{{/regexMatch}}
// regex: regular expression string
// flags: optional flags for regular expression
// e.g. "i" for case-insensitive, "g" for match all
- const argCount = args.length - 1;
- const options = args[argCount];
- let value = typeof options.fn === 'function' ? options.fn(context) : '';
+ const argCount = args.length;
+ let value = this._asString(options.fn(context));
if (argCount > 2) {
value = `${args.slice(2, -1).join('')}${value}`;
}
if (argCount > 0) {
try {
- const flags = argCount > 1 ? args[1] : '';
- const regex = new RegExp(args[0], flags);
+ const [pattern, flags] = args;
+ if (typeof pattern !== 'string') { throw new Error('Invalid pattern'); }
+ const regex = new RegExp(pattern, typeof flags === 'string' ? flags : '');
+ /** @type {string[]} */
const parts = [];
- value.replace(regex, (g0) => parts.push(g0));
+ value.replace(regex, (g0) => { parts.push(g0); return g0; });
value = parts.join('');
} catch (e) {
return `${e}`;
@@ -244,11 +288,18 @@ export class AnkiTemplateRenderer {
return value;
}
- _mergeTags(context, object, isGroupMode, isMergeMode) {
+ /**
+ * @type {import('template-renderer').HelperFunction<string>}
+ */
+ _mergeTags(args) {
+ const [object, isGroupMode, isMergeMode] = /** @type {[object: import('anki-templates').TermDictionaryEntry, isGroupMode: boolean, isMergeMode: boolean]} */ (args);
const tagSources = [];
if (isGroupMode || isMergeMode) {
- for (const definition of object.definitions) {
- tagSources.push(definition.definitionTags);
+ const {definitions} = object;
+ if (Array.isArray(definitions)) {
+ for (const definition of definitions) {
+ tagSources.push(definition.definitionTags);
+ }
}
} else {
tagSources.push(object.definitionTags);
@@ -265,7 +316,9 @@ export class AnkiTemplateRenderer {
return [...tags].join(', ');
}
- _eachUpTo(context, iterable, maxCount, options) {
+ /** @type {import('template-renderer').HelperFunction<string>} */
+ _eachUpTo(args, context, options) {
+ const [iterable, maxCount] = /** @type {[iterable: Iterable<unknown>, maxCount: number]} */ (args);
if (iterable) {
const results = [];
let any = false;
@@ -279,14 +332,15 @@ export class AnkiTemplateRenderer {
return results.join('');
}
}
- return options.inverse(context);
+ return this._asString(options.inverse(context));
}
- _spread(context, ...args) {
+ /** @type {import('template-renderer').HelperFunction<unknown[]>} */
+ _spread(args) {
const result = [];
- for (let i = 0, ii = args.length - 1; i < ii; ++i) {
+ for (const array of /** @type {Iterable<unknown>[]} */ (args)) {
try {
- result.push(...args[i]);
+ result.push(...array);
} catch (e) {
// NOP
}
@@ -294,15 +348,22 @@ export class AnkiTemplateRenderer {
return result;
}
- _op(context, ...args) {
+ /** @type {import('template-renderer').HelperFunction<unknown>} */
+ _op(args) {
+ const [operator] = /** @type {[operator: string, operand1: import('core').SafeAny, operand2?: import('core').SafeAny, operand3?: import('core').SafeAny]} */ (args);
switch (args.length) {
- case 3: return this._evaluateUnaryExpression(args[0], args[1]);
- case 4: return this._evaluateBinaryExpression(args[0], args[1], args[2]);
- case 5: return this._evaluateTernaryExpression(args[0], args[1], args[2], args[3]);
+ case 2: return this._evaluateUnaryExpression(operator, args[1]);
+ case 3: return this._evaluateBinaryExpression(operator, args[1], args[2]);
+ case 4: return this._evaluateTernaryExpression(operator, args[1], args[2], args[3]);
default: return void 0;
}
}
+ /**
+ * @param {string} operator
+ * @param {import('core').SafeAny} operand1
+ * @returns {unknown}
+ */
_evaluateUnaryExpression(operator, operand1) {
switch (operator) {
case '+': return +operand1;
@@ -313,6 +374,12 @@ export class AnkiTemplateRenderer {
}
}
+ /**
+ * @param {string} operator
+ * @param {import('core').SafeAny} operand1
+ * @param {import('core').SafeAny} operand2
+ * @returns {unknown}
+ */
_evaluateBinaryExpression(operator, operand1, operand2) {
switch (operator) {
case '+': return operand1 + operand2;
@@ -341,6 +408,13 @@ export class AnkiTemplateRenderer {
}
}
+ /**
+ * @param {string} operator
+ * @param {import('core').SafeAny} operand1
+ * @param {import('core').SafeAny} operand2
+ * @param {import('core').SafeAny} operand3
+ * @returns {unknown}
+ */
_evaluateTernaryExpression(operator, operand1, operand2, operand3) {
switch (operator) {
case '?:': return operand1 ? operand2 : operand3;
@@ -348,8 +422,11 @@ export class AnkiTemplateRenderer {
}
}
- _get(context, key) {
+ /** @type {import('template-renderer').HelperFunction<unknown>} */
+ _get(args) {
+ const [key] = /** @type {[key: string]} */ (args);
const stateStack = this._stateStack;
+ if (stateStack === null) { throw new Error('Invalid state'); }
for (let i = stateStack.length; --i >= 0;) {
const map = stateStack[i];
if (map.has(key)) {
@@ -359,19 +436,21 @@ export class AnkiTemplateRenderer {
return void 0;
}
- _set(context, ...args) {
+ /** @type {import('template-renderer').HelperFunction<string>} */
+ _set(args, context, options) {
const stateStack = this._stateStack;
+ if (stateStack === null) { throw new Error('Invalid state'); }
switch (args.length) {
- case 2:
+ case 1:
{
- const [key, options] = args;
+ const [key] = /** @type {[key: string]} */ (args);
const value = options.fn(context);
stateStack[stateStack.length - 1].set(key, value);
}
break;
- case 3:
+ case 2:
{
- const [key, value] = args;
+ const [key, value] = /** @type {[key: string, value: unknown]} */ (args);
stateStack[stateStack.length - 1].set(key, value);
}
break;
@@ -379,8 +458,10 @@ export class AnkiTemplateRenderer {
return '';
}
- _scope(context, options) {
+ /** @type {import('template-renderer').HelperFunction<unknown>} */
+ _scope(_args, context, options) {
const stateStack = this._stateStack;
+ if (stateStack === null) { throw new Error('Invalid state'); }
try {
stateStack.push(new Map());
return options.fn(context);
@@ -391,14 +472,25 @@ export class AnkiTemplateRenderer {
}
}
- _property(context, ...args) {
- const ii = args.length - 1;
+ /** @type {import('template-renderer').HelperFunction<unknown>} */
+ _property(args) {
+ const ii = args.length;
if (ii <= 0) { return void 0; }
try {
let value = args[0];
for (let i = 1; i < ii; ++i) {
- value = value[args[i]];
+ if (typeof value !== 'object' || value === null) { throw new Error('Invalid object'); }
+ const key = args[i];
+ switch (typeof key) {
+ case 'number':
+ case 'string':
+ case 'symbol':
+ break;
+ default:
+ throw new Error('Invalid key');
+ }
+ value = /** @type {import('core').UnknownObject} */ (value)[key];
}
return value;
} catch (e) {
@@ -406,38 +498,51 @@ export class AnkiTemplateRenderer {
}
}
- _noop(context, options) {
+ /** @type {import('template-renderer').HelperFunction<unknown>} */
+ _noop(_args, context, options) {
return options.fn(context);
}
- _isMoraPitchHigh(context, index, position) {
+ /** @type {import('template-renderer').HelperFunction<boolean>} */
+ _isMoraPitchHigh(args) {
+ const [index, position] = /** @type {[index: number, position: number]} */ (args);
return this._japaneseUtil.isMoraPitchHigh(index, position);
}
- _getKanaMorae(context, text) {
+ /** @type {import('template-renderer').HelperFunction<string[]>} */
+ _getKanaMorae(args) {
+ const [text] = /** @type {[text: string]} */ (args);
return this._japaneseUtil.getKanaMorae(`${text}`);
}
- _getTypeof(context, ...args) {
- const ii = args.length - 1;
- const value = (ii > 0 ? args[0] : args[ii].fn(context));
+ /** @type {import('template-renderer').HelperFunction<import('core').TypeofResult>} */
+ _getTypeof(args, context, options) {
+ const ii = args.length;
+ const value = (ii > 0 ? args[0] : options.fn(context));
return typeof value;
}
- _join(context, ...args) {
- return args.length > 1 ? args.slice(1, args.length - 1).flat().join(args[0]) : '';
+ /** @type {import('template-renderer').HelperFunction<string>} */
+ _join(args) {
+ return args.length > 0 ? args.slice(1, args.length).flat().join(/** @type {string} */ (args[0])) : '';
}
- _concat(context, ...args) {
+ /** @type {import('template-renderer').HelperFunction<string>} */
+ _concat(args) {
let result = '';
- for (let i = 0, ii = args.length - 1; i < ii; ++i) {
+ for (let i = 0, ii = args.length; i < ii; ++i) {
result += args[i];
}
return result;
}
- _pitchCategories(context, data) {
- const {pronunciations, headwords} = data.dictionaryEntry;
+ /** @type {import('template-renderer').HelperFunction<string[]>} */
+ _pitchCategories(args) {
+ const [data] = /** @type {[data: import('anki-templates').NoteData]} */ (args);
+ const {dictionaryEntry} = data;
+ if (dictionaryEntry.type !== 'term') { return []; }
+ const {pronunciations, headwords} = dictionaryEntry;
+ /** @type {Set<string>} */
const categories = new Set();
for (const {headwordIndex, pitches} of pronunciations) {
const {reading, wordClasses} = headwords[headwordIndex];
@@ -452,6 +557,9 @@ export class AnkiTemplateRenderer {
return [...categories];
}
+ /**
+ * @returns {HTMLElement}
+ */
_getTemporaryElement() {
let element = this._temporaryElement;
if (element === null) {
@@ -461,14 +569,28 @@ export class AnkiTemplateRenderer {
return element;
}
+ /**
+ * @param {Element} node
+ * @returns {string}
+ */
_getStructuredContentHtml(node) {
return this._getHtml(node, this._structuredContentStyleApplier, this._structuredContentDatasetKeyIgnorePattern);
}
+ /**
+ * @param {Element} node
+ * @returns {string}
+ */
_getPronunciationHtml(node) {
return this._getHtml(node, this._pronunciationStyleApplier, null);
}
+ /**
+ * @param {Element} node
+ * @param {CssStyleApplier} styleApplier
+ * @param {?RegExp} datasetKeyIgnorePattern
+ * @returns {string}
+ */
_getHtml(node, styleApplier, datasetKeyIgnorePattern) {
const container = this._getTemporaryElement();
container.appendChild(node);
@@ -478,20 +600,27 @@ export class AnkiTemplateRenderer {
return this._safeString(result);
}
+ /**
+ * @param {Element} root
+ * @param {CssStyleApplier} styleApplier
+ * @param {?RegExp} datasetKeyIgnorePattern
+ */
_normalizeHtml(root, styleApplier, datasetKeyIgnorePattern) {
const {ELEMENT_NODE, TEXT_NODE} = Node;
const treeWalker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT);
+ /** @type {HTMLElement[]} */
const elements = [];
+ /** @type {Text[]} */
const textNodes = [];
while (true) {
const node = treeWalker.nextNode();
if (node === null) { break; }
switch (node.nodeType) {
case ELEMENT_NODE:
- elements.push(node);
+ elements.push(/** @type {HTMLElement} */ (node));
break;
case TEXT_NODE:
- textNodes.push(node);
+ textNodes.push(/** @type {Text} */ (node));
break;
}
}
@@ -508,8 +637,11 @@ export class AnkiTemplateRenderer {
}
}
+ /**
+ * @param {Text} textNode
+ */
_replaceNewlines(textNode) {
- const parts = textNode.nodeValue.split('\n');
+ const parts = /** @type {string} */ (textNode.nodeValue).split('\n');
if (parts.length <= 1) { return; }
const {parentNode} = textNode;
if (parentNode === null) { return; }
@@ -521,6 +653,10 @@ export class AnkiTemplateRenderer {
parentNode.replaceChild(fragment, textNode);
}
+ /**
+ * @param {import('anki-templates').NoteData} data
+ * @returns {StructuredContentGenerator}
+ */
_createStructuredContentGenerator(data) {
const contentManager = new AnkiTemplateRendererContentManager(this._mediaProvider, data);
const instance = new StructuredContentGenerator(contentManager, this._japaneseUtil, document);
@@ -528,44 +664,65 @@ export class AnkiTemplateRenderer {
return instance;
}
- _formatGlossary(context, dictionary, content, options) {
+ /**
+ * @type {import('template-renderer').HelperFunction<string>}
+ */
+ _formatGlossary(args, context, options) {
+ const [dictionary] = /** @type {[dictionary: string]} */ (args);
const data = options.data.root;
+ const content = /** @type {import('dictionary-data').TermGlossary} */ (args[0]);
if (typeof content === 'string') { return this._stringToMultiLineHtml(this._escape(content)); }
if (!(typeof content === 'object' && content !== null)) { return ''; }
switch (content.type) {
case 'image': return this._formatGlossaryImage(content, dictionary, data);
case 'structured-content': return this._formatStructuredContent(content, dictionary, data);
+ case 'text': return this._stringToMultiLineHtml(this._escape(content.text));
}
return '';
}
+ /**
+ * @param {import('dictionary-data').TermGlossaryImage} content
+ * @param {string} dictionary
+ * @param {import('anki-templates').NoteData} data
+ * @returns {string}
+ */
_formatGlossaryImage(content, dictionary, data) {
const structuredContentGenerator = this._createStructuredContentGenerator(data);
const node = structuredContentGenerator.createDefinitionImage(content, dictionary);
return this._getStructuredContentHtml(node);
}
+ /**
+ * @param {import('dictionary-data').TermGlossaryStructuredContent} content
+ * @param {string} dictionary
+ * @param {import('anki-templates').NoteData} data
+ * @returns {string}
+ */
_formatStructuredContent(content, dictionary, data) {
const structuredContentGenerator = this._createStructuredContentGenerator(data);
const node = structuredContentGenerator.createStructuredContent(content.content, dictionary);
return node !== null ? this._getStructuredContentHtml(node) : '';
}
- _hasMedia(context, ...args) {
- const ii = args.length - 1;
- const options = args[ii];
- return this._mediaProvider.hasMedia(options.data.root, args.slice(0, ii), options.hash);
+ /**
+ * @type {import('template-renderer').HelperFunction<boolean>}
+ */
+ _hasMedia(args, _context, options) {
+ return this._mediaProvider.hasMedia(options.data.root, args, options.hash);
}
- _getMedia(context, ...args) {
- const ii = args.length - 1;
- const options = args[ii];
- return this._mediaProvider.getMedia(options.data.root, args.slice(0, ii), options.hash);
+ /**
+ * @type {import('template-renderer').HelperFunction<?string>}
+ */
+ _getMedia(args, _context, options) {
+ return this._mediaProvider.getMedia(options.data.root, args, options.hash);
}
- _pronunciation(context, ...args) {
- const ii = args.length - 1;
- const options = args[ii];
+ /**
+ * @type {import('template-renderer').HelperFunction<string>}
+ */
+ _pronunciation(_args, _context, options) {
let {format, reading, downstepPosition, nasalPositions, devoicePositions} = options.hash;
if (typeof reading !== 'string' || reading.length === 0) { return ''; }
@@ -586,18 +743,30 @@ export class AnkiTemplateRenderer {
}
}
- _hiragana(context, ...args) {
- const ii = args.length - 1;
- const options = args[ii];
+ /**
+ * @type {import('template-renderer').HelperFunction<string>}
+ */
+ _hiragana(args, context, options) {
+ const ii = args.length;
const {keepProlongedSoundMarks} = options.hash;
const value = (ii > 0 ? args[0] : options.fn(context));
- return this._japaneseUtil.convertKatakanaToHiragana(value, keepProlongedSoundMarks === true);
+ return typeof value === 'string' ? this._japaneseUtil.convertKatakanaToHiragana(value, keepProlongedSoundMarks === true) : '';
}
- _katakana(context, ...args) {
- const ii = args.length - 1;
- const options = args[ii];
+ /**
+ * @type {import('template-renderer').HelperFunction<string>}
+ */
+ _katakana(args, context, options) {
+ const ii = args.length;
const value = (ii > 0 ? args[0] : options.fn(context));
- return this._japaneseUtil.convertHiraganaToKatakana(value);
+ return typeof value === 'string' ? this._japaneseUtil.convertHiraganaToKatakana(value) : '';
+ }
+
+ /**
+ * @param {unknown} value
+ * @returns {string}
+ */
+ _asString(value) {
+ return typeof value === 'string' ? value : `${value}`;
}
}