-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhelper.py
More file actions
executable file
·258 lines (197 loc) · 8.16 KB
/
helper.py
File metadata and controls
executable file
·258 lines (197 loc) · 8.16 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
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
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python3
import argparse
import json
import urllib.request
import os
from typing import List, Set, Dict, Tuple
parser = argparse.ArgumentParser(description="typst-fontawesome helper script")
parser.add_argument(
"-v",
"--version",
help="FontAwesome version. (Example: -v 6.7.2,7.0.0)",
required=True,
)
parser.add_argument(
"-o", "--output", help="Output dir (default: current dir .)", default="."
)
parser.add_argument(
"-g",
"--generate",
help="Generate typst files (can be `lib`, `doc`)",
default=["lib", "doc"],
)
API_URL = "https://api.fontawesome.com"
QUERY_TEMPLATE = """
query {{
release (version: "{version}") {{
icons {{
id,
unicode,
aliases {{ names }}
}}
}}
}}
"""
def fetch_icons(version: str) -> Set[Tuple[str, str]]:
"""
Fetch icons from FontAwesome API for a specific version.
Args:
version: The version of FontAwesome to fetch icons for.
Returns:
Set: A set of tuples containing icon IDs and their corresponding unicode characters.
"""
req = urllib.request.Request(
API_URL,
headers={"Content-Type": "application/json", "User-Agent": "Mozilla/5.0"},
data=json.dumps({"query": QUERY_TEMPLATE.format(version=version)}).encode(
"utf-8"
),
)
data = {}
with urllib.request.urlopen(req) as response:
data = json.load(response)
if "errors" in data:
print(f"GraphQL errors: {data['errors']}")
raise Exception(f"GraphQL query failed: {data['errors']}")
icon_unicode_set = set()
for icon in data["data"]["release"]["icons"]:
icon_unicode_set.add((icon["id"], icon["unicode"]))
if icon["aliases"]:
for alias in icon["aliases"]["names"]:
icon_unicode_set.add((alias, icon["unicode"]))
return icon_unicode_set
def map_icons(versions: List[str]) -> Dict[str, List[Tuple[str, str]]]:
"""
Map icons from multiple FontAwesome versions to their unicode characters.
Args:
versions: A list of FontAwesome versions to map icons from.
Returns:
Dict: A dictionary where keys are version strings and values are lists of tuples
containing icon IDs and their corresponding unicode characters.
"""
origin_icon_sets = []
for version in versions:
icon_set = fetch_icons(version)
major_version = int(version.split(".")[0])
origin_icon_sets.append((major_version, icon_set))
icon_unicodes = {}
# Collect all icon unicodes across versions
# If an icon has the same unicode in different versions, it will be marked as common
# If an icon has different unicodes in different versions, it will be marked as conflict
for version, icon_set in origin_icon_sets:
for icon_id, unicode in icon_set:
if icon_id not in icon_unicodes:
icon_unicodes[icon_id] = set()
icon_unicodes[icon_id].add((version, unicode))
for icon_id, unicodes in icon_unicodes.items():
unicode_set = set(unicode for _, unicode in unicodes)
if len(unicode_set) == 1:
icon_unicodes[icon_id] = set()
icon_unicodes[icon_id].add(("common", unicode_set.pop()))
icon_map = {}
common = [
(icon_id, unicodes.pop()[1])
for icon_id, unicodes in icon_unicodes.items()
if len(unicodes) == 1
]
icon_map["common"] = common
conflict = [
(icon_id, unicodes)
for icon_id, unicodes in icon_unicodes.items()
if len(unicodes) > 1
]
for icon_id, unicodes in conflict:
for version, unicode in unicodes:
if version not in icon_map:
icon_map[version] = []
icon_map[version].append((icon_id, unicode))
for _, map in icon_map.items():
map.sort(key=lambda x: (x[1], x[0])) # Sort by unicode first, then by icon_id
return icon_map
def generate_lib(icon_maps: Dict[str, List[Tuple[str, str]]], output: str):
"""
Generate typst library files for FontAwesome icons.
Args:
icon_maps: A dictionary containing icon maps for different FontAwesome versions.
output: The output directory where the typst files will be generated.
"""
lib_map_file = os.path.join(output, "lib-gen-map.typ")
lib_func_file = os.path.join(output, "lib-gen-func.typ")
with open(lib_map_file, "w") as f:
f.write("// Generated icon maps of Font Awesome\n\n")
icon_func_str = ""
f.write("// Common icons\n")
f.write("#let fa-icon-map-common = (\n")
for icon in icon_maps["common"]:
icon_id, unicode_char = icon
f.write(f' "{icon_id}": "\\u{{{unicode_char}}}",\n')
icon_func_str += (
f'#let fa-{icon_id} = fa-icon.with("\\u{{{unicode_char}}}")\n'
)
f.write(")\n\n")
del icon_maps["common"]
latest_version = max(icon_maps.keys()) if icon_maps else None
for version, icons in sorted(icon_maps.items()):
f.write(f"// Version: {version}\n")
f.write(f"#let fa-icon-map-{version} = (\n")
for icon_id, unicode_char in icons:
f.write(f' "{icon_id}": "\\u{{{unicode_char}}}",\n')
icon_func_str += f'#let fa-{icon_id}-{version} = fa-icon.with("\\u{{{unicode_char}}}")\n'
if version == latest_version:
icon_func_str += (
f'#let fa-{icon_id} = fa-icon.with("\\u{{{unicode_char}}}")\n'
)
f.write(")\n\n")
f.write("#let fa-icon-map-version = (\n")
for version in sorted(icon_maps.keys()):
f.write(f' "{version}": fa-icon-map-{version},\n')
f.write(")\n")
with open(lib_func_file, "w") as f:
f.write('#import "lib-impl.typ": fa-icon\n\n')
f.write("// Generated icon functions of Font Awesome\n\n")
f.write(icon_func_str)
def generate_gallery(icon_maps: Dict[str, List[Tuple[str, str]]], output: str):
"""
Generate a typst gallery file for FontAwesome icons.
Args:
icon_maps: A dictionary containing icon maps for different FontAwesome versions.
output: The output directory where the typst gallery file will be generated.
"""
gallery_file = os.path.join(output, "gallery.typ")
with open(gallery_file, "w") as f:
f.write('#import "lib.typ": *\n\n')
f.write("= Gallery\n\n")
f.write(
"Here are all the icons in the library. The first column is the icon function you can use, and the second and third columns are the icon in regular and solid versions. The fourth column is what you get when you use `fa-icon` with the icon name.\n\n"
)
f.write(
"Since I only use the Free set, some icons may render as a square in the following table. Please turn to the official website for the correct glyph.\n\n"
)
f.write(
"#table(\n columns: (3fr, 1fr, 1fr, 2fr),\n stroke: none,\n table.header([typst code], [default], [solid], [`fa-icon` with text]),\n"
)
for icon in icon_maps["common"]:
icon_id, _unicode_char = icon
f.write(
f' ```typst #fa-{icon_id}()```, fa-{icon_id}(), fa-{icon_id}(solid: true), fa-icon("{icon_id}"),\n'
)
del icon_maps["common"]
for version, icons in sorted(icon_maps.items()):
for icon_id, _unicode_char in icons:
f.write(
f' ```typst #fa-{icon_id}-{version}()```, fa-{icon_id}-{version}(), fa-{icon_id}-{version}(solid: true), fa-icon("{icon_id}-{version}"),\n'
)
f.write(")")
def main():
args = parser.parse_args()
versions = args.version.split(",")
print(f"Generating typst-fontawesome for versions: {versions}")
icon_maps = map_icons(versions)
# with open(os.path.join(args.output, "icon-maps.json"), "w") as f:
# json.dump(icon_maps, f, indent=2)
if "lib" in args.generate:
generate_lib(icon_maps.copy(), args.output)
if "doc" in args.generate:
generate_gallery(icon_maps.copy(), args.output)
if __name__ == "__main__":
main()