-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.py
139 lines (107 loc) · 3.85 KB
/
helper.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
import argparse
import json
import zipfile
import urllib.request
import shutil
import os
import glob
import textwrap
parser = argparse.ArgumentParser(description="typst-fontawesome helper script")
parser.add_argument("-v", "--version", help="FontAwesome version", 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,
familyStylesByLicense {{
free {{ style }},
pro {{ style }}
}},
aliases {{ names }}
}}
}}
}}
"""
def generate_lib(version, output):
print(f"Generating typst lib for FontAwesome {version}")
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)
LIB_PREAMBLE_TEMPLATE = """\
#import "lib-impl.typ": fa-icon
// Generated icon list of Font Aewsome {version}
#let fa-icon-map = (
"""
lib_preamble = textwrap.dedent(LIB_PREAMBLE_TEMPLATE).format(version=version)
lib_file = os.path.join(output, "lib-gen.typ")
icon_func_str = ""
with open(lib_file, "w") as f:
f.write(lib_preamble)
for icon in data["data"]["release"]["icons"]:
f.write(f' "{icon["id"]}": "\\u{{{icon["unicode"]}}}",\n')
icon_func_str += (
f'#let fa-{icon["id"]} = fa-icon.with("\\u{{{icon["unicode"]}}}")\n'
)
if icon["aliases"]:
for alias in icon["aliases"]["names"]:
f.write(f' "{alias}": "\\u{{{icon["unicode"]}}}",\n')
icon_func_str += (
f'#let fa-{alias} = fa-icon.with("\\u{{{icon["unicode"]}}}")\n'
)
f.write(")\n")
f.write(icon_func_str)
def generate_gallery(version, output):
print(f"Generating typst gallery for FontAwesome {version}")
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)
gallery_file = os.path.join(output, "gallery.typ")
with open(gallery_file, "w") as f:
f.write('#import "lib.typ": *\n')
f.write(
# "#grid(columns: (20em, 3em, 3em, 3em), [typst code], [default], [solid], [`fa-icon` with text])\n"
"#table(columns: (3fr, 1fr, 1fr, 2fr), stroke: none, table.header([typst code], [default], [solid], [`fa-icon` with text]),\n"
)
for icon in data["data"]["release"]["icons"]:
f.write(
f'```typst #fa-{icon["id"]}()```, fa-{icon["id"]}(), fa-{icon["id"]}(solid: true), fa-icon("{icon["id"]}"),\n'
)
if icon["aliases"]:
for alias in icon["aliases"]["names"]:
f.write(
f'```typst #fa-{alias}()```, fa-{alias}(), fa-{alias}(solid: true), fa-icon("{alias}"),\n'
)
f.write(")")
def main():
args = parser.parse_args()
if "lib" in args.generate:
generate_lib(args.version, args.output)
if "doc" in args.generate:
generate_gallery(args.version, args.output)
if __name__ == "__main__":
main()