-
Notifications
You must be signed in to change notification settings - Fork 1
/
articles.py
247 lines (218 loc) · 6.78 KB
/
articles.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# -*- coding: UTF-8 -*-
from django.template import loader, Context
from os import environ as env
env["DJANGO_SETTINGS_MODULE"] = "settings"
from itertools import chain
from utils import makedirs
from pprint import pprint
import codecs
from os.path import join
from markup import markup
from re import compile as re, escape
import regions, names, labels, links, locations, books
from names import language_index
REGIONS = regions.regions2()
NAMES = names.names()
LANGUAGES = names.languages
LANGUAGE_NAMES = names.language_names()
LINKS = links.links()
LOCATIONS = locations.locations()
BOOKS_LIST = books.books_iter()
BOOK_ABBRS = list(zip(*BOOKS_LIST)[0])
BOOKS = dict(BOOKS_LIST)
DIR = join('build', 'articles')
parts_re = re(r'[^\w\d\.]')
def parse_citation(citation):
if citation.startswith("http://"):
return {
"link": '<a href="%s">†</a>' % (
citation,
)
}
parts = parts_re.split(citation)
part = parts.pop(0)
if part in BOOKS:
trailer = citation[len(part):]
parts = trailer.split("#")
if len(parts) == 2:
trailer = '%s(<abbr title="occurrences">%s</abbr>)' % tuple(parts)
book = BOOKS[part]
link = book["HTML"]
book_html = part
if book["Title"]:
book_html = '<abbr title="%s">%s</abbr>' % (
book["Title"],
book_html
)
book_html = '>%s<' % book_html
part_html = '>%s<' % part.replace("&", "&")
return {
"link": link
.replace("<a ", '<a target="_blank" ')
.replace(part_html, book_html) + trailer,
"book": part
}
else:
return {
"label": citation,
}
def citation_index(citation):
try:
return BOOK_ABBRS.index(citation["book"])
except (ValueError, KeyError):
return len(BOOK_ABBRS)
def build_region(canonical, region):
location = LOCATIONS[canonical]
source_names = list(NAMES[canonical])
language_names = LANGUAGE_NAMES[canonical]
links = LINKS.get(canonical, [])
names = []
def add_name(name):
if name in source_names:
names.append({
"name": name["Name"],
"credit": ", ".join(name["Constructed"].split(" / ")),
"language": name["Language"],
"languages": [
language
for language in ("Sindarin", "English", "German")
if name[language] == "*" and
language != name["Language"]
],
"meaning": name["English Meaning"],
"citations": dict(
(klass, sorted([
parse_citation(citation)
for citation in citations.split(", ")
if citation
], key=citation_index)) for klass, citations in (
('manual', name["Manual Citations"]),
('computed_sensitive', name["Computed Case-sensitive Citations"]),
('computed_insensitive', name["Computed Case-insensitive Citations"]),
)
),
"construction": [
definition_html(construct)
for construct in name["Construction"].split("; ")
if construct
],
"notes": markup(name["Notes"])
})
source_names.pop(source_names.index(name))
for language in LANGUAGES:
if language in language_names:
add_name(language_names[language])
source_names = sorted(
source_names,
key = lambda name: language_index(
name["Language"]
)
)
while source_names:
add_name(source_names[0])
article_href = "%s.html" % canonical
map_href = "http://3rin.gs/#%s" % ",".join("%s" % val for val in [
region.height,
region.width,
region.y,
region.x,
'l',
canonical,
])
try:
name = language_names["English"]["Name"]
except KeyError, key:
print canonical
raise
location = {
"name": name,
"names": names,
"links": links,
"map_href": map_href,
"article_href": article_href
}
context = Context({
"location": location,
"disqus": {
"id": canonical
}
})
for extension in ('frag.html', 'html'):
template = loader.get_template('article.' + extension)
content = template.render(context)
file = codecs.open(
join(DIR, "%s.%s" % (canonical, extension)),
"w",
"utf-8"
)
file.write(content)
file.close()
return location
def build_index(locations):
template = loader.get_template("articles.html")
content = template.render(Context({
"locations": locations,
"disqus": {
"id": "articles"
}
}))
file = codecs.open(join(DIR, "index.html"), "w", "utf-8")
file.write(content)
file.close()
def build():
makedirs(DIR)
locations = []
for canonical, region in REGIONS.items():
location = build_region(canonical, region)
locations.append(location)
build_index(sorted(
locations,
key = lambda location: location['name']
))
def definition_html(definition):
return ", ".join(term_html(term) for term in definition.split(", "))
def term_html(term):
return " / ".join(
" – ".join(
" – ".join(
" → ".join(
" ← ".join(
"⁓".join(
words_html(words)
for words in tilde.split("~")
) for tilde in rarr.split(" < ")
) for rarr in ndash.split(" > ")
) for ndash in mdash.split(" - ")
) for mdash in solidus.split(" -- ")
) for solidus in term.split(" / ")
)
def words_html(words):
return " ".join(
word_html(word)
for word in
words.split(" ")
)
def word_html(word):
if word.startswith("http://"):
return '<a href="%s" target="_blank">†</a>' % word
if word in LANGUAGE_ABBRS:
return language_abbr_html(word)
return word
LANGUAGE_ABBRS = {
"E": "English",
"S": "Sindarin",
"Q": "Quenya",
"Ñ": "Ñoldorin",
"W": "Westron",
"R": "Rohirric",
}
LANGUAGE_ABBRS_NORMAL = {
"N": "Ñ",
}
def language_abbr_html(language):
language = LANGUAGE_ABBRS_NORMAL.get(language, language)
return """<em><abbr title="%s">%s</abbr></em>""" % (
LANGUAGE_ABBRS.get(language, language),
language
)
build()