-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
206 lines (183 loc) · 4.68 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
#
# Default to the right shell
#
SHELL := /bin/bash
BUILD_DIR := build/
#
# Starting with posts, we have to generate
# a list of all the .tmp files we need to
# create.
#
POST_FILES = $(wildcard posts/*.txt)
TMP_FILES = $(addprefix $(BUILD_DIR), $(notdir $(POST_FILES:.txt=.tmp)))
#
# Reverses a list
#
reverse = $(if $(1),$(call reverse,$(wordlist 2,$(words $(1)),$(1)))) $(firstword $(1))
#
# It's stupid hard to get a space character
# in subst searching.
#
space = $(empty) $(empty)
#
# Creates a formatted date from a file name.
#
date_from_filename = $(shell date $(join $(addprefix -v, $(wordlist 1, 3, $(subst -, , $(notdir $(1))))), y m d) "+%B %m, %Y")
#
# Takes a filepath of a (tmp,txt,html) file
# and returns just the file name. Date info
# stripped.
#
post_filename = $(subst $(space),-,$(wordlist 4, $(words $(subst -,$(space) , $(notdir $(1)))), $(subst -,$(space), $(notdir $(1)))))
html_post_filename = $(call post_filename, $(1:.tmp=.html))
#
# Makes a y/m/d/ for a .tmp file.
#
path_from_filename = $(subst $(space),/,$(wordlist 1, 3, $(subst -,$(space), $(notdir $(1)))))
#
# Generates all the post.html files that
# need to be built.
#
html_post_files = $(foreach f,$(TMP_FILES),$(call path_from_filename, $(f))/$(call post_filename, $(f:.tmp=.html)))
#
# Default build. Ensure the config file is
# there and then start building.
#
all: clean config build
@echo "All done."
#
# The build/ directory is the only thing that needs
# to get wiped.
#
clean:
@echo "Cleaning";
@rm -rf $(BUILD_DIR)*;
#
# The only phony rule that builds all the index files
#
.PHONY: build
build: $(addprefix $(BUILD_DIR),$(html_post_files)) $(BUILD_DIR)index.html
@echo "Build completed."
#
# This builds almost everything.
#
%.html: $(TMP_FILES) $(BUILD_DIR)index.tmp $(BUILD_DIR)index.html
@echo "Building $(@)"
@mkdir -p $(dir $(@))
@awk 'BEGIN {\
html_output = "";\
post_output = "";\
new_line = "";\
}\
{\
post_output = post_output $$0 "\r\n"\
}\
END {\
while (getline < "templates/base.txt"){\
new_line = $$0;\
sub(/\{\{main\}\}/, post_output, new_line);\
sub(/\{\{page_title\}\}/, "Dungeon", new_line);\
html_output = html_output new_line "\r\n";\
}\
print html_output;\
}\
' $(addprefix $(BUILD_DIR), $(subst /,-, $(@:$(BUILD_DIR)%.html=%.tmp))) > $@;
#
# If just building the index.
#
$(BUILD_DIR)index.tmp: $(TMP_FILES)
@cat > $(BUILD_DIR)index.tmp $(call reverse, $(wordlist 1, 10, $(TMP_FILES)))
#
# This builds all the .tmp files used for
# posts and for the index.tmp which is used
# by the index.html
#
$(BUILD_DIR)%.tmp: posts/%.txt
@echo "Building $@"
@echo "---"
@echo $(call html_post_filename, $@)
@echo "---"
@if [ -x Markdown.pl ]; \
then \
split -p----------------------------------- $^ build/; \
tail -n +2 build/ab > build/ac; \
./Markdown.pl build/ac > build/ac.md; \
cat build/aa .source/splitter.txt build/ac.md > build/$(notdir $^); \
rm build/aa build/ab build/ac build/ac.md; \
fi;
@if [ ! -x Markdown.pl ]; \
then \
cp $^ build/; \
fi;
@awk -v pub_date="$(call date_from_filename, $@)" -v permalink="/$(call path_from_filename, $@)/$(call html_post_filename, $@)" 'BEGIN {\
post_output = "";\
}\
{\
if (!seen){\
if($$1 ~ "-----------------------------------"){\
seen = 1;\
}else if($$1 ~ "title:"){\
$$1="";\
title = $$0;\
}else if($$1 ~ "category:"){\
$$1="";\
category = $$0\
}\
}else{\
body = body "\n" $$0;\
}\
}\
END {\
while (getline < "templates/post.txt"){\
sub(/\{\{title\}\}/, title, $$0);\
sub(/\{\{body\}\}/, body, $$0);\
sub(/\{\{pub_date\}\}/, pub_date, $$0);\
sub(/\{\{permalink\}\}/, permalink, $$0);\
sub(/\{\{category\}\}/, category, $$0);\
post_output = post_output $$0 "\n";\
}\
print post_output;\
}\
' build/$(notdir $<) > $@;
@echo "Done";
#
# TBD
#
$(BUILD_DIR)atom.xml:
@echo "Making atom.xml"
#
# Building the index.html file requires
# the build/index.tmp file to be built.
#
$(BUILD_DIR)index.html: $(BUILD_DIR)index.tmp
@echo "Building index.html"
@awk 'BEGIN {\
html_output = "";\
index_output = "";\
new_line = "";\
}\
{\
index_output = index_output $$0\
}\
END {\
while (getline < "templates/base.txt"){\
new_line = $$0;\
sub(/\{\{main\}\}/, index_output, new_line);\
sub(/\{\{page_title\}\}/, "Grampa", new_line);\
html_output = html_output new_line "\r\n";\
}\
print html_output;\
}\
' $(BUILD_DIR)index.tmp > $@;
config:
@yes n | cp -i .source/config.example config
.PHONY: setup
setup: config
@mkdir -p build;
@mkdir -p posts;
@mkdir -p templates;
-@yes n | cp -i .source/templates/* templates/ 2>/dev/null
-@yes n | cp -i .source/deploy.sh.example deploy.sh 2>/dev/null
.PHONY: deploy
deploy:
@./deploy.sh $(BUILD_DIR)