py/gadialog_html.py
author Oleksandr Gavenko <gavenkoa@gmail.com>
Wed, 01 Sep 2021 10:38:30 +0300
changeset 1319 679972640f47
parent 1224 23dc533e3dd3
permissions -rw-r--r--
Fixed another portion of incompatibilities with Python 2 (for "make html").

# -*- coding: utf-8 -*-
"""HTML format writer"""

import io
import sys
import codecs
from xml.sax.saxutils import escape

import gadialog

FINAME = None
FONAME = None

for idx in range(1, len(sys.argv)):
    arg = sys.argv[idx]
    if not FINAME:
        FINAME = arg
        continue
    if not FONAME:
        FONAME = arg
        continue
    raise Exception("Superfluous argument: '{:s}'".format(arg))

if not FINAME:
    raise Exception("Input file name is not passed...")
if FONAME is None:
    raise Exception("Output file name is not passed...")

################################################################

FIN = io.open(FINAME, mode='r', buffering=1, encoding="utf-8")

PARSER = gadialog.Parser()
try:
    DOM = PARSER.parse(FIN)
finally:
    FIN.close()

if FONAME is None:
    FOUT = sys.stdout
else:
    FOUT = codecs.open(FONAME, "w", "utf-8")

################################################################

HTML_HEADER = u"""<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>{title}</title>
<style>
body {{ max-width: 30em; margin: 0 auto; }}
ul {{ list-style: none; }}
li:before {{ content: "- " }}
li.even:before {{ content: "ยป " }}
.odd {{ color: blue; }}
.even {{ color: green; }}
</style>
</head>
<body>
"""

HTML_FOOTER = """</body>
</html>
"""

NAME = escape("Dialogs")

FOUT.write(HTML_HEADER.format(title=NAME))
FOUT.write("<h1>{} dictionary</h1>\n".format(NAME))

for num, lines in DOM.items():
    FOUT.write("<hr>")
    FOUT.write("<ul>\n")
    odd = True
    for line in lines:
        FOUT.write(u'<li class="{:s}">{:s}</li>\n'.format("odd" if odd else "even", escape(line)))
        odd = not odd
    FOUT.write("</ul>")

FOUT.write(HTML_FOOTER)
FOUT.close()