Skip to content

Commit be56a45

Browse files
committed
add HTML comments, update style, and minor fixes
- add legalcode HTML comments (for increased translation clarity) - add language footer HTML comments (to support future work to resolve creativecommons#1063) - replace string concatenation with f-strings - remove unused option handling - skip symlinks
1 parent d6885b1 commit be56a45

File tree

1 file changed

+63
-26
lines changed

1 file changed

+63
-26
lines changed

tools/prep_cc4_code.py

+63-26
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from pathlib import Path
1818
import getopt
19+
import os
1920
import re
2021
import sys
2122

@@ -44,6 +45,14 @@ class UpdateLicenseCode(object):
4445
"<!-- Language Selector Start - DO NOT DELETE -->",
4546
"<!-- Language Selector End - DO NOT DELETE -->",
4647
),
48+
"legalcode": (
49+
"<!-- Legalcode Start - DO NOT DELETE -->",
50+
"<!-- Legalcode End - DO NOT DELETE -->",
51+
),
52+
"language-footer": (
53+
"<!-- Language Footer Start - DO NOT DELETE -->",
54+
"<!-- Language Footer End - DO NOT DELETE -->",
55+
),
4756
}
4857

4958
image_map = {
@@ -78,12 +87,9 @@ def get_args(self):
7887
return False
7988

8089
self.verbose = False
81-
self.add_placeholders = False
8290
for option in opts:
8391
if "-v" in option:
8492
self.verbose = True
85-
elif "-a" in option:
86-
self.add_placeholders = True
8793

8894
return True
8995

@@ -115,7 +121,7 @@ def process_file(self, filepath):
115121
- Remove references to deed3 css files
116122
- Remove inline styles
117123
- Remove Creative Commons text header"""
118-
self.log("Processing: " + filepath.name, "verbose")
124+
self.log(f"Processing: {filepath.name}", "verbose")
119125
with filepath.open(encoding="utf-8") as infile:
120126
content = infile.read()
121127
license_attrs = self.get_license_attrs(filepath.name)
@@ -130,7 +136,7 @@ def process_file(self, filepath):
130136
content = self.add_type_logos(content, license_attrs["type"])
131137
content = self.handling_consideration_blockquotes(content)
132138

133-
self.log(" Updating content: " + filepath.name, "verbose")
139+
self.log(f" Updating content: {filepath.name}", "verbose")
134140
with filepath.open("w", encoding="utf-8") as outfile:
135141
outfile.write(content)
136142

@@ -142,23 +148,60 @@ def handle_placeholders(self, content):
142148
for placeholder_pair in sorted(UpdateLicenseCode.placeholders):
143149
if self.has_placeholders(content, placeholder_pair):
144150
self.log(
145-
" Found placeholder: " + placeholder_pair + ", skipping",
151+
f" Found placeholder: {placeholder_pair}, skipping",
146152
"verbose",
147153
)
148154
else:
149155
start, end = UpdateLicenseCode.placeholders[placeholder_pair]
150156
if placeholder_pair == "head":
151157
target = "</head>"
152-
replacement = start + "\n" + end + "\n" + target
158+
replacement = f"{start}\n{end}\n{target}"
153159
elif placeholder_pair == "header":
154160
target = re.search("<body.*?>", content).group()
155-
replacement = target + "\n" + start + "\n" + end
161+
replacement = f"{target}\n{start}\n{end}"
156162
elif placeholder_pair == "footer":
157163
target = "</body>"
158-
replacement = start + "\n" + end + "\n" + target
164+
replacement = f"{start}\n{end}\n{target}"
159165
elif placeholder_pair == "language-selector":
160166
target = "<!-- Site Header End - DO NOT DELETE -->"
161-
replacement = target + "\n" + start + "\n" + end
167+
replacement = f"{target}\n{start}\n{end}"
168+
elif placeholder_pair == "legalcode":
169+
re_pattern = re.compile(
170+
r"""
171+
^\s*<div\ id="deed"
172+
.*
173+
^\s*<li\ id="s8d">.*</li>\s*</ol>$
174+
(?=\s*<p\ class="shaded">)
175+
""",
176+
re.DOTALL | re.MULTILINE | re.VERBOSE,
177+
)
178+
target = re_pattern.search(content).group()
179+
replacement = f"\n{start}\n{target.strip()}\n{end}\n"
180+
elif placeholder_pair == "language-footer":
181+
re_pattern = re.compile(
182+
r"""
183+
^\s*<p\ class="shaded(?:\ a-nobreak)?">\s*
184+
<a(?:\ name="languages")?\ id="languages">
185+
.*(?:\s*</p>)?
186+
(?=\s*</div>\s*</div>\s*<div\ id="deed-foot">)
187+
|
188+
^\s*<p\ class="shaded(?:\ a-nobreak)?">\s*
189+
<a(?:\ name="languages")?\ id="languages">
190+
.*\s*</p>
191+
(?=\s*</div>\s*<div\ id="deed-foot">)
192+
|
193+
^\s*<p\ class="shaded(?:\ a-nobreak)?">\s*
194+
<a(?:\ name="languages")?\ id="languages">
195+
.*\s*</p>
196+
(?=
197+
\s*</li>\s*</ol>\s*</div>\s*</div>\s*"
198+
\s*<div\ id="deed-foot">
199+
)
200+
""",
201+
re.DOTALL | re.MULTILINE | re.VERBOSE,
202+
)
203+
target = re_pattern.search(content).group()
204+
replacement = f"{start}\n{target.strip()}\n{end}\n"
162205
content = content.replace(target, replacement, 1)
163206
return content
164207

@@ -259,28 +302,18 @@ def add_type_logos(self, content, lic_type):
259302
filename = UpdateLicenseCode.image_map[lic_attr]["file"]
260303
alt_text = UpdateLicenseCode.image_map[lic_attr]["alt_text"]
261304
image_tag = (
262-
'<img src="/images/deed/svg/'
263-
+ filename
264-
+ '" alt="'
265-
+ alt_text
266-
+ '"/>'
305+
f'<img src="/images/deed/svg/{filename}" alt="{alt_text}"/>'
267306
)
268307
lic_images += (
269-
'<span class="cc-icon-'
270-
+ lic_attr
271-
+ '">'
272-
+ image_tag
273-
+ "</span>"
308+
f'<span class="cc-icon-{lic_attr}">{image_tag}</span>'
274309
)
275310
cc_logo_section = re.search(
276311
'<div id="cc-logo">.*?</div>', content, re.DOTALL
277312
).group()
278313
new_cc_logo_section = (
279-
'<div id="cc-logo">'
280-
+ '<span class="cc-icon-logo">'
281-
+ '<img src="/images/deed/svg/cc_white.svg" alt="CC"/></span>'
282-
+ lic_images
283-
+ "</div>"
314+
'<div id="cc-logo"><span class="cc-icon-logo">'
315+
'<img src="/images/deed/svg/cc_white.svg" alt="CC"/></span>'
316+
f"{lic_images}</div>"
284317
)
285318
content = content.replace(cc_logo_section, new_cc_logo_section)
286319
return content
@@ -295,7 +328,11 @@ def handling_consideration_blockquotes(self, content):
295328
def main(self):
296329
"""Get the command line arguments, find the files, and process them"""
297330
if self.get_args() and self.get_path():
298-
file_list = [f for f in self.path.glob("*4.0*.html")]
331+
file_list = [
332+
f
333+
for f in self.path.glob("*4.0*.html")
334+
if not os.path.islink(f)
335+
]
299336
self.process_files(file_list)
300337

301338

0 commit comments

Comments
 (0)