summaryrefslogtreecommitdiff
path: root/build.py
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2021-09-29 23:18:33 +0100
committerdavidovski <david@davidovski.xyz>2021-09-29 23:18:33 +0100
commiteed4ab6ebf07a696ed13bfeb70ccd73784208748 (patch)
treef1b4735e7ea23a10c6ff249cced8281d8d4bc660 /build.py
parent410f2b19877df9fe39eb2ee80d6a4403072a3ae7 (diff)
removed html
Diffstat (limited to 'build.py')
-rw-r--r--build.py128
1 files changed, 123 insertions, 5 deletions
diff --git a/build.py b/build.py
index 45d8982..e922ac9 100644
--- a/build.py
+++ b/build.py
@@ -2,6 +2,9 @@ import markdown
import os
import time
import shutil
+import subprocess
+from html import escape
+
from const import *
@@ -60,10 +63,22 @@ def make():
os.makedirs(os.path.join(dist, "entries"))
except:
print("Already have content")
- shutil.rmtree(os.path.join(dist, "src"))
- shutil.rmtree(os.path.join(dist, "images"))
- shutil.copytree(source, os.path.join(dist, "src"))
- shutil.copytree(images, os.path.join(dist, "images"))
+ 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
pages = listPages()
@@ -117,8 +132,111 @@ def make():
rss_templ.replace("%items%", itemsXML)
)
+ for f in os.listdir(resources):
+ shutil.copy(os.path.join(resources, f), dist)
+
print(f"built in {len(pages)} pages")
-make()
+
+def get_repos():
+ repos = []
+ if os.path.exists("git_repos.txt"):
+ with open("git_repos.txt", "r") as file:
+ repos = file.read().split("\n")[:-1]
+ 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
+
+def format_file(page_templ, content, v):
+ return page_templ.replace("%title%", v["name"])\
+ .replace("%up%", v["above"])\
+ .replace("%filename%", v["filename"])\
+ .replace("%commit%", str(v["commit"]))\
+ .replace("%content%", content)
+
+
+def traverse_repo(path, name, commit):
+ page_templ = getTemplateHTML("page.html")
+ page_templ = page_templ.replace("%content%", getTemplateHTML("file.html"))
+
+ for root, dirs, files in os.walk(path):
+ index_content = "<ul>"
+
+ for d in dirs:
+ if not d.startswith("."):
+ index_content += f"<a href='{d}'><li>{d}/</li></a>"
+
+ for f in files:
+ if not f.startswith("."):
+ index_content += f"<a href='{f}.html'><li>{f}</li></a>"
+ f = os.path.join(root,f)
+
+ try:
+ print(f"editing {f}...")
+ with open(f, "r") as file:
+ content = file.read()
+
+ if f.endswith(".md"):
+ content = markdown.markdown(content)
+
+ content = format_file(page_templ, "<p><pre><code>" + escape(content) + "</code></p></pre>", {
+ "name": name,
+ "commit": commit,
+ "filename": "/".join(f.split("/")[1:]),
+ "above": "/".join(f.split("/")[1:-1]),
+ })
+
+ with open(f + ".html", "w") as file:
+ file.write(content)
+ except UnicodeDecodeError:
+ pass
+
+ index_content += "</ul>"
+
+ index_content = format_file(page_templ, index_content, {
+ "name": name,
+ "commit": commit,
+ "filename": "/".join(root.split("/")[1:]),
+ "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():
+ print(repo)
+
+ os.system(f"cd {dist}/git; git clone {repo}")
+ name = ".".join(repo.split("/")[-1].split(".")[:-1])
+
+ command = subprocess.run(f"cd {dist}/git/{name} && git log --pretty=format:'%h%x09%an%x09%ad%x09%s' --no-decorate -1", stdout=subprocess.PIPE, shell=True)
+
+ commit = command.stdout.decode()
+
+ traverse_repo(os.path.join(git_path, name), name, commit)
+make()
+create_repos()