py/gadict_srs_anki.py
author Oleksandr Gavenko <gavenkoa@gmail.com>
Fri, 11 Nov 2016 00:19:47 +0200
changeset 667 5f69f0776c37
parent 657 c1d10b3b9ee4
child 673 ca1d4a6e201b
permissions -rw-r--r--
Add missing imports.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
646
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
     2
"""Anki card writer"""
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     3
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     4
import os
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     5
import io
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     6
import sys
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     7
import codecs
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     8
import tempfile
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     9
import shutil
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    10
import signal
644
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    11
import regex
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    12
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    13
import gadict
646
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    14
import gadict_freq
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    15
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    16
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    17
FINAME = None
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    18
FONAME = None
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    19
LANGS = None
646
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    20
FREQ_SOURCES = []
644
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    21
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    22
# -lang:ru,uk
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    23
ARG_LANG_RE = regex.compile("-lang:(.+)")
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    24
# -freq:var:TAG=FILE or -freq:freq:TAG=FILE
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    25
ARG_FREQ_RE = regex.compile("-freq:(freq|var):([^=]+)=(.+)")
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    26
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    27
look_for_files = False
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    28
for idx in range(1, len(sys.argv)):
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    29
    arg = sys.argv[idx]
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    30
    if arg == "--":
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    31
        look_for_files = True
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    32
        continue
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    33
    if not look_for_files:
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    34
        m = ARG_LANG_RE.match(arg)
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    35
        if m:
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    36
            LANGS = set(m.group(1).split(","))
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    37
            for lang in LANGS:
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    38
                if len(lang) != 2:
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    39
                    raise Exception("Incorrect language specification: '{:s}'".format(arg))
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    40
            continue
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    41
        m = ARG_FREQ_RE.match(arg)
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    42
        if m:
646
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    43
            mode = m.group(1)
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    44
            tag = m.group(2)
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    45
            fname = m.group(3)
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    46
            with io.open(fname, mode='r', buffering=1, encoding="utf-8") as stream:
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    47
                if mode == "var":
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    48
                    parser = gadict_freq.HeadVarParser(stream)
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    49
                elif mode == "freq":
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    50
                    parser = gadict_freq.FreqlistParser(stream)
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    51
                else:
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    52
                    raise Exception("Unsupported mode: '{:s}'".format(mode))
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    53
                wlist = parser.parse()
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    54
            FREQ_SOURCES.append((tag, set(wlist)))
644
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    55
            continue
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    56
        if arg.startswith("-"):
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    57
            raise Exception("Unsupported option format: '{:s}'".format(arg))
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    58
    if not FINAME:
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    59
        FINAME = arg
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    60
        continue
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    61
    if not FONAME:
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    62
        FONAME = arg
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    63
        continue
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    64
    raise Exception("Unnecessary argument: '{:s}'".format(arg))
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    65
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    66
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    67
FIN = io.open(FINAME, mode='r', buffering=1, encoding="utf-8")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    68
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    69
PARSER = gadict.Parser()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    70
try:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    71
    DOM = PARSER.parse(FIN)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    72
except gadict.ParseException as ex:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    73
    sys.stdout.write("{:s}{:s}\n".format(FINAME, repr(ex)))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    74
    if __debug__:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    75
        import traceback
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    76
        traceback.print_exc()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    77
    exit(1)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    78
finally:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    79
    FIN.close()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    80
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    81
def cleanup(collection, tmpdir):
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    82
    if collection:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    83
        collection.close()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    84
    if tmpdir:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    85
        shutil.rmtree(tmpdir, ignore_errors=True)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    86
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    87
# signal.signal(signal.SIGINT, lambda signal, frame: cleanup())
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    88
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    89
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    90
if FONAME is None:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    91
    raise Exception('Missing output file name')
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    92
# Looks like anki libs change working directory to media directory of current deck
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    93
# Therefore absolute path should be stored before creating temporary deck
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    94
FONAME = os.path.abspath(FONAME)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    95
FBASENAME, _ = os.path.splitext(os.path.basename(FONAME))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    96
TMPDIR = tempfile.mkdtemp(dir = os.path.dirname(FONAME))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    97
608
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
    98
try:
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
    99
    FDEL = io.open(FBASENAME+".del", mode='r', buffering=1, encoding="utf-8")
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   100
except:
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   101
    FDEL = None
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   102
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   103
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   104
import hashlib
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   105
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   106
import anki
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   107
from anki.exporting import AnkiPackageExporter
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   108
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   109
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   110
class AnkiDbBuilder:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   111
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   112
    def __init__(self, tmpdir, name):
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   113
        self.tmpdir = tmpdir
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   114
        self.name = name
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   115
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   116
        self.collection = collection = anki.Collection(os.path.join(self.tmpdir, 'collection.anki2'))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   117
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   118
        deck_id = collection.decks.id(self.name + "_deck")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   119
        deck = collection.decks.get(deck_id)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   120
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   121
        model = collection.models.new(self.name + "_model")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   122
        model['tags'].append(self.name + "_tag")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   123
        model['did'] = deck_id
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   124
        model['css'] = """
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   125
.card {
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   126
  font-family: arial;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   127
  font-size: 20px;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   128
  text-align: center;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   129
  color: black;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   130
  background-color: white;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   131
}
556
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   132
.ask {
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   133
  margin-bottom: 1.2em;
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   134
}
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   135
span.headword {
556
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   136
  font-weight: bolt;
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   137
}
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   138
.pron {
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   139
  color: magenta;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   140
}
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   141
.pos {
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   142
  color: green;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   143
  font-style: italic;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   144
}
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   145
.lang {
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   146
  color: red;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   147
  font-style: italic;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   148
}
618
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   149
.topic {
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   150
  color: #B04080;
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   151
}
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   152
.ant {
565
ac68f2680ea0 Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 562
diff changeset
   153
  color: #404080;
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   154
}
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   155
.syn {
565
ac68f2680ea0 Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 562
diff changeset
   156
  color: #804040;
ac68f2680ea0 Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 562
diff changeset
   157
}
ac68f2680ea0 Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 562
diff changeset
   158
.rel {
618
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   159
  color: #604080;
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   160
}
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   161
.hyper {
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   162
  color: #600080;
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   163
}
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   164
.hypo {
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   165
  color: #606080;
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   166
}
556
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   167
.attrs {
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   168
  color: blue;
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   169
}
560
dad049a0f585 Include examples into dictionary articles and Packaged Anki Desk files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 559
diff changeset
   170
span.ex {
dad049a0f585 Include examples into dictionary articles and Packaged Anki Desk files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 559
diff changeset
   171
  font-style: italic;
dad049a0f585 Include examples into dictionary articles and Packaged Anki Desk files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 559
diff changeset
   172
  font-size: .85em;
dad049a0f585 Include examples into dictionary articles and Packaged Anki Desk files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 559
diff changeset
   173
}
569
5709d5bd349b Fix: reverse card missed glossary.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 566
diff changeset
   174
div.glos .lang {
5709d5bd349b Fix: reverse card missed glossary.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 566
diff changeset
   175
  color: brown;
5709d5bd349b Fix: reverse card missed glossary.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 566
diff changeset
   176
}
566
0bba61492c37 Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 565
diff changeset
   177
span.glos {
569
5709d5bd349b Fix: reverse card missed glossary.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 566
diff changeset
   178
  font-size: .95em;
566
0bba61492c37 Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 565
diff changeset
   179
}
646
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   180
.freq {
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   181
  color: red;
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   182
  font-weight: bold;
650
b13c09aaa9a3 Fix typo and decrease font size of frequency list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 647
diff changeset
   183
  font-size: small;
646
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   184
}
608
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   185
.del {
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   186
  color: red;
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   187
  font-weight: bold;
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   188
}
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   189
"""
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   190
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   191
        collection.models.addField(model, collection.models.newField('From'))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   192
        collection.models.addField(model, collection.models.newField('To'))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   193
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   194
        tmpl = collection.models.newTemplate('From -> To')
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   195
        tmpl['qfmt'] = '<div class="from">{{From}}</div>'
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   196
        tmpl['afmt'] = '{{FrontSide}}\n\n<hr id=answer>\n\n<div class="to">{{To}}</div>'
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   197
        collection.models.addTemplate(model, tmpl)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   198
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   199
        # Equivalent of:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   200
        #   collection.models.add(model)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   201
        # without setting auto-generated ID. It is essential to keep model['id']
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   202
        # unchanged between upgrades or notes will be skipped!!
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   203
        model['id'] = int(hashlib.sha1(self.name).hexdigest(), 16) % (2**63)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   204
        collection.models.update(model)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   205
        collection.models.setCurrent(model)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   206
        collection.models.save(model)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   207
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   208
    def guid(self, type_, headword):
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   209
        """
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   210
        :type_ used to generate different notes from same headword
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   211
        """
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   212
        h = hashlib.md5(":".join((self.name, type_, headword)))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   213
        return h.hexdigest()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   214
608
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   215
    def add_note(self, type_, id_, from_, to_, tags_ = None):
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   216
        note = self.collection.newNote()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   217
        note['From'] = from_
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   218
        # print(from_)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   219
        note['To'] = to_
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   220
        # print(to_)
608
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   221
        if isinstance(tags_, str):      note.tags = [tags_]
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   222
        elif isinstance(tags_, list):   note.tags = tags_
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   223
        elif tags_ is None:             pass
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   224
        else:                           raise Exception('Expect string or list of tags...')
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   225
        note.guid = self.guid(type_, id_)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   226
        self.collection.addNote(note)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   227
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   228
    def export(self, fname):
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   229
        export = AnkiPackageExporter(self.collection)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   230
        export.exportInto(fname)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   231
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   232
    def close(self):
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   233
        self.collection.close()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   234
570
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   235
def write_sense(buf, sense, with_examples = True):
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   236
    buf.append("<div class='sense'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   237
    if sense.pos:
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   238
        buf.append("<span class='pos'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   239
        buf.append(sense.pos)
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   240
        buf.append("</span>")
574
5478477ab2ae Add related words to Anki cards. Break line if any reference is provided.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 570
diff changeset
   241
    have_ref = False
570
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   242
    if sense.ant_list and len(sense.ant_list) > 0:
574
5478477ab2ae Add related words to Anki cards. Break line if any reference is provided.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 570
diff changeset
   243
        have_ref = True
570
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   244
        buf.append(" <span class='ant'>ant: ")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   245
        buf.append("; ".join(sense.ant_list))
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   246
        buf.append("</span>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   247
    if sense.syn_list and len(sense.syn_list) > 0:
574
5478477ab2ae Add related words to Anki cards. Break line if any reference is provided.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 570
diff changeset
   248
        have_ref = True
570
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   249
        buf.append(" <span class='syn'>syn: ")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   250
        buf.append("; ".join(sense.syn_list))
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   251
        buf.append("</span>")
618
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   252
    if sense.hyper_list and len(sense.hyper_list) > 0:
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   253
        have_ref = True
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   254
        buf.append(" <span class='hyper'>hyper: ")
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   255
        buf.append("; ".join(sense.hyper_list))
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   256
        buf.append("</span>")
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   257
    if sense.hypo_list and len(sense.hypo_list) > 0:
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   258
        have_ref = True
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   259
        buf.append(" <span class='hypo'>hypo: ")
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   260
        buf.append("; ".join(sense.hypo_list))
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   261
        buf.append("</span>")
574
5478477ab2ae Add related words to Anki cards. Break line if any reference is provided.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 570
diff changeset
   262
    if sense.rel_list and len(sense.rel_list) > 0:
5478477ab2ae Add related words to Anki cards. Break line if any reference is provided.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 570
diff changeset
   263
        have_ref = True
5478477ab2ae Add related words to Anki cards. Break line if any reference is provided.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 570
diff changeset
   264
        buf.append(" <span class='rel'>rel: ")
5478477ab2ae Add related words to Anki cards. Break line if any reference is provided.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 570
diff changeset
   265
        buf.append("; ".join(sense.rel_list))
5478477ab2ae Add related words to Anki cards. Break line if any reference is provided.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 570
diff changeset
   266
        buf.append("</span>")
570
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   267
    for (lang, tr) in sense.tr_list or []:
574
5478477ab2ae Add related words to Anki cards. Break line if any reference is provided.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 570
diff changeset
   268
        if len(sense.tr_list) > 1 or have_ref:
570
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   269
            buf.append("<div class='sense'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   270
        buf.append(" <span class='lang'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   271
        buf.append(lang)
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   272
        buf.append("</span>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   273
        buf.append(" <span class='tr'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   274
        buf.append(tr)
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   275
        buf.append("</span>")
574
5478477ab2ae Add related words to Anki cards. Break line if any reference is provided.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 570
diff changeset
   276
        if len(sense.tr_list) > 1 or have_ref:
570
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   277
            buf.append("</div>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   278
    for (lang, tr) in sense.glos_list or []:
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   279
        buf.append("<div class='glos'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   280
        buf.append(" <span class='lang'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   281
        buf.append(lang)
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   282
        buf.append("</span>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   283
        buf.append(" <span class='glos'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   284
        buf.append(tr)
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   285
        buf.append("</span>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   286
        buf.append("</div>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   287
    for (lang, tr) in (with_examples and sense.ex_list) or []:
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   288
        buf.append("<div class='ex'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   289
        buf.append(" <span class='lang'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   290
        buf.append(lang)
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   291
        buf.append("</span>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   292
        buf.append(" <span class='ex'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   293
        buf.append(tr)
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   294
        buf.append("</span>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   295
        buf.append("</div>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   296
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   297
try:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   298
    builder = AnkiDbBuilder(TMPDIR, FBASENAME)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   299
608
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   300
    for identity in FDEL or []:
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   301
        identity = identity.strip()
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   302
        warnmsg = "<div class='del'>Please delete this note ({})</div>".format(identity)
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   303
        builder.add_note("en->tr", identity, warnmsg, warnmsg+" en->tr", "del")
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   304
        builder.add_note("tr->en", identity, warnmsg, warnmsg+" tr->en", "del")
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   305
        builder.add_note("irregular1", identity, warnmsg, warnmsg+" irregular1", "del")
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   306
        builder.add_note("irregular2", identity, warnmsg, warnmsg+" irregular2", "del")
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   307
        builder.add_note("irregular3", identity, warnmsg, warnmsg+" irregular3", "del")
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   308
        builder.add_note("singular", identity, warnmsg, warnmsg+" singular", "del")
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   309
        builder.add_note("plural", identity, warnmsg, warnmsg+" plural", "del")
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   310
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   311
    for (headwords, translations) in DOM[1:]:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   312
        identity = headwords[0].headword
650
b13c09aaa9a3 Fix typo and decrease font size of frequency list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 647
diff changeset
   313
        if 'rare' in headwords[0].attrs:
647
6ae5399c8087 Add ``rare`` attribute to headword to filter low frequency headwords out from
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 646
diff changeset
   314
            continue
646
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   315
        freqtags = []
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   316
        for (freqtag, freqset) in FREQ_SOURCES:
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   317
            if identity in freqset:
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   318
                freqtags.append(freqtag)
657
c1d10b3b9ee4 Add freq marker to riddle cards too.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 650
diff changeset
   319
        freqmsg = ""
646
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   320
        if len(freqtags) > 0:
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   321
            freqmsg = ",".join(freqtags)
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   322
            freqmsg = "<div class='freq'>{:s}</div>".format(freqmsg)
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   323
        buf = []
556
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   324
        v1, v2, v3 = (None, None, None)
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   325
        singular, plural = (None, None)
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   326
        for hw in headwords:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   327
            buf.append("<div clsas='headword'>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   328
            buf.append("<span clsas='headword'>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   329
            buf.append(hw.headword)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   330
            buf.append("</span>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   331
            if hw.pron is not None:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   332
                buf.append(" <span class='pron'>[")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   333
                buf.append(hw.pron)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   334
                buf.append("]</span>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   335
            if len(hw.attrs) > 0:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   336
                l = [u"«"+x+u"»" for x in hw.attrs]
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   337
                l.sort()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   338
                buf.append("<span class='attrs'>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   339
                buf.append(", ".join(l))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   340
                buf.append("</span>")
556
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   341
            if 'v1' in hw.attrs:
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   342
                v1 = (hw.headword, hw.pron)
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   343
            if 'v2' in hw.attrs:
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   344
                v2 = (hw.headword, hw.pron)
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   345
            if 'v3' in hw.attrs:
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   346
                v3 = (hw.headword, hw.pron)
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   347
            if 's' in hw.attrs:
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   348
                singular = (hw.headword, hw.pron)
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   349
            if 'pl' in hw.attrs:
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   350
                plural = (hw.headword, hw.pron)
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   351
            buf.append("</div>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   352
        direct_from = "".join(buf)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   353
        buf = []
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   354
        for sense in translations:
570
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   355
            write_sense(buf, sense, with_examples = True)
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   356
        direct_to = "".join(buf)
560
dad049a0f585 Include examples into dictionary articles and Packaged Anki Desk files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 559
diff changeset
   357
        buf = []
dad049a0f585 Include examples into dictionary articles and Packaged Anki Desk files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 559
diff changeset
   358
        for sense in translations:
570
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   359
            write_sense(buf, sense, with_examples = False)
560
dad049a0f585 Include examples into dictionary articles and Packaged Anki Desk files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 559
diff changeset
   360
        reverse_from = "".join(buf)         # without examples!!
657
c1d10b3b9ee4 Add freq marker to riddle cards too.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 650
diff changeset
   361
        builder.add_note("en->tr", identity, direct_from + freqmsg, direct_to)
c1d10b3b9ee4 Add freq marker to riddle cards too.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 650
diff changeset
   362
        builder.add_note("tr->en", identity, reverse_from, direct_from + freqmsg)
556
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   363
        if v1 and v2 and v3:
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   364
            question = u"<div class='ask'>Find irregular verb:</div>"
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   365
            riddle1 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v1</span>".format(v1[0], v1[1])
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   366
            riddle2 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v2</span>".format(v2[0], v2[1])
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   367
            riddle3 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v3</span>".format(v3[0], v3[1])
657
c1d10b3b9ee4 Add freq marker to riddle cards too.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 650
diff changeset
   368
            answer = direct_from + direct_to
c1d10b3b9ee4 Add freq marker to riddle cards too.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 650
diff changeset
   369
            builder.add_note("irregular1", identity, question + riddle1 + freqmsg, answer)
c1d10b3b9ee4 Add freq marker to riddle cards too.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 650
diff changeset
   370
            builder.add_note("irregular2", identity, question + riddle2 + freqmsg, answer)
c1d10b3b9ee4 Add freq marker to riddle cards too.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 650
diff changeset
   371
            builder.add_note("irregular3", identity, question + riddle3 + freqmsg, answer)
556
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   372
        if singular and plural:
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   373
            question = u"<div class='ask'>Find plural:</div>"
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   374
            riddle_s = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>s</span>".format(singular[0], singular[1])
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   375
            riddle_pl = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>pl</span>".format(plural[0], plural[1])
657
c1d10b3b9ee4 Add freq marker to riddle cards too.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 650
diff changeset
   376
            answer = direct_from + direct_to
c1d10b3b9ee4 Add freq marker to riddle cards too.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 650
diff changeset
   377
            builder.add_note("singular", identity, question + riddle_s + freqmsg, answer)
c1d10b3b9ee4 Add freq marker to riddle cards too.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 650
diff changeset
   378
            builder.add_note("plural", identity, question + riddle_pl + freqmsg, answer)
556
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   379
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   380
    builder.export(FONAME)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   381
finally:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   382
    builder.close()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   383
    shutil.rmtree(TMPDIR, ignore_errors=True)