py/gadialog_html.py
changeset 1224 23dc533e3dd3
child 1319 679972640f47
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/py/gadialog_html.py	Fri Aug 21 12:18:08 2020 +0300
@@ -0,0 +1,83 @@
+# -*- coding: utf-8 -*-
+"""HTML format writer"""
+
+import io
+import sys
+import codecs
+import html
+
+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 = """<!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 = html.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('<li class="{:s}">{:s}</li>\n'.format("odd" if odd else "even", html.escape(line)))
+        odd = not odd
+    FOUT.write("</ul>")
+
+FOUT.write(HTML_FOOTER)
+FOUT.close()
+