summaryrefslogtreecommitdiffstats
path: root/Makefile
blob: 9e389b3d945347c62ad1dcd40a64f16c04d177f7 (plain) (blame)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
export DEPLOY_TARGET = vulpes@94.130.78.123:/srv/http/volpeon.ink/
export MODE = prod

-include Env.mk

#
# FILE GROUPS
#

PANDOC_FILTERS        := $(patsubst %,--lua-filter %,$(wildcard filters/*.lua))
GLOBAL_METADATA       := $(patsubst %,--metadata-file %,$(wildcard metadata/*.yaml))

CONTENT_SRC           := $(shell find content -type f -name "*.md")
FEED_SRC              := $(shell grep -l "^feed: *true$$" $(CONTENT_SRC))
CONTENT_TEMPLATES_SRC := $(shell find templates -type f -name "*.html")
FEED_TEMPLATES_SRC    := $(shell find templates -type f -name "*.xml")
CSS_SRC               := $(shell find assets/css -type f -name "*.scss")

CONTENT_META          := $(patsubst content/%.md,.cache/meta/%.json,$(CONTENT_SRC))
CONTENT_FILES         := $(patsubst content/%.md,out/%.html,$(CONTENT_SRC))
FEED_FILES            := $(patsubst content/%.md,out/%.xml,$(FEED_SRC))
FONT_FILES            := $(patsubst assets/fonts/%.ttf,out/%.woff2,$(wildcard assets/fonts/*.ttf))
CSS_FILES             := $(patsubst assets/css/%.scss,out/%.css,$(wildcard assets/css/style.scss))
STATIC_FILES          := $(patsubst content/%,out/%,$(shell find content -type f ! -name "*.md"))
TOP_STATIC_FILES      := $(patsubst content/%,out/%,$(shell find content -maxdepth 1 -type f ! -name "*.md"))
ASSET_FILES           := $(FONT_FILES) $(CSS_FILES) out/icons.svg $(TOP_STATIC_FILES)

#
# TARGETS
#

all: content_meta content_files feed_files font_files css_files static_files

content_meta:  $(CONTENT_META)
content_files: $(CONTENT_FILES)
feed_files:    $(FEED_FILES)
font_files:    $(FONT_FILES)
css_files:     $(CSS_FILES)
static_files:  $(STATIC_FILES)

#
# RULES
#

.SECONDEXPANSION:

namespace = $(patsubst %/index,%,$(basename $(patsubst $(2)%,%,$(1))))

subpages = $(patsubst content/%.md,.cache/meta/%.json, \
	$(shell test -d $(patsubst .cache/meta%,content%,$(1)) && find $(patsubst .cache/meta%,content%,$(1)) -maxdepth 1 -type f -name "*.md" ! -name "index.md") \
	$(shell test -d $(patsubst .cache/meta%,content%,$(1)) && find $(patsubst .cache/meta%,content%,$(1)) -mindepth 2 -maxdepth 2 -type f -name "index.md"))

.cache/meta/%.json: content/%.md $$(call subpages,$$(call namespace,$$@,)) filters/*.lua scripts/content.lua scripts/subpages.jq scripts/metadata.json | .cache/meta
	$(info [META] $< -> $@)
	
	mkdir -p $(@D)
	$(eval PAGES_FILES = $(filter .cache/meta/%.json,$^))
	$(eval NAMESPACE = $(call namespace,$@,.cache/meta))
	$(if $(PAGES_FILES),jq -s --arg namespace "$(NAMESPACE)" -f scripts/subpages.jq $(PAGES_FILES) > "$@.pages",echo "{ \"pages\": [] }" > "$@.pages")
	pandoc \
		-f markdown-citations-implicit_figures \
		-t html5 \
		--wrap preserve \
		--no-highlight \
		--metadata-file "$@.pages" \
		--template scripts/metadata.json \
		--metadata namespace="$(NAMESPACE)" \
		--metadata file_out="$(patsubst .cache/meta/%.json,/%.html,$@)" \
		$(PANDOC_FILTERS) \
		-o "$@.meta" "$<"
	rm "$@.pages"
	pandoc \
		-f markdown-citations-implicit_figures \
		-t html5 \
		--wrap preserve \
		--lua-filter scripts/content.lua \
		$(GLOBAL_METADATA) \
		--metadata-file "$@.meta" \
		$(PANDOC_FILTERS) \
		-o "$@.content" "$<"
	jq '. + { content: $$content }' --rawfile content "$@.content" "$@.meta" > "$@"
	rm "$@.meta"
	rm "$@.content"

.cache/meta.json: .cache/meta/index.json scripts/metadata.lua | .cache
	$(info [META] $@)
	
	mkdir -p $(@D)
	pandoc \
		-f markdown-citations-implicit_figures \
		-t html5 \
		--wrap preserve \
		--no-highlight \
		--lua-filter scripts/metadata.lua \
		$(GLOBAL_METADATA) \
		--metadata-file "$<" \
		--metadata build.mode="$(MODE)" \
		--template scripts/metadata.json \
		-o "$@.meta" "$<"
	jq '{ tree: . }' "$@.meta" > "$@"
	rm "$@.meta"

.cache/assets.json: $(ASSET_FILES) | .cache
	$(info [ASET] $@)

	sha256sum $^ | sed -r 's/([0-9a-f]+) +out\/(.*)/{ "\2": "\/\2?\1" }/' | jq '{ assets: [inputs] | add | with_entries( .key |= gsub( "\\."; "_") ) }' > "$@"

out/%.html: content/%.md .cache/meta.json .cache/assets.json $(CONTENT_TEMPLATES_SRC) metadata/*.yaml filters/*.lua scripts/page.lua | out
	$(info [MARK] $< -> $@)
	
	mkdir -p $(@D)
	$(eval NAMESPACE = $(call namespace,$@,out))
	pandoc \
		-f markdown-citations-implicit_figures \
		-t html5 \
		--wrap preserve \
		--no-highlight \
		--template templates/base.html \
		--lua-filter scripts/page.lua \
		$(GLOBAL_METADATA) \
		--metadata-file .cache/assets.json \
		--metadata-file .cache/meta.json \
		--metadata build.namespace="$(NAMESPACE)" \
		--metadata build.file_out="$(patsubst out/%,/%,$@)" \
		$(PANDOC_FILTERS) \
		-o "$@" "$<"

out/%.xml: content/%.md .cache/meta.json $(FEED_TEMPLATES_SRC) metadata/*.yaml filters/*.lua scripts/page.lua | out
	$(info [FEED] $< -> $@)
	
	mkdir -p $(@D)
	$(eval NAMESPACE = $(call namespace,$@,out))
	pandoc \
		-f markdown-citations-implicit_figures \
		-t html5 \
		--wrap preserve \
		--no-highlight \
		--template templates/feed.xml \
		--lua-filter scripts/page.lua \
		$(GLOBAL_METADATA) \
		--metadata-file .cache/meta.json \
		--metadata build.namespace="$(NAMESPACE)" \
		--metadata build.file_out="$(patsubst out/%,/%,$@)" \
		$(PANDOC_FILTERS) \
		-o "$@" "$<"

out/%/thumbnail.png: content/%/thumbnail.png | out
	$(info [THMB] $< -> $@)

	mkdir -p $(@D)
	convert -thumbnail 550x280^ -gravity center -extent 550x280 "$<" png:- | pngquant - > "$@"

out/%/thumbnail.jpg: content/%/thumbnail.jpg | out
	$(info [THMB] $< -> $@)

	mkdir -p $(@D)
	convert -thumbnail 550x280^ -gravity center -extent 550x280 "$<" ppm:- | cjpeg -quality 80 > "$@"

out/%.woff2: assets/fonts/%.ttf assets/fonts/glyphs.txt | out
	$(info [FONT] $< -> $@)
	
	pyftsubset "$<" \
		--text-file="assets/fonts/glyphs.txt" \
		--layout-features+=ss09,dlig \
		--flavor="woff2" \
		--output-file="$@"

out/%.css: assets/css/%.scss $(CSS_SRC) | out
	$(info [SCSS] $< -> $@)

	sass --load-path=node_modules "$<" | ./node_modules/.bin/postcss --use autoprefixer --no-map -o "$@"

out/icons.svg: | out
	$(info [ICON] $@)

	node ./node_modules/iro-icons/scripts/create_sprite.js \
		asterisk \
		arrow-left \
		arrow-right \
		arrow-up-right \
		shuffle \
		envelope \
		hash \
		messages \
		parallelogram \
		globe \
		image \
		graph \
		git-branch \
		smile \
		link \
		x \
		chevron-left \
		chevron-right \
		"$@"

out/%: content/% | out
	$(info [COPY] $< -> $@)

	mkdir -p $(@D)
	cp "$<" "$@"

.cache/hash: | .cache
	mkdir -p .cache/hash

.cache/meta: | .cache
	mkdir -p .cache/meta

.cache:
	mkdir -p .cache

out:
	mkdir -p out

#
# UTILITIES
#

watch: all
	while true; do \
		inotifywait -qr -e modify -e create -e delete -e move assets content filters metadata scripts templates; \
		$(MAKE); \
	done

serve: all
	python -m http.server --bind 127.0.0.1 --directory out 8000

dev: watch serve

compress: all
	pigz -R -k -9 -- `find out -type f -iregex '.*\\.\\(css\\|js\\|json\\|html\\|xml\\|txt\\|svg\\|ico\\)'` 
	brotli -k -- `find out -type f -iregex '.*\\.\\(css\\|js\\|json\\|html\\|xml\\|txt\\|svg\\|ico\\|woff\\)'`

deploy: compress
	rsync --progress --stats -rvz --delete out/ "$(DEPLOY_TARGET)"

clean:
	rm -rf out

.PHONY: all watch serve dev deploy clean