Debian Codegraph @

Lib/notobuilder/ghpages/__main__.py

raw symbols SCIP
1 from collections import defaultdict
2 import os
3 import re
4 from glob import glob
5 from pathlib import Path
6 from urllib.parse import quote
7 from importlib.resources import files
8
9 import chevron
10 from fontTools.ttLib import TTFont
11 from gftools.utils import font_sample_text
12 from sh import git
13
14
15 DIFFBROWSERS_PROOF_RE = r"^(.*)-diffbrowsers_(.*).html$"
16
17 class FileTreeMaker(object):
18 def _recurse(self, parent_path, file_list, prefix, output_buf, level):
19 if len(file_list) == 0:
20 return
21 else:
22 file_list.sort(key=lambda f: os.path.isfile(os.path.join(parent_path, f)))
23 for idx, sub_path in enumerate(file_list):
24 full_path = os.path.join(parent_path, sub_path)
25
26 if os.path.isdir(full_path):
27 output_buf.append(f'<li class="li-{level}">{sub_path}<ul>')
28 self._recurse(
29 full_path,
30 os.listdir(full_path),
31 "",
32 output_buf,
33 level + 1,
34 )
35 elif os.path.isfile(full_path):
36 output_buf.append(f'<li><a href="{full_path}"> {sub_path}</a></li>')
37 output_buf.append("%s </ul>" % (prefix))
38
39 def make(self, root):
40 self.root = root
41 buf = ["<ul>"]
42 path_parts = self.root.rsplit(os.path.sep, 1)
43 self._recurse(self.root, os.listdir(self.root), "", buf, 0)
44
45 output_str = "\n".join(buf)
46 return output_str
47
48
49 def main():
50 commit = git("rev-parse", "--short", "HEAD")
51 github_repo = os.environ.get("GITHUB_REPOSITORY", "")
52 reponame = github_repo.split("/")[1]
53 repo_url = (
54 os.environ.get("GITHUB_SERVER_URL", "https://github.com/") + "/" + github_repo
55 )
56
57 raw_url = "https://notofonts.github.io/" + reponame + "/badges"
58 shields_url = "https://img.shields.io/endpoint?url=" + quote(raw_url, safe="")
59
60 families = []
61 for family in glob("fonts/*"):
62 basename = os.path.basename(family)
63 fname = re.sub(r"([a-z])([A-Z])", r"\1 \2", basename)
64 fonttree = FileTreeMaker().make(family)
65 fontspector = []
66 for result in glob(f"out/fontspector/*{basename}*html"):
67 result = result[4:]
68 if "unhinted" in result:
69 fontspector.append({"name": "Noto fonts, unhinted", "path": result})
70 elif "hinted" in result:
71 fontspector.append({"name": "Noto fonts, hinted", "path": result})
72 elif "googlefonts" in result:
73 fontspector.append({"name": "Google Fonts", "path": result})
74 fontspector = list(reversed(sorted(fontspector, key=lambda l: l["name"])))
75 diffenator = []
76 for result in glob(f"out/qa/{basename}/Diffenator/*/report.html"):
77 diffenator.append(
78 {
79 "name": "Diffenator report, " + Path(result).parent.stem,
80 "path": result[4:],
81 }
82 )
83 proofs = defaultdict(list)
84 for result in glob(f"out/proof/{basename}/*html"):
85 if m := re.match(DIFFBROWSERS_PROOF_RE, os.path.basename(result)):
86 style, prooftype = m[1], m[2]
87 proofs[prooftype].append(
88 {
89 "name": style,
90 "path": result[4:],
91 }
92 )
93 # Sort by style
94 for k, v in proofs.items():
95 proofs[k] = list(sorted(v, key=lambda l: l["name"]))
96 families.append(
97 {
98 "name": fname,
99 "fonttree": fonttree,
100 "fontspector": fontspector,
101 "diffenator": diffenator,
102 "proofs": proofs,
103 }
104 )
105
106
107 unhinted = glob("fonts/*/unhinted/ttf/*.ttf")
108 grab_a_font = None
109 if unhinted:
110 grab_a_font = unhinted[0]
111
112 sample_text = ""
113 if grab_a_font:
114 sample_text = font_sample_text(TTFont(grab_a_font))
115 sample_text = " ".join(sample_text)
116 sample_text += " " + sample_text
117
118
119 with open("README.md") as readme:
120 lines = readme.read()
121 m = re.match("^# (.*)", lines)
122 if not m:
123 project = "Unknown Project"
124 else:
125 project = m[1]
126
127 print(families)
128
129 template = files('notobuilder.ghpages').joinpath('template.html').read_text()
130
131 with open("out/index.html", "w") as fw:
132 fw.write(
133 chevron.render(
134 template,
135 {
136 "families": families,
137 "repo_url": repo_url,
138 "commit": commit,
139 "project": project,
140 "shields_url": shields_url,
141 "a_font": grab_a_font,
142 "sample_text": sample_text,
143 },
144 )
145 )
146
147 if __name__ == "__main__":
148 main()