py/gadict_html.py
author Oleksandr Gavenko <gavenkoa@gmail.com>
Sat, 22 Jul 2023 00:13:47 +0300
changeset 1351 ae963ee79b49
parent 1319 679972640f47
permissions -rw-r--r--
Added support for 25k freq wordlist for dictionary as an HTML page.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     2
"""HTML format writer"""
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     3
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     4
import io
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     5
import sys
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     6
import codecs
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     7
import re
1319
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
     8
from xml.sax.saxutils import escape
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     9
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    10
import gadict
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    11
import gadict_freq
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    12
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    13
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    14
FINAME = None
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    15
FONAME = None
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    16
LANGS = None
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    17
FREQ_SOURCES = []
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    18
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    19
# -lang:ru,uk
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    20
ARG_LANG_RE = re.compile("-lang:(.+)")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    21
# -freq:var:TAG=FILE or -freq:freq:TAG=FILE
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    22
ARG_FREQ_RE = re.compile("-freq:(freq|var):([^=]+)=(.+)")
1351
ae963ee79b49 Added support for 25k freq wordlist for dictionary as an HTML page.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1319
diff changeset
    23
# -grp:GLOB
ae963ee79b49 Added support for 25k freq wordlist for dictionary as an HTML page.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1319
diff changeset
    24
ARG_GRP_RE = re.compile("-grp:(.+)")
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    25
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    26
look_for_files = False
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    27
for idx in range(1, len(sys.argv)):
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    28
    arg = sys.argv[idx]
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    29
    if arg == "--":
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    30
        look_for_files = True
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    31
        continue
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    32
    if not look_for_files:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    33
        m = ARG_LANG_RE.match(arg)
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    34
        if m:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    35
            LANGS = set(m.group(1).split(","))
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    36
            for lang in LANGS:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    37
                if len(lang) != 2:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    38
                    raise Exception("Incorrect language specification: '{:s}'".format(arg))
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    39
            continue
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    40
        m = ARG_FREQ_RE.match(arg)
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    41
        if m:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    42
            mode = m.group(1)
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    43
            tag = m.group(2)
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    44
            fname = m.group(3)
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    45
            with io.open(fname, mode='r', buffering=1, encoding="utf-8") as stream:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    46
                if mode == "var":
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    47
                    parser = gadict_freq.WordformParser(stream)
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    48
                elif mode == "freq":
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    49
                    parser = gadict_freq.FreqlistParser(stream)
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    50
                else:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    51
                    raise Exception("Unsupported mode: '{:s}'".format(mode))
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    52
                wlist = parser.parse()
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    53
            FREQ_SOURCES.append((tag, set(wlist)))
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    54
            continue
1351
ae963ee79b49 Added support for 25k freq wordlist for dictionary as an HTML page.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1319
diff changeset
    55
        m = ARG_GRP_RE.match(arg)
ae963ee79b49 Added support for 25k freq wordlist for dictionary as an HTML page.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1319
diff changeset
    56
        if m:
ae963ee79b49 Added support for 25k freq wordlist for dictionary as an HTML page.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1319
diff changeset
    57
            patt = m.group(1)
ae963ee79b49 Added support for 25k freq wordlist for dictionary as an HTML page.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1319
diff changeset
    58
            parser = gadict_freq.WordformGroupParser(patt)
ae963ee79b49 Added support for 25k freq wordlist for dictionary as an HTML page.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1319
diff changeset
    59
            for (tag, wset) in parser.parse().items():
ae963ee79b49 Added support for 25k freq wordlist for dictionary as an HTML page.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1319
diff changeset
    60
                FREQ_SOURCES.append((tag, wset))
ae963ee79b49 Added support for 25k freq wordlist for dictionary as an HTML page.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1319
diff changeset
    61
            continue
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    62
        if arg.startswith("-"):
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    63
            raise Exception("Unsupported option format: '{:s}'".format(arg))
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    64
    if not FINAME:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    65
        FINAME = arg
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    66
        continue
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    67
    if not FONAME:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    68
        FONAME = arg
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    69
        continue
1223
d592572cc546 Extracted parser of gadialog files to separate module.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1032
diff changeset
    70
    raise Exception("Superfluous argument: '{:s}'".format(arg))
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    71
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    72
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    73
FIN = io.open(FINAME, mode='r', buffering=1, encoding="utf-8")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    74
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    75
PARSER = gadict.Parser()
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    76
try:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    77
    DOM = PARSER.parse(FIN)
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    78
except gadict.ParseException as ex:
1319
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
    79
    sys.stdout.write(u"{:s}{:s}\n".format(FINAME, repr(ex)))
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    80
    if __debug__:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    81
        import traceback
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    82
        traceback.print_exc()
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    83
    exit(1)
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    84
finally:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    85
    FIN.close()
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    86
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    87
PRELUDE = DOM[0]
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    88
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    89
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    90
if FONAME is None:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    91
    FOUT = sys.stdout
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    92
else:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    93
    FOUT = codecs.open(FONAME, "w", "utf-8")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    94
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    95
HTML_HEADER = """<!DOCTYPE html>
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    96
<html>
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    97
<head>
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    98
  <meta charset="utf-8">
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    99
  <title>{title}</title>
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   100
<style>
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   101
body {{ max-width: 30em; margin: 0 auto; }}
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   102
.article {{
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   103
  margin: .5em 0;
836
8fe8a90e11b3 Made article delimiter better visible.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 778
diff changeset
   104
  border-bottom: 1px dotted brown;
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   105
}}
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   106
.sense {{ margin-top: .5em; }}
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   107
.head {{ color: black; font-weight: bold; }}
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   108
.pron {{ color: green; }}
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   109
.attr {{ color: magenta; }}
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   110
.pos {{ color: green; font: weight: bold; }}
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   111
.lang {{ color: brown; font: weight: bold; }}
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   112
.ex {{ font-style: italic; }}
778
9823606d61bd Make freq list less disturbing.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 776
diff changeset
   113
.freq {{ color: red; font-family: monospace; }}
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   114
</style>
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   115
</head>
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   116
<body>
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   117
"""
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   118
HTML_FOOTER = """</body>
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   119
</html>
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   120
"""
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   121
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   122
FOUT.write(HTML_HEADER.format(title=PRELUDE.name))
1319
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
   123
FOUT.write(u"<h1>{} dictionary</h1>\n".format(escape(PRELUDE.name)))
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   124
# FOUT.write("<a href='{}'>Home page</a>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   125
# FOUT.write(" , ".join(PRELUDE.urls))
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   126
FOUT.write("<p>License: ")
1319
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
   127
FOUT.write(escape(", ".join(PRELUDE.licences)))
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   128
# FOUT.write("</p>\n<p>")
1319
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
   129
# FOUT.write(escape(PRELUDE.about))
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   130
FOUT.write("</p>\n")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   131
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   132
def link(lst):
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   133
    buf = []
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   134
    for el in lst:
1319
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
   135
        el = escape(el)
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
   136
        lnk = u"<a href='#{}'>{}</a>".format(el, el)
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   137
        buf.append(lnk)
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   138
    return "; ".join(buf)
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   139
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   140
for (headwords, translations) in DOM[1:]:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   141
    identity = headwords[0].headword
1319
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
   142
    FOUT.write(u"<div id='{}' class='article'>\n".format(escape(identity)))
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   143
    for hw in headwords:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   144
        FOUT.write("<div>")
1319
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
   145
        FOUT.write(u"<span class='head'>{}</span>".format(escape(hw.headword)))
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   146
        if hw.pron is not None:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   147
            FOUT.write(" <span class='pron'>[")
1319
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
   148
            FOUT.write(escape(hw.pron))
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   149
            FOUT.write("]</span>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   150
        if len(hw.attrs) > 0:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   151
            FOUT.write(" <span class='attr'>")
1319
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
   152
            l = [u"«" + x + u"»" for x in hw.attrs]
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   153
            l.sort()
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   154
            FOUT.write(", ".join(l))
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   155
            FOUT.write("</span>")
1032
b8e3e97178ca Added support for homonyms into HTML form.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 836
diff changeset
   156
        if hw.homo is not None and len(hw.homo) > 0:
b8e3e97178ca Added support for homonyms into HTML form.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 836
diff changeset
   157
            FOUT.write(" homo: <span class='homo'>")
b8e3e97178ca Added support for homonyms into HTML form.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 836
diff changeset
   158
            FOUT.write(link(hw.homo))
b8e3e97178ca Added support for homonyms into HTML form.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 836
diff changeset
   159
            FOUT.write("</span>")
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   160
        FOUT.write("</div>\n")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   161
    for sense in translations:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   162
        if not sense:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   163
            raise Exception("""Empty sense for article: """ + headwords.__iter__().__next__())
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   164
        FOUT.write("<div class='sense'>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   165
        if sense.pos:
1319
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
   166
            FOUT.write(u"<span class='pos'>«")
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
   167
            FOUT.write(escape(sense.pos))
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
   168
            FOUT.write(u"»</span> ")
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   169
            need_sep = False
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   170
            if sense.topic_list and len(sense.topic_list) > 0:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   171
                FOUT.write("<span class='topic'>")
1319
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
   172
                FOUT.write(escape(", ".join(sense.topic_list)))
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   173
                FOUT.write("</span>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   174
                need_sep = True
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   175
            if sense.ant_list and len(sense.ant_list) > 0:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   176
                FOUT.write(" ant: <span class='ant'>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   177
                FOUT.write(link(sense.ant_list))
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   178
                FOUT.write("</span>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   179
                need_sep = True
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   180
            if sense.syn_list and len(sense.syn_list) > 0:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   181
                FOUT.write(" syn: <span class='syn'>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   182
                FOUT.write(link(sense.syn_list))
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   183
                FOUT.write("</span>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   184
                need_sep = True
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   185
            if sense.hyper_list and len(sense.hyper_list) > 0:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   186
                FOUT.write(" hyper: <span class='hyper'>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   187
                FOUT.write(link(sense.hyper_list))
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   188
                FOUT.write("</span>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   189
                need_sep = True
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   190
            if sense.hypo_list and len(sense.hypo_list) > 0:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   191
                FOUT.write(" hypo: <span class='hypo'>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   192
                FOUT.write(link(sense.hypo_list))
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   193
                FOUT.write("</span>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   194
                need_sep = True
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   195
            if sense.rel_list and len(sense.rel_list) > 0:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   196
                FOUT.write(" see: <span class='see'>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   197
                FOUT.write(link(sense.rel_list))
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   198
                FOUT.write("</span>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   199
        for (lang, tr) in sense.tr_list or []:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   200
            if LANGS and lang not in LANGS:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   201
                continue
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   202
            FOUT.write("<div>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   203
            if not LANGS or len(LANGS) > 1:
1319
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
   204
                FOUT.write(u"<span class='lang tr'>{}</span> ".format(escape(lang)))
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
   205
            FOUT.write(u"<span class='tr'>{}</span>".format(escape(tr)))
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   206
            FOUT.write("</div>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   207
        for (lang, tr) in sense.glos_list or []:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   208
            if LANGS and lang not in LANGS:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   209
                continue
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   210
            FOUT.write("<div>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   211
            if not LANGS or len(LANGS) > 1:
1319
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
   212
                FOUT.write(u"<span class='lang glos'>{}</span> ".format(escape(lang)))
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
   213
            FOUT.write(u"<span class='glos'>{}</span>".format(escape(tr)))
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   214
            FOUT.write("</div>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   215
        for (lang, tr) in sense.ex_list or []:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   216
            if LANGS and lang not in LANGS:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   217
                continue
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   218
            FOUT.write("<div>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   219
            if not LANGS or len(LANGS) > 1:
1319
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
   220
                FOUT.write(u"<span class='lang ex'>{}</span> ".format(escape(lang)))
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
   221
            FOUT.write(u"<span class='ex'>{}</span>".format(escape(tr)))
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   222
            FOUT.write("</div>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   223
        FOUT.write("</div>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   224
    freqtags = []
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   225
    for (freqtag, freqset) in FREQ_SOURCES:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   226
        if identity in freqset:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   227
            freqtags.append(freqtag)
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   228
    if len(freqtags) > 0:
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   229
        FOUT.write("<p class='freq'>")
1319
679972640f47 Fixed another portion of incompatibilities with Python 2 (for "make html").
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
   230
        FOUT.write(escape(",".join(freqtags)))
776
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   231
        FOUT.write("</p>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   232
    FOUT.write("</div>")
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   233
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   234
FOUT.write(HTML_FOOTER)
d57c28843156 Generate HTML form of dictionary for online presents.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   235
FOUT.close()