-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhex.py
More file actions
150 lines (115 loc) · 3.73 KB
/
hex.py
File metadata and controls
150 lines (115 loc) · 3.73 KB
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
import argparse
import csv
import os
import re
import string
import sys
import jinja2
import hexmap as hm
parser = argparse.ArgumentParser()
parser.add_argument(
"-f", "--format", dest="fmt", default="html", help="Format of output: html or text"
)
parser.add_argument("CSV", help="The CSV file with the hex descriptions.")
parser.add_argument("Title", help="The title of this hex map.")
args = parser.parse_args(sys.argv[1:])
if args.fmt == "html":
template_name = os.path.basename(args.CSV)[:-3] + "html"
elif args.fmt == "text":
template_name = "text.txt"
# Read CSV dump of Google Docs hex map descriptions and create hexmap of
# the data.
with open(args.CSV) as csvfile:
hexmap = hm.HexMap(csv.reader(csvfile))
if args.fmt == "stats":
print("Most referenced Hexes:")
for l, count in hexmap.reference_histogram[-10:]:
print("\t%s mentioned %d times" % (l, count))
print("Themes found in hexes:")
for l, count in hexmap.themes_histogram:
print("\t%s mentioned %d times" % (l, count))
exit(0)
# Output Data to Template
def settlementlink(m):
# Look up settlement in settlement map and create link if the settlement
# exists.
settlement = m.group(1).upper().strip()
if settlement in hexmap.settlements:
return "<a href='#{hex}' class='city-link'>{settlement}</a>".format(
settlement=settlement, hex=hexmap.settlements[settlement]
)
return settlement
def hex2link(text):
# Add links were appropriate
if args.fmt == "html":
text = re.sub(
r"\[\[(\d\d\d\d)\]\]", r"<a class='hex-link' href='#\1'>\1</a>", text
)
text = re.sub(r"\[\[(.*?)\]\]", settlementlink, text)
else:
# Convert link short-hand to plain text.
text = re.sub(r"\[\[(.*?)\]\]", r"\1", text)
return text
def getreferences(h):
# return references for this hex.
if h in hexmap.references:
return ", ".join(
"<a class='hex-link' href='#%s'>%s</a>" % (l, l)
for l in sorted(hexmap.references[h])
)
return ""
def coordinates(location):
return int(location[:2]), int(location[2:])
last_hex = sorted(hexmap.hexes.keys())[-1]
max_x, max_y = coordinates(last_hex)
def nw(location):
x, y = coordinates(location)
if x % 2 == 1:
y -= 1
x -= 1
return "" if x <= 0 or y <= 0 else "%02d%02d" % (x, y)
def ne(location):
x, y = coordinates(location)
if x % 2 == 1:
y -= 1
x += 1
return "" if x >= max_x or y <= 0 else "%02d%02d" % (x, y)
def se(location):
x, y = coordinates(location)
if x % 2 == 0:
y += 1
x += 1
return "" if x >= max_x or y >= max_y else "%02d%02d" % (x, y)
def sw(location):
x, y = coordinates(location)
if x % 2 == 1:
y += 1
x -= 1
return "" if x <= 0 or y >= max_y else "%02d%02d" % (x, y)
def process(description):
# Fix some poor grammar / punctuation
description = description.strip()
if description[-1] not in string.punctuation:
description = description + "."
if description[0].islower():
description = description.capitalize()
description = hex2link(description)
return description
env = jinja2.Environment(loader=jinja2.FileSystemLoader("templates"))
env.filters["process"] = process
env.filters["references"] = getreferences
env.filters["nw"] = nw
env.filters["ne"] = ne
env.filters["sw"] = sw
env.filters["se"] = se
template = env.get_template(template_name)
context = {
"hexes": sorted(hexmap.hexes.items()),
"authors": ", ".join(
"%s (%s)" % (author, count)
for author, count in hexmap.author_histogram.most_common()
),
"references": hexmap.references,
"title": args.Title,
}
print(template.render(**context))