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