summaryrefslogtreecommitdiff
path: root/docs/templates.md
blob: 8e88a7dccdc7b5a738010c0d243111ae8d3fe0bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# Templates

## Helpers

Yomichan supports several custom Handlebars helpers for rendering templates.
The source code for these templates can be found [here](../ext/js/templates/template-renderer.js).


### `dumpObject`

Converts an object to a pretty-printed JSON string.
This function can be helpful for debugging values when creating templates.

<details>
  <summary>Syntax:</summary>

  <code>{{#dumpObject}}<i>&lt;object&gt;</i>{{/dumpObject}}</code>

  * _`object`_ <br>
    The object to convert.
</details>
<details>
  <summary>Example:</summary>

  ```handlebars
  <pre>{{#dumpObject}}{{.}}{{/dumpObject}}</pre>
  ```

  Output:
  ```html
  <pre>{
      "key": "value"
  }</pre>
  ```

  Preview:
  ```html
  {
      "key": "value"
  }
  ```
</details>


### `furigana`

Converts a definition or expression/reading pair to its furigana representation.

<details>
  <summary>Syntax:</summary>

  <code>{{#furigana}}<i>&lt;definition&gt;</i>{{/furigana}}</code><br>
  <code>{{#furigana <i>expression</i> <i>reading</i>}}{{/furigana}}</code><br>

  * _`definition`_ <br>
    The definition to convert.
  * _`expression`_ <br>
    The expression to convert.
  * _`reading`_ <br>
    The reading to convert.
</details>
<details>
  <summary>Example:</summary>

  ```handlebars
  {{#furigana}}{{.}}{{/furigana}}
  {{#furigana "読む" "よむ"}}{{/furigana}}
  ```

  Output:
  ```html
  <ruby><rt></rt></ruby>  ```

  Preview
  <pre><ruby>読<rt>よ</rt></ruby>む</pre>
</details>


### `furiganaPlain`

Converts a definition or expression/reading pair to its simplified furigana representation.

<details>
  <summary>Syntax:</summary>

  <code>{{#furiganaPlain}}<i>&lt;definition&gt;</i>{{/furigana}}</code>
  <code>{{#furiganaPlain <i>expression</i> <i>reading</i>}}{{/furiganaPlain}}</code><br>

  * _`definition`_ <br>
    The definition to convert.
  * _`expression`_ <br>
    The expression to convert.
  * _`reading`_ <br>
    The reading to convert.
</details>
<details>
  <summary>Example:</summary>

  ```handlebars
  {{~#furiganaPlain~}}{{.}}{{~/furiganaPlain~}}
  {{#furiganaPlain "読む" "よむ"}}{{/furiganaPlain}}
  ```

  Output:
  ```html
  読[よ]む
  ```
</details>


### `multiLine`

Replaces newline characters with a forced HTML line break `<br>`.

<details>
  <summary>Syntax:</summary>

  <code>{{#multiLine}}<i>text with multiple lines</i>{{/multiLine}}</code>
</details>
<details>
  <summary>Example:</summary>

  ```handlebars
  {{#kanjiLinks~}}
  some
  multiline
  text
  {{~/kanjiLinks}}
  ```

  Output:
  ```html
  some<br>multiline<br>text
  ```

  Preview:
  <pre>some<br>multiline<br>text</pre>
</details>


### `regexReplace`

Uses a [regular expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) to replace a pattern with the specified text.

<details>
  <summary>Syntax:</summary>

  <code>{{#regexReplace <i>regex</i> <i>replacement</i> <i>[flags]</i>}}<i>text-to-modify</i>{{/regexReplace}}</code>

  * _`regex`_ <br>
    The raw string used to create the regular expression. This value is passed to the [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp) constructor.
  * _`replacement`_ <br>
    The text used to replace pattern matches. This supports the standard [special capture group replacements](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter) as supported by the web browser.
  * _`flags`_ _(optional)_ <br>
    Optional flags to pass to the [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp) constructor.
  * _`text-to-modify`_ <br>
    The text that the regular expression is applied to.
</details>
<details>
  <summary>Example:</summary>

  ```handlebars
  {{#regexReplace "\(([^)]*)\)" "$1" "g"~}}Here is (some) (text) (in) (parentheses){{~/regexReplace}}
  ```

  Output:
  ```html
  Here is some text in parentheses
  ```
</details>


### `regexMatch`

Uses a [regular expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) to return only the content that matches the pattern.

<details>
  <summary>Syntax:</summary>

  <code>{{#regexMatch <i>regex</i> <i>[flags]</i>}}<i>text-to-modify</i>{{/regexMatch}}</code>

  * _`regex`_ <br>
    The raw string used to create the regular expression. This value is passed to the [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp) constructor.
  * _`flags`_ _(optional)_ <br>
    Optional flags to pass to the [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp) constructor.
  * _`text-to-modify`_ <br>
    The text that the regular expression is applied to.
</details>
<details>
  <summary>Example:</summary>

  ```handlebars
  {{#regexMatch "\(([^)]*)\)" "g"~}}Here is (some) (text) (in) (parentheses){{~/regexMatch}}
  ```

  Output:
  ```html
  (some)(text)(in)(parentheses)
  ```
</details>


### `mergeTags`

Creates a set of all unique tags for the definition and returns a text representation of the tags separated by commas.

<details>
  <summary>Syntax:</summary>

  <code>{{#mergeTags <i>definition</i> <i>isGroupMode</i> <i>isMergeMode</i>}}{{/mergeTags}}</code>

  * _`definition`_ <br>
    The root definition object.
  * _`isGroupMode`_ _(optional)_ <br>
    Whether or not the display mode is the 'group' mode.
  * _`isMergeMode`_ <br>
    Whether or not the display mode is the 'merge' mode.
</details>
<details>
  <summary>Example:</summary>

  ```handlebars
  {{~#mergeTags definition group merge}}{{/mergeTags~}}
  ```

  Output:
  ```html
  v5m, vt, JMdict (English)
  ```
</details>


### `eachUpTo`

Similar to the built-in `each` function, but iterates up to a maximum count.
If the iterable is falsy or empty, the `else` condition will be used.

<details>
  <summary>Syntax:</summary>

  <code>{{#eachUpTo <i>iterable</i> <i>maxCount</i>}}<i>(modification)</i>{{else}}<i>(else-modification)</i>{{/eachUpTo}}</code>

  * _`iterable`_ <br>
    The object that should be looped over. A JavaScript [`for...of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) loop is used, so the object only needs to be iterable.
  * _`maxCount`_ _(optional)_ <br>
    The maximum number of entries to loop over.
  * _`modification`_ <br>
    The template used to modify the value. The context is changed to the current item of iteration.
  * _`else-modification`_ <br>
    The template used in case the iterable is falsy or empty. The context is unchanged.
</details>
<details>
  <summary>Example:</summary>

  ```handlebars
  {{~#eachUpTo someArray 5}}{{{.}}}<br>{{else}}Empty{{/mergeTags~}}
  ```

  Output:
  ```html
  someArray[0]<br>someArray[1]<br>someArray[2]<br>someArray[3]<br>someArray[4]<br>
  ```

  Preview:
  <pre>someArray[0]<br>someArray[1]<br>someArray[2]<br>someArray[3]<br>someArray[4]<br></pre>
</details>


### `spread`

Uses the JavaScript [spread](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) operator to convert one or more iterables into a single array.
This allows it to be used similar to an [`Array.concat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat) operation.

<details>
  <summary>Syntax:</summary>

  <code>{{#spread <i>iterable1</i> <i>iterable2</i> <i>...</i> <i>iterableN</i>}}{{/spread}}</code>

  * _`iterableN`_ <br>
    A variable amount of iterable objects to combine into a single array.
</details>
<details>
  <summary>Example:</summary>

  ```handlebars
  {{#each (spread array1 array2)}}{{{.}}}<br>{{/each}}
  ```

  Output:
  ```html
  array1[0]<br>array1[1]<br>array2[0]<br>array2[1]<br>
  ```

  Preview:
  <pre>array1[0]<br>array1[1]<br>array2[0]<br>array2[1]<br></pre>
</details>


### `op`

Performs a simple operation on one, two, or three arguments. The operations available are:

* Unary operators: `+`, `-`, `~`, `!`
* Binary operators: `+`, `-`, `/`, `*`, `%`, `**`, `==`, `!=`, `===`, `!==`, `<`, `<=`, `>`, `>=`, `<<`, `>>`, `>>>`, `&`, `|`, `^`, `&&`, `||`
* Ternary operators: `?:`

If an unknown operator is specified, the `undefined` value is returned.

<details>
  <summary>Syntax:</summary>

  <code>{{#op <i>operator</i> <i>operand1</i> <i>[operand2]</i> <i>[operand3]</i>}}{{/op}}</code>

  * _`operator`_ <br>
    One of the unary, binary, or ternary operators.
  * _`operand1`_ <br>
    The first operand of the operation.
  * _`operand2`_ _(Optional)_<br>
    The second operand of the operation.
  * _`operand3`_ _(Optional)_<br>
    The third operand of the operation.
</details>
<details>
  <summary>Example:</summary>

  ```handlebars
  {{#if (op "===" value1 value2)}}Values are equal{{/op~}}<br>
  {{~#op "-" value1}}{{/op~}}<br>
  {{~#op "?:" value1 "a" "b"}}{{/op}}
  ```

  Output:
  ```html
  Values are equal<br>-32<br>a
  ```

  Preview:
  <pre>Values are equal<br>-32<br>a</pre>
</details>


### `get`

Gets a value from the custom state stack.

<details>
  <summary>Syntax:</summary>

  <code>{{#get <i>name</i>}}{{/get}}</code>

  * _`name`_ <br>
    The name of the variable to get.
</details>
<details>
  <summary>Example:</summary>

  ```handlebars
  {{#get "some-text"}}{{/get}}
  ```

  Output:
  ```html
  This is the value of some-text!
  ```
</details>


### `set`

Assigns a value to the custom state stack.

<details>
  <summary>Syntax:</summary>

  <code>{{#set <i>name</i>}}<i>value</i>{{/get}}</code><br>
  <code>{{#set <i>name</i> <i>value</i>}}{{/get}}</code><br>

  * _`name`_ <br>
    The name of the variable to assign.
  * _`value`_ <br>
    The value of the variable.
</details>
<details>
  <summary>Example:</summary>

  ```handlebars
  {{#set "some-text"}}This is the value of some-text!{{/set~}}
  {{~#set "some-number" 32}}{{/set}}
  ```

  Output:
  ```html
  ```
</details>


### `scope`

Pushes a new variable scope to the custom state stack.
Variable assignments are applied to the most recent scope,
and variable lookups will start from the most recent scope and work backwards until a value is found.

<details>
  <summary>Syntax:</summary>

  <code>{{#scope}}<i>content</i>{{/scope}}</code>

  * _`name`_ <br>
    The name of the variable to assign.
  * _`value`_ <br>
    The value of the variable.
</details>
<details>
  <summary>Example:</summary>

  ```handlebars
  {{~#set "key" 32}}{{/set~}}
  {{~#get "key"}}{{/get~}},
  {{~#scope~}}
    {{~#get "key"}}{{/get~}},
    {{~#set "key" 64}}{{/set~}}
    {{~#get "key"}}{{/get~}},
  {{~/scope~}}
  {{~#get "key"}}{{/get~}}
  ```

  Output:
  ```html
  32,32,64,32
  ```
</details>


### `property`

Repeatedly gets a property of an object.

<details>
  <summary>Syntax:</summary>

  <code>{{#property <i>object</i> <i>property1</i> <i>property2</i> <i>...</i> <i>propertyN</i>}}{{/property}}</code>

  * _`object`_ <br>
    The initial object to use.
  * _`propertyN`_ <br>
    A chain of property names to get on the object.
</details>
<details>
  <summary>Example:</summary>

  ```handlebars
  {{property someObject "field" 0 "toString"}}
  ```

  Output:
  ```html
  function toString() { [native code] }
  ```
</details>


### `noop`

No-op. Returns the inner contents of the template.

<details>
  <summary>Syntax:</summary>

  <code>{{#noop}}<i>content</i>{{/noop}}</code>
</details>
<details>
  <summary>Example:</summary>

  ```handlebars
  {{noop}}Unchanged content{{/noop}}
  ```

  Output:
  ```html
  Unchanged content
  ```
</details>


### `isMoraPitchHigh`

Returns whether or not a mora will have a high pitch, given the index of the mora and the position of the downstep.

<details>
  <summary>Syntax:</summary>

  <code>{{#isMoraPitchHigh <i>index</i> <i>position</i>}}{{/isMoraPitchHigh}}</code>
</details>
<details>
  <summary>Example:</summary>

  ```handlebars
  {{#if (isMoraPitchHigh 1 2)}}High pitch{{else}}Low pitch{{/if}}
  ```

  Output:
  ```html
  High pitch
  ```
</details>


### `getKanaMorae`

Returns an array of the mora for a kana string.

<details>
  <summary>Syntax:</summary>

  <code>{{#getKanaMorae <i>kana-string</i>}}{{/getKanaMorae}}</code>
</details>
<details>
  <summary>Example:</summary>

  ```handlebars
  {{#each (getKanaMorae "よみちゃん")}}{{{.}}}<br>{{/each}}
  ```

  Output:
  ```html<br><br>ちゃ<br><br>
  ```

  Preview:
  <pre>よ<br>み<br>ちゃ<br>ん<br></pre>
</details>


## Legacy Helpers

Yomichan has historically used Handlebars templates to generate the HTML used on the search page and results popup.
To simplify the and improve Yomichan's capabilities, the HTML elements are now generated directly using a different process.

As such, there are several leftover Handlebars helpers that do not have much utility for Anki templates, but are kept for compatibility purposes.


### `kanjiLinks`

Replaces kanji characters in the text with linkified versions.

<details>
  <summary>Syntax:</summary>

  <code>{{#kanjiLinks}}<i>text</i>{{/kanjiLinks}}</code>
</details>
<details>
  <summary>Example:</summary>

  ```handlebars
  {{#kanjiLinks}}読む{{/kanjiLinks}}
  ```

  Output:
  ```html
  <a href="#" class="kanji-link"></a>  ```

  Preview:
  <pre><a href="#" class="kanji-link">読</a>む</pre>
</details>


### `sanitizeCssClass`

Sanitizes text so it can be used as a CSS class name.

<details>
  <summary>Syntax:</summary>

  <code>{{#sanitizeCssClass}}<i>text</i>{{/sanitizeCssClass}}</code>
</details>
<details>
  <summary>Example:</summary>

  ```handlebars
  {{#sanitizeCssClass}}some text with many types of characters !@#$%^ 読む{{/sanitizeCssClass}}
  ```

  Output:
  ```html
  some_text_with_many_types_of_characters________読む
  ```
</details>