2017-02-13 23:19:26 +00:00
|
|
|
# Make Site
|
|
|
|
# author: Jonathan Bernard <jonathan@jdbernard.com>
|
|
|
|
#
|
|
|
|
# Stupid-simple static site generator. Operates on the current working
|
|
|
|
# directory. Outputs rendered HTML to a directory named 'rendered'. Reads in an
|
|
|
|
# HTML template from a file 'template.html'. Walks the current working directory
|
|
|
|
# looking for files with the '.rst' extension, splits out optional front-matter
|
|
|
|
# (JSON), compiles the rest into HTML fragments, stuffs the fragments into the
|
|
|
|
# template and write an HTML file at the same relative path in the 'rendered'
|
|
|
|
# directory with the same filename (extension changed to .html).
|
|
|
|
|
2017-02-13 23:49:39 +00:00
|
|
|
import json, nre, os, osproc
|
2017-02-13 23:19:26 +00:00
|
|
|
from strutils import endsWith, rfind
|
|
|
|
|
|
|
|
# Read in the template
|
|
|
|
let htmlTemplate = readFile("template.html")
|
|
|
|
let contentKey = re"<%CONTENT%>"
|
2017-02-13 23:49:39 +00:00
|
|
|
let mdExtRe = re"md$"
|
|
|
|
let tempFile = "temp.make_site.md"
|
2017-02-13 23:19:26 +00:00
|
|
|
|
|
|
|
# Make our output directory if necessary.
|
|
|
|
if not dirExists("rendered"): createDir("rendered")
|
|
|
|
|
2017-02-13 23:49:39 +00:00
|
|
|
# Walk the directory looking for .md files.
|
2017-02-13 23:19:26 +00:00
|
|
|
for file in walkDirRec("."):
|
2017-02-13 23:49:39 +00:00
|
|
|
if not file.endsWith(".md"): continue
|
2017-02-13 23:19:26 +00:00
|
|
|
|
2017-02-13 23:49:39 +00:00
|
|
|
echo "> Rendering " & file
|
2017-02-13 23:19:26 +00:00
|
|
|
var renderedTemplate = htmlTemplate
|
2017-02-13 23:49:39 +00:00
|
|
|
var mdDoc: string
|
|
|
|
let outputFile = "rendered/" & file.replace(mdExtRe, "html")
|
2017-02-13 23:19:26 +00:00
|
|
|
let outputDir = "rendered/" & file[0..file.rfind('/')]
|
|
|
|
|
|
|
|
if not existsDir(outputDir): createDir(outputDir)
|
|
|
|
|
|
|
|
# Split out front-matter
|
|
|
|
let docSplit = file.readFile.split(re"\+\+\+", 2)
|
|
|
|
|
|
|
|
# Parse the front-matter (if there is any)
|
|
|
|
if docSplit.len > 1:
|
|
|
|
let metadata = json.parseJson(docSplit[0])
|
2017-02-13 23:49:39 +00:00
|
|
|
mdDoc = docSplit[1]
|
2017-02-13 23:19:26 +00:00
|
|
|
for key, value in metadata:
|
|
|
|
let replKey = re("<%" & key & "%>")
|
|
|
|
renderedTemplate = htmlTemplate.replace(replKey, value.getStr)
|
2017-02-13 23:49:39 +00:00
|
|
|
else: mdDoc = docSplit[0]
|
2017-02-13 23:19:26 +00:00
|
|
|
|
|
|
|
# Compile the file into an HTML fragment
|
2017-02-13 23:49:39 +00:00
|
|
|
writeFile(tempFile, mdDoc)
|
|
|
|
let htmlDoc = execProcess("markdown " & tempFile)
|
2017-02-13 23:19:26 +00:00
|
|
|
renderedTemplate = renderedTemplate.replace(contentKey, htmlDoc)
|
|
|
|
|
|
|
|
writeFile(outputFile, renderedTemplate)
|
2017-02-13 23:49:39 +00:00
|
|
|
|
|
|
|
removeFile(tempFile)
|