-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgen-chooser.py
183 lines (149 loc) · 6.55 KB
/
gen-chooser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
""" Generate a static HTML expert-system site based on hxl-knowledge-base.json
Requires Python3.
Usage:
python generate-tag-assist.py [lang] > docs/index.html
Started 2019-04-16 by David Megginson
"""
import json, re, sys
DEFAULT_LANG = "en"
""" Default language to generate """
# Requires Python3 or higher
if sys.version_info < (3, ):
raise Exception("Requires Python3 or higher")
# TODO: specify file on command line
with open("hxl-knowledge-base.json", "r") as input:
base = json.load(input)
with open("translations.json", "r") as input:
translations = json.load(input)
if len(sys.argv) == 2:
lang = sys.argv[1].lower()
else:
lang = DEFAULT_LANG
print("Generating in {}".format(lang), file=sys.stderr)
def esc(s):
""" Escape HTML special characters """
return s.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """).replace("'", "'")
def t(s):
""" Return a translation if available """
if s in translations and lang in translations[s]:
return translations[s][lang]
else:
return s
def make_tagspec(hashtag, attributes):
attributes = [attribute for attribute in attributes if attribute]
return " +".join(["#" + hashtag] + attributes)
def make_html_id(id, hashtag, attributes):
if hashtag is None:
return "_" + id
elif not attributes:
return hashtag
else:
return "_".join([hashtag] + attributes)
def text (info):
if lang in info:
return esc(info[lang])
elif DEFAULT_LANG in info:
return esc(info[DEFAULT_LANG])
else:
raise Exception("No text in {}".format(DEFAULT_LANG))
def display_question(id, hashtag=None, attributes=[], previous_id=None):
question = base[id]
html_id = make_html_id(id, hashtag, attributes)
# rendering
print(" <section class=\"question\" id=\"{}\">".format(esc(html_id)))
print(" <h2>{}</h2>".format(text(question["question"])))
# progress so far
if hashtag is not None:
print(" <p class=\"progress\"><span class=\"tagspec\">{}</span></p>".format(esc(make_tagspec(hashtag, attributes))))
if "pre-text" in question:
print(" <p class=\"pre-text\">{}</p>".format(text(question["pre-text"])))
# Display next options
for option in question["options"]:
display_option(option, hashtag, attributes)
if "post-text" in question:
print(" <p class=\"post-text\">{}</p>".format(text(question["post-text"])))
# navigation
print(" <div class=\"nav\">")
if previous_id is not None:
print(" <a href=\"#{}\">◀️ {}</a>".format(esc(previous_id), esc(t("Back"))))
else:
print(" <a> </a>")
if id == "top":
print(" <a>{}</a>".format(esc(t("HXL Hashtag Chooser"))))
else:
print(" <a href=\"#_top\">{}</a>".format(esc(t("Restart"))))
for link_lang in ("en", "fr", "es",):
if lang != link_lang:
print(" <a href=\"../{lang}/index.html\">{lang}</a>".format(lang=link_lang))
print(" <a href=\"http://hxlstandard.org/standard/1-1final/dictionary\" target=\"_blank\">📖 {}</a>".format(esc(t("HXL Dictionary"))))
print(" </div>")
# end of question
print(" </section>")
# recursion
if "options" in question:
for option in question["options"]:
opt_hashtag = hashtag
opt_attributes = list(attributes)
if "hashtag" in option:
opt_hashtag = option["hashtag"]
if "attribute" in option:
opt_attributes.append(option["attribute"])
elif opt_hashtag is not None:
opt_attributes.append(option.get("attribute", ""))
if "dest" in option:
display_question(option["dest"], opt_hashtag, opt_attributes, html_id)
else:
display_result(option, opt_hashtag, opt_attributes, html_id)
def display_option(option, hashtag, attributes):
if "include" in option and hashtag not in option["include"]:
return
elif "exclude" in option and hashtag in option["exclude"]:
return
opt_hashtag = hashtag
opt_attributes = list(attributes)
if "hashtag" in option:
opt_hashtag = option["hashtag"]
if "attribute" in option:
opt_attributes.append(option["attribute"])
elif opt_hashtag is not None:
opt_attributes.append(option.get("attribute", ""))
link = make_html_id(option.get("dest"), opt_hashtag, opt_attributes)
if "dest" not in option:
link = link + "_000"
print(" <p class=\"option\"><a href=\"#{}\">{}</a></p>".format(
esc(link),
text(option["text"])
))
def display_result(option, hashtag, attributes, previous_id):
print(" <section class=\"result\" id=\"{}_000\">".format(esc(make_html_id(id, hashtag, attributes))))
print(" <h2>{}</h2>".format(esc(t("Finished!"))))
print(" <div class=\"tagspec-container\">")
print(" <p class=\"tagspec final-tagspec\">{}</p>".format(esc(make_tagspec(hashtag, attributes))))
print(" </div>")
for attribute in attributes:
if re.match(r"[A-Z]", attribute):
print(" <p>{} <b><code>+{}</code></b>.</p>".format(esc(t("Don't forget to substitute your own attribute for")), esc(attribute)))
if "note" in option:
print(" <p class=\"note\">{}</p>".format(text(option["note"])))
print(" <p class=\"post-text\">{}</p>".format(esc(t("You are free to add more attributes, or to make up your own, if you need to make further distinctions."))))
print(" <div class=\"nav\">")
print(" <a href=\"#{}\">◀️ {}</a>".format(esc(previous_id), esc(t("Back"))))
print(" <a href=\"#_top\">{}</a>".format(esc(t("Restart"))))
print(" <a href=\"http://hxlstandard.org/standard/1-1final/dictionary\" target=\"_blank\">📖 {}</a>".format(esc(t("HXL dictionary"))))
print(" </div>")
print(" </section>")
print("<!DOCTYPE html>")
print("<html>")
print(" <head>")
print(" <title>HXL hashtag chooser</title>")
print(" <link rel=\"stylesheet\" href=\"../style.css\"/>")
print(" <link rel=\"icon\" href=\"../icon.png\"/>")
print(" <link rel=\"manifest\" href=\"../manifest.webmanifest\"/>")
print(" <meta charset=\"utf-8\"/>")
print(" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>")
print(" </head>")
print(" <body>")
display_question("top")
print(" </body>")
print(" <script src=\"../script.js\"></script>")
print("</html>")