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 ()
0 commit comments