Skip to content

Latest commit

 

History

History
926 lines (667 loc) · 20.9 KB

templates.md

File metadata and controls

926 lines (667 loc) · 20.9 KB

Templates

Helpers

Yomitan supports several custom Handlebars helpers for rendering templates. The source code for these templates can be found here.

dumpObject

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

Syntax:

{{dumpObject object}}

  • object
    The object to convert.
Example:
<pre>{{dumpObject .}}</pre>

Output:

<pre>{
    "key": "value"
}</pre>

Preview:

{
    "key": "value"
}

furigana

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

Syntax:

{{furigana definition}}
{{furigana expression reading}}

  • definition
    The definition to convert.
  • expression
    The expression to convert.
  • reading
    The reading to convert.
Example:
{{furigana .}}
{{furigana "読む" "よむ"}}

Output:

<ruby><rt></rt></ruby>

Preview

furiganaPlain

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

Syntax:

{{furiganaPlain definition}} {{furiganaPlain expression reading}}

  • definition
    The definition to convert.
  • expression
    The expression to convert.
  • reading
    The reading to convert.
Example:
{{~furiganaPlain .~}}
{{furiganaPlain "読む" "よむ"}}

Output:

読[よ]む

multiLine

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

Syntax:

{{#multiLine}}text with multiple lines{{/multiLine}}

Example:
{{#multiLine~}}
some
multiline
text
{{~/multiLine}}

Output:

some<br>multiline<br>text

Preview:

some
multiline
text

regexReplace

Uses a regular expression to replace a pattern with the specified text.

Syntax:

{{#regexReplace regex replacement [flags]}}text-to-modify{{/regexReplace}}
{{regexReplace regex replacement [flags] [text-to-modify]...}}

  • regex
    The raw string used to create the regular expression. This value is passed to the RegExp constructor.
  • replacement
    The text used to replace pattern matches. This supports the standard special capture group replacements as supported by the web browser.
  • flags (optional)
    Optional flags to pass to the RegExp constructor.
  • text-to-modify
    The text that the regular expression is applied to. If multiple arguments are present, they are all concatenated.
Example:
{{#regexReplace "\(([^)]*)\)" "$1" "g"~}}Here is (some) (text) (in) (parentheses){{~/regexReplace}}

Output:

Here is some text in parentheses

regexMatch

Uses a regular expression to return only the content that matches the pattern.

Syntax:

{{#regexMatch regex [flags]}}text-to-modify{{/regexMatch}}
{{regexMatch regex [flags] [text-to-modify]...}}

  • regex
    The raw string used to create the regular expression. This value is passed to the RegExp constructor.
  • flags (optional)
    Optional flags to pass to the RegExp constructor.
  • text-to-modify
    The text that the regular expression is applied to. If multiple arguments are present, they are all concatenated.
Example:
{{#regexMatch "\(([^)]*)\)" "g"~}}Here is (some) (text) (in) (parentheses){{~/regexMatch}}

Output:

(some)(text)(in)(parentheses)

mergeTags

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

Syntax:

{{mergeTags definition isGroupMode isMergeMode}}

  • definition
    The root definition object.
  • isGroupMode (optional)
    Whether or not the display mode is the 'group' mode.
  • isMergeMode
    Whether or not the display mode is the 'merge' mode.
Example:
{{~mergeTags definition group merge~}}

Output:

v5m, vt, JMdict (English)

#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.

Syntax:

{{#eachUpTo iterable maxCount}}(modification){{else}}(else-modification){{/eachUpTo}}

  • iterable
    The object that should be looped over. A JavaScript for...of loop is used, so the object only needs to be iterable.
  • maxCount (optional)
    The maximum number of entries to loop over.
  • modification
    The template used to modify the value. The context is changed to the current item of iteration.
  • else-modification
    The template used in case the iterable is falsy or empty. The context is unchanged.
Example:
{{~#eachUpTo someArray 5}}{{{.}}}<br>{{else}}Empty{{/mergeTags~}}

Output:

someArray[0]<br>someArray[1]<br>someArray[2]<br>someArray[3]<br>someArray[4]<br>

Preview:

someArray[0]
someArray[1]
someArray[2]
someArray[3]
someArray[4]

spread

Uses the JavaScript spread operator to convert one or more iterables into a single array. This allows it to be used similar to an Array.concat operation.

Syntax:

{{spread iterable1 iterable2 ... iterableN}}

  • iterableN
    A variable amount of iterable objects to combine into a single array.
Example:
{{#each (spread array1 array2)}}{{{.}}}<br>{{/each}}

Output:

array1[0]<br>array1[1]<br>array2[0]<br>array2[1]<br>

Preview:

array1[0]
array1[1]
array2[0]
array2[1]

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.

Syntax:

{{op operator operand1 [operand2] [operand3]}}

  • operator
    One of the unary, binary, or ternary operators.
  • operand1
    The first operand of the operation.
  • operand2 (Optional)
    The second operand of the operation.
  • operand3 (Optional)
    The third operand of the operation.
Example:
{{#if (op "===" value1 value2)}}Values are equal{{/if~}}<br>
{{~#op "-" value1}}{{/op~}}<br>
{{~op "?:" value1 "a" "b"}}

Output:

Values are equal<br>-32<br>a

Preview:

Values are equal
-32
a

get

Gets a value from the custom state stack.

Syntax:

{{get name}}

  • name
    The name of the variable to get.
Example:
{{get "some-text"}}

Output:

This is the value of some-text!

set

Assigns a value to the custom state stack.

Syntax:

{{#set name}}value{{/get}}
{{set name value}}

  • name
    The name of the variable to assign.
  • value
    The value of the variable.
Example:
{{#set "some-text"}}This is the value of some-text!{{/set~}}
{{~set "some-number" 32}}

Output:

#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.

Syntax:

{{#scope}}content{{/scope}}

  • name
    The name of the variable to assign.
  • value
    The value of the variable.
Example:
{{~set "key" 32~}}
{{~get "key"~}},
{{~#scope~}}
  {{~#get "key"~}},
  {{~#set "key" 64~}}
  {{~#get "key"~}},
{{~/scope~}}
{{~get "key"~}}

Output:

32,32,64,32

property

Repeatedly gets a property of an object.

Syntax:

{{property object property1 property2 ... propertyN}}

  • object
    The initial object to use.
  • propertyN
    A chain of property names to get on the object.
Example:
{{property someObject "field" 0 "toString"}}

Output:

function toString() { [native code] }

noop

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

Syntax:

{{#noop}}content{{/noop}}

Example:
{{noop}}Unchanged content{{/noop}}

Output:

Unchanged content

isMoraPitchHigh

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

Syntax:

{{isMoraPitchHigh index position}}

Example:
{{#if (isMoraPitchHigh 1 2)}}High pitch{{else}}Low pitch{{/if}}

Output:

High pitch

getKanaMorae

Returns an array of the mora for a kana string.

Syntax:

{{getKanaMorae kana-string}}

Example:
{{#each (getKanaMorae "よみたん")}}{{{.}}}<br>{{/each}}

Output:

<br><br><br><br>

Preview:





typeof

Returns the type of a value. #typeof in the block form will always return 'string'.

Syntax:

{{typeof value}}
{{#typeof}}value{{/typeof}}

  • value
    The value to check.
Example:
{{typeof "よみたん"}}
{{typeof 1}}
{{#typeof}}よみたん{{/typeof}}

Output:

string
number
string

join

Joins the arguments to a single string with a separator, flattening any arguments that are arrays.

Syntax:

{{join separator value1 value2 valueN...}}

  • separator
    The separator string to use between values.
  • valueN
    An individual value to join into the resulting string
Example:
{{set "index" 32~}}
{{~join "_" "yomitan" (get "index") "value"}}

Output:

yomitan_32_value

concat

Joins the arguments to a single string, without flattening arguments that are arrays.

Syntax:

{{concat value1 value1 valueN...}}

  • valueN
    A value to join into the resulting string
Example:
{{set "index" 32~}}
{{~concat "yomitan_" (get "index") "_value"}}

Output:

yomitan_32_value

pitchCategories

Returns an array representing the different pitch categories for a specific term.

Syntax:

{{pitchCategories @root}}

  • @root
    The argument passed should always be the root data object.
Example:
[{{#each (pitchCategories @root)}}{{.}}{{#unless @last}}, {{/unless}}{{/each}}]

Output:

[heiban, kifuku]

formatGlossary

Formats a glossary entry to a HTML content string. This helper handles image and structured-content generation.

Syntax:

{{formatGlossary dictionary definitionEntry}}

  • dictionary
    The dictionary that the glossary entry belongs to.
  • definitionEntry
    The definition entry object in raw form.
Example:
{{#each glossary}}{{formatGlossary ../dictionary .}}{{/each}}

Output:

Here is the content of a gloss, which may include formatted HTML.

hasMedia & getMedia

Checks to see if a certain type of media is available for a card and injects the relevant content. These functions are used together in order to request media and other types of optional asynchronous content.

Syntax:

{{hasMedia type args...}}
{{getMedia type args... [escape=true|false]}}

  • type
    The type of media to check for.
  • args
    Additional arguments for the media. The arguments depend on the media type.
  • escape (optional)
    Whether or not the resulting text should be HTML-escaped. If omitted, defaults to true.

Available media types and arguments

  • "audio"
  • "screenshot"
  • "clipboardImage"
  • "clipboardText"
  • "popupSelectionText"
  • "textFurigana" japaneseText readingMode="default|hiragana|katakana"
  • "dictionaryMedia" fileName dictionary="Dictionary Name"
Examples:
{{#if (hasMedia "audio")}}The audio file name is: {{getMedia "audio"}}{{/if}}

{{#if (hasMedia "screenshot")}}The screenshot file name is: {{getMedia "screenshot"}}{{/if}}

{{#if (hasMedia "clipboardImage")}}The clipboard image file name is: {{getMedia "clipboardImage"}}{{/if}}

{{#if (hasMedia "clipboardText")}}The clipboard text is: {{getMedia "clipboardText"}}{{/if}}

{{#if (hasMedia "popupSelectionText")}}The popup selection text is: {{getMedia "popupSelectionText"}}{{/if}}

{{#if (hasMedia "textFurigana" "日本語")}}This is an example of text with generated furigana: {{getMedia "textFurigana" "日本語" escape=false}}{{/if}}

{{#if (hasMedia "dictionaryMedia" "image.png" dictionary="Example Dictionary")}}The remapped file name for image.png is: {{getMedia "dictionaryMedia" "image.png" dictionary="Example Dictionary"}}{{/if}}

Output:

The audio file name is: yomitan_audio_にほんご_日本語.mp3

The screenshot file name is: yomitan_browser_screenshot_にほんご_日本語.png

The clipboard image file name is: yomitan_clipboard_image_にほんご_日本語.png

The clipboard text is: This is the clipboard text

The selection text is: This is the selection text

The selection text is: This is the selection text

This is an example of text with generated furigana: <ruby>日本語<rt>にほんご</rt></ruby>

The remapped file name for image.png is: yomitan_dictionary_media_1_にほんご_日本語.png

pronunciation

Converts pronunciation information into a formatted HTML content string. The display layout is the same as the system used for generating popup and search page dictionary entries.

Syntax:

{{pronunciation format=string reading=string downstepPosition=integer [nasalPositions=array] [devoicePositions=array]}}

  • format
    The format of the HTML to generate. This can be any of the following values:
    • 'text'
    • 'graph'
    • 'position'
  • reading
    The kana reading of the term.
  • downstepPosition
    The mora position of the downstep in the reading.
  • nasalPositions (optional)
    An array of indices of mora that have a nasal pronunciation.
  • devoicePositions (optional)
    An array of indices of mora that are devoiced.
Example:
{{~pronunciation format='text' reading='よむ' downstepPosition=1~}}

hiragana

Converts katakana text to hiragana.

Syntax:

{{hiragana value [keepProlongedSoundMarks=true|false]}}
{{#hiragana [keepProlongedSoundMarks=true|false]}}value{{/hiragana}}

  • value
    The text to convert.
  • keepProlongedSoundMarks (optional)
    Whether or not the character should be kept or converted to a vowel character. Defaults to false if not specified.
Example:
{{hiragana "よみたん ヨミたん ヨミタン"}}
{{#hiragana}}よみたん ヨミたん ヨミタン{{/hiragana}}
{{#hiragana}}ローマ字{{/hiragana}}
{{#hiragana keepProlongedSoundMarks=true}}ローマ字{{/hiragana}}

Output:

よみたん よみたん よみたん
よみたん よみたん よみたん
ろうま字
ろーま字

katakana

Converts hiragana text to katakana.

Syntax:

{{katakana text}}
{{#katakana}}text{{/katakana}}

  • text
    The text to convert.
Example:
{{katakana "よみたん ヨミたん ヨミタン"}}
{{#katakana}}よみたん ヨミたん ヨミタン{{/katakana}}

Output:

ヨミタン ヨミタン ヨミタン
ヨミタン ヨミタン ヨミタン