-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
233 lines (193 loc) · 8.36 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
# EPUB/PDF Makefile helper by djazz
# https://github.com/daniel-j/novelmake
# Do not run this makefile's jobs in parallel/multicore (-j)
RELEASENAME = "My Book %y%m%d"
CURRENTEPUB = current.epub
SOURCE = ./src/
EPUBFILE = ./build/ebook.epub
KEPUBFILE = ./build/ebook.kepub.epub
KINDLEFILE = ./build/ebook.mobi
AZW3FILE = ./build/ebook.azw3
PDFFILE = ./build/book.pdf
SHELL := /bin/bash
SOURCEFILES = $(shell find $(SOURCE) | sort)
XHTMLFILES = $(shell find $(SOURCE) -name '*.xhtml' | grep -v 'nav\.xhtml\|cover\.xhtml\|title\.xhtml' | sort)
TEXPARTS = $(shell find $(SOURCE) -name '*.xhtml' | grep -v 'nav\.xhtml\|cover\.xhtml\|title\.xhtml' | sed -e 's/.*Text\/\(.*\)\.xhtml/.\/build\/latex\/\1.xhtml.tex/' | sort)
PDFPARTS = $(shell find $(SOURCE) -name '*.xhtml' | grep -v 'nav\.xhtml\|cover\.xhtml\|title\.xhtml' | sed -e 's/.*Text\/\(.*\)\.xhtml/.\/build\/parts\/\1.pdf/' | sort)
PNGFILES = $(shell find $(SOURCE) -name '*.png' | sort)
ARTWORKFILES = $(shell find artwork -name '*.png' -or -name '*.jpg' | grep -v -w "artwork/cover.png" | sort | sed -e 's/^artwork\//.\/build\/artwork\//' -e 's/-bw\.jpg$$/-bw\.png/')
EPUBCHECK := ./tools/epubcheck/epubcheck.jar
KINDLEGEN := $(shell command -v kindlegen 2>&1 || echo ./tools/kindlegen/kindlegen)
KEPUBIFY := $(shell command -v kepubify 2>&1 || echo ./tools/kepubify)
EBOOKPOLISH := $(shell command -v ebook-polish 2>&1)
EBOOKVIEWER := $(shell command -v ebook-viewer 2>&1)
JAVA := $(shell command -v java 2>&1)
INOTIFYWAIT := $(shell command -v inotifywait 2>&1)
# https://github.com/IDPF/epubcheck/releases
EPUBCHECK_VERSION = 4.2.2
EPUBCHECK_URL = https://github.com/IDPF/epubcheck/releases/download/v$(EPUBCHECK_VERSION)/epubcheck-$(EPUBCHECK_VERSION).zip
# http://www.amazon.com/gp/feature.html?docId=1000765211
KINDLEGEN_URL_LINUX = http://kindlegen.s3.amazonaws.com/kindlegen_linux_2.6_i386_v2_9.tar.gz
# https://github.com/geek1011/kepubify/releases
KEPUBIFY_VERSION = 2.3.3
KEPUBIFY_URL_LINUX = https://github.com/geek1011/kepubify/releases/download/v$(KEPUBIFY_VERSION)/kepubify-linux-64bit
.PRECIOUS: build/latex/%.xhtml.tex
.PHONY: all clean validate build buildkepub buildkindle buildcover buildbook buildtexparts buildpdfparts buildartwork extractcurrent watchcurrent release publish
all: build
release: clean build validate buildkepub buildkindle
build: $(EPUBFILE)
$(EPUBFILE): $(SOURCEFILES)
@echo "Building EPUB..."
@mkdir -p `dirname $(EPUBFILE)`
@rm -f "$(EPUBFILE)"
@cd "$(SOURCE)" && zip -Xr9D "../$(EPUBFILE)" mimetype
@cd "$(SOURCE)" && zip -Xr9D "../$(EPUBFILE)" . -x mimetype -x "*.DS_Store"
buildkepub: $(KEPUBFILE)
$(KEPUBFILE): $(EPUBFILE) $(SOURCEFILES) $(KEPUBIFY)
@echo "Building Kobo EPUB..."
@$(KEPUBIFY) $(EPUBFILE) -o build
buildkindle: $(KINDLEFILE)
$(KINDLEFILE): $(EPUBFILE) $(KINDLEGEN)
@echo Building Kindle file...
@cp -f "$(EPUBFILE)" "$(KINDLEFILE).epub"
@for current in $(PNGFILES); do \
channels=$$(identify -format '%[channels]' "$$current"); \
if [[ "$$channels" == "graya" ]]; then \
mkdir -p "$$(dirname "tmp/$$current")"; \
echo "Converting $$current to RGB..."; \
convert "$$current" -colorspace rgb "tmp/$$current"; \
fi; \
done
@cd "tmp/$(SOURCE)" && zip -Xr9D "../../$(KINDLEFILE).epub" . || exit 0
@rm -rf "tmp/"
@$(KINDLEGEN) "$(KINDLEFILE).epub" -dont_append_source -c1 || exit 0 # -c1 means standard PalmDOC compression. -c2 takes too long but probably makes it even smaller.
@rm -f "$(KINDLEFILE).epub"
@mv "$(KINDLEFILE).mobi" "$(KINDLEFILE)"
buildazw3: $(AZW3FILE)
$(AZW3FILE): $(EPUBFILE)
@echo Building Kindle AZW3 file...
ebook-convert "$(EPUBFILE)" "$(AZW3FILE)" --pretty-print --no-inline-toc --max-toc-links=0 --disable-font-rescaling --cover=./src/OEBPS/Images/cover.jpg --book-producer=djazz
# Builds the LaTeX files from XHTML
build/latex/%.xhtml.tex: src/OEBPS/Text/%.xhtml src/OEBPS/Styles/style.css tools/html2latex-novel.py buildartwork
@mkdir -p "build/latex/"
python3 -B tools/html2latex-novel.py --style "src/OEBPS/Styles/style.css" --input "$<" --output "$@"
buildtexparts: $(TEXPARTS)
build/tex/impnattypo/impnattypo.sty: tools/impnattypo/impnattypo.ins tools/impnattypo/impnattypo.dtx
@rm -rf build/tex/impnattypo
@mkdir -p build/tex/impnattypo
@cd tools/impnattypo && latex -draftmode -output-directory=../../build/tex/impnattypo impnattypo.ins
# Builds the PDF from LaTeX files
$(PDFFILE): $(TEXPARTS) book/* build/tex/impnattypo/impnattypo.sty tools/novel/*
@echo Building book...
@tools/novelrun book/book.tex
@touch $(PDFFILE)
buildbook: $(PDFFILE)
build/parts/%.pdf: build/latex/%.xhtml.tex book/* tools/novel/*
@echo Building part $*.pdf...
@mkdir -p build/parts
@TEXINPUTS=./tools/novel/:$(kpsewhich -var-value TEXINPUTS) tools/latexrun --latex-cmd lualatex --latex-args="--jobname=\"$*\"" -Wall -Wno-fontspec --verbose-cmds -O "build/.parts/$*" -o "$@" book/single.tex
@touch "$@"
buildpdfparts: $(PDFPARTS)
buildcover: build/cover.pdf
build/cover.pdf: artwork/cover.png book/cover.tex
@echo Making book cover...
@cd tools/novel/scripts && ./makecmyk ../../../../artwork/cover.png
@tools/novelrun ./book/cover.tex
@mv tools/novel/scripts/output/cover-softproof.tif tools/novel/scripts/output/cover-NOTpdfx.pdf build/
define BUILDARTWORK
@echo Converting image "$<" to "$@"
@mkdir -p build/artwork
@name="$$(basename "$<")"; \
noext="$${name%.*}"; \
ext="$${name##*.}"; \
if [[ "$<" == *"-bw."* ]]; then \
(cd tools/novel/scripts && ./makebw "../../../../$<"); \
mv -v "tools/novel/scripts/output/$$noext-"*"-BW.png" "$@"; \
else \
(cd tools/novel/scripts && ./makegray "../../../../$<"); \
mv -v "tools/novel/scripts/output/$$noext-"*"-GRAY.$$ext" "$@"; \
fi
endef
build/artwork/%-bw.png: artwork/%-bw.png
$(BUILDARTWORK)
build/artwork/%-bw.png: artwork/%-bw.jpg
$(BUILDARTWORK)
build/artwork/%.png: artwork/%.png
$(BUILDARTWORK)
build/artwork/%.jpg: artwork/%.jpg
$(BUILDARTWORK)
buildartwork: $(ARTWORKFILES)
@echo All images converted!
$(EPUBCHECK):
@echo Downloading epubcheck...
@curl -o "epubcheck.zip" -L "$(EPUBCHECK_URL)" --connect-timeout 30
@mkdir -p `dirname $(EPUBCHECK)`
@unzip -q "epubcheck.zip"
@rm -rf `dirname $(EPUBCHECK)`
@mv "epubcheck-$(EPUBCHECK_VERSION)" "`dirname $(EPUBCHECK)`"
@rm epubcheck.zip
$(KINDLEGEN):
@echo Downloading kindlegen...
@curl -o "kindlegen.tar.gz" -L "$(KINDLEGEN_URL_LINUX)" --connect-timeout 30
@mkdir -p `dirname $(KINDLEGEN)`
@tar -zxf "kindlegen.tar.gz" -C `dirname $(KINDLEGEN)`
@rm "kindlegen.tar.gz"
$(KEPUBIFY):
@echo Downloading kepubify...
@curl -o "tools/kepubify.tmp" -L "$(KEPUBIFY_URL_LINUX)" --connect-timeout 30
@mv "tools/kepubify.tmp" "tools/kepubify"
@chmod +x "tools/kepubify"
validate: $(EPUBFILE) $(EPUBCHECK)
ifndef JAVA
@echo "Warning: Java was not found. Unable to validate ebook."
else
@echo "Validating EPUB..."
@$(JAVA) -jar "$(EPUBCHECK)" "$(EPUBFILE)"
endif
optimize: $(EPUBFILE)
ifndef EBOOKPOLISH
@echo "Warning: Calibre was not found. Skipping compression."
else
@echo "Compressing images and subsetting fonts. This may take a while..."
@ebook-polish --verbose --compress-images --subset-fonts "$(EPUBFILE)" "$(EPUBFILE)"
endif
view: $(EPUBFILE)
ifndef EBOOKVIEWER
@echo "Warning: Calibre was not found. Unable to open ebook viewer."
else
@ebook-viewer --detach "$(EPUBFILE)"
endif
clean:
rm -f "$(EPUBFILE)"
rm -f "$(KEPUBFILE)"
rm -f "$(KINDLEFILE)"
rm -f "$(AZW3FILE)"
rm -rf build/.book build/parts build/.parts build/latex build/build build/tex
rm -rf build/artwork
rm -rf .texmf-var
@# only remove dir if it's empty:
@(rmdir `dirname $(EPUBFILE)`; exit 0)
extractcurrent: $(CURRENTEPUB)
@echo "Extracting $(CURRENTEPUB) into $(SOURCE)"
@rm -rf "$(SOURCE)"
@mkdir -p "$(SOURCE)"
@unzip "$(CURRENTEPUB)" -d "$(SOURCE)"
watchcurrent: $(CURRENTEPUB) $(EPUBCHECK)
ifndef JAVA
$(error Java was not found. Unable to validate ebook)
endif
ifndef INOTIFYWAIT
$(error inotifywait was not found. Unable to watch ebook for changes)
endif
@echo "Watching $(CURRENTEPUB)"
@while true; do \
$(INOTIFYWAIT) -qe close_write "$(CURRENTEPUB)"; \
echo "Validating $(CURRENTEPUB)..."; \
$(JAVA) -jar "$(EPUBCHECK)" "$(CURRENTEPUB)"; \
done
publish: $(EPUBFILE) $(KINDLEFILE) $(KEPUBFILE) $(AZW3FILE)
@mkdir -pv release
cp "$(EPUBFILE)" "release/$$(date +$(RELEASENAME)).epub"
cp "$(KEPUBFILE)" "release/$$(date +$(RELEASENAME)).kepub.epub"
cp "$(KINDLEFILE)" "release/$$(date +$(RELEASENAME)).mobi"
cp "$(AZW3FILE)" "release/$$(date +$(RELEASENAME)).azw3"