-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
130 lines (94 loc) · 3.59 KB
/
helpers.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import re
import os
import sys
import datetime
ld = os.listdir("v1/")
def rreplace(s, old, new):
li = s.rsplit(old, 1) #Split only once
return new.join(li)
with open("v1/index.html", "r", encoding='utf-8') as e:
t = e.read()
lst = re.findall(r'<a href="\./(.*)/">', t)
for s in ld:
if ("." not in s) and (s[0].isdigit()) and (s not in lst):
t = rreplace(t, "</ul>", f""" <li><a href="./{s}/">{s.replace('-', '.', 1).replace('-', chr(92), 1).replace('-', ' ').replace(chr(92), ' - ')}</a></li>
</ul>""")
lst.append(s)
with open("v1/index.html", "w") as f:
f.write(t)
with open("sitemap.xml", "r", encoding='utf-8') as g:
x = g.read()
sitemap = re.findall(r'(?:v1|revision)/(.*)/<', x)
for string in lst:
if string not in sitemap:
lastmod = os.path.getmtime("./v1/" + string)
x = x.replace("</urlset>", f"""
<url>
<loc>https://cs.rujulnayak.com/v1/{string}/</loc>
<lastmod>{datetime.datetime.fromtimestamp(lastmod).strftime('%Y-%m-%d')}</lastmod>
<changefreq>monthly</changefreq>
<priority>0.7</priority>
</url>
</urlset>""")
x = re.sub(r"<lastmod>(.*)</lastmod> <!-- marker -->", f"<lastmod>{datetime.datetime.today().strftime('%Y-%m-%d')}</lastmod> <!-- marker -->", x)
with open("sitemap.xml", "w") as h:
h.write(x)
with open("newfile.txt", "r") as f:
filename = f.read()
if filename == "":
sys.exit(0)
with open("newfile.txt", "w") as g:
pass
path = "v1/" + filename.replace(".", "-").replace(" ", "-")
os.mkdir(path)
n, *t = filename.split()
r = " ".join(t)
p = sorted(os.listdir("v1/"))[-6]
a, b, *c = p.split("-")
with open(path + "/index.html", "w+") as h:
h.write(f"""<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="../../main.css">
<link rel="stylesheet" href="../main.css">
<title>{n} - {r} | Computer Science Revision</title>
<link rel="icon" type="image/png" href="../icons/logo.svg"/>
<meta property="og:image" content="../icons/logo.png" />
<meta property="og:image:width" content="640" />
<meta property="og:image:height" content="512" />
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="{n} - {r} | Computer Science Revision (an online, mobile-friendly revision guide for GCSE Computer Science students)">
</head>
<body>
<h1>{n} – {r}</h1>
<h3 class="home-link"><a href="../">Computer Science Revision</a></h3>
<br />
<a class="prev-link" href="../{p}/">Previous: {a}.{b} - {' '.join(c)}</a>
<br /> <br />
<!-- -->
<br />
<hr />
<p class="question">
<img src="../icons/question.svg" class="icon" />
</p>
<p class="spoiler noselect">
<span class="default">Tap/click to reveal</span>
<span class="answer">
</span>
</p>
<br />
<hr />
<br /> <br />
<div id="sub">© Rujul Nayak <span id="year">2024-</span></div>
<script src="../../year.js"></script>
<script src="../spoilers.js"></script>
</body>
</html>""")
with open("v1/" + p + "/index.html", "r") as i:
txt = i.read().replace('<div id="sub"', f'''<a class="next-link" href="../{path.split("/")[1]}/">Next: {n} - {r}</a>
<br /> <br />
<br /> <br />
<div id="sub"''')
with open("v1/" + p + "/index.html", "w") as j:
j.write(txt)