summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordaniel-Jones <daniel@danieljon.es>2017-08-27 18:27:26 +0930
committerdaniel-Jones <daniel@danieljon.es>2017-08-27 18:27:26 +0930
commitf2c6c0a3842a11a41f1f3bbdaec510e8400ef147 (patch)
tree9e0892a0eb513f7a82b4f6dc8bb23bfc1d5f42bc
parent17677821301647ef4ba7a1d6187e8b60dedf6655 (diff)
downloadwebsite-f2c6c0a3842a11a41f1f3bbdaec510e8400ef147.tar.gz
website-f2c6c0a3842a11a41f1f3bbdaec510e8400ef147.zip
README.md
initial code commit
-rw-r--r--generatesite.py139
-rw-r--r--settings.cfg16
-rw-r--r--template.txt18
3 files changed, 173 insertions, 0 deletions
diff --git a/generatesite.py b/generatesite.py
new file mode 100644
index 0000000..5770852
--- /dev/null
+++ b/generatesite.py
@@ -0,0 +1,139 @@
+#!/usr/bin/env python3
+
+'''
+script to auto-generate my static website/blog/portfolio @ https://danieljon.es
+'''
+
+import configparser;
+import errno;
+import os;
+import glob;
+from shutil import copyfile;
+from tempfile import mkstemp;
+from shutil import move;
+from os import fdopen, remove;
+from time import gmtime, strftime;
+
+def replace(file_path, pattern, subst):
+ fh, abs_path = mkstemp();
+ with fdopen(fh, "w") as new_file:
+ with open(file_path) as old_file:
+ for line in old_file:
+ new_file.write(line.replace(pattern, subst));
+ remove(file_path);
+ move(abs_path, file_path);
+
+def generateindex():
+ template = cfg.get("output", "template");
+ outdir = cfg.get("output", "dir");
+ indexsrc = cfg.get("index", "src");
+ print("generating {}/index.html from {}".format(outdir, indexsrc));
+ copyfile(template, outdir + "/index.html");
+ with open(indexsrc, "r") as contentfile:
+ content = contentfile.read().replace('\n', '');
+ replace(outdir + "/index.html", "{TITLE}", cfg.get("index", "title"));
+ replace(outdir + "/index.html", "{INFO}", "Index");
+ replace(outdir + "/index.html", "{CONTENT}", content);
+ replace(outdir + "/index.html", "{TIME}", strftime("%Y-%m-%d %H:%M:%S", gmtime()));
+
+def generatepagebar(currentpage, pagecount):
+ print("generating page bar for {} total {}".format(currentpage, pagecount));
+ pages = "<center>page ";
+ for x in range(1, int(pagecount)+1):
+ if x == int(currentpage):
+ pages += "<strong><i><a href=" + str(x) + ".html>" + str(x) + "</a></i></strong> ";
+ else:
+ pages += "<a href=" + str(x) + ".html>" + str(x) + "</a> ";
+ pages += "</center>";
+ return pages;
+
+def generateportfolio():
+ template = cfg.get("output", "template");
+ outdir = cfg.get("output", "dir");
+ portfoliosrc = cfg.get("portfolio", "src");
+ print("generating {}/portfolio.html from {}".format(outdir, portfoliosrc));
+ copyfile(template, outdir + "/portfolio.html");
+ with open(portfoliosrc, "r") as contentfile:
+ content = contentfile.read().replace('\n', '');
+ replace(outdir + "/portfolio.html", "{TITLE}", cfg.get("portfolio", "title"));
+ replace(outdir + "/portfolio.html", "{INFO}", "Portfolio");
+ replace(outdir + "/portfolio.html", "{CONTENT}", content);
+ replace(outdir + "/portfolio.html", "{TIME}", strftime("%Y-%m-%d %H:%M:%S", gmtime()));
+
+
+
+def deletefiles(ddir):
+ # delete all blog files
+ for the_file in os.listdir(ddir):
+ file_path = os.path.join(ddir, the_file)
+ if os.path.isfile(file_path):
+ os.unlink(file_path);
+
+
+def generateblog():
+ template = cfg.get("output", "template");
+ outdir = cfg.get("output", "dir");
+ blogdir = cfg.get("blog", "dir");
+ blogsrc = cfg.get("blog", "srcdir");
+ perpage = cfg.get("blog", "perpage");
+ print("generating {}/{}/ from directory /{}/final".format(outdir, blogdir, blogsrc));
+ os.makedirs(outdir + "/" + blogdir, exist_ok=True);
+ os.makedirs(blogsrc + "/final", exist_ok=True);
+ # number of blog posts
+ postcount = len(glob.glob1(blogsrc, "*.txt"));
+ pagecount = postcount/int(perpage)+1;
+ print("{} posts, {} per page = {} pages".format(postcount, perpage, int(pagecount)));
+ folder = outdir + "/" + blogdir;
+ # delete generated blog files
+ deletefiles(outdir + "/" + blogdir);
+ # delete renamed blog files
+ deletefiles(blogsrc + "/final");
+ '''
+ copy our blog posts to blogsrc/final, rename them, 60 becomes 1, 59 becomes 2 etc
+ this is done because I am extremely lazy and had troubles doing it any other way.
+ sorry if you're reading this.
+ '''
+ #integrate this with the loop below it?
+ count = 1;
+ for x in range(int(postcount), 0, -1):
+ copyfile(blogsrc + "/" + str(x) + ".txt", blogsrc + "/final/" + str(count) + ".txt");
+ count += 1;
+ # generate pages
+ for x in range(1, int(pagecount) + 1):
+ copyfile(template, outdir + "/" + blogdir + "/" + str(x) + ".html");
+ with open(outdir + "/" + blogdir + "/" + str(x) + ".html", "r") as contentfile:
+ content = contentfile.read().replace('\n', '');
+ replace(outdir + "/" + blogdir + "/" + str(x) + ".html", "{TITLE}", cfg.get("blog", "title"));
+ replace(outdir + "/" + blogdir + "/" + str(x) + ".html", "{INFO}", "Blog");
+ replace(outdir + "/" + blogdir + "/" + str(x) + ".html", "{TIME}", strftime("%Y-%m-%d %H:%M:%S", gmtime()));
+ # place blog posts into their pages
+ count = 1;
+ cpage = 1;
+ page = "";
+ # count from 1 - perpage, add perpage to count, count from count - count + perpage
+ while count < postcount + 1:
+ for x in range(count, count + int(perpage)):
+ if count < postcount + 1:
+ with open(blogsrc + "/final/" + str(x) + ".txt") as contentfile:
+ content = contentfile.read().replace('\n', '');
+ page += content + "<hr>";
+ count += 1;
+ if cpage <= pagecount:
+ page += generatepagebar(str(cpage), str(int(pagecount)));
+ page = generatepagebar(str(cpage), str(int(pagecount))) + page;
+ replace(outdir + "/" + blogdir + "/" + str(cpage) + ".html", "{CONTENT}", page);
+ page = "";
+ cpage += 1;
+ '''
+ for x in range(1, postcount + 1):
+ with open(blogsrc + "/" + str(x) + ".txt", "r") as t:
+ print(t.read());
+ '''
+
+if __name__ == "__main__":
+ cfg = configparser.ConfigParser();
+ cfg.read("settings.cfg");
+ os.makedirs(cfg.get("output", "dir"), exist_ok=True);
+ generateindex();
+ generateblog();
+ generateportfolio();
diff --git a/settings.cfg b/settings.cfg
new file mode 100644
index 0000000..c094212
--- /dev/null
+++ b/settings.cfg
@@ -0,0 +1,16 @@
+[output]
+template = template.txt
+dir = output
+[index]
+title = interesting stuff
+src = content/index.txt
+[portfolio]
+title = daniel's portfolio
+src = content/portfolio.txt
+[blog]
+title = daniel's blog
+# blog directory
+dir = blog
+srcdir = content/blog
+# number of posts per page
+perpage = 10
diff --git a/template.txt b/template.txt
new file mode 100644
index 0000000..cac279f
--- /dev/null
+++ b/template.txt
@@ -0,0 +1,18 @@
+<html>
+<head>
+<title>{TITLE}</title>
+</head>
+
+<body bgcolor="#222" text="#eee" link="#3F51B5" vlink="#3F51B5">
+<center>
+<h1>danieljon.es</h1>
+<a href="/">index</a> <a href="/blog/1.html">blog</a> <a href="/portfolio.html">portfolio</a> </font> <br /> <br />
+<a href="https://reddit.com/u/daniel_j-">reddit</a>
+<a href="http://steamcommunity.com/id/imnotstraightatall/">steam</a>
+<a href="https://github.com/daniel-Jones">github</a>
+<h1>{INFO}</h1>
+</center>
+{CONTENT}
+<center><br /><font size="2"> page generated on {TIME} </font></center>
+</body>
+</html>