aboutsummaryrefslogtreecommitdiff
path: root/Makefile.bookup
blob: 327558aec6dd2c8bcf1d3dbe959355918078c684 (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
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
#
# Parameters
#
# Customize to your needs in your main Makefile. Examples:
#
# 	BOOKUP  = vendor/bookup
# 	OUTPUT  = my-essay
# 	OUTPUT  = essay
# 	WEBSITE = some.hostname

# Bookup base folder
BOOKUP ?= .

# Project name
PROJECT ?= bookup

# Output file names (prefix)
OUTPUT ?= book

# Output folder
OUTPUT_FOLDER ?= $(OUTPUT)

# Destination website hostname (for rsync+ssh)
WEBSITE ?= $(PROJECT)

# HTML style
PDF_STYLE ?= bookdown::tufte_book2

# HTML style
HTML_STYLE ?= bookdown::gitbook

#
# Exclusion patterns
#

#EXCLUDES = --exclude=.htpasswd
#EXCLUDES = --exclude=Makefile.local
EXCLUDES  = archive slides

#
# Metadata
#

REVISION = $(shell git describe --tags 2> /dev/null || git log -1 --format=oneline | cut -d ' ' -f 1)
DATE     = $(shell date)
YEAR     = $(shell date +%Y)

#
# Phony targets
#

.PHONY: archive slides site

#
# Default target
#

all: submodules book notes site assemble clean
	@true

#
# Archive
#

archive:
	@$(BOOKUP)/bin/archive

#
# Cleanup
#

clean:
	@true
	@rm -f $(OUTPUT).aux $(OUTPUT).log $(OUTPUT).nav $(OUTPUT).out $(OUTPUT).snm $(OUTPUT).tex $(OUTPUT).toc $(OUTPUT).vrb
	@#rm -f index.md index.Rmd
	@rm -f _main.log _main.tex _main.loe _main.Rmd
	@rm -rf _book _bookdown_files

#
# Submodules
#

submodules:
	@git submodule update --init --recursive

#
# Metadata
#

metadata:
	@mkdir -p           .metadata
	@echo $(REVISION) > .metadata/revision.txt
	@echo $(DATE)     > .metadata/date.txt
	@echo $(YEAR)     > .metadata/year.txt

#
# Compilation
#

compile_book: metadata
	@rm -f $(OUTPUT).md
	@#cat sections/*.md  >> $(OUTPUT).md
	@/bin/bash -c 'cat _bookup.yml >> $(OUTPUT).md ; echo "" >> $(OUTPUT).md'
	@/bin/bash -c 'cat _biblio.yml >> $(OUTPUT).md ; echo "" >> $(OUTPUT).md'
	@/bin/bash -c 'for file in $(BOOKUP)/structure/00*.yml; do cat $$file >> $(OUTPUT).md ; echo "---" >> $(OUTPUT).md; done'
	@/bin/bash -c 'for file in $(BOOKUP)/structure/00*.md; do cat $$file >> $(OUTPUT).md ; echo "" >> $(OUTPUT).md; done'
	@#/bin/bash -c 'for file in content/sections/*/*.md; do cat $$file >> $(OUTPUT).md ; echo "" >> $(OUTPUT).md ; cat $(BOOKUP)/templates/references.md >> $(OUTPUT).md ; echo "" >> $(OUTPUT).md; done'
	@#/bin/bash -c 'for file in content/sections/*.md; do cat $$file >> $(OUTPUT).md ; echo "" >> $(OUTPUT).md ; cat $(BOOKUP)/templates/references.md >> $(OUTPUT).md ; echo "" >> $(OUTPUT).md; done'
	@/bin/bash -c 'find content/sections -type f | sort | while read file; do cat $$file >> $(OUTPUT).md ; echo "" >> $(OUTPUT).md ; cat $(BOOKUP)/templates/references.md >> $(OUTPUT).md ; echo "" >> $(OUTPUT).md; done'
	@/bin/bash -c 'for file in $(BOOKUP)/structure/99*.md; do cat $$file >> $(OUTPUT).md ; echo "" >> $(OUTPUT).md; done'
	@sed -i -e "s|%%revision%%|$(REVISION)|g" -e "s|%%date%%|$(DATE)|g" $(OUTPUT).md
	@test -e snippets/terminology.sed && sed -i -f snippets/terminology.sed $(OUTPUT).md || true
	@ln -sf $(OUTPUT).md $(OUTPUT).Rmd

compile_notes: metadata
	@rm -f notes.md
	@/bin/bash -c 'find content/notes -type f | sort | while read file; do cat $$file >> notes.md; echo "" >> notes.md; done; echo "# Bibliografia" >> notes.md'
	@sed -i -e "s|%%revision%%|$(REVISION)|g" -e "s|%%date%%|$(DATE)|g" notes.md
	@test -e snippets/terminology.sed && sed -i -f snippets/terminology.sed notes.md || true
	@ln -s notes.md notes.Rmd

compile: compile_book move_book compile_notes move_note
	@true

#
# Main bookdown targets
#

bookdown_clean:
	@rm -f _main.Rmd

bookdown_html:
	@# Needs to backup the index.html to avoid overwriting
	@#mv index.html index.html.old

	# Cleanup
	@#rm -rf $(OUTPUT)/html $(OUTPUT)/$(OUTPUT).html
	@rm -rf $(OUTPUT_FOLDER)

	# Fix references
	# Use sed to fix a weird reference issue on fig.cap inside knitr blocks that are not properly converted.
	# Not using "sed -i" since it yields a permission denied error when running inside a guest VM.
	# This fix no longer works with citeproc.
	@#cp $(OUTPUT).md $(OUTPUT).md.base
	@#sed -e 's/\\\\citet{\([^}]*\)}/@\1/g' $(OUTPUT).md > $(OUTPUT).new && mv $(OUTPUT).new $(OUTPUT).md

	# Build
	Rscript -e 'bookdown::render_book("$(OUTPUT).md", "$(HTML_STYLE)")'

	# Restore $(OUTPUT).md after the weird reference fix
	@#mv $(OUTPUT).md.base $(OUTPUT).md

	# See https://stackoverflow.com/questions/58340924/bookdown-generates-index-file-with-a-chapter-title-instead-of-index-html-when
	# Fixed using section ID naming in the preamble
	#@cd _book && ln -s "inĂ­cio.html" index.html
	@#mv _book $(OUTPUT)/html
	@#mv index.html.old index.html
	@mv _book $(OUTPUT_FOLDER)

	# Replace fuse.js from CDN to local
	# This ensures the HTML output can be browsed offline
	#@find $(OUTPUT)/html -name *.html -exec sed -i -e 's|https://cdn.jsdelivr.net/npm/fuse.js@[^"]*|../../vendor/Fuse.js/dist/fuse.min.js|' {} \;
	@mkdir $(OUTPUT_FOLDER)/js
	@cp vendor/bookup/vendor/Fuse.js/dist/fuse.min.js $(OUTPUT_FOLDER)/js
	@find $(OUTPUT_FOLDER) -name *.html -exec sed -i -e 's|https://cdn.jsdelivr.net/npm/fuse.js@[^"]*|js/fuse.min.js|' {} \;

  # Add revision information
	@echo $(REVISION) > $(OUTPUT_FOLDER)/revision

	# Add symlink to the archive and to the slides
	# This symlink is useful for checking things in localhost
	# But it tends to be replaced during remote deployment
	@cd $(OUTPUT_FOLDER) > /dev/null && ln -s ../archive
	@cd $(OUTPUT_FOLDER) > /dev/null && ln -s ../slides

bookdown_pdf:
	# Cleanup old builds
	@rm -rf _book/_main.pdf

	# Build
	@Rscript -e 'bookdown::render_book("$(OUTPUT).md", "$(PDF_STYLE)")'

	# Move
	@#mv _main.pdf $(OUTPUT).pdf
	@#mv _book/_main.pdf $(OUTPUT)/$(OUTPUT).pdf
	@mv _book/_main.pdf $(OUTPUT_FOLDER)/$(OUTPUT).pdf

# Use sed to convert tufte::margin_note to marginfigure
# Also, "sed -i" yields a permission denied error when running inside a guest VM.
bookdown_epub:
	@# EPUB format is broken right now
	@true

	# Fix margin notes
	@#sed -e 's/r tufte::margin_note/marginfigure/' $(OUTPUT).md > $(OUTPUT).new && mv $(OUTPUT).new $(OUTPUT).md

	# Build
	@#Rscript -e 'bookdown::render_book("$(OUTPUT).md", "bookdown::epub_book")'

	# Move
	@##mv _book/_main.epub $(OUTPUT)/$(OUTPUT).epub
	@#mv _book/_main.epub $(OUTPUT_FOLDER)/$(OUTPUT)s.epub

	# Cleanup
	@#rm _book/reference-keys.txt
	@#rmdir _book

bookdown: bookdown_clean bookdown_html bookdown_pdf bookdown_epub

#
# Build the book
#

move_book:
	@mv $(OUTPUT).* $(OUTPUT_FOLDER)/

assemble:
	@$(BOOKUP)/bin/assemble

book: archive compile_book bookdown move_book clean

#
# Notes
#

move_notes:
	@mv notes.* notebook

# Too many section levels (nesting) to produce a PDF output
#bookdown_notes_pdf:
#	@Rscript -e 'bookdown::render_book("notes.Rmd", "bookdown::tufte_book2")'

bookdown_notes_html:
	# Fix references
	# Use sed to fix a weird reference issue
	# Also, "sed -i" yields a permission denied error when running inside a guest VM.
	@sed -e 's/\\\\citet{\([^}]*\)}/@\1/g' notes.md > notes.new && mv notes.new notes.md

	# Build
	@#Rscript -e 'bookdown::render_book("notes.Rmd", "bookdown::html_book")'
	@Rscript  -e 'bookdown::render_book("notes.Rmd", "bookdown::gitbook")'

	# Cleanup old notes
	@rm -rf notebook

	# Move
	@mv _book notebook

	# Replace fuse.js from CDN to local
	# This ensures the HTML output can be browsed offline
	@find notebook -name *.html -exec sed -i -e 's|https://cdn.jsdelivr.net/npm/fuse.js@[^"]*|../js/fuse.min.js|' {} \;

notes: compile_notes bookdown_clean bookdown_notes_html move_notes clean

#
# Website
# This is a phony target to be overrided in another Makefile
#
site:
	@true

#
# Remote copies
#

web_deploy:
	@rsync -avz --delete $(EXCLUDES) ./build/   $(WEBSITE):/
	@rsync -avz --delete             ./archive/ $(WEBSITE):/archive/
	@rsync -avz --delete             ./slides/  $(WEBSITE):/slides/

#
# Offline copies
#

usb_stick:
	sudo rsync -av --exclude=backups --delete ./ /media/usb/$(PROJECT)/