50 # Therefore absolute path should be stored before creating temporary deck |
50 # Therefore absolute path should be stored before creating temporary deck |
51 FONAME = os.path.abspath(FONAME) |
51 FONAME = os.path.abspath(FONAME) |
52 FBASENAME, _ = os.path.splitext(os.path.basename(FONAME)) |
52 FBASENAME, _ = os.path.splitext(os.path.basename(FONAME)) |
53 TMPDIR = tempfile.mkdtemp(dir = os.path.dirname(FONAME)) |
53 TMPDIR = tempfile.mkdtemp(dir = os.path.dirname(FONAME)) |
54 |
54 |
|
55 try: |
|
56 FDEL = io.open(FBASENAME+".del", mode='r', buffering=1, encoding="utf-8") |
|
57 except: |
|
58 FDEL = None |
|
59 |
|
60 |
55 import hashlib |
61 import hashlib |
56 |
62 |
57 import anki |
63 import anki |
58 from anki.exporting import AnkiPackageExporter |
64 from anki.exporting import AnkiPackageExporter |
59 |
65 |
143 :type_ used to generate different notes from same headword |
153 :type_ used to generate different notes from same headword |
144 """ |
154 """ |
145 h = hashlib.md5(":".join((self.name, type_, headword))) |
155 h = hashlib.md5(":".join((self.name, type_, headword))) |
146 return h.hexdigest() |
156 return h.hexdigest() |
147 |
157 |
148 def add_note(self, type_, id_, from_, to_): |
158 def add_note(self, type_, id_, from_, to_, tags_ = None): |
149 note = self.collection.newNote() |
159 note = self.collection.newNote() |
150 note['From'] = from_ |
160 note['From'] = from_ |
151 # print(from_) |
161 # print(from_) |
152 note['To'] = to_ |
162 note['To'] = to_ |
153 # print(to_) |
163 # print(to_) |
|
164 if isinstance(tags_, str): note.tags = [tags_] |
|
165 elif isinstance(tags_, list): note.tags = tags_ |
|
166 elif tags_ is None: pass |
|
167 else: raise Exception('Expect string or list of tags...') |
154 note.guid = self.guid(type_, id_) |
168 note.guid = self.guid(type_, id_) |
155 self.collection.addNote(note) |
169 self.collection.addNote(note) |
156 |
170 |
157 def export(self, fname): |
171 def export(self, fname): |
158 export = AnkiPackageExporter(self.collection) |
172 export = AnkiPackageExporter(self.collection) |
213 buf.append("</span>") |
227 buf.append("</span>") |
214 buf.append("</div>") |
228 buf.append("</div>") |
215 |
229 |
216 try: |
230 try: |
217 builder = AnkiDbBuilder(TMPDIR, FBASENAME) |
231 builder = AnkiDbBuilder(TMPDIR, FBASENAME) |
|
232 |
|
233 for identity in FDEL or []: |
|
234 identity = identity.strip() |
|
235 warnmsg = "<div class='del'>Please delete this note ({})</div>".format(identity) |
|
236 builder.add_note("en->tr", identity, warnmsg, warnmsg+" en->tr", "del") |
|
237 builder.add_note("tr->en", identity, warnmsg, warnmsg+" tr->en", "del") |
|
238 builder.add_note("irregular1", identity, warnmsg, warnmsg+" irregular1", "del") |
|
239 builder.add_note("irregular2", identity, warnmsg, warnmsg+" irregular2", "del") |
|
240 builder.add_note("irregular3", identity, warnmsg, warnmsg+" irregular3", "del") |
|
241 builder.add_note("singular", identity, warnmsg, warnmsg+" singular", "del") |
|
242 builder.add_note("plural", identity, warnmsg, warnmsg+" plural", "del") |
218 |
243 |
219 for (headwords, translations) in DOM[1:]: |
244 for (headwords, translations) in DOM[1:]: |
220 identity = headwords[0].headword |
245 identity = headwords[0].headword |
221 buf = [] |
246 buf = [] |
222 v1, v2, v3 = (None, None, None) |
247 v1, v2, v3 = (None, None, None) |
273 riddle_pl = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>pl</span>".format(plural[0], plural[1]) |
298 riddle_pl = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>pl</span>".format(plural[0], plural[1]) |
274 answer = u"{}<br>{}<br>{}".format(riddle_s, riddle_pl, direct_to) |
299 answer = u"{}<br>{}<br>{}".format(riddle_s, riddle_pl, direct_to) |
275 builder.add_note("singular", identity, question + riddle_s, answer) |
300 builder.add_note("singular", identity, question + riddle_s, answer) |
276 builder.add_note("plural", identity, question + riddle_pl, answer) |
301 builder.add_note("plural", identity, question + riddle_pl, answer) |
277 |
302 |
278 |
|
279 builder.export(FONAME) |
303 builder.export(FONAME) |
280 finally: |
304 finally: |
281 builder.close() |
305 builder.close() |
282 shutil.rmtree(TMPDIR, ignore_errors=True) |
306 shutil.rmtree(TMPDIR, ignore_errors=True) |