|
1 # -*- coding: utf-8 -*- |
|
2 """HTML format writer""" |
|
3 |
|
4 import io |
|
5 import sys |
|
6 import codecs |
|
7 import html |
|
8 |
|
9 import gadialog |
|
10 |
|
11 FINAME = None |
|
12 FONAME = None |
|
13 |
|
14 for idx in range(1, len(sys.argv)): |
|
15 arg = sys.argv[idx] |
|
16 if not FINAME: |
|
17 FINAME = arg |
|
18 continue |
|
19 if not FONAME: |
|
20 FONAME = arg |
|
21 continue |
|
22 raise Exception("Superfluous argument: '{:s}'".format(arg)) |
|
23 |
|
24 if not FINAME: |
|
25 raise Exception("Input file name is not passed...") |
|
26 if FONAME is None: |
|
27 raise Exception("Output file name is not passed...") |
|
28 |
|
29 ################################################################ |
|
30 |
|
31 FIN = io.open(FINAME, mode='r', buffering=1, encoding="utf-8") |
|
32 |
|
33 PARSER = gadialog.Parser() |
|
34 try: |
|
35 DOM = PARSER.parse(FIN) |
|
36 finally: |
|
37 FIN.close() |
|
38 |
|
39 if FONAME is None: |
|
40 FOUT = sys.stdout |
|
41 else: |
|
42 FOUT = codecs.open(FONAME, "w", "utf-8") |
|
43 |
|
44 ################################################################ |
|
45 |
|
46 HTML_HEADER = """<!DOCTYPE html> |
|
47 <html> |
|
48 <head> |
|
49 <meta charset="utf-8"> |
|
50 <title>{title}</title> |
|
51 <style> |
|
52 body {{ max-width: 30em; margin: 0 auto; }} |
|
53 ul {{ list-style: none; }} |
|
54 li:before {{ content: "- " }} |
|
55 li.even:before {{ content: "ยป " }} |
|
56 .odd {{ color: blue; }} |
|
57 .even {{ color: green; }} |
|
58 </style> |
|
59 </head> |
|
60 <body> |
|
61 """ |
|
62 |
|
63 HTML_FOOTER = """</body> |
|
64 </html> |
|
65 """ |
|
66 |
|
67 NAME = html.escape("Dialogs") |
|
68 |
|
69 FOUT.write(HTML_HEADER.format(title=NAME)) |
|
70 FOUT.write("<h1>{} dictionary</h1>\n".format(NAME)) |
|
71 |
|
72 for num, lines in DOM.items(): |
|
73 FOUT.write("<hr>") |
|
74 FOUT.write("<ul>\n") |
|
75 odd = True |
|
76 for line in lines: |
|
77 FOUT.write('<li class="{:s}">{:s}</li>\n'.format("odd" if odd else "even", html.escape(line))) |
|
78 odd = not odd |
|
79 FOUT.write("</ul>") |
|
80 |
|
81 FOUT.write(HTML_FOOTER) |
|
82 FOUT.close() |
|
83 |