davidovski/build.py

289 lines
9.2 KiB
Python
Raw Normal View History

2021-08-26 03:15:22 +00:00
import markdown
import os
import time
import shutil
2021-09-29 22:18:33 +00:00
import subprocess
from html import escape
2021-08-26 03:15:22 +00:00
from const import *
def getTemplateHTML(name):
html = ""
with open(os.path.join(templates, name), "r") as file:
html = file.read();
return html
2021-08-27 02:08:43 +00:00
def lowerHeadings(html):
# This is a dumb lol
return html.replace("<h6>", "<p>")\
.replace("</h6>", "</p>")\
.replace("<h5>", "<h6>")\
.replace("</h5>", "</h6>")\
.replace("<h4>", "<h5>")\
.replace("</h4>", "</h5>")\
.replace("<h3>", "<h4>")\
.replace("</h3>", "</h4>")\
.replace("<h2>", "<h3>")\
.replace("</h2>", "</h3>")\
.replace("<h1>", "<h2>")\
.replace("</h1>", "</h2>")\
2021-08-26 03:15:22 +00:00
def listPages():
return [
(lambda path:
(lambda content:
(lambda timestamp:
(lambda name: {
"source_file" : path,
"source_content" : content,
2021-08-27 02:08:43 +00:00
"html" : markdown.markdown("\n".join(content.split("\n...\n"))),
"summary" : lowerHeadings(markdown.markdown(content.split("\n...\n")[0])),
2021-08-26 03:15:22 +00:00
"timestamp" : timestamp,
"date": time.strftime(date_format, time.localtime(timestamp)),
"name" : name,
"url" : f"entries/{name}.html"
})(".".join(p.split(".")[:-1]))
)(os.stat(path).st_ctime)
)(open(path, "r").read())
)(os.path.join(source, p)) for p in os.listdir(source)
]
2021-08-27 02:08:43 +00:00
2021-08-26 03:15:22 +00:00
def formatEntry(content, page):
return content.replace("%date%", page["date"])\
.replace("%name%", page["name"])\
.replace("%time%", str(page["timestamp"]))\
.replace("%source%", site_index + page["source_file"])\
.replace("%url%", site_index + page["url"])
def make():
try:
os.makedirs(os.path.join(dist, "entries"))
except:
print("Already have content")
2021-09-29 22:18:33 +00:00
try:
shutil.rmtree(os.path.join(dist, "src"))
except:
pass
try:
shutil.rmtree(os.path.join(dist, "images"))
except:
pass
try:
shutil.copytree(source, os.path.join(dist, "src"))
except:
pass
try:
shutil.copytree(images, os.path.join(dist, "images"))
except:
pass
2021-08-26 03:15:22 +00:00
pages = listPages()
2021-10-02 14:37:51 +00:00
pages = sorted(pages, key=lambda p: p["timestamp"])
2021-08-26 03:15:22 +00:00
summary_templ = getTemplateHTML("summary.html")
2021-08-27 02:08:43 +00:00
summariesHTML = getTemplateHTML("about.html") + "\n<hr>\n"+ "\n<hr>\n".join(
[
formatEntry(summary_templ, page)
.replace(
"%content%",
page["summary"] + (f"<a href={page['url']}>read more...</a>" if len(page["source_content"].split("\n...\n")) > 1 else "")
)
2021-08-26 03:15:22 +00:00
2021-08-27 02:08:43 +00:00
for page in pages
][: : -1]
2021-08-26 03:15:22 +00:00
)
entry_templ = getTemplateHTML("page.html")
2021-10-02 14:37:51 +00:00
2021-08-26 03:15:22 +00:00
for page in pages:
with open(os.path.join(dist, page["url"]), "w") as entry:
entry.write(
formatEntry(
entry_templ,
page
)
.replace("%content%", page["html"])
)
2021-08-27 02:08:43 +00:00
index_templ = getTemplateHTML("page.html")
2021-08-26 03:15:22 +00:00
with open(os.path.join(dist, "index.html"), "w") as index:
index.write(
2021-08-27 02:08:43 +00:00
index_templ.replace("%content%", summariesHTML)
2021-08-26 03:15:22 +00:00
)
item_templ = getTemplateHTML("item.xml")
rss_templ = getTemplateHTML("rss.xml")
itemsXML = "\n".join(
[
formatEntry(item_templ, page).replace("%content%", page["html"])
for page in pages
2021-09-01 01:45:04 +00:00
][: : -1]
2021-08-26 03:15:22 +00:00
)
with open(os.path.join(dist, "rss.xml"), "w") as index:
index.write(
rss_templ.replace("%items%", itemsXML)
)
2021-09-29 22:18:33 +00:00
for f in os.listdir(resources):
shutil.copy(os.path.join(resources, f), dist)
2021-08-26 03:15:22 +00:00
print(f"built in {len(pages)} pages")
2021-09-29 22:18:33 +00:00
def get_repos():
repos = []
if os.path.exists("git_repos.txt"):
with open("git_repos.txt", "r") as file:
2021-10-02 14:37:51 +00:00
repos = [l for l in file.readlines() if l.startswith("http")]
2021-09-29 22:18:33 +00:00
return repos
def list_files(path):
files = []
dirlist = [path]
while len(dirlist) > 0:
for (dirpath, dirnames, filenames) in os.walk(dirlist.pop()):
dirlist.extend(dirnames)
files.extend(map(lambda n: os.path.join(*n), zip([dirpath] * len(filenames), filenames)))
print(len(files))
return files
2021-10-02 14:37:51 +00:00
def linkify_path(path):
output = []
full = "/"
for s in path.split("/"):
full += s + "/"
output.append(f"<a href='{full}'>{s}</a>")
return "/" + "/".join(output)
2021-09-29 22:18:33 +00:00
def format_file(page_templ, content, v):
return page_templ.replace("%title%", v["name"])\
.replace("%up%", v["above"])\
2021-10-02 14:37:51 +00:00
.replace("%filename%", linkify_path(v["filename"]))\
2021-09-29 22:18:33 +00:00
.replace("%commit%", str(v["commit"]))\
2021-10-02 14:37:51 +00:00
.replace("%url%", str(v["url"]))\
2021-09-29 22:18:33 +00:00
.replace("%content%", content)
2021-10-02 14:37:51 +00:00
def traverse_repo(path, name, commit, url):
2021-09-29 22:18:33 +00:00
page_templ = getTemplateHTML("page.html")
page_templ = page_templ.replace("%content%", getTemplateHTML("file.html"))
2021-10-02 14:37:51 +00:00
date = time.strftime(date_format, time.localtime())
footer = f"<p class='small'>This repo has been compiled for web view on <b>{date}</b> and may not be the latest version</p>"
2021-09-29 22:18:33 +00:00
for root, dirs, files in os.walk(path):
2021-10-02 14:37:51 +00:00
filename = "/".join(root.split("/")[1:])
2021-09-29 22:18:33 +00:00
index_content = "<ul>"
2021-10-02 14:37:51 +00:00
index_content += f"<a href='../'><li>../</li></a>"
2021-09-29 22:18:33 +00:00
for d in dirs:
2021-10-02 14:37:51 +00:00
if d.startswith("."):
shutil.rmtree(os.path.join(root, d))
else:
2021-09-29 22:18:33 +00:00
index_content += f"<a href='{d}'><li>{d}/</li></a>"
2021-10-02 14:37:51 +00:00
for file_name in files:
if file_name.startswith("."):
os.remove(f)
else:
f = os.path.join(root, file_name)
2021-09-29 22:18:33 +00:00
try:
with open(f, "r") as file:
content = file.read()
if f.endswith(".md"):
content = markdown.markdown(content)
2021-10-02 14:37:51 +00:00
content = content.replace("\"/", "\"/" + filename + "/")
else:
content = "<p><pre><code>" + escape(content) + "</code></p></pre>"
2021-09-29 22:18:33 +00:00
2021-10-02 14:37:51 +00:00
content += footer
content = format_file(page_templ, content, {
2021-09-29 22:18:33 +00:00
"name": name,
"commit": commit,
2021-10-02 14:37:51 +00:00
"url": url,
2021-09-29 22:18:33 +00:00
"filename": "/".join(f.split("/")[1:]),
"above": "/".join(f.split("/")[1:-1]),
})
with open(f + ".html", "w") as file:
file.write(content)
2021-10-02 14:37:51 +00:00
if file_name != "README.md":
os.remove(f)
index_content += f"<a href='{file_name}.html'><li>{file_name}</li></a>"
2021-09-29 22:18:33 +00:00
except UnicodeDecodeError:
2021-10-02 14:37:51 +00:00
index_content += f"<a href='{file_name}'><li>{file_name}</li></a>"
index_content += "</ul><hr>"
readme = os.path.join(root, "README.md")
if os.path.exists(readme):
with open(readme) as file:
readme_content = markdown.markdown(file.read())
#massive hack
readme_content = readme_content.replace("\"/", "\"/" + filename + "/")
index_content += readme_content
2021-09-29 22:18:33 +00:00
2021-10-02 14:37:51 +00:00
index_content += "<hr>"
2021-09-29 22:18:33 +00:00
2021-10-02 14:37:51 +00:00
index_content += footer
2021-09-29 22:18:33 +00:00
index_content = format_file(page_templ, index_content, {
"name": name,
"commit": commit,
2021-10-02 14:37:51 +00:00
"url": url,
"filename": filename,
2021-09-29 22:18:33 +00:00
"above": "/".join(root.split("/")[1:-1]),
})
with open(os.path.join(root,"index.html"), "w") as file:
file.write(index_content)
def create_repos():
try:
shutil.rmtree(os.path.join(dist, "git"))
except:
pass
git_path = os.path.join(dist, "git")
try:
os.makedirs(git_path)
except:
print("Already have git path")
for repo in get_repos():
2021-11-09 20:52:41 +00:00
repo = repo.strip()
2021-09-29 22:18:33 +00:00
print(repo)
2021-11-02 10:30:49 +00:00
name = ".".join(repo.split("/")[-1].split(".")[:-1])
2021-11-09 20:52:41 +00:00
os.system(f"mkdir -p /tmp/repos/{name} ;\
cd /tmp/repos/{name} ;\
git pull || git clone {repo} /tmp/repos/{name}")
2021-09-29 22:18:33 +00:00
2021-11-09 20:52:41 +00:00
command = subprocess.run(f"cd /tmp/repos/{name} && git log --pretty=format:'%h%x09%an%x09%ad%x09%s' --no-decorate -1", stdout=subprocess.PIPE, shell=True)
2021-09-29 22:18:33 +00:00
commit = command.stdout.decode()
2021-10-02 14:37:51 +00:00
traverse_repo(os.path.join(git_path, name), name, commit, repo)
2021-08-26 03:15:22 +00:00
2021-11-09 20:52:41 +00:00
os.system(f"cp -r /tmp/repos/* {dist}/git")
2021-09-29 22:18:33 +00:00
make()
create_repos()