aboutsummaryrefslogtreecommitdiff
path: root/Makefile.bookup
blob: 1a3081767bb90f283ac10ce0b08b35081dc6bc18 (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
#
# 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 ?= $(PROJECT)

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

#
# Exclusion patterns
#

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

#
# 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

#
# Default target
#

all: submodules book 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 >> essays.md ; echo "" >> $(OUTPUT).md'
	@/bin/bash -c 'cat _biblio.yml >> essays.md ; echo "" >> $(OUTPUT).md'
	@/bin/bash -c 'for file in $(BOOKUP)/structure/00*.yml; do cat $$file >> essays.md ; echo "---" >> essays.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/published/*/*.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 $(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
	@#sed -i -f snippets/terminology.sed $(OUTPUT).md
	@ln -sf $(OUTPUT).md $(OUTPUT).Rmd

compile: compile_book move_book
	@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 compiled

	# 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", "bookdown::gitbook")'

	# 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 compiled

	# 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 compiled/js
	@cp vendor/bookup/vendor/Fuse.js/dist/fuse.min.js compiled/js
	@find compiled -name *.html -exec sed -i -e 's|https://cdn.jsdelivr.net/npm/fuse.js@[^"]*|js/fuse.min.js|' {} \;

  # Add revision information
	@echo $(REVISION) > compiled/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 compiled > /dev/null && ln -s ../archive
	@cd compiled > /dev/null && ln -s ../slides

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

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

	# Move
	@#mv _main.pdf $(OUTPUT).pdf
	@#mv _book/_main.pdf $(OUTPUT)/$(OUTPUT).pdf
	@mv _book/_main.pdf compiled/$(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 compiled/$(OUTPUT)s.epub

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

bookdown: bookdown_clean bookdown_html bookdown_pdf bookdown_epub

#
# Build the essays
#

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

book: archive compile_book bookdown move_book clean

#
# Remote copies
#

web_deploy:
	@rsync -avz --delete $(EXCLUDES) ./compiled/ $(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)/