FooSoft / yomichan

Japanese pop-up dictionary extension for Chrome and Firefox.
https://foosoft.net/projects/yomichan
Other
1.05k stars 205 forks source link

Export definition of different dictionaries in different fields #1345

Closed ddc251992 closed 3 years ago

ddc251992 commented 3 years ago

1) I was trying to modify the handlebars from yomichan to see if I could export the definitions available from the dictionaries in different fields of my card in anki but I don't seem to get it right. 2) Also I don't know if it is possible to specify, how many definition could be imported from each dictionary.

Could it be possible to have a sample code explaining how to do it (if it is possible)? Thanks for your time. 1

toasted-nutbread commented 3 years ago

Here is an example based off the latest definitions. Note that the {glossary} and {glossary-single} definitions have changed since the last release, but the behaviour is the same.

Base file: https://github.com/FooSoft/yomichan/blob/6c9f4a2be98012e791485dadb7078c16cb8d063c/ext/bg/data/default-anki-field-templates.handlebars

Modifications patch below. If you are unfamiliar with diffs, the red is the value of the old file, the green is the changes or additions you would have to make.

diff --git a/ext/bg/data/default-anki-field-templates.handlebars b/ext/bg/data/default-anki-field-templates.handlebars
index 0f390e8d7..ab2f97573 100644
--- a/ext/bg/data/default-anki-field-templates.handlebars
+++ b/ext/bg/data/default-anki-field-templates.handlebars
@@ -1,4 +1,5 @@
 {{#*inline "glossary-single"}}
+    {{~#if (op "||" (op "===" targetDictionary undefined) (op "===" targetDictionary dictionary))~}}
     {{~#unless brief~}}
         {{~#scope~}}
             {{~#set "any" false}}{{/set~}}
@@ -28,6 +29,7 @@
         <ul>{{#each glossary}}<li>{{#multiLine}}{{.}}{{/multiLine}}</li>{{/each}}</ul>
     {{~/if~}}
     {{~#set "previousDictionary" dictionary~}}{{~/set~}}
+    {{~/if~}}
 {{/inline}}

 {{#*inline "audio"}}
@@ -101,12 +103,12 @@
     <div style="text-align: left;">
     {{~#scope~}}
         {{~#if (op "===" definition.type "term")~}}
-            {{~> glossary-single definition brief=brief noDictionaryTag=noDictionaryTag ~}}
+            {{~> glossary-single definition brief=brief noDictionaryTag=noDictionaryTag targetDictionary=targetDictionary ~}}
         {{~else if (op "||" (op "===" definition.type "termGrouped") (op "===" definition.type "termMerged"))~}}
             {{~#if (op ">" definition.definitions.length 1)~}}
-                <ol>{{~#each definition.definitions~}}<li>{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag ~}}</li>{{~/each~}}</ol>
+                <ol>{{~#each definition.definitions~}}<li>{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag targetDictionary=../targetDictionary ~}}</li>{{~/each~}}</ol>
             {{~else~}}
-                {{~#each definition.definitions~}}{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag ~}}{{~/each~}}
+                {{~#each definition.definitions~}}{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag targetDictionary=../targetDictionary ~}}{{~/each~}}
             {{~/if~}}
         {{~else if (op "===" definition.type "kanji")~}}
             {{~#if (op ">" definition.glossary.length 1)~}}
@@ -127,6 +129,10 @@
     {{~> glossary brief=true ~}}
 {{/inline}}

+{{~#*inline "glossary-jmdict"~}}
+    {{~> glossary targetDictionary="JMdict (English)" ~}}
+{{~/inline~}}
+
 {{#*inline "kunyomi"}}
     {{~#each definition.kunyomi}}{{.}}{{#unless @last}}, {{/unless}}{{/each~}}
 {{/inline}}

In short:

Note: this setup may result in some empty list elements for skipped glossary entries, but it is the most simple way to demonstrate the process.

ddc251992 commented 3 years ago

Thank you so much solved the problem. I also leave here the example I used for anyone interested.

template Dictionary HandleBars_without bullet points.txt

bcokert commented 2 years ago

This still works great on the latest (Oct 2021) versions of Yomichan and Anki.

You can fix the empty list elements toasted-nutbread mentioned by changing one line (remove the ol and li tags, since you'll always have exactly 1 matching dictionary):

Replace

<ol>{{~#each definition.definitions~}}<li>{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag targetDictionary=../targetDictionary ~}}</li>{{~/each~}}</ol>

With

{{~#each definition.definitions~}}{{~> glossary-single . brief=../brief noDictionaryTag=../noDictionaryTag targetDictionary=../targetDictionary ~}}{{~/each~}}

Here's an example with a single bilingual and a single monolingual dictionary: Yomichan: yomichan Anki: anki