grid.in.th/Makefile -rw-r--r-- 13.5 KiB View raw
                                                                                
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# This Makefile generates my personal blog at https://grid.in.th/
#
# The file is designed to be used with BSDmake. It is not designed to be
# reusable, but if you found any parts useful, the file is licensed under
# BSD-3-Clause.
#
# If you have any questions, contact <sirn@ogsite.net>.
#
#   "Jesus. That’s hideous"
#   -- a friend, on the whole idea of using Makefile as static site generator

# =============================================================================
# Configurations
# =============================================================================

# Configure site metadata:
#
# SITE_TITLE	title of the site
# SITE_DESC	description of the site
# SITE_AUTHOR	name of the site primary author
# SITE_EMAIL	email of the primary author
# SITE_ROOT	root url prefix for the site
#
SITE_TITLE=	Gridth
SITE_DESC=	Commentary about tech, life and universe
SITE_AUTHOR=	Kridsada Thanabulpong
SITE_EMAIL=	sirn@ogsite.net
SITE_ROOT=	https://grid.in.th
SITE_UPDATED!=	date +"%Y-%m-%dT%H:%M:%S%Z"

# Configure locations:
#
# SITE_SRC 	where the source files (articles, images, etc.) are stored.
# SITE_DST 	where the compiled files are stored.
# SITE_CACHE 	where the intermediate files are stored.
#
SITE_SRC= 	src/
SITE_DST= 	dst/
SITE_CACHE= 	cache/

# Configure content types:
#
# CT_INDEX	content type to display on index page
# CT_*		any path matching * will be assigned a content type after
#		the equal sign, e.g. TMPL_src/2016 will assign all content
#		under src/2016 directory to have "post" content type.
#
CT_INDEX=	post

# Treat all pages under src/20xx as a post.
_POST_SRCS!=	find ${SITE_SRC} -path '${SITE_SRC}20[0-9][0-9]'
.for _S in ${_POST_SRCS}
CT_${_S}=	post
.endfor

# Configure template locations:
#
# TMPL_DEFAULT	template to apply to all files
# TMPL_*	template to apply before TMPL_DEFAULT where * may match the
#		content type, e.g. if CT_src/2016 is set to post, TMPL_post
#		will cause the template to be applied to any files in src/2016
#
TMPL_PATH=	templates/
TMPL_DEFAULT=	${TMPL_PATH}default.html
TMPL_post=	${TMPL_PATH}post.html

# Configure deploy targets, or override them during running make with
#
#   $ make deploy RSYNC_DST=user@example.com:/
#
# known_hosts file contains the fingerprint to the homenet.grid.in.th
# server, which is where grid.in.th is hosted at.
RSYNC=		rsync
RSYNC_SSH?= 	ssh -o UserKnownHostsFile=${.CURDIR}/known_hosts -p 2222
RSYNC_DST?=	homenet.grid.in.th:/


# =============================================================================
# Logging
# =============================================================================

E=		@
ECHO_FORMAT=	"%s %s\\n"

.ifdef VERBOSE
.  if "${VERBOSE}" != "0"
E=
.  endif
.endif


# =============================================================================
# Building
# =============================================================================

.MAIN: build

_BUILD_SRCS!=	find ${SITE_SRC} -type f
_BUILD?=
_EXTRA_BUILD?=
_MANIFEST=

# Manifest loop
# -------------
#
# This loop assign various paths to __${_F}_* variables and append the name
# of the source file to _MANIFEST. Variable assignments in BSDmake are done
# after a loop, we need to assign variables to __${_F} and use another loop
# to access them.
#
# The variables defined here are as follows:
#
# __${_F}_TARGET	filename to be used as build target dependencies.
# __${_F}_TYPE		type of the source file: markdown, html
# __${_F}_HTML		intermediate HTML source file
.for _F in ${_BUILD_SRCS:S/^${SITE_SRC}//}

# index.html: src/index.html -> cache/index.html -> dst/index.html
#
# index.html doesn't need to go through filename/index.html transformation
# like all other files (otherwise we'd end up with index/index.html)
.  if "${_F:M*index.html}" != ""
__${_F}_TARGET=	${SITE_DST}${_F}
__${_F}_HTML=	${SITE_CACHE}${_F}
__${_F}_TYPE=	html
_MANIFEST+=	${_F}

# *.markdown: src/foo.markdown -> cache/foo.html -> dst/foo/index.html
.  elif "${_F:M*.markdown}" != ""
__${_F}_TARGET=	${SITE_DST}${_F:S/.markdown$/\/index.html/}
__${_F}_HTML=	${SITE_CACHE}${_F:S/.markdown$/.html/}
__${_F}_TYPE=	markdown
_MANIFEST+=	${_F}

# *.html: src/about.html -> cache/about.html -> dst/about/index.html
.  elif "${_F:M*.html}" != ""
__${_F}_TARGET=	${SITE_DST}${_F:S/.html/\/index.html/}
__${_F}_HTML=	${SITE_CACHE}${_F}
__${_F}_TYPE=	html
_MANIFEST+=	${_F}

# *: src/* -> dst/*
#
# Other targets are copy as-is and doesn't require any transformations
# so the target is defined here without appending _MANIFEST.
.  else
_BUILD+=	${SITE_DST}${_F}

${SITE_DST}${_F}: ${SITE_SRC}${_F}
	${E}printf ${ECHO_FORMAT} "COPY" "$@"
	${E}mkdir -p ${@:C/\/[^\/]*$//}
	${E}cp ${SITE_SRC}${_F} $@

.  endif

# Break src path foo/bar/baz into foo/bar/baz and foo/bar and foo
# to be used to determine a content type.
__${_F}_T!=	printf "${SITE_SRC}${_F}" | awk 'FS="/" {\
  for (i=NF; i>0; i--) {\
    for (j=1; j<=i; j++) {\
      if (j > 1) { printf "/" };\
      printf "%s", $$j\
    };\
    printf "\n"\
  }\
}'
.endfor

# Metadata loop
# -------------
#
# Derived from a frontmatter:
#
# __${_M}_TITLE		title of the page
# __${_M}_DRAFT		whether or not the page is a draft
# __${_M}_DATE		publication date of the page in MMM dd, yyyy format
# __${_M}_ISODATE	publication date of the page in ISO8601 format
# __${_M}_PATH		permanent path to the page (e.g. /foo)
# __${_M}_LINK		permanent link to the page (e.g. https://site_root/foo)
#
# Other metadata (defined here to save a loop):
#
# __${_M}_CT		content type
# __${_M}_TMPL		template file to apply before applying global template
#
# Global:
#
# _CONTENT_*		list of manifest belonging to a content type
#
.for _M in ${_MANIFEST}
__${_M}_TITLE!=	awk '/title: / {\
  print substr($$0, length($$1)+2);\
  exit\
}' ${SITE_SRC}${_M}

__${_M}_DRAFT!=	awk '/draft: / {\
  print substr($$0, length($$1)+2);\
  exit\
}' ${SITE_SRC}${_M}

# 2019/05/01 -> May 1, 2019
__${_M}_DATE!=	awk '/date: / {\
  split(substr($$0, length($$1)+2), part, "-");\
  split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec", months, ",");\
  printf months[int(part[2])] " " int(part[3]) ", " part[1];\
  exit\
}' ${SITE_SRC}${_M}

# We don't have publication time, so they're set to 00:00:00
__${_M}_ISODATE!=	awk '/date: / {\
  print substr($$0, length($$1)+2) "T00:00:00Z";\
  exit\
}' ${SITE_SRC}${_M}

__${_M}_PATH=	${__${_M}_TARGET:S/^${SITE_DST}/\//:S/index.html$//}
__${_M}_URL=	${SITE_ROOT}${__${_M}_PATH}

# Find a content type for a source file using paths generated in __${_M}_T
# as _T and assigning the first match of CT_${_T} to __${_M}_CT.
.  ifdef __${_M}_T
.    for _T in ${__${_M}_T}
.      ifndef __${_M}_CT
.        ifdef CT_${_T}
__${_M}_CT=	${CT_${_T}}
.          ifdef TMPL_${CT_${_T}}
__${_M}_TMPL=	${TMPL_${CT_${_T}}}
.          endif
.        endif
.      endif
.    endfor
.undef __${_M}_T
.  endif
.endfor

# Target loop
# -----------
#
# Build all the targets as defined in _MANIFEST and __${_M}_* variables.
# Only page not marked as draft are processed.
.for _M in ${_MANIFEST}
.  if ${__${_M}_DRAFT} == ""

.    ifdef __${_M}_CT
.      ifndef _CONTENT_${__${_M}_CT}
_CONTENT_${__${_M}_CT}=
.      endif
_CONTENT_${__${_M}_CT}+=	${_M}
.    endif

${__${_M}_HTML}: ${SITE_SRC}${_M}
	${E}mkdir -p ${@:C/\/[^\/]*$//}
# n=0 print
# n=1 initial "---" is found, look for the next "---"
# n=2 next "---" is found, look for the next non-empty line
	${E}awk '{\
if (FNR == 1) { if ($$0 == "---") { n=1 } else { n=0 } };\
if (FNR > 1 && n == 2 && $$0 != "") { n=0 };\
if (n == 0) { print };\
if (FNR > 1 && n == 1 && $$0 == "---") { n=2 };\
}' ${SITE_SRC}${_M} > "${SITE_CACHE}${_M}.strip"
.    if "${__${_M}_TYPE}" == "markdown"
	${E}printf ${ECHO_FORMAT} "MKDN" "$@"
	${E}pandoc ${SITE_CACHE}${_M}.strip -o "$@"
	${E}rm ${SITE_CACHE}${_M}.strip
.    elif "${__${_M}_TYPE}" == "html"
	${E}printf ${ECHO_FORMAT} "HTML" "$@"
	${E}mkdir -p ${@:C/\/[^\/]*$//}
	${E}mv ${SITE_CACHE}${_M}.strip "$@"
.    endif

_BUILD+=	${__${_M}_TARGET}

${__${_M}_TARGET}: ${__${_M}_HTML} ${__${_M}_TMPL} ${TMPL_DEFAULT}
	${E}printf ${ECHO_FORMAT} "GENF" "$@"
	${E}cp ${__${_M}_HTML} ${__${_M}_HTML}.target
# Each page may have its own pre-template defined. See TMPL_*
.    for _T in ${__${_M}_TMPL} ${TMPL_DEFAULT}
# Pass the HTML source file via stdin and substitute it in template
# retrieved by awk's getline, then save it to .target2 file (otherwise
# otherwise .target will get written before it's read)
#
# The awk script will cleanup the following:
# 1. \ converted to \\ before substitution (e.g. "\" -> "\\")
# 2. " converted to \" before substitution (e.g. """ -> "\"")
# 3. > in title escaped as &gt; in awk
# 4. < in title escaped as &lt; in awk
# 5. <!--more--> in body stripped out in awk
#
# Then substitute the following:
# 1. $title$ in template to the title defined in frontmatter
# 2. $date$ to the MMM dd, yyyy date format defined in frontmatter
# 3. $body$ to the body of the page
	${E}awk '\
{ body=body "\n" $$0 }\
END {\
  while (getline t <"${_T}" > 0) { tmpl=tmpl "\n" t };\
  title="${__${_M}_TITLE:S/\\/\\\\/g:S/"/\\"/g}";\
  sub("\n", "", body);\
  gsub("&", "\\\\&", body);\
  gsub(">", "\\\\\\&gt;", title);\
  gsub("<", "\\\\\\&lt;", title);\
  gsub("<!--more-->\n", "", body);\
  gsub("\\$$site_title\\$$", "${SITE_TITLE}", tmpl);\
  gsub("\\$$site_desc\\$$", "${SITE_DESC}", tmpl);\
  gsub("\\$$site_author\\$$", "${SITE_AUTHOR}", tmpl);\
  gsub("\\$$site_email\\$$", "${SITE_EMAIL}", tmpl);\
  gsub("\\$$site_root\\$$", "${SITE_ROOT}", tmpl);\
  gsub("\\$$site_updated\\$$", "${SITE_UPDATED}", tmpl);\
  gsub("\\$$url\\$$", "${__${_M}_URL}", tmpl);\
  gsub("\\$$path\\$$", "${__${_M}_PATH}", tmpl);\
  gsub("\\$$title\\$$", title, tmpl);\
  gsub("\\$$date\\$$", "${__${_M}_DATE}", tmpl);\
  gsub("\\$$isodate\\$$", "${__${_M}_ISODATE}", tmpl);\
  gsub("\\$$body\\$$", body, tmpl);\
  sub("\n", "", tmpl);\
  print tmpl\
}' "${__${_M}_HTML}.target" > ${__${_M}_HTML}.target2
	${E}mv ${__${_M}_HTML}.target2 ${__${_M}_HTML}.target
.    endfor
	${E}mkdir -p ${@:C/\/[^\/]*$//}
	${E}mv "${__${_M}_HTML}.target" "$@"
.  endif

.endfor

# Build
# -----

# Extra build targets:
_EXTRA_BUILD+=	${SITE_DST}index.html
_EXTRA_BUILD+=	${SITE_DST}index.xml
_EXTRA_BUILD+=	${SITE_DST}assets/styles.css

.PHONY: build

build: ${_BUILD} ${_EXTRA_BUILD}


# =============================================================================
# Extra build: index
# =============================================================================

_INDEXM!=	echo ${_CONTENT_${CT_INDEX}} | xargs -n1 | sort -r

.for _S in index.html index.xml
${SITE_DST}${_S}: ${_BUILD} ${TMPL_PATH}${_S} ${TMPL_DEFAULT}
	${E}printf ${ECHO_FORMAT} "GENF" "$@"
	${E}mkdir -p ${SITE_CACHE}
	${E}printf "" > "${SITE_CACHE}${_S}.target"
.  for _M in ${_INDEXM}
# This is mostly copy-pasted from ${__${_M}_TARGET} above. Unfortunately my
# make/awk-fu is not strong enough to come up with a way to not repeat this
# block of code.
#
# [TODO] find a way to merge this with ${__${_M}_TARGET}
	${E}awk '\
BEGIN { s=0 } /<!--more-->/ { s=1 } { if (s == 0) { body=body "\n" $$0 } }\
END {\
  while (getline t <"${TMPL_PATH}${_S}_${CT_INDEX}" > 0) { tmpl=tmpl "\n" t };\
  title="${__${_M}_TITLE:S/\\/\\\\/g:S/"/\\"/g}";\
  sub("\n", "", body);\
  gsub("&", "\\\\&", body);\
  gsub(">", "\\\\\\&gt;", title);\
  gsub("<", "\\\\\\&lt;", title);\
  gsub("\\$$site_title\\$$", "${SITE_TITLE}", tmpl);\
  gsub("\\$$site_desc\\$$", "${SITE_DESC}", tmpl);\
  gsub("\\$$site_author\\$$", "${SITE_AUTHOR}", tmpl);\
  gsub("\\$$site_email\\$$", "${SITE_EMAIL}", tmpl);\
  gsub("\\$$site_root\\$$", "${SITE_ROOT}", tmpl);\
  gsub("\\$$site_updated\\$$", "${SITE_UPDATED}", tmpl);\
  gsub("\\$$url\\$$", "${__${_M}_URL}", tmpl);\
  gsub("\\$$path\\$$", "${__${_M}_PATH}", tmpl);\
  gsub("\\$$title\\$$", title, tmpl);\
  gsub("\\$$date\\$$", "${__${_M}_DATE}", tmpl);\
  gsub("\\$$isodate\\$$", "${__${_M}_ISODATE}", tmpl);\
  gsub("\\$$body\\$$", body, tmpl);\
  sub("\n", "", tmpl);\
  print tmpl\
}' ${__${_M}_HTML} >> "${SITE_CACHE}${_S}.target"
.  endfor
# Outer template of a loop.
.  for _T in ${TMPL_PATH}${_S} ${TMPL_DEFAULT}
.    if "${_T:E}" == "${_S:E}"
	${E}awk '\
{ body=body "\n" $$0 }\
END {\
  while (getline t <"${_T}" > 0) { tmpl=tmpl "\n" t };\
  sub("\n", "", body);\
  gsub("&", "\\\\&", body);\
  gsub("\\$$site_title\\$$", "${SITE_TITLE}", tmpl);\
  gsub("\\$$site_desc\\$$", "${SITE_DESC}", tmpl);\
  gsub("\\$$site_author\\$$", "${SITE_AUTHOR}", tmpl);\
  gsub("\\$$site_email\\$$", "${SITE_EMAIL}", tmpl);\
  gsub("\\$$site_root\\$$", "${SITE_ROOT}", tmpl);\
  gsub("\\$$site_updated\\$$", "${SITE_UPDATED}", tmpl);\
  gsub("\\$$title\\$$", "Home", tmpl);\
  gsub("\\$$body\\$$", body, tmpl);\
  sub("\n", "", tmpl);\
  print tmpl\
}' "${SITE_CACHE}${_S}.target" > ${SITE_CACHE}${_S}.target2
	${E}mv ${SITE_CACHE}${_S}.target2 ${SITE_CACHE}${_S}.target
.    endif
.  endfor
	${E}mkdir -p ${@:C/\/[^\/]*$//}
	${E}mv "${SITE_CACHE}${_S}.target" "$@"
.endfor


# =============================================================================
# Extra build: assets
# =============================================================================

_ASSET_SRCS!=	find assets/ -type f

${SITE_DST}assets/styles.css: yarn.lock ${_ASSET_SRCS}
	mkdir -p ${SITE_DST}assets
	yarn install
	yarn run postcss --no-map assets/stylesheets/main.css -o $@


# =============================================================================
# Misc
# =============================================================================

PHONY: .clean
clean:
	rm -rf -- ${SITE_DST} ${SITE_CACHE}


# =============================================================================
# Deploy
# =============================================================================

PHONY: .deploy
deploy:
	${RSYNC} -rpcLDvz \
		-e "${RSYNC_SSH}" \
		--chmod=D0775,F0664 \
		${SITE_DST} \
		${RSYNC_DST}