Skip to content

Commit f779e4c

Browse files
committed
First attempt at auto-building in the repo.
1 parent 1cf9063 commit f779e4c

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed

bin/configure-spec-data.py

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import os
2+
from os.path import join
3+
import re
4+
import hashlib
5+
import json
6+
7+
from bikeshed.config import scriptPath as bikeshedScriptPath
8+
9+
# This script changes the Bikeshed spec data to have any cross-references to
10+
# CSSWG specs linked to their mirror URL.
11+
12+
ORIGINAL_BASE_URL = "https://drafts.csswg.org/"
13+
BASE_URL = "https://w3c.github.io/csswg-drafts/"
14+
15+
spec_data_folder = bikeshedScriptPath("spec-data")
16+
17+
changed_hashes = {}
18+
19+
# We only need to change anchors, biblio and headings, as well as the
20+
# manifest.txt
21+
22+
23+
def generate_manifest_hash(text):
24+
encoded = text.encode("ascii", "xmlcharrefreplace")
25+
return hashlib.md5(encoded).hexdigest()
26+
27+
28+
def update_anchors_and_biblio(file):
29+
changed = False
30+
replacedText = ""
31+
path = join(spec_data_folder, file)
32+
with open(path, encoding="utf-8") as f:
33+
for line in f:
34+
if line.startswith(ORIGINAL_BASE_URL):
35+
changed = True
36+
line = BASE_URL + line[len(ORIGINAL_BASE_URL):]
37+
replacedText += line
38+
if changed:
39+
with open(path, mode="w", encoding="utf-8") as f:
40+
f.write(replacedText)
41+
changed_hashes[file] = generate_manifest_hash(replacedText)
42+
43+
44+
def update_headings(file):
45+
changed = False
46+
path = join(spec_data_folder, file)
47+
with open(path) as f:
48+
headings_map = json.load(f)
49+
50+
for heading_id in headings_map.values():
51+
if type(heading_id) != dict:
52+
continue
53+
for heading in heading_id.values():
54+
if heading["url"].startswith(ORIGINAL_BASE_URL):
55+
changed = True
56+
heading["url"] = BASE_URL + \
57+
heading["url"][len(ORIGINAL_BASE_URL):]
58+
59+
if changed:
60+
replacedText = json.dumps(
61+
headings_map, ensure_ascii=False, indent=2, sort_keys=True)
62+
with open(path, mode="w", encoding="utf-8") as f:
63+
f.write(replacedText)
64+
changed_hashes[file] = generate_manifest_hash(replacedText)
65+
66+
67+
def update_manifest():
68+
if changed_hashes:
69+
replacedText = ""
70+
manifestFilename = join(spec_data_folder, "manifest.txt")
71+
with open(manifestFilename, encoding="utf-8") as f:
72+
for line in f:
73+
match = re.match("([0-9a-f]{32}) (.*)\n", line)
74+
if match:
75+
filename = match.group(2)
76+
if filename in changed_hashes:
77+
sha = changed_hashes[filename]
78+
else:
79+
sha = match.group(1)
80+
replacedText += "{} {}\n".format(sha, filename)
81+
else:
82+
replacedText += line
83+
with open(manifestFilename, mode="w", encoding="utf-8") as f:
84+
f.write(replacedText)
85+
86+
87+
for folder in ["anchors", "biblio"]:
88+
for file in os.listdir(join(spec_data_folder, folder)):
89+
update_anchors_and_biblio(join(folder, file))
90+
91+
for file in os.listdir(join(spec_data_folder, "headings")):
92+
update_headings(join("headings", file))
93+
94+
update_manifest()

workflows/build-specs.yml

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: Spec Deployment
2+
on:
3+
push:
4+
branches: [ "main" ]
5+
# Allows you to run this workflow manually from the Actions tab
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: read
10+
pages: write
11+
id-token: write
12+
13+
# Allow one concurrent deployment
14+
concurrency:
15+
group: "pages"
16+
cancel-in-progress: true
17+
18+
jobs:
19+
build-specs:
20+
runs-on: ubuntu-latest
21+
22+
environment:
23+
name: github-pages
24+
url: ${{ steps.deployment.outputs.page_url }}
25+
26+
steps:
27+
- uses: actions/checkout@v3
28+
29+
- uses: actions/setup-python@v4
30+
with:
31+
python-version: "3.10"
32+
cache: 'pip'
33+
34+
- run: pip install bikeshed
35+
- run: bikeshed update
36+
37+
# The following chunk of code all stolen from andeubotella
38+
# Thanks, dude!
39+
- name: Correct local links
40+
run: python ./bin/configure-spec-data.py
41+
- name: Compile
42+
run: |
43+
set -e
44+
for file in ./**/*.bs; do
45+
bikeshed -f spec "$file"
46+
done
47+
- name: Fix directory defaults
48+
# The default pages for directories are assumed to be Overview.html, but
49+
# that doesn't work for Github Pages. So we copy them as index.html.
50+
run: |
51+
set -e
52+
for file in ./csswg-drafts/**/Overview.html; do
53+
cp "$file" "$(dirname "$file")/index.html"
54+
done
55+
56+
# The index code uses more dependencies,
57+
# so I'll figure it out later.
58+
# - name: Build index
59+
# run: python ./bin/build-index.py
60+
- run: rm -rf ./.git{,attributes,ignore}
61+
62+
63+
- name: Setup Pages
64+
uses: actions/configure-pages@v2
65+
- name: Upload artifact
66+
uses: actions/upload-pages-artifact@v1
67+
with:
68+
# Upload entire repository
69+
path: '.'
70+
- name: Deploy to GitHub Pages
71+
id: deployment
72+
uses: actions/deploy-pages@v1

0 commit comments

Comments
 (0)