You May Just Need A Makefile
& A Ruby Script
When I decided to add/write some content over there, I knew that I didn’t wanted to make a blog, just more or less a collection of lose pages.
I wanted something simple, that doesn’t get ever in my way.
So I just decided to do a Makefile
. To create a new page/hiearchy in this website,
all I have to do is create folder(s) and .md
files in src/
.
I type make
and it updates what needs to be updated, copy file to their deployement destination (Local or S3),
and purge my Varnish frontend cache. Easy.
Dependencies
make
(BSD/Unix, GNU Make not required)ruby
and thekramdown
,yaml
gemsminify
- optional:
git
- optional:
rsync
- optional:
aws
- optional:
varnishadm
The Code
cat Makefile
#BUCKET=rndm-www
#S3_HOST="${BUCKET}.s3.something"
RSYNC_DEST="/mnt/front/www/random.sh/"
VARNISH_SECRET_FILE="/var/db/varnish.secret"
VARNISH_HOST="front.stairway.internal.random.sh:8081"
VARNISH_PURGE_HOST="random.sh"
URL="https://random.sh"
SITE_NAME=random.sh
GIT_DESCR!=git describe --all --always --dirty --long --broken
LAYOUT=includes/layout.tpl.html
STYLE=src/site.css
STYLE_LIBS=includes/css/modern-normalize.min.css
MARKDOWNFILES!=find src/ -type f -name '*.md'
ASSETS!=find src/ -type f -not -name '*.md' -not -name "site.css"
.export SITE_NAME BUCKET S3_HOST URL LAYOUT GIT_DESCR
.MAIN: all
all: sync
build: .build
sync: .published
resync:
-rm .published
${MAKE} sync
clean:
-rm -r build/*
.PHONY: all build sync resync clean
# -- CSS
${STYLE:S/^src/build/}: ${STYLE} ${STYLE_LIBS}
minify --bundle ${STYLE_LIBS} ${STYLE} -o ${.TARGET}
BUILDS+=${STYLE:S/^src/build/}
# -- MD
.for __md in ${MARKDOWNFILES}
#.info "(markdown) ${__md} => ${__md:S/^src/build/:S/.md$/.html/}"
${__md:S/^src/build/:S/.md$/.html/}: ${__md} bin/render-markdown ${LAYOUT} ${STYLE:S/^src/build/}
@mkdir -p "${.TARGET:H}"
bin/render-markdown "${LAYOUT}" "${__md}" > "${.TARGET}"
BUILDS+=${__md:S/^src/build/:S/.md$/.html/}
.endfor
# -- Assets
.for __s in ${ASSETS}
#.info "(static) ${__s} => ${__s:S/^src/build/}"
${__s:S/^src/build/}: ${__s}
@mkdir -p "${.TARGET:H}"
ln -s ${__s:tA} ${.TARGET}
BUILDS+=${__s:S/^src/build/}
.endfor
.build: $(BUILDS)
@touch ${.TARGET}
.published: .build
#aws s3 sync --delete "build/" "s3://${BUCKET}/"
rsync --delete --copy-links -avz build/ "${RSYNC_DEST}"
varnishadm -S "${VARNISH_SECRET_FILE}" -T "${VARNISH_HOST}" ban "req.http.host ~ ${VARNISH_PURGE_HOST}"
@touch ${.TARGET}
cat includes/layout.tpl.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="/site.css?2022-11-30.6">
<title>%{title}</title>
</head>
<body>
<header>
<h1><a href="/">./random.sh</a></h1>
</header>
<article>
%{body}
</article>
<footer>
</footer>
</body>
<!-- generated_at="%{built_at}" git="%{git_descr}" from="%{source_file}" -->
</html>
cat bin/render-markdown
#!/usr/bin/env ruby
require 'kramdown'
require 'yaml'
@site_name = ENV["SITE_NAME"]
@url = ENV["URL"]
@git_descr = ENV["GIT_DESCR"]
template = ARGV.shift
source_file = ARGV.first
source = ARGF.read
meta = {}
if (md = source.match(/^(?---\s*\n.*?\n?)^(---\s*$\n?)/m))
source = md.post_match
meta = YAML.load(md[:metadata]) || {}
end
body = Kramdown::Document.new(source).to_html
if meta["title"]
title="#{@site_name} / #{meta["title"]}"
else
title=@site_name
end
puts(File.read(template) % {
body: body, title: title, built_at: Time.now.utc, source_file: source_file,
site_name: @site_name, url: @url, git_descr: @git_descr
})
</code></pre>