# HG changeset patch # User Oleksandr Gavenko # Date 1598001488 -10800 # Node ID 23dc533e3dd3b7e24cfffc2b282737ba258f3fbc # Parent d592572cc54628ee68d2ca00750419fd9e558b4a Added HTML formatter for gadialog files. diff -r d592572cc546 -r 23dc533e3dd3 Makefile --- a/Makefile Fri Aug 21 12:17:35 2020 +0300 +++ b/Makefile Fri Aug 21 12:18:08 2020 +0300 @@ -156,7 +156,9 @@ $(patsubst %.gaphrase,dist/anki/%.apkg,$(GAPHRASE_FILES)) \ $(patsubst %.gadialog,dist/anki/%.apkg,$(GADIALOG_FILES)) -DICT_HTML_FILES := $(patsubst %.gadict,dist/html/%.html,$(GADICT_FILES)) +DICT_HTML_FILES := \ + $(patsubst %.gadict,dist/html/%.html,$(GADICT_FILES)) \ + dist/html/gadialog.html RST_TMPL_FILE = dist/misc/rst.tmpl RST_CSS_FILE = www/tmpl/rst.css @@ -716,6 +718,9 @@ dist/html/%.html: %.gadict py/gadict.py py/gadict_html.py $(FREQLIST_DEP) $(MAKEFILE_LIST) | dist/html/ python3 -B py/gadict_html.py $(FREQLIST_OPT) $< $@ +dist/html/gadialog.html: gadialog.gadialog py/gadialog.py py/gadialog_html.py | dist/html/ + python3 -B py/gadialog_html.py $< $@ + dist/html/: mkdir -p $@ diff -r d592572cc546 -r 23dc533e3dd3 py/gadialog_html.py --- /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 = """ + + + + {title} + + + +""" + +HTML_FOOTER = """ + +""" + +NAME = html.escape("Dialogs") + +FOUT.write(HTML_HEADER.format(title=NAME)) +FOUT.write("

{} dictionary

\n".format(NAME)) + +for num, lines in DOM.items(): + FOUT.write("
") + FOUT.write("") + +FOUT.write(HTML_FOOTER) +FOUT.close() +