summaryrefslogtreecommitdiff
path: root/Makefile
blob: e985d346d1ccb7a35cf08fb72511d26a71788551 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# BENZIN Makefile

include config.mk

# Directories
SRC                  ?= src
TEMPLATES            ?= templates
OUT                  ?= out

# User supplied variables
WEBSITE_URL          ?= https://example.com
WEBSITE_TITLE        ?= Example Website
CONVERT              ?= pandoc --from=gfm+emoji --to=html --section-divs --shift-heading-level-by=1
EMOJI_SIZE           ?= 20
EMOJI_STYLE          ?= margin-bottom: -4px;
BLOG_PREVIEW_MARKER  ?= Recent blog posts

# File locations
PAGE_TEMPLATE        ?= templates/page.html
OPENRING_TEMPLATE    ?= templates/openring.html
RSS_TEMPLATE         ?= templates/rss.xml
RSS_ITEM_TEMPLATE    ?= templates/rss_item.xml
OPENRING_URLS        ?= src/urls
RSS                  ?= out/blog/index.xml

# Sources
BLOG_SOURCES=$(wildcard src/blog/*.md)
EMOJI_SOURCES=$(wildcard src/public/emoji/*)
PUBLIC_SOURCES=$(shell find src/public -type f)
SOURCES=$(shell find src -type f -name "*.md")

# Outputs
TMP_HTML=$(patsubst src/%.md, tmp/%.content.html, $(SOURCES))
HTML=$(patsubst src/%.md, out/%.html, $(SOURCES))
OPENRING_FOOTER=$(patsubst templates/%, tmp/%, $(OPENRING_TEMPLATE))
BLOG_XML=$(patsubst src/%.md, tmp/%.xml, $(filter-out src/blog/index.md, $(BLOG_SOURCES)))
PUBLIC=$(patsubst src/%, out/%, $(PUBLIC_SOURCES))


.PHONY: clean all serve
.SECONDARY: $(TMP_HTML) $(BLOG_XML)


all: $(HTML) $(RSS) $(PUBLIC)

out/%.html: tmp/%.content.html tmp/%.meta.title tmp/%.meta.description $(PAGE_TEMPLATE) $(OPENRING_FOOTER)
	@mkdir -p $(@D)
	sed "/<\/main>/e cat $< $(OPENRING_FOOTER)" $(PAGE_TEMPLATE) > $@
	sed -i \
		-e '/<head>/{' \
			-e 'a <title>' -e "r tmp/$*.meta.title" -e "a | $(WEBSITE_TITLE) <\/title>" \
			-e 'a <meta name="description" content="' -e "r tmp/$*.meta.description" -e 'a "/>' \
		-e '}' $@

tmp/%.content.html: src/%.md tmp/emoji.sed
	@mkdir -p $(@D)
	sed -e "s/.md)/.html)/g" -f tmp/emoji.sed $< | $(CONVERT) > $@

out/index.html: tmp/index.content.html $(PAGE_TEMPLATE)
	@mkdir -p $(@D)
	sed "/<\/main>/e cat $<" $(PAGE_TEMPLATE) > $@
	sed -i \
		-e '/<head>/{' \
			-e "a <title>$(WEBSITE_TITLE)<\/title>" \
		-e '}' $@

tmp/index.content.html: src/index.md tmp/emoji.sed tmp/blog/preview.md
	@mkdir -p $(@D)
	sed -e "/$(BLOG_PREVIEW_MARKER)/r tmp/blog/preview.md" $< \
		| sed -e "s/.md)/.html)/g" -f tmp/emoji.sed \
		| $(CONVERT) > $@

tmp/%.meta.title: src/%.md
	@mkdir -p $(@D)
	sed '/^#/q' $< | sed 's/#* //; s/:.*://;' > $@

tmp/%.meta.description: src/%.md
	@mkdir -p $(@D)
	sed '2,/^$$/!d' $< > $@

tmp/blog/%.meta.date: src/blog/%.md
	@mkdir -p $(@D)
	@date --rfc-email -d $* > $@

tmp/blog/preview.md: src/blog/index.md
	@mkdir -p $(@D)
	sed -n "s/^-/ -/; s|(|(blog/| ; /^ -/p" $< | head -n 4 > $@

$(RSS): $(BLOG_XML) $(RSS_TEMPLATE)
	@mkdir -p $(@D)
	sed "/<\/channel>/e cat $(BLOG_XML)" $(RSS_TEMPLATE) > $@
	sed -i \
		-e '/<channel>/{' \
			-e "a <title>$(WEBSITE_TITLE)<\/title>" \
			-e "a <link>$(WEBSITE_URL)<\/link>" \
			-e "a <lastPubDate>$$(date --rfc-email)<\/lastPubDate>" \
		-e '}' $@

tmp/%.xml: tmp/%.content.html tmp/%.meta.title tmp/%.meta.date $(RSS_ITEM_TEMPLATE)
	@mkdir -p $(@D)
	sed "/CDATA/r $<" $(RSS_ITEM_TEMPLATE) > $@
	sed -i \
		-e '/<item>/{' \
			-e 'a <title>' -e "r tmp/$*.meta.title" -e 'a <\/title>' \
			-e 'a <pubDate>' -e "r tmp/$*.meta.date" -e 'a <\/pubDate>' \
			-e "a <link>$(WEBSITE_URL)/$*.html<\/link>" \
		-e '}' $@

$(OPENRING_FOOTER): $(OPENRING_TEMPLATE) $(OPENRING_URLS)
	@mkdir -p $(@D)
	openring $$(sed '/^#/d; s/^/-s /' $(OPENRING_URLS)) < $< > $@

out/public/%: src/public/%
	@mkdir -p $(@D)
	@cp $^ $@

out/public/emoji/%.png: src/public/emoji/%.png
	@mkdir -p $(@D)
	convert $< -resize $(EMOJI_SIZE)x$(EMOJI_SIZE) $@

tmp/emoji.sed: $(EMOJI_SOURCES)
	@mkdir -p $(@D)
	for emoji in $^; do\
		basename=$$(basename $$emoji); \
		name=$${basename%.*}; \
		echo "s|:$${name}:|<img src=\"/public/emoji/$${basename}\" alt=\"$${name}-emoji\" height=\"$(EMOJI_SIZE)\" width=\"$(EMOJI_SIZE)\" style=\"$(EMOJI_STYLE)\">|g;" >> $@; \
	done

serve: $(HTML)
	quark -h 127.0.0.1 -p 8000 -d out -l

clean:
	rm -rf tmp out/*