-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathepair.py
More file actions
114 lines (83 loc) · 2.79 KB
/
epair.py
File metadata and controls
114 lines (83 loc) · 2.79 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
import re
import sys
import unicodedata
# ---------- CONFIG ----------
BASE_OUTPUT_DIR = "pages"
AUTO_CREATE_PAGES = True
# ----------------------------
def slugify(text: str) -> str:
text = unicodedata.normalize("NFKD", text)
text = text.encode("ascii", "ignore").decode("ascii")
text = text.lower()
text = re.sub(r"[^a-z0-9]+", "-", text)
return text.strip("-")
def make_paths(title: str, current_file: str):
slug = slugify(title)
filename = f"{slug}.html"
# place pages folder next to the input file
base_dir = os.path.dirname(current_file)
output_dir = os.path.join(base_dir, BASE_OUTPUT_DIR)
filepath = os.path.join(output_dir, filename)
# relative link from current file → pages/
href = os.path.relpath(filepath, start=base_dir).replace("\\", "/")
return filepath, href
def generate_page(title: str, desc: str, filepath: str):
if os.path.exists(filepath):
return
os.makedirs(os.path.dirname(filepath), exist_ok=True)
html = f"""<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>{title}</title>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/5/w3.css">
</head>
<body class="w3-container">
<h1>{title}</h1>
<p>{desc}</p>
<p>Content not written yet.</p>
<a href="../index.html" class="w3-button w3-teal">Back</a>
</body>
</html>
"""
with open(filepath, "w", encoding="utf-8") as f:
f.write(html)
def patch_html(file_path: str):
with open(file_path, "r", encoding="utf-8") as f:
html = f.read()
pattern = re.compile(
r'(<li class="w3-padding-16">.*?'
r'<span class="w3-large">(.*?)</span><br>\s*'
r'<span>(.*?)</span>.*?'
r'<a class="w3-button.*?>Read More</a>)',
re.DOTALL
)
def replace_block(match):
full = match.group(1)
title = match.group(2).strip()
desc = match.group(3).strip()
filepath, href = make_paths(title, file_path)
if AUTO_CREATE_PAGES:
generate_page(title, desc, filepath)
new_link = f'<a href="{href}" class="w3-button w3-small w3-margin-top w3-teal">Read More</a>'
updated = re.sub(
r'<a class="w3-button.*?>Read More</a>',
new_link,
full
)
return updated
new_html = pattern.sub(replace_block, html)
with open(file_path, "w", encoding="utf-8") as f:
f.write(new_html)
print(f"[OK] Patched: {file_path}")
# ---------- ENTRY POINT ----------
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python3 patcher.py <file1.html> [file2.html ...]")
sys.exit(1)
for file in sys.argv[1:]:
if not os.path.exists(file):
print(f"[SKIP] Not found: {file}")
continue
patch_html(file)