This repository has been archived by the owner on Jul 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert-html-banner.py
85 lines (69 loc) 路 2.73 KB
/
insert-html-banner.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
""" Script to automatically insert a disclaimer html into each html page """
from html.parser import HTMLParser
from pathlib import Path
from typing import List
from glob import glob
BANNER_TO_INSERT: str = """
<div id="disclaimer-banner" style="display: flex; justify-content: center; background-color: white;">
<p style="padding: 0.5rem; font-style: italic; margin: 0;">
Educational, non-profit project created by students
</p>
<a href="https://www.htl-leonding.at/" style="padding: 0.5rem 0; font-style: italic;">
@ HTL Leonding.
</a>
</div>
"""
class InsertHTMLParser(HTMLParser):
"""
The deployment html parser, which will update the files and generate
a new file with the banner added
"""
def __init__(self):
self.content = ""
super().__init__()
def insert_banner(self, content: str) -> str:
""" Inserts the banner into the passed html string """
self.content = content
self.feed(content)
# The file should be updated now -> return new string
_ = self.content
self.content = ""
return _
def handle_starttag(self, tag, attrs):
""" Handles a html starttag """
if tag == "body":
# Getting the line for the <body> tag
line, col = self.getpos()
# Getting the file content split up into its sections
file_lines = self.content.split("\n")
prev_lines = file_lines[:line-1]
insert_section = file_lines[line-1]
after_lines = file_lines[line:]
for pos, c in enumerate(insert_section, col):
if c == ">":
# Appending the banner in this exact location
insert_section = f"{insert_section[:pos+1]}" \
f"\n{BANNER_TO_INSERT}\n" \
f"{insert_section[pos+1:]}"
# Merging the strings back together
nl: str = '\n'
self.content = f"{nl.join(prev_lines)}" \
f"{insert_section}{nl}" \
f"{nl.join(after_lines)}"
def discover_files() -> List[Path]:
""" Discovers all html files and updates their content """
return list(
Path(i) for i in glob("src/**/*.html", recursive=True)
if "404" not in i
)
def update_files(to_update: List[Path]) -> None:
""" Updates every file by appending the banner """
for file in to_update:
with open(file, 'r+', encoding="utf-8") as f:
content = f.read()
content = InsertHTMLParser().insert_banner(content)
f.seek(0)
f.write(content)
if __name__ == '__main__':
files: List[Path] = discover_files()
update_files(files)