|
| 1 | +""" |
| 2 | +All the drafts are built by the build-specs workflow itself. |
| 3 | +This handles the rest of the work: |
| 4 | +
|
| 5 | +* creates an index page listing all specs |
| 6 | +* creates symlinks for unlevelled urls, linking to the appropriate levelled folder |
| 7 | +* builds timestamps.json, which provides metadata about the specs |
| 8 | +""" |
| 9 | + |
| 10 | +import json |
| 11 | +import os |
| 12 | +import os.path |
| 13 | +import re |
| 14 | +import subprocess |
| 15 | +from collections import defaultdict |
| 16 | +from datetime import datetime, timezone |
| 17 | + |
| 18 | +import bikeshed |
| 19 | +from html.parser import HTMLParser |
| 20 | + |
| 21 | + |
| 22 | +def title_from_html(file): |
| 23 | + class HTMLTitleParser(HTMLParser): |
| 24 | + def __init__(self): |
| 25 | + super().__init__() |
| 26 | + self.in_title = False |
| 27 | + self.title = "" |
| 28 | + self.done = False |
| 29 | + |
| 30 | + def handle_starttag(self, tag, attrs): |
| 31 | + if tag == "title": |
| 32 | + self.in_title = True |
| 33 | + |
| 34 | + def handle_data(self, data): |
| 35 | + if self.in_title: |
| 36 | + self.title += data |
| 37 | + |
| 38 | + def handle_endtag(self, tag): |
| 39 | + if tag == "title" and self.in_title: |
| 40 | + self.in_title = False |
| 41 | + self.done = True |
| 42 | + self.reset() |
| 43 | + |
| 44 | + parser = HTMLTitleParser() |
| 45 | + with open(file, encoding="UTF-8") as f: |
| 46 | + for line in f: |
| 47 | + parser.feed(line) |
| 48 | + if parser.done: |
| 49 | + break |
| 50 | + if not parser.done: |
| 51 | + parser.close() |
| 52 | + |
| 53 | + return parser.title if parser.done else None |
| 54 | + |
| 55 | + |
| 56 | +def get_date_authored_timestamp_from_git(path): |
| 57 | + source = os.path.realpath(path) |
| 58 | + proc = subprocess.run(["git", "log", "-1", "--format=%at", source], |
| 59 | + capture_output=True, encoding="utf_8") |
| 60 | + return int(proc.stdout.splitlines()[-1]) |
| 61 | + |
| 62 | + |
| 63 | +def get_bs_spec_metadata(folder_name, path): |
| 64 | + spec = bikeshed.Spec(path) |
| 65 | + spec.assembleDocument() |
| 66 | + |
| 67 | + level = int(spec.md.level) if spec.md.level else 0 |
| 68 | + shortname = spec.md.shortname |
| 69 | + |
| 70 | + return { |
| 71 | + "timestamp": get_date_authored_timestamp_from_git(path), |
| 72 | + "shortname": shortname, |
| 73 | + "level": level, |
| 74 | + "title": spec.md.title, |
| 75 | + "workStatus": spec.md.workStatus |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | +def get_html_spec_metadata(folder_name, path): |
| 80 | + match = re.match("^([a-z0-9-]+)-([0-9]+)$", folder_name) |
| 81 | + shortname = match.group(1) if match else folder_name |
| 82 | + title = title_from_html(path) |
| 83 | + |
| 84 | + return { |
| 85 | + "shortname": shortname, |
| 86 | + "level": int(match.group(2)) if match else 0, |
| 87 | + "title": title, |
| 88 | + "workStatus": "completed" # It's a good heuristic |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | +def create_symlink(shortname, spec_folder): |
| 93 | + """Creates a <shortname> symlink pointing to the given <spec_folder>.""" |
| 94 | + |
| 95 | + if spec_folder in timestamps: |
| 96 | + timestamps[shortname] = timestamps[spec_folder] |
| 97 | + |
| 98 | + try: |
| 99 | + os.symlink(spec_folder, shortname) |
| 100 | + except OSError: |
| 101 | + pass |
| 102 | + |
| 103 | + |
| 104 | +def format_timestamp(ts): |
| 105 | + """Format a Unix timestamp as a human-readable date string.""" |
| 106 | + dt = datetime.fromtimestamp(ts, tz=timezone.utc) |
| 107 | + return dt.strftime("%Y-%m-%d") |
| 108 | + |
| 109 | + |
| 110 | +def escape_html(text): |
| 111 | + """Escape HTML special characters.""" |
| 112 | + return (text |
| 113 | + .replace("&", "&") |
| 114 | + .replace("<", "<") |
| 115 | + .replace(">", ">") |
| 116 | + .replace('"', """)) |
| 117 | + |
| 118 | + |
| 119 | +CURRENT_WORK_EXCEPTIONS = {} |
| 120 | + |
| 121 | +# ------------------------------------------------------------------------------ |
| 122 | + |
| 123 | + |
| 124 | +bikeshed.messages.state.dieOn = "nothing" |
| 125 | + |
| 126 | +specgroups = defaultdict(list) |
| 127 | +timestamps = defaultdict(list) |
| 128 | + |
| 129 | +for entry in os.scandir("."): |
| 130 | + if entry.is_dir(follow_symlinks=False): |
| 131 | + bs_file = os.path.join(entry.path, "Overview.bs") |
| 132 | + html_file = os.path.join(entry.path, "Overview.html") |
| 133 | + if os.path.exists(bs_file): |
| 134 | + metadata = get_bs_spec_metadata(entry.name, bs_file) |
| 135 | + timestamps[entry.name] = metadata["timestamp"] |
| 136 | + elif os.path.exists(html_file): |
| 137 | + metadata = get_html_spec_metadata(entry.name, html_file) |
| 138 | + else: |
| 139 | + # Not a spec |
| 140 | + continue |
| 141 | + |
| 142 | + metadata["dir"] = entry.name |
| 143 | + metadata["currentWork"] = False |
| 144 | + specgroups[metadata["shortname"]].append(metadata) |
| 145 | + |
| 146 | +# Reorder the specs with common shortname based on their level, |
| 147 | +# and determine which spec is the current work. |
| 148 | +for shortname, specgroup in specgroups.items(): |
| 149 | + if len(specgroup) == 1: |
| 150 | + if shortname != specgroup[0]["dir"]: |
| 151 | + create_symlink(shortname, specgroup[0]["dir"]) |
| 152 | + else: |
| 153 | + specgroup.sort(key=lambda spec: spec["level"]) |
| 154 | + |
| 155 | + for spec in specgroup: |
| 156 | + if shortname in CURRENT_WORK_EXCEPTIONS: |
| 157 | + if CURRENT_WORK_EXCEPTIONS[shortname] == spec["level"]: |
| 158 | + spec["currentWork"] = True |
| 159 | + currentWorkDir = spec["dir"] |
| 160 | + break |
| 161 | + elif spec["workStatus"] != "completed": |
| 162 | + spec["currentWork"] = True |
| 163 | + currentWorkDir = spec["dir"] |
| 164 | + break |
| 165 | + else: |
| 166 | + specgroup[-1]["currentWork"] = True |
| 167 | + currentWorkDir = specgroup[-1]["dir"] |
| 168 | + |
| 169 | + if shortname != currentWorkDir: |
| 170 | + create_symlink(shortname, currentWorkDir) |
| 171 | + |
| 172 | +with open('./timestamps.json', 'w') as f: |
| 173 | + json.dump(timestamps, f, indent=2, sort_keys=True) |
| 174 | + |
| 175 | +# Build the index page |
| 176 | +rows = [] |
| 177 | +for shortname in sorted(specgroups.keys()): |
| 178 | + specgroup = specgroups[shortname] |
| 179 | + for spec in specgroup: |
| 180 | + title = escape_html(spec["title"] or spec["dir"]) |
| 181 | + level_suffix = f" Level {spec['level']}" if spec["level"] else "" |
| 182 | + current_label = ' <span class="current-work">(Current Work)</span>' if spec["currentWork"] else "" |
| 183 | + dir_name = spec["dir"] |
| 184 | + |
| 185 | + ts = timestamps.get(dir_name) |
| 186 | + date_str = format_timestamp(ts) if ts else "" |
| 187 | + |
| 188 | + rows.append( |
| 189 | + f' <tr>\n' |
| 190 | + f' <td><a href="./{dir_name}/">{title}</a>{current_label}</td>\n' |
| 191 | + f' <td>{date_str}</td>\n' |
| 192 | + f' </tr>' |
| 193 | + ) |
| 194 | + |
| 195 | +rows_html = "\n".join(rows) |
| 196 | + |
| 197 | +with open("./index.html", mode='w', encoding="UTF-8") as f: |
| 198 | + f.write(f"""\ |
| 199 | +<!doctype html> |
| 200 | +<html lang="en"> |
| 201 | +<head> |
| 202 | + <meta charset="utf-8"> |
| 203 | + <title>CSS Houdini Task Force Editor Drafts</title> |
| 204 | + <style> |
| 205 | + body {{ |
| 206 | + font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; |
| 207 | + max-width: 900px; |
| 208 | + margin: 2em auto; |
| 209 | + padding: 0 1em; |
| 210 | + color: #333; |
| 211 | + }} |
| 212 | + h1 {{ |
| 213 | + border-bottom: 1px solid #ccc; |
| 214 | + padding-bottom: 0.3em; |
| 215 | + }} |
| 216 | + table {{ |
| 217 | + width: 100%; |
| 218 | + border-collapse: collapse; |
| 219 | + margin-top: 1em; |
| 220 | + }} |
| 221 | + th, td {{ |
| 222 | + text-align: left; |
| 223 | + padding: 0.5em 0.75em; |
| 224 | + border-bottom: 1px solid #eee; |
| 225 | + }} |
| 226 | + th {{ |
| 227 | + border-bottom: 2px solid #ccc; |
| 228 | + font-weight: 600; |
| 229 | + }} |
| 230 | + td:last-child {{ |
| 231 | + white-space: nowrap; |
| 232 | + color: #666; |
| 233 | + }} |
| 234 | + a {{ |
| 235 | + color: #0366d6; |
| 236 | + text-decoration: none; |
| 237 | + }} |
| 238 | + a:hover {{ |
| 239 | + text-decoration: underline; |
| 240 | + }} |
| 241 | + .current-work {{ |
| 242 | + color: #080; |
| 243 | + font-size: 0.9em; |
| 244 | + }} |
| 245 | + </style> |
| 246 | +</head> |
| 247 | +<body> |
| 248 | + <h1>CSS Houdini Task Force Editor Drafts</h1> |
| 249 | + <table> |
| 250 | + <thead> |
| 251 | + <tr> |
| 252 | + <th>Specification</th> |
| 253 | + <th>Last Update</th> |
| 254 | + </tr> |
| 255 | + </thead> |
| 256 | + <tbody> |
| 257 | +{rows_html} |
| 258 | + </tbody> |
| 259 | + </table> |
| 260 | +</body> |
| 261 | +</html> |
| 262 | +""") |
0 commit comments