# HG changeset patch # User Oleksandr Gavenko # Date 1474488419 -10800 # Node ID 181897a421e99d813eb05f4445d0a7bfe5b79a6c # Parent 5709d5bd349b2db9ed9d25ccd3218b081dce6c75 Remove code duplication. diff -r 5709d5bd349b -r 181897a421e9 py/gadict_srs_anki.py --- 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("
") + if sense.pos: + buf.append("") + buf.append(sense.pos) + buf.append("") + if sense.ant_list and len(sense.ant_list) > 0: + buf.append(" ant: ") + buf.append("; ".join(sense.ant_list)) + buf.append("") + if sense.syn_list and len(sense.syn_list) > 0: + buf.append(" syn: ") + buf.append("; ".join(sense.syn_list)) + buf.append("") + for (lang, tr) in sense.tr_list or []: + if len(sense.tr_list) > 1: + buf.append("
") + buf.append(" ") + buf.append(lang) + buf.append("") + buf.append(" ") + buf.append(tr) + buf.append("") + if len(sense.tr_list) > 1: + buf.append("
") + for (lang, tr) in sense.glos_list or []: + buf.append("
") + buf.append(" ") + buf.append(lang) + buf.append("") + buf.append(" ") + buf.append(tr) + buf.append("") + buf.append("
") + for (lang, tr) in (with_examples and sense.ex_list) or []: + buf.append("
") + buf.append(" ") + buf.append(lang) + buf.append("") + buf.append(" ") + buf.append(tr) + buf.append("") + buf.append("
") + try: builder = AnkiDbBuilder(TMPDIR, FBASENAME) @@ -198,95 +242,11 @@ direct_from = "".join(buf) buf = [] for sense in translations: - buf.append("
") - if sense.pos: - buf.append("") - buf.append(sense.pos) - buf.append("") - if sense.ant_list and len(sense.ant_list) > 0: - buf.append(" ant: ") - buf.append("; ".join(sense.ant_list)) - buf.append("") - if sense.syn_list and len(sense.syn_list) > 0: - buf.append(" syn: ") - buf.append("; ".join(sense.syn_list)) - buf.append("") - for (lang, tr) in sense.tr_list or []: - if len(sense.tr_list) > 1: - buf.append("
") - buf.append(" ") - buf.append(lang) - buf.append("") - buf.append(" ") - buf.append(tr) - buf.append("") - if len(sense.tr_list) > 1: - buf.append("
") - for (lang, tr) in sense.ex_list or []: - buf.append("
") - buf.append(" ") - buf.append(lang) - buf.append("") - buf.append(" ") - buf.append(tr) - buf.append("") - buf.append("
") - for (lang, tr) in sense.glos_list or []: - buf.append("
") - buf.append(" ") - buf.append(lang) - buf.append("") - buf.append(" ") - buf.append(tr) - buf.append("") - buf.append("
") + write_sense(buf, sense, with_examples = True) direct_to = "".join(buf) buf = [] for sense in translations: - buf.append("
") - if sense.pos: - buf.append("") - buf.append(sense.pos) - buf.append("") - need_sep = False - if sense.ant_list and len(sense.ant_list) > 0: - buf.append(" ant: ") - buf.append("; ".join(sense.ant_list)) - buf.append("") - need_sep = True - if sense.syn_list and len(sense.syn_list) > 0: - if need_sep: - buf.append(" |") - buf.append(" syn: ") - buf.append("; ".join(sense.syn_list)) - buf.append("") - need_sep = True - if sense.rel_list and len(sense.rel_list) > 0: - if need_sep: - buf.append(" |") - buf.append(" rel: ") - buf.append("; ".join(sense.rel_list)) - buf.append("") - for (lang, tr) in sense.tr_list or []: - if len(sense.tr_list) > 1: - buf.append("
") - buf.append(" ") - buf.append(lang) - buf.append("") - buf.append(" ") - buf.append(tr) - buf.append("") - if len(sense.tr_list) > 1: - buf.append("
") - for (lang, tr) in sense.glos_list or []: - buf.append("
") - buf.append(" ") - buf.append(lang) - buf.append("") - buf.append(" ") - buf.append(tr) - buf.append("") - buf.append("
") + 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)