Browse Source

markdown post support

master
hex 3 years ago
parent
commit
c4536e02e1
1 changed files with 40 additions and 5 deletions
  1. 40
    5
      stagen.py

+ 40
- 5
stagen.py View File

@@ -1,6 +1,9 @@
from jinja2 import Template, Environment, PackageLoader, select_autoescape, FileSystemLoader, PrefixLoader
import os
import errno
import markdown
import codecs
import time

env = Environment(
loader=PrefixLoader({
@@ -10,6 +13,13 @@ env = Environment(
)

def ensure_dir_exists(path):
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise

def filter_templates(template):
if template.startswith("content/") and template.endswith('.html'):
return True
@@ -21,12 +31,37 @@ for template_name in content_template_names:
fname = template_name[len("content/"):] # trim off front of path
fname = os.path.join("output", fname)

try:
os.makedirs(os.path.dirname(fname))
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
ensure_dir_exists(os.path.dirname(fname))

f = open(fname, 'w')
f.write(template.render())

ensure_dir_exists("posts")
md = markdown.Markdown(extensions = ['markdown.extensions.meta'])
post_files = os.listdir("posts")
post_list = []

for post_file in post_files:
text = codecs.open(os.path.join("posts", post_file), mode="r", encoding="utf-8").read()
html = md.convert(text)

postinfo = dict()
postinfo['url'] = os.path.join("/posts/" + md.Meta['slug'][0] + '.html')
postinfo['date'] = time.strptime(md.Meta['date'][0], "%Y-%m-%d")
postinfo['title'] = md.Meta['title'][0]
post_list.append(postinfo)

fname = os.path.join("output/posts", md.Meta['slug'][0] + '.html')
ensure_dir_exists(os.path.dirname(fname))

template = env.get_template("templates/post.html")
page = template.render(html=html, meta=md.Meta)

f = open(fname, 'w')
f.write(page)

#sort post list by date
sorted_post_list = sorted(post_list, key=lambda k: k['date'], reverse=True)
template = env.get_template("templates/posts.html")
f = open("output/posts.html", 'w')
f.write(template.render(posts=sorted_post_list))

Loading…
Cancel
Save