-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
937 lines (821 loc) · 28.7 KB
/
Makefile
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
# Makesite
# Copyright (c) 2020-2021 Nicolas Peugnet
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# See <https://www.gnu.org/licenses/gpl-3.0.txt> for the full text of the
# GNU General Public License version 3.0.
## run `make docs` to see the rendered version. (requires discount)
##
# Documentation
# ==============================================================================
# Basics
# ------
# Makesite is a static-site-generator based on a mere Makefile.
# The content of the website must be located in a directory called `pages`.
# The result of the compilation will be located in the `public` directory.
# During the compilation, Makesite will keep intermediate files in the `build`
# directory. This way it does not have to recompile everything on each call.
# Lastly the `templates` directory contains the view and layout files in their
# respective folders.
# Here is a summary of the tree structure's directories:
#
# .
# ├── Makefile # This file
# ├── build # Intermediate build files
# ├── pages # Pages content
# ├── public # Public website root
# └── templates
# ├── layout # Layout files
# └── view # View files
# In the `pages` directory, *every folder is a page*. Thus a *page's URL is its
# path*. The content of a page is rendered from every `*.md`, `*.html`, and
# image files it contains, to which a list of the subpages is appended.
# Each `*.js`, `*.css` files and the `favicon.*` it contains are automatically
# added to the current page and all of its children.
# There is one exception to this principle: each page can have an `assets`
# folder that is ignored. This folder can be used to store images or scripts
# that wont be automatically included in the content so that they can manually
# be included as needed.
# Configuration
# -------------
# Each page can contain a `config` file to override the default variables. Here
# is a sample page's `config` file containing all the variables you can define
# inside (all of them are optional):
#
# # var example default
# title = Home # (capitalized dir name)
# layout = page # (root layout)
# view = list # (root view)
# date = 2021-04-12 # (dir mtime)
# keywords = makefile,static-site-generator
# description = Home page of the Makesite's website
# sort = date/desc # title/asc
# feed = 1
# cover = assets/image.jpg # cover.*
# There is a "root `config` file" at the same level as this file that contains
# the website's global configuration. Here are the variables that can be set
# inside (all of them are optional):
#
# sitename = Makesite
# scheme = https
# domain = club1.fr
# basepath = some/sub/folder
# authorname = nicolas
# authoremail = [email protected]
# layout = custom
# view = title
# dateformat = %d/%m/%y
# imagesext = png|gif
# markdownc = cmark
# loglevel = debug
# Tags
# ----
# It is possible to add tags to a page by adding each of them in a new line of
# its `tags` file. These tags will replace the `{{tags}}` portion of the layout
# and provides another way to browse the website.
# An atom/rss feed is generated for each tag and made autodiscoverable.
# Usage
# ==============================================================================
# Dependencies
# ------------
# Makesite requires the following programs to be in the PATH:
#
# - [make](https://www.gnu.org/software/make/) (obviously)
# - [coreutils](https://www.gnu.org/software/coreutils/)
# - [sed](https://www.gnu.org/software/sed/)
# These programs are optional dependencies for some features to work properly:
#
# - [discount](https://www.pell.portland.or.us/~orc/Code/discount/) for markdown
# render and docs.
# - [busybox](https://busybox.net/) for test server.
# - [fswatch](https://emcrisostomo.github.io/fswatch/) for files watcher.
# - [rsync](https://rsync.samba.org/) as the default deploy command
# Tutorial
# --------
# Download the latest version of this file in an empty directory and run it:
#
# wget https://github.com/n-peugnet/makesite/raw/master/Makefile
# make
#
# This will create the base directory structure detailled above and the first
# page of your website: `index.html` in the `public` directory. To browse the
# website you will have to make it accessible with a webserver. For this, you
# can use the test server (requires busybox, ctrl+c to stop):
#
# make serve
# To add content to the home page you can add a `.html` file in the `pages`
# folder then run makesite once again, but this time in watch mode (requires
# fswatch, ctrl+c to stop):
#
# make watch
#
# This mode will automatically rebuild the site on file changes. It is possible
# to run both of these commands at once using `make run`.
# You can also add new directories in `pages` to create new pages.
# Makesite will automatically add links in the parent page and breadcrumbs to
# every pages to make your website fully navigable.
# For this feature to work properly you will have to set the `basepath` variable
# accordingly.
# If you deleted some files from the `pages` directory, they will not be used
# anymore but they will still be present in `public`. If you want to fully
# delete them, then it is better to run:
#
# make siteclean
# make
# **Tip**: If your computer has multiple cores, you can use `-j` option of Make
# to speed up the build, for instance:
#
# make -j4
# Customization
# -------------
# It is of course possible to customize the website created by a great extent.
# Any number of templates can be added in their respective folder and used in
# specific pages or all of them using the `layout` and `view` variables of the
# according `config` file.
# Styles can also be easily defined for one or a set of pages by adding `*.css`
# files in the `pages` folders.
# Deployment
# ----------
# The command `deploy` makes deployments easy. It uses rsync by default so you
# just have to set the variable `deploydest` in the root config to make it work.
# If rsync is not enough, you can define a custom command in `deploycmd`.
# Limitations
# -----------
# A static-site-generator based on a Makefile is not really the sanest idea.
# This is why Makesite has some serious limitations. Here is the full list:
#
# - The <code> </code> character is not allowed in any file or folder name
# (and the use of any special character is strongly discouraged). Instead you
# can use the character `_` which will be converted to space in the default
# title.
# - The `$` character must be written `$$` in `config` files not to be
# interpreted as a variable in Makesite.
# - Relative path in content (images,links,...) must start with `./` or `../`.
# Developement
# ==============================================================================
# Code style:
#
# - Tab is 8 spaces.
# - Max width is 80 columns.
# - Configurable variables are [a-z] other variables are SCREAMING_SNAKE_CASE.
#
# The set of targets under `dev/` are there to help developers. The `init` one
# will create a basic dev environment.
#
# make dev/init
##
ifneq ($(word 2,$(MAKECMDGOALS)),)
ifneq ($(filter clean,$(MAKECMDGOALS)),)
$(error cannot run clean and other targets at the same time)
endif
endif
include build/utils.mk
ifeq (0,$(MAKELEVEL))
include config
# default config values
export sitename ?= Makesite
export sitename := $(call esc,$(sitename))
export scheme ?= http
export domain ?= localhost
export domain := $(subst /,,$(domain))
export baseurl ?= $(scheme)://$(domain)
export basepath ?=
export basepath := $(subst //,/,/$(basepath)/)
export basepath_e := $(call esc,$(basepath))
export authorname ?= nobody
export authorname := $(call esc,$(authorname))
export authoremail ?= $(authorname)@$(domain)
export authoremail := $(call esc,$(authoremail))
export layout ?= page
export view ?= full
export dateformat ?= %FT%T%:z # ISO 8601
export imagesext ?= png|jpe?g|gif|tiff
export separator ?= /
export loglevel ?= info # trace|debug|info|error
export testport ?= 8000
export watchexclude ?= /\.[^/]+(~|\.sw[a-z])$$
ifdef (deploycmd)
export deploydest ?= none
endif
export deployflags ?= -rltzh --delete --copy-unsafe-links
export deploycmd ?= rsync $(deployflags) public$(basepath) $(deploydest)
export markdownc ?= markdown -f toc,urlencodedanchor,idanchor
export l0:=$(if $(filter trace,$(loglevel)),,@) # trace
export l1:=$(if $(filter trace debug,$(loglevel)),,@) # debug
export l2:=$(if $(filter trace debug info,$(loglevel)),,@) # info
MAKEFLAGS+=$(if $(filter trace debug,$(loglevel)),, --no-print-directory)
export ROOT=$(CURDIR)
endif
################################# SubMakefile ##################################
define SUB_MAKEFILE
PAGE := $*
PAGE_DIR := $$(ROOT)/$<
ROOT_CONFIG := $$(ROOT)/config
UTILS_FILE := $$(ROOT)/build/utils.mk
CONFIG_FILE := $$(wildcard $$(PAGE_DIR)/config)
TAGS_FILE := $$(wildcard $$(PAGE_DIR)/tags)
include $$(UTILS_FILE)
include $$(CONFIG_FILE)
# default config values
title ?= $(shell echo $(subst _, ,$(notdir /$<)) \
| awk '{$$1=toupper(substr($$1,0,1))substr($$1,2)}1')
layout ?= page
view ?= full
date ?= @$$(shell stat -c %Y $$(PAGE_DIR))
keywords ?=
description ?=
sort ?= title/asc
feed ?=
cover ?= $$(wildcard $$(PAGE_DIR)/cover.*)
# sanitize values
date :=$$(shell date -d '$$(date)' +%s)
title :=$$(call esc,$$(title))
keywords :=$$(call esc,$$(keywords))
description :=$$(call esc,$$(description))
dateformat :=$$(strip $$(dateformat))
cover :=$$(cover:$$(PAGE_DIR)/%=%)
PAGESDIR := $$(ROOT)/pages
PAGE_PATH = $$(subst //,/,$$(basepath)$$(PAGE)/)
PAGE_PATH_e = $$(call esc,$$(PAGE_PATH))
PARENT := $(patsubst pages%$(notdir $*),%,$<)
SUBPAGES := $$(shell find $$(PAGE_DIR) -maxdepth 1 -mindepth 1 -type d \
\! -name assets)
SUBMETADATA := $$(SUBPAGES:$$(PAGE_DIR)/%=%/metadata)
SORT_FIELD := $$(subst /,,$$(dir $$(sort)))
SORT_ORDER := $$(notdir $$(sort))
TPL_DIR := $$(ROOT)/build/templates
LAYOUT_FILE := $$(TPL_DIR)/layout/$$(layout).html
VIEW_FILE := $$(TPL_DIR)/view/$$(view).html
TAG_VIEW := $$(TPL_DIR)/view/tag.html
PAGE_HTML := $$(wildcard $$(PAGE_DIR)/*.html)
PAGE_MD := $$(wildcard $$(PAGE_DIR)/*.md)
RENDERED_MD := $$(patsubst $$(PAGE_DIR)/%.md,%.md.html,$$(PAGE_MD))
ALL_HTML := $$(PAGE_HTML) $$(RENDERED_MD)
JS := $$(wildcard $$(PAGE_DIR)/*.js)
CSS := $$(wildcard $$(PAGE_DIR)/*.css)
ICO := $$(firstword $$(wildcard $$(PAGE_DIR)/favicon.*))
ICO_EXT := $$(subst .,,$$(suffix $$(ICO)))
ASSETS := $$(JS) $$(CSS) $$(ICO)
PREV_ASSETS := $$(strip $$(shell cat assets 2> /dev/null))
PREV_COVER := $$(strip $$(shell cat cover 2> /dev/null))
PREV_CONTENT := $$(strip $$(shell cat content 2> /dev/null))
PREV_SUBPAGES := $$(strip $$(shell cat subpages 2> /dev/null))
IMG := $$(shell find $$(PAGE_DIR) -maxdepth 1 \
| grep -P '(?<!cover|favicon)\.($(imagesext))$$$$')
COVER = $$(if $$(cover),$$(call esc,<img class="cover" alt="[cover picture]" \
src="$$(PAGE_PATH)$$(cover)" />),)
CONTENT := $$(ALL_HTML) $$(IMG)
ifeq ($$(strip $$(feed)),1)
OTHER += feed.atom
endif
ifeq ($$(SORT_FIELD),title)
SORT_FLAGS = -k1
else ifeq ($$(SORT_FIELD),date)
SORT_FLAGS = -k2 -n
endif
ifeq ($$(SORT_ORDER),desc)
SORT_FLAGS += -r
endif
# Default target.
.PHONY: build/$<
build/$<: index.html metadata tagspage $$(OTHER)
ifeq ($$(strip $$(l1)),@)
@:
endif
index.html: head.html breadcrumbs.html tags.html content.html pages.html \
$$(LAYOUT_FILE) $$(CONFIG_FILE) $$(ROOT_CONFIG) cover
$$(l0)sed $$(LAYOUT_FILE) \
-e 's~{{sitename}}~$$(sitename)~' \
-e 's~{{title}}~$$(title)~' \
-e 's~{{cover}}~$$(COVER)~' \
-e 's~{{keywords}}~$$(keywords)~' \
-e 's~{{date}}~$$(shell date -d @$$(date) +'$$(dateformat)')~' \
-e 's~{{description}}~$$(description)~' \
-e '/{{head}}/{r head.html' -e 'd}' \
-e '/{{breadcrumbs}}/{r breadcrumbs.html' -e 'd}' \
-e '/{{tags}}/{r tags.html' -e 'd}' \
-e '/{{content}}/{r content.html' -e 'd}' \
-e '/{{pages}}/{r pages.html' -e 'd}' \
> $$@
$$(l1)#GEN build/$</$$@
head.html: J=$$(JS:$$(PAGESDIR)/%=<script src="$$(basepath)%" async></script>)
head.html: C=$$(CSS:$$(PAGESDIR)/%=<link href="$$(basepath)%" rel="stylesheet">)
head.html: I=$$(ICO:$$(PAGESDIR)/%=<link href="$$(basepath)%" rel="icon" \
type="image/$$(ICO_EXT)">)
head.html: ../head.html $$(CONFIG_FILE) assets | $$(ASSETS)
$$(l0)cp $$< $$@
$$(l0)echo '$$(J) $$(C) $$(I)' >> $$@
ifeq ($$(strip $$(feed)),1)
$$(l0)echo '<link href="$$(PAGE_PATH)feed.atom" title="$$(title) feed" \
rel="alternate" type="application/atom+xml" />' >> $$@
endif
$$(l1)#GEN build/$</$$@
breadcrumbs.html: ../breadcrumbs.html $$(wildcard ../metadata)
$$(l0)cp $$< $$@
ifneq ($$(strip $$(PARENT)),)
$$(l0)printf '<a href="$$(subst //,/,$$(basepath)$$(PARENT))"\
>$$(shell cut -f1 ../metadata)</a> $$(separator) ' >> $$@
endif
$$(l1)#GEN build/$</$$@
content.html: I=$$(IMG:$$(PAGESDIR)/%=<img src="$$(basepath)%" alt="[auto]"/>)
content.html: $$(CONTENT) content
$$(l0)echo '$$I' > $$@
ifneq ($$(strip $$(ALL_HTML)),)
$$(l0)cat $$(ALL_HTML) \
| sed -E \
-e 's~(src|href)="/([^"]*)"~\\1="$$(basepath_e)\\2"~g' \
-e 's~(src|href)="\./([^"]*)"~\\1="$$(PAGE_PATH_e)\\2"~g' \
>> $$@
endif
$$(l1)#GEN build/$</$$@
%.md.html: $$(PAGE_DIR)/%.md
$$(l0)$$(markdownc) $$< > $$@
$$(l1)#MDC build/$</$$@
pages.html: $$(SUBMETADATA) $$(VIEW_FILE) $$(CONFIG_FILE) $$(ROOT_CONFIG) \
subpages
$$(l0)echo '<ul>' > $$@
ifneq ($$(strip $$(SUBMETADATA)),)
$$(l0)$$(call pages,$$(SUBMETADATA),$$(VIEW_FILE),$$@,$$(dateformat), \
$$(SORT_FLAGS))
endif
$$(l0)echo '</ul>' >> $$@
$$(l1)#GEN build/$</$$@
ifeq ($$(strip $$(feed)),1)
feed.atom: pages.atom $$(TPL_DIR)/layout/feed.atom
$$(l0)$$(call atomfeed,$$(PAGE),$$<,$$@,$$(title))
$$(l1)#GEN $@
pages.atom: $$(TPL_DIR)/view/entry.atom $$(SUBMETADATA) \
$$(ROOT_CONFIG)
$$(l0)echo > $$@
$$(l0)$$(call pages,$$(SUBMETADATA),$$<,$$@,%FT%T%:z,-nrk2)
$$(l1)#GEN $@
endif
$$(SUBMETADATA): head.html breadcrumbs.html metadata .FORCE
$$(l0)$$(MAKE) -C $$(@D)
metadata: $$(CONFIG_FILE) cover
$$(l0)echo '$$(title)\t$$(date)\t$$(description)\t$$(PAGE)\t\t$\
$$(COVER)' > $$@
$$(l1)#GEN build/$</$$@
ifneq ($$(PREV_ASSETS),$$(strip $$(ASSETS)))
assets: .FORCE
else
assets:
endif
$$(l0)echo '$$(ASSETS)' > $$@
ifneq ($$(PREV_COVER),$$(strip $$(COVER)))
cover: .FORCE
else
cover:
endif
$$(l0)echo '$$(COVER)' > $$@
ifneq ($$(PREV_CONTENT),$$(strip $$(CONTENT)))
content: .FORCE
else
content:
endif
$$(l0)echo '$$(CONTENT)' > $$@
ifneq ($$(PREV_SUBPAGES),$$(strip $$(SUBPAGES)))
subpages: .FORCE
else
subpages:
endif
$$(l0)echo '$$(SUBPAGES)' > $$@
tagspage: tags breadcrumbs.html content.html $$(CONFIG_FILE)
# The last column is only there to generate a diff in build/tags
$$(l0)cat $$< | xargs -I % echo \
%'\t$$(title)\t$$(date)\t$$(description)\t$$(PAGE)$\
\t$$(call esc,$$(shell cat breadcrumbs.html))\t$$(COVER)$\
\t$$(shell stat -c %Y content.html)' > $$@
$$(l1)#GEN build/$</$$@
tags.html: tags
$$(l0)echo '<ul>' > $$@
$$(l0)cat $$< | while read tag; do \
sed $$(TAG_VIEW) \
-e "s~{{tag}}~$$$$tag~" \
-e "s~{{path}}~$$(basepath_e)tags/$$$$tag~" \
| tr -d '\\n' >> $$@; \
done
$$(l0)echo '</ul>' >> $$@
$$(l1)#GEN build/$</$$@
tags: $$(TAGS_FILE)
ifneq ($$(strip $$(TAGS_FILE)),)
$$(l0)$$(call slugify,$$(TAGS_FILE)) > $$@
$$(l1)#SAN build/$</$$@
else
$$(l0)touch $$@
endif
../head.html ../breadcrumbs.html:
$$(l0)touch $$@
.FORCE:
endef
################################# Page layout ##################################
define PAGE_LAYOUT
<!DOCTYPE html>
<html>
<head>
<title>{{title}} - {{sitename}}</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta name="Revisit-After" content="15 days"/>
<meta name="Robots" content="All"/>
<meta name="Title" content="{{title}} - {{sitename}}"/>
<meta name="Keywords" content="{{keywords}}"/>
<meta name="Description" content="{{description}}"/>
{{head}}
<style>
.tags ul {padding: 0;}
.tag {list-style-type: none; display: inline-block; margin:2px;\
padding: 2px 6px; background-color: grey; border-radius: 15px;}
.tag a {color: white; text-decoration: none;}
h1 {border-bottom: solid 1px grey;}
code {padding: 2px 4px; background: lightgrey; border-radius: 4px;}
pre code {display: block; padding: 6px 10px; overflow-x: auto}
</style>
</head>
<body>
<section>
<p class="breadcrumbs">
{{breadcrumbs}} {{title}}
</p>
{{cover}}
<h1>{{title}}</h1>
<p class="date">{{date}}</p>
<div class="tags">
{{tags}}
</div>
{{content}}
</section>
<section>
{{pages}}
</section>
</body>
</html>
endef
################################## Full view ###################################
define FULL_VIEW
<li>
<p>
{{cover}}
{{breadcrumbs}} <a href="{{path}}">{{title}}</a>
<span class="date">{{date}}</span>
</p>
<p class="description">{{description}}</p>
<article class="content">
{{content}}
</article>
</li>
endef
################################### Tag view ###################################
define TAG_VIEW
<li class="tag tag-{{tag}}">
<a href="{{path}}">{{tag}}</a>
</li>
endef
################################# Atom layout ##################################
define ATOM_LAYOUT
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>{{title}} - {{sitename}}</title>
<link href="{{link}}" rel="self" />
<link href="{{root}}" />
<id>{{id}}</id>
<updated>{{date}}</updated>
{{pages}}
</feed>
endef
################################ Atomentry view ################################
define ATOMENTRY_VIEW
<entry>
<title>{{title}}</title>
<link href="{{baseurl}}{{path}}"/>
<id>{{id}}</id>
<updated>{{date}}</updated>
<summary>{{description}}</summary>
<author>
<name>{{authorname}}</name>
<email>{{authoremail}}</email>
</author>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
{{content}}
</div>
</content>
</entry>
endef
#################################### Utils #####################################
define UTILS
# Make function to escape char for sed
define esc
$$(strip $$(subst $$$$,\x24,$$(subst ~,\x7e,$$(subst ",\x22,$$(subst ',\x27,\
$$1)))))
endef
# Make function to print current variables
define printvars
$$(foreach V,$$(sort $$(filter-out printvars $(NO_PRINT),$$(.VARIABLES))),
$$(if $$(filter-out environment% default automatic,$$(origin $$V)),\
$$(info \033[31m$$V\033[0m=$$($$V))))
endef
# Shell function to convert names into slugs
define slugify
sort $$1 | iconv -c -t ascii//TRANSLIT | sed -E 's/[~^]+//g' | sed -E \
's/[^a-zA-Z0-9]+/-/g' | sed -E 's/^-+|-+$$$$//g' | tr A-Z a-z | uniq
endef
# render a list of pages based on:
# 1. metadata file
# 2. view file
# 3. destination file
# 4. date format
# 5. sort flags
define pages
cat $$1 | sort $$5 -t'\t' | while read -r p; do \
title=`echo "$$$$p" | cut -f1`; \
timestamp=`echo "$$$$p" | cut -f2`; \
description=`echo "$$$$p" | cut -f3`; \
path=`echo "$$$$p" | cut -f4`; \
breadcrumbs=`echo "$$$$p" | cut -f5`; \
cover=`echo "$$$$p" | cut -f6`; \
date=`date +'$$4' -d @$$$$timestamp`; \
sed $$2 \
-e "s~{{cover}}~$$$$cover~" \
-e "s~{{title}}~$$$$title~" \
-e "s~{{date}}~$$$$date~" \
-e "s~{{description}}~$$$$description~" \
-e "s~{{baseurl}}~$$(baseurl)~" \
-e "s~{{path}}~$$(basepath_e)$$$$path~" \
-e "s~{{id}}~$$(baseurl)$$(basepath_e)$$$$path~" \
-e "s~{{authorname}}~$$(authorname)~" \
-e "s~{{authoremail}}~$$(authoremail)~" \
-e "s~{{breadcrumbs}}~$$$$breadcrumbs~" \
-e "/{{content}}/{r $$(ROOT)/build/pages/$$$$path/content.html" -e 'd}'\
>> $$3; \
done
endef
# generate a feed based on:
# 1. the directory of the feed
# 2. the page entries file
# 3. the destination file
# 4. the title of the feed
define atomfeed
sed $$(ROOT)/build/templates/layout/feed.atom \
-e 's~{{title}}~$$4~' \
-e 's~{{id}}~$$(baseurl)$$(basepath_e)$$1~' \
-e 's~{{sitename}}~$$(sitename)~' \
-e 's~{{link}}~$$(baseurl)$$(basepath_e)$$1feed.atom~' \
-e 's~{{root}}~$$(baseurl)$$(basepath_e)~' \
-e 's~{{date}}~$$(DATE)~' \
-e 's~{{sitename}}~$$(sitename)~' \
-e '/{{pages}}/{r $$2' -e 'd}' \
> $$3
endef
endef
NO_PRINT = UTILS ATOMENTRY_VIEW ATOM_LAYOUT TAG_VIEW FULL_VIEW PAGE_LAYOUT \
SUB_MAKEFILE
################################ Main Makefile #################################
export DATE = $(shell date --iso-8601=seconds)
ifneq (,$(wildcard pages))
PAGE_LIST := $(shell find pages -mindepth 1 -type d \! -name assets)
PAGE_TAGS_LIST := $(shell find pages -mindepth 1 -type f -name tags)
PAGE_CONF_LIST := $(shell find pages -mindepth 1 -type f -name config)
IMG := $(shell find pages | grep -E '($(imagesext))$$')
PAGE_FEED_LIST := $(shell grep -rlE 'feed ?= ?1' pages --include config)
endif
ifneq (,$(wildcard templates))
TPL_LIST := $(shell find templates -mindepth 1 -type f -name '*.html')
endif
BUILD_TAGS_LIST:= $(patsubst %,build/%page,$(PAGE_TAGS_LIST))
BUILD_MK_LIST := $(patsubst %,build/%/Makefile,$(PAGE_LIST))
PUBD := public$(basepath)
PUBLIC_PAGES := $(patsubst pages/%,$(PUBD)%,$(PAGE_LIST))
PUBLIC_INDEXES := $(patsubst %,%/index.html,$(PUBLIC_PAGES)) $(PUBD)index.html
JS := $(foreach d,pages $(PAGE_LIST),$(wildcard $(d)/*.js))
CSS := $(foreach d,pages $(PAGE_LIST),$(wildcard $(d)/*.css))
PUBLIC_JS := $(JS:pages/%=$(PUBD)%)
PUBLIC_CSS := $(CSS:pages/%=$(PUBD)%)
PUBLIC_IMG := $(IMG:pages/%=$(PUBD)%)
ASSETS := $(PUBLIC_JS) $(PUBLIC_CSS) $(PUBLIC_IMG)
TEMPLATES := layout/page view/full view/tag
TEMPLATES := $(TEMPLATES:%=templates/%.html)
BUILD_TPL := $(patsubst %,build/%,$(sort $(TEMPLATES) $(TPL_LIST)))
ATOM_TPL := $(patsubst %,build/templates/%.atom,layout/feed view/entry)
TAGS_VIEW := build/templates/view/$(view).html
TAGS_LAYOUT := build/templates/layout/$(layout).html
DOCS_LAYOUT := build/templates/layout/page.html
# theses are the variables that will be replaced by the content of a file.
R_VARS = head breadcrumbs tags content pages
R_VARS_EXP = $(patsubst %,-e 's/\({{%}}\)/\n\1\n/',$(R_VARS))
ifneq ($(strip $(PAGE_TAGS_LIST)),)
TAGS := $(shell $(call slugify,$(PAGE_TAGS_LIST)))
TAGS_META := $(TAGS:%=build/tags/%/metadata)
TAGS_INDEXES := $(TAGS:%=$(PUBD)tags/%/index.html)
TAGS_FEEDS := $(TAGS:%=$(PUBD)tags/%/feed.atom)
endif
PAGE_FEEDS := $(PAGE_FEED_LIST:pages/%/config=$(PUBD)%/feed.atom)
.PHONY: site
site: pages $(ASSETS) $(TAGS_INDEXES) $(TAGS_FEEDS) $(PAGE_FEEDS)\
$(PUBLIC_INDEXES)
pages templates build public:
$(l0)mkdir $@
$(l2)#CREA $@ directory
$(ASSETS): $(PUBD)%: pages/%
$(l0)mkdir -p $(@D)
$(l0)cp $< $@
$(l2)#PUB $@
$(PUBLIC_INDEXES) $(PAGE_FEEDS): $(PUBD)%: build/pages/%
$(l0)mkdir -p $(@D)
$(l0)cp $< $@
$(l2)#PUB $@
$(TAGS_INDEXES): $(PUBD)tags/%/index.html: build/tags/%/pages.html \
build/tags/%/head.html \
build/pages/head.html \
build/pages/metadata \
$(TAGS_LAYOUT)
$(l0)mkdir -p $(@D)
$(l0)root=`cut build/pages/metadata -f1`; \
sed $(TAGS_LAYOUT) \
-e 's~{{sitename}}~$(sitename)~' \
-e 's~{{title}}~Tag: $*~' \
-e 's~{{cover}}~~' \
-e 's~{{keywords}}~~' \
-e 's~{{date}}~~' \
-e 's~{{description}}~Tag: $*~' \
-e "s~{{breadcrumbs}}~<a href=$(basepath)>$$root</a> $(separator)~" \
-e 's~{{tags}}~~' \
-e 's~{{content}}~~' \
-e '/{{head}}/{r build/pages/head.html' -e 'r $(word 2,$^)' -e 'd}' \
-e '/{{pages}}/{r $<' -e 'd}' \
> $@
$(l2)#PUB $@
$(TAGS_FEEDS): $(PUBD)%: build/%
$(l0)mkdir -p $(@D)
$(l0)cp $< $@
$(l2)#PUB $@
build/pages/%: build/pages ;
.PHONY: build/pages
build/pages: build/pages/Makefile $(BUILD_MK_LIST) $(BUILD_TPL) $(ATOM_TPL) \
build/utils.mk
$(l0)$(MAKE) -C $@
build/pages/Makefile: export CONTENT=$(SUB_MAKEFILE)
build/pages/Makefile: pages Makefile
$(l0)mkdir -p $(@D)
$(l0)echo "$$CONTENT" > $@
$(l1)#GEN $@
build/pages/%/Makefile: export CONTENT=$(SUB_MAKEFILE)
build/pages/%/Makefile: pages/% Makefile
$(l0)mkdir -p $(@D)
$(l0)echo "$$CONTENT" > $@
$(l1)#GEN $@
build/tags/%/pages.html: $(TAGS_VIEW) build/tags/%/metadata
$(l0)mkdir -p $(@D)
$(l0)echo '<ul>' > $@
$(l0)$(call pages,build/tags/$*/metadata,$<,$@,$(dateformat),-nrk2)
$(l0)echo '</ul>' >> $@
$(l1)#GEN $@
build/tags/%/head.html: Makefile
$(l0)mkdir -p $(@D)
$(l0)echo '<link href="feed.atom" title="Tag: $* Atom feed"' \
'rel="alternate" type="application/atom+xml">' > $@
$(l1)#GEN $@
$(TAGS_META): build/tags ;
.PHONY: build/tags
build/tags: $(BUILD_TAGS_LIST) | build/pages
ifneq ($(strip $(BUILD_TAGS_LIST)),)
$(l0)mkdir -p $@
$(l0)cat $(BUILD_TAGS_LIST) > $@/metadata
$(l0)for t in `cut -f1 $@/metadata | sort | uniq`; do \
mkdir -p $@/$$t; \
sed -n "s~^$$t\t\(.*\)~\1~p" $@/metadata \
> $@/$$t/metadata.new; \
cmp --silent $@/$$t/metadata $@/$$t/metadata.new \
|| cp $@/$$t/metadata.new $@/$$t/metadata; \
done
endif
$(BUILD_TAGS_LIST): $(PAGE_TAGS_LIST) $(PAGE_CONF_LIST) | build/pages ;
build/tags/%/feed.atom: build/tags/%/pages.atom build/templates/layout/feed.atom
$(l0)$(call atomfeed,tags/$*/,$<,$@,Tag: $*)
$(l1)#GEN $@
.PRECIOUS: build/tags/%/pages.atom
build/tags/%/pages.atom: build/templates/view/entry.atom build/tags/%/metadata\
config
$(l0)echo > $@
$(l0)$(call pages,build/tags/$*/metadata,$<,$@,%FT%T%:z,-nrk2)
$(l1)#GEN $@
# sanitize templates to avoid problems later:
$(BUILD_TPL): build/%: %
$(l0)mkdir -p $(@D)
$(l0)sed $< $(R_VARS_EXP) > $@
$(l1)#SAN $@
templates/layout/page.html: export CONTENT=$(PAGE_LAYOUT)
templates/view/full.html: export CONTENT=$(FULL_VIEW)
templates/view/tag.html: export CONTENT=$(TAG_VIEW)
build/templates/layout/feed.atom: export CONTENT=$(ATOM_LAYOUT)
build/templates/view/entry.atom: export CONTENT=$(ATOMENTRY_VIEW)
build/utils.mk: export CONTENT=$(UTILS)
$(TEMPLATES) $(ATOM_TPL) build/utils.mk: Makefile | templates build pages
$(l0)mkdir -p $(@D)
$(l0)echo "$$CONTENT" > $@
$(l1)#GEN $@
config:
$(l0)touch $@
$(l2)#CREA $@
.PHONY: clean
clean: siteclean buildclean templatesclean docsclean
.PHONY: buildclean
buildclean:
$(l0)rm -rf build
$(l2)#RMV build files
.PHONY: templatesclean
templatesclean:
$(l0)rm -rf $(TEMPLATES)
$(l2)#RMV template files
.PHONY: docsclean
docsclean:
$(l0)rm -rf docs.html
$(l2)#RMV docs files
.PHONY: siteclean
siteclean:
$(l0)rm -rf public
$(l2)#RMV public files
.PHONY: run
run:
$(l0)$(MAKE) serve watch -j2
.PHONY: watch
watch:
ifeq (, $(shell which fswatch))
#ERR could not find fswatch
else
$(l2)#RUN files watcher
$(l0)while true; do \
$(MAKE) site; \
fswatch -1r --event 30 -Ee "$(watchexclude)" \
pages templates config Makefile \
$(if $(strip $(l1)),,#)> /dev/null; \
done
endif
.PHONY: serve
serve: public
ifeq (, $(shell which busybox))
#ERR could not find busybox
else
$(l2)#RUN test server, visit http://localhost:$(testport)$(basepath)
$(l0)busybox httpd -f -h $< -p $(testport)
endif
.PHONY: deploy
deploy: site
ifeq (, $(deploydest))
#ERR deploydest is not defined
else
$(l0)$(deploycmd)
$(l2)#RUN finished deployment, visit $(baseurl)$(basepath)
endif
.PHONY: docs
docs: docs.html
$(l2)#RUN open $< with web browser
$(l0)open $<
docs.html: build/docs.html $(DOCS_LAYOUT)
$(l0)sed $(DOCS_LAYOUT) \
-e '/{{breadcrumbs}}/,+1 d' \
-e 's~{{sitename}}~Documentation~' \
-e 's~{{title}}~Makesite~' \
-e 's~{{description}}~Makesite static site generator docs~' \
-e 's~{{date}}~$(shell date --iso-8601)~' \
-e 's~{{cover}}~~' \
-e 's~{{keywords}}~~' \
-e 's~{{tags}}~~' \
-e '/{{content}}/{r $<' -e 'd}' \
-e 's~{{head}}~~' \
-e 's~{{pages}}~~' \
> $@
$(l2)#GEN $@
build/docs.html: Makefile | build
$(l0)sed -En '/^##$$/,/^##$$/s/^#*\s?//p' $< | $(markdownc) -T > $@
$(l1)#GEN $@
.PHONY: dev/init
dev/init: .gitignore .vscode/settings.json
echo loglevel = trace >> config
.PHONY: dev/update-docs
dev/update-docs: I=index.html
dev/update-docs: REF=$(shell git branch --show-current)
dev/update-docs: HEAD=$(shell git rev-parse HEAD)
dev/update-docs: docs.html
git checkout docs
cp $< $I
git add -f $I
git commit -m "docs for $(HEAD)"
git push
git checkout $(REF)
.PHONY: dev/print-vars
dev/print-vars:
$(l0)$(call printvars)
.vscode/settings.json:
mkdir -p $(@D)
echo '{"editor.rulers": [{ "column": 80 }],"editor.tabSize": 8}' > $@
.gitignore:
echo '*' >> $@
echo '!Makefile' >> $@
.FORCE: