summaryrefslogtreecommitdiff
path: root/ext/bg/js/template-renderer.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-08-01 16:23:33 -0400
committerGitHub <noreply@github.com>2020-08-01 16:23:33 -0400
commit838fd211c6737ce7e2b6802a43837cf4300b60d2 (patch)
tree24fb7fd7d8e6c494a3e51defc7f32a6c3aa73107 /ext/bg/js/template-renderer.js
parent1e839cd230e53f822478f945cb415a8af2b09aef (diff)
Pitch accent Anki field templates (#701)
* Template helper updates * Add pitch data to exported field formatting data * Reuse note data * Add no-op * Set up pitch accent templates * Refactor version update functions * Implement upgrade process for new Anki templates * Consistency * Update README and anki.js to have matching markers
Diffstat (limited to 'ext/bg/js/template-renderer.js')
-rw-r--r--ext/bg/js/template-renderer.js55
1 files changed, 40 insertions, 15 deletions
diff --git a/ext/bg/js/template-renderer.js b/ext/bg/js/template-renderer.js
index ef05cbd8..59af74c8 100644
--- a/ext/bg/js/template-renderer.js
+++ b/ext/bg/js/template-renderer.js
@@ -82,7 +82,10 @@ class TemplateRenderer {
['get', this._get.bind(this)],
['set', this._set.bind(this)],
['scope', this._scope.bind(this)],
- ['isMoraPitchHigh', this._isMoraPitchHigh.bind(this)]
+ ['property', this._property.bind(this)],
+ ['noop', this._noop.bind(this)],
+ ['isMoraPitchHigh', this._isMoraPitchHigh.bind(this)],
+ ['getKanaMorae', this._getKanaMorae.bind(this)]
];
for (const [name, helper] of helpers) {
@@ -316,21 +319,20 @@ class TemplateRenderer {
_set(context, ...args) {
switch (args.length) {
case 2:
- {
- const [key, options] = args;
- const value = options.fn(context);
- this._stateStack[this._stateStack.length - 1].set(key, value);
- return value;
- }
+ {
+ const [key, options] = args;
+ const value = options.fn(context);
+ this._stateStack[this._stateStack.length - 1].set(key, value);
+ }
+ break;
case 3:
- {
- const [key, value] = args;
- this._stateStack[this._stateStack.length - 1].set(key, value);
- return value;
- }
- default:
- return void 0;
+ {
+ const [key, value] = args;
+ this._stateStack[this._stateStack.length - 1].set(key, value);
+ }
+ break;
}
+ return '';
}
_scope(context, options) {
@@ -344,7 +346,30 @@ class TemplateRenderer {
}
}
- _isMoraPitchHigh(context, position, index) {
+ _property(context, ...args) {
+ const ii = args.length - 1;
+ if (ii <= 0) { return void 0; }
+
+ try {
+ let value = args[0];
+ for (let i = 1; i < ii; ++i) {
+ value = value[args[i]];
+ }
+ return value;
+ } catch (e) {
+ return void 0;
+ }
+ }
+
+ _noop(context, options) {
+ return options.fn(context);
+ }
+
+ _isMoraPitchHigh(context, index, position) {
return jp.isMoraPitchHigh(index, position);
}
+
+ _getKanaMorae(context, text) {
+ return jp.getKanaMorae(`${text}`);
+ }
}