Skip to content

Commit e265a05

Browse files
annevkdomenic
andauthored
Use SG's db.json as source of truth
See whatwg/sg#111 for context. Closes whatwg/sg#75. Co-authored-by: Domenic Denicola <[email protected]>
1 parent c3e467c commit e265a05

File tree

10 files changed

+145
-350
lines changed

10 files changed

+145
-350
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ whatwg.org/principles
1111
whatwg.org/policies
1212
whatwg.org/working-mode
1313

14+
# Ignore files generated by convert_sg_db.py
15+
__pycache__/
16+
idea.whatwg.org/index.html
17+
resources.whatwg.org/biblio.json
18+
spec.whatwg.org/index.html
19+
whatwg.org/workstreams
20+
1421
# Ignore generated snapshot logos
1522
resources.whatwg.org/logo*-snapshot.svg
1623

convert-policy.py renamed to convert_policy.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import re
55

66

7-
def parse_link_mapping(link_mapping):
7+
def obtain_link_mapping():
8+
link_mapping = open("sg/policy-link-mapping.txt", "r", encoding="utf-8").read()
89
return [line.split('=',1) for line in link_mapping.split("\n") if len(line) > 0]
910

1011

@@ -52,9 +53,10 @@ def rewrite_defs(policy_markdown):
5253
return re.sub(r'<a id=([^>]*)>[*][*]([^*]*)[*][*]</a>', '<dfn id=\\1>\\2</dfn>', policy_markdown)
5354

5455

55-
def preprocess_markdown(policy_markdown, mapping_pairs):
56+
def markdown(policy_markdown, mapping_pairs = []):
5657
result = apply_link_mapping(policy_markdown, mapping_pairs)
5758
result = rewrite_defs(result)
59+
result = commonmark.commonmark(result)
5860

5961
return result
6062

@@ -83,7 +85,7 @@ def markdown_title(policy_markdown):
8385

8486

8587
def main():
86-
link_mapping_pairs = parse_link_mapping(open("sg/policy-link-mapping.txt", "r", encoding="utf-8").read())
88+
link_mapping_pairs = obtain_link_mapping()
8789
template = open("site-template.html", "r", encoding="utf-8").read()
8890
for resource, link in link_mapping_pairs:
8991
if link.startswith("https:"):
@@ -92,9 +94,8 @@ def main():
9294
policy_markdown = open("sg" + resource[1:].replace("%20", " "), "r", encoding="utf-8").read()
9395

9496
(title, policy_markdown) = markdown_title(policy_markdown)
95-
preprocessed_policy_markdown = preprocess_markdown(policy_markdown, link_mapping_pairs)
9697

97-
policy_html = commonmark.commonmark(preprocessed_policy_markdown)
98+
policy_html = markdown(policy_markdown, link_mapping_pairs)
9899

99100
postprocessed_policy_html = postprocess_html(policy_html, template, title)
100101

convert_sg_db.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/usr/bin/env python
2+
3+
import json
4+
from datetime import date
5+
from convert_policy import markdown, obtain_link_mapping
6+
7+
8+
def create_biblio(db):
9+
biblio = {}
10+
11+
for workstream in db["workstreams"]:
12+
for standard in workstream["standards"]:
13+
reference, contents = standard_or_idea_to_biblio(standard)
14+
contents["title"] += " Standard"
15+
biblio[reference] = contents
16+
17+
for idea in db["ideas"]:
18+
reference, contents = standard_or_idea_to_biblio(idea)
19+
contents["status"] = "Living Idea"
20+
biblio[reference] = contents
21+
22+
for biblio_item in db["biblio"]:
23+
reference, contents = biblio_item_to_biblio(biblio_item)
24+
biblio[reference] = contents
25+
26+
string = json.dumps(biblio, ensure_ascii=False, allow_nan=False, sort_keys=True, indent=2)
27+
return string + "\n"
28+
29+
def standard_or_idea_to_biblio(document):
30+
return (document["reference"], {
31+
"authors": [author_to_biblio(author) for author in document["authors"]],
32+
"href": document["href"],
33+
"title": document["name"]
34+
})
35+
36+
def biblio_item_to_biblio(item):
37+
contents = {
38+
"href": item["href"],
39+
"title": item["title"]
40+
}
41+
if len(item["authors"]):
42+
contents["authors"] = [author_to_biblio(author) for author in item["authors"]]
43+
if "obsoletedBy" in item:
44+
contents["obsoletedBy"] = item["obsoletedBy"]
45+
return (item["reference"], contents)
46+
47+
def author_to_biblio(person):
48+
return person["name"]
49+
50+
51+
def create_spec_whatwg_org(db, template):
52+
content = "<p>The WHATWG works on a number of technologies that are fundamental parts of the web platform. They are organised somewhat arbitrarily based on the preferences of those editing the standard for those technologies.</p>\n\n"
53+
content += "<dl>"
54+
for workstream in db["workstreams"]:
55+
for standard in workstream["standards"]:
56+
content += standard_or_idea_to_html(standard)
57+
content += "</dl>\n\n<p>The WHATWG also works on a <a href=\"https://idea.whatwg.org/\">number of ideas</a> that aspire to become standards one day.</p>\n\n"
58+
59+
template = template.replace("<a href=\"https://spec.whatwg.org/\">Standards</a>", "<a>Standards</a>")
60+
return wrap_in_site_template(template, "Standards", content)
61+
62+
def create_idea_whatwg_org(db, template):
63+
content = "<p>The WHATWG works on a number of ideas that aspire to become web platform standards.</p>\n\n"
64+
content += "<dl>"
65+
for idea in db["ideas"]:
66+
content += standard_or_idea_to_html(idea)
67+
content += "</dl>\n\n"
68+
69+
return wrap_in_site_template(template, "Ideas", content)
70+
71+
def standard_or_idea_to_html(document):
72+
output = "\n <dt><a href=\"{0}\">{1}</a></dt>".format(document["href"], document["name"])
73+
return output + "\n <dd>{}</dd>\n".format(markdown(document["description"])[:-1]) # Strip trailing \n
74+
75+
76+
def create_workstreams(db, template):
77+
introduction = open("sg/Workstreams Introduction.md", "r", encoding="utf-8").read()
78+
introduction = introduction.replace("<!-- This ends up being included at https://whatwg.org/workstreams -->\n", "")
79+
content = markdown(introduction, obtain_link_mapping())
80+
for workstream in db["workstreams"]:
81+
content += "\n<h3>{}</h3>".format(workstream["name"])
82+
content += """\n<dl class="compact">"""
83+
content += "\n <div>"
84+
content += "\n <dt>Scope</dt>"
85+
content += "\n <dd>{}</dd>".format(markdown(workstream["scope"])[3:-5]) # Strip leading <p> and trailing </p>\n
86+
content += "\n </div>"
87+
content += "\n <div>"
88+
content += "\n <dt>Editor{}</dt>".format("" if len(workstream["editors"]) == 1 else "s")
89+
for editor in workstream["editors"]:
90+
content += """\n <dd>{0}, <a href="mailto:{1}">{1}</a></dd>""".format(editor["name"], editor["email"])
91+
content += "\n </div>"
92+
content += "\n <div>"
93+
content += "\n <dt>Standard{}</dt>".format("" if len(workstream["standards"]) == 1 else "s")
94+
for standard in workstream["standards"]:
95+
content += """\n <dd><a href="{0}">{1}</a></dd>""".format(standard["href"], standard["name"])
96+
content += "\n </div>"
97+
content += "\n</dl>"
98+
99+
return wrap_in_site_template(template, "Workstreams", content)
100+
101+
102+
def wrap_in_site_template(template, title, content):
103+
output = template.replace("@TITLE_GOES_HERE@", title)
104+
return output.replace("@CONTENT_GOES_HERE@", content)
105+
106+
def main():
107+
template = open("site-template.html", "r", encoding="utf-8").read()
108+
copyright = """<footer>
109+
<p><small>Copyright © {} WHATWG (Apple, Google, Mozilla, Microsoft). This work is licensed under a <a rel="license" href="https://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>.</small></p>
110+
</footer>""".format(str(date.today().year))
111+
template = template.replace("@CONTENT_GOES_HERE@", "@CONTENT_GOES_HERE@" + copyright)
112+
db = json.load(open("sg/db.json", "r", encoding="utf-8"))
113+
114+
for filename, content in [
115+
("resources.whatwg.org/biblio.json", create_biblio(db)),
116+
("spec.whatwg.org/index.html", create_spec_whatwg_org(db, template)),
117+
("idea.whatwg.org/index.html", create_idea_whatwg_org(db, template)),
118+
("whatwg.org/workstreams", create_workstreams(db, template))
119+
]:
120+
open(filename, "w", encoding="utf-8").write(content)
121+
122+
main()

deploy.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ header() {
77

88
header "Importing content from whatwg/sg and making it suitable for whatwg.org"
99
git clone --depth=1 https://github.com/whatwg/sg sg
10-
./convert-policy.py
10+
./convert_policy.py
11+
./convert_sg_db.py
1112
rm -rf sg
1213
echo ""
1314

idea.whatwg.org/index.html

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)