Remove code duplication.
--- a/py/gadict_srs_anki.py Wed Sep 21 22:57:21 2016 +0300
+++ b/py/gadict_srs_anki.py Wed Sep 21 23:06:59 2016 +0300
@@ -161,6 +161,50 @@
def close(self):
self.collection.close()
+def write_sense(buf, sense, with_examples = True):
+ buf.append("<div class='sense'>")
+ if sense.pos:
+ buf.append("<span class='pos'>")
+ buf.append(sense.pos)
+ buf.append("</span>")
+ if sense.ant_list and len(sense.ant_list) > 0:
+ buf.append(" <span class='ant'>ant: ")
+ buf.append("; ".join(sense.ant_list))
+ buf.append("</span>")
+ if sense.syn_list and len(sense.syn_list) > 0:
+ buf.append(" <span class='syn'>syn: ")
+ buf.append("; ".join(sense.syn_list))
+ buf.append("</span>")
+ for (lang, tr) in sense.tr_list or []:
+ if len(sense.tr_list) > 1:
+ buf.append("<div class='sense'>")
+ buf.append(" <span class='lang'>")
+ buf.append(lang)
+ buf.append("</span>")
+ buf.append(" <span class='tr'>")
+ buf.append(tr)
+ buf.append("</span>")
+ if len(sense.tr_list) > 1:
+ buf.append("</div>")
+ for (lang, tr) in sense.glos_list or []:
+ buf.append("<div class='glos'>")
+ buf.append(" <span class='lang'>")
+ buf.append(lang)
+ buf.append("</span>")
+ buf.append(" <span class='glos'>")
+ buf.append(tr)
+ buf.append("</span>")
+ buf.append("</div>")
+ for (lang, tr) in (with_examples and sense.ex_list) or []:
+ buf.append("<div class='ex'>")
+ buf.append(" <span class='lang'>")
+ buf.append(lang)
+ buf.append("</span>")
+ buf.append(" <span class='ex'>")
+ buf.append(tr)
+ buf.append("</span>")
+ buf.append("</div>")
+
try:
builder = AnkiDbBuilder(TMPDIR, FBASENAME)
@@ -198,95 +242,11 @@
direct_from = "".join(buf)
buf = []
for sense in translations:
- buf.append("<div class='sense'>")
- if sense.pos:
- buf.append("<span class='pos'>")
- buf.append(sense.pos)
- buf.append("</span>")
- if sense.ant_list and len(sense.ant_list) > 0:
- buf.append(" <span class='ant'>ant: ")
- buf.append("; ".join(sense.ant_list))
- buf.append("</span>")
- if sense.syn_list and len(sense.syn_list) > 0:
- buf.append(" <span class='syn'>syn: ")
- buf.append("; ".join(sense.syn_list))
- buf.append("</span>")
- for (lang, tr) in sense.tr_list or []:
- if len(sense.tr_list) > 1:
- buf.append("<div class='sense'>")
- buf.append(" <span class='lang'>")
- buf.append(lang)
- buf.append("</span>")
- buf.append(" <span class='tr'>")
- buf.append(tr)
- buf.append("</span>")
- if len(sense.tr_list) > 1:
- buf.append("</div>")
- for (lang, tr) in sense.ex_list or []:
- buf.append("<div class='ex'>")
- buf.append(" <span class='lang'>")
- buf.append(lang)
- buf.append("</span>")
- buf.append(" <span class='ex'>")
- buf.append(tr)
- buf.append("</span>")
- buf.append("</div>")
- for (lang, tr) in sense.glos_list or []:
- buf.append("<div class='glos'>")
- buf.append(" <span class='lang'>")
- buf.append(lang)
- buf.append("</span>")
- buf.append(" <span class='glos'>")
- buf.append(tr)
- buf.append("</span>")
- buf.append("</div>")
+ write_sense(buf, sense, with_examples = True)
direct_to = "".join(buf)
buf = []
for sense in translations:
- buf.append("<div class='sense'>")
- if sense.pos:
- buf.append("<span class='pos'>")
- buf.append(sense.pos)
- buf.append("</span>")
- need_sep = False
- if sense.ant_list and len(sense.ant_list) > 0:
- buf.append(" <span class='ant'>ant: ")
- buf.append("; ".join(sense.ant_list))
- buf.append("</span>")
- need_sep = True
- if sense.syn_list and len(sense.syn_list) > 0:
- if need_sep:
- buf.append(" |")
- buf.append(" <span class='syn'>syn: ")
- buf.append("; ".join(sense.syn_list))
- buf.append("</span>")
- need_sep = True
- if sense.rel_list and len(sense.rel_list) > 0:
- if need_sep:
- buf.append(" |")
- buf.append(" <span class='rel'>rel: ")
- buf.append("; ".join(sense.rel_list))
- buf.append("</span>")
- for (lang, tr) in sense.tr_list or []:
- if len(sense.tr_list) > 1:
- buf.append("<div class='sense'>")
- buf.append(" <span class='lang'>")
- buf.append(lang)
- buf.append("</span>")
- buf.append(" <span class='tr'>")
- buf.append(tr)
- buf.append("</span>")
- if len(sense.tr_list) > 1:
- buf.append("</div>")
- for (lang, tr) in sense.glos_list or []:
- buf.append("<div class='glos'>")
- buf.append(" <span class='lang'>")
- buf.append(lang)
- buf.append("</span>")
- buf.append(" <span class='glos'>")
- buf.append(tr)
- buf.append("</span>")
- buf.append("</div>")
+ write_sense(buf, sense, with_examples = False)
reverse_from = "".join(buf) # without examples!!
builder.add_note("en->tr", identity, direct_from, direct_to)
builder.add_note("tr->en", identity, reverse_from, direct_from)