-
Notifications
You must be signed in to change notification settings - Fork 18
/
post-process-html.py
executable file
·97 lines (78 loc) · 2.86 KB
/
post-process-html.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python3
##
# Post-process mdbook generated HTML files to add Google Analytics tag and optionally cheatsheet
# pre style tag + other page specific HTML title additions
##
import glob
import re
# Constants
google_analytics_script_tag = """
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-D0T2GQ8R19"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-D0T2GQ8R19');
</script>
"""
large_pre_style_script_tag = """
<style>
pre {
font-size: 22px;
}
pre > .buttons {
visibility: hidden;
}
</style>
"""
end_head_html_target = '</head>'
analytics_html_snippet = google_analytics_script_tag + ' ' + end_head_html_target
large_pre_plus_analytics_html_snippet = (large_pre_style_script_tag + google_analytics_script_tag +
' ' + end_head_html_target)
start_title_html_target = '<title>'
select_distinct_prefix_html_snippet = start_title_html_target + 'SQL SELECT DISTINCT equivalent: '
####
# Main function finding each HTML file and sending it to have tags and metadata added
####
def main():
print('Starting to post-process HTML files')
filepaths = glob.glob('./docs/**/*.html', recursive=True)
for filepath in filepaths:
add_extra_heaad_tags_to_html_file(filepath)
add_extra_select_distinct_text_to_html_file(filepath)
print('Ending post-processing of HTML files')
####
# Add Google Analytics tag + plus optional cheatsheet pre style tag to HTML before </head> tag.
####
def add_extra_heaad_tags_to_html_file(filepath):
print(f' {filepath}')
with open(filepath, 'r+') as f:
text = f.read()
replaceText = analytics_html_snippet
if filepath.endswith("cheatsheet.html"):
print(" CHEATSHEET - extra style tag added")
replaceText = large_pre_plus_analytics_html_snippet
text = re.sub(end_head_html_target, replaceText, text)
f.seek(0)
f.write(text)
f.truncate()
####
# Add 'SQL SELECT DISTINCT equivalent:' prefix to title of "Distinct List Of Values" example title
# for SEO as commonly searched topic
####
def add_extra_select_distinct_text_to_html_file(filepath):
print(f' {filepath}')
with open(filepath, 'r+') as f:
text = f.read()
if filepath.endswith("distinct-values.html"):
print(" DISTINCT LIST OF VALUES - added extra SELECT DISTINCT title prefix")
text = re.sub(start_title_html_target, select_distinct_prefix_html_snippet, text)
f.seek(0)
f.write(text)
f.truncate()
####
# Bootstrap
####
if __name__ == '__main__':
main()