From 057ccd0a39eaf7ef3c9e6cf3e70044cac14c6bcf Mon Sep 17 00:00:00 2001 From: Oxore Date: Sun, 29 Sep 2019 18:35:42 +0300 Subject: Implement basic template based static page generator --- .gitignore | 1 + build.py | 43 ++++++++++++++ oreily-memes.html | 123 ----------------------------------------- templates/oreily-memes.html.j2 | 22 ++++++++ 4 files changed, 66 insertions(+), 123 deletions(-) create mode 100644 build.py delete mode 100644 oreily-memes.html create mode 100644 templates/oreily-memes.html.j2 diff --git a/.gitignore b/.gitignore index 225c381..dfbedba 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.png *.jpg +oreily-memes.html diff --git a/build.py b/build.py new file mode 100644 index 0000000..eb9fdbb --- /dev/null +++ b/build.py @@ -0,0 +1,43 @@ +#!/usr/bin/python + +import os +from jinja2 import Template + +def get_memes(directory): + memes = [] + files = os.listdir(directory + "/orig/") + files.sort() + for f in files: + meme = { + "orig": "./{}/orig/{}".format(directory, f), + "thumb": "./{}/thumb/{}".format(directory, f), + } + memes.append(meme) + return memes + +def bake_thumbnails(memes): + for meme in memes: + if not os.access(meme.get("thumb"), os.F_OK): + os.system("convert {} -resize 320x320^\> {}" + .format(meme.get("orig"), meme.get("thumb"))) + +def render_template(template_fname, output_fname, memes): + template_file = open(template_fname, "r") + output_file = open(output_fname, "w") + template = Template(template_file.read()) + output_file.write(template.render(memes=memes)) + output_file.close() + + +outdir = "." +memes_dir = "oreily-memes" +# The "om_" prefix means "oreily memes" +om_template_fname = "templates/oreily-memes.html.j2" +om_outdir = outdir +om_output_fname = om_outdir + "/" + "oreily-memes.html" + +if not os.access(outdir, os.F_OK): + os.mkdir(outdir, int('0775', 8)) +memes = get_memes(memes_dir) +bake_thumbnails(memes) +render_template(om_template_fname, om_output_fname, memes) diff --git a/oreily-memes.html b/oreily-memes.html deleted file mode 100644 index 4cae627..0000000 --- a/oreily-memes.html +++ /dev/null @@ -1,123 +0,0 @@ - - - - O'Reily Memes - - - - -

O'Reily Memes

-

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- -

Related links

-

- r/orlybooks/ -

- - - diff --git a/templates/oreily-memes.html.j2 b/templates/oreily-memes.html.j2 new file mode 100644 index 0000000..b1a8ce0 --- /dev/null +++ b/templates/oreily-memes.html.j2 @@ -0,0 +1,22 @@ + + + + O'Reily Memes + + + + +

O'Reily Memes

+

{% for meme in memes %} + {% endfor %} +

+ +

Related links

+

+ r/orlybooks/ +

+ + + -- cgit v1.2.3