summaryrefslogtreecommitdiff
path: root/Makefile
blob: 6d89a2239db0f777e3c550e75db9a8a28a6ef58e (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
SHEETDIR	:= $(HOME)/.crustytoothpaste/groups/xsl-sheets/docbook

FO_STYLE	:= report
EPUB_STYLE	:= base
HTML_STYLE	:= base

PROFILE		:= /usr/share/xml/docbook/stylesheet/docbook-xsl-ns/profiling/profile.xsl
STYLE		:= $(SHEETDIR)/fo/style/$(FO_STYLE)/cvt.xsl
PSTP		:= $(SHEETDIR)/fo/style/$(FO_STYLE)/pstp.xsl
EPUBSTYLE	:= $(SHEETDIR)/epub/style/$(EPUB_STYLE)/cvt.xsl
HTMLSTYLE	:= $(SHEETDIR)/xhtml5/style/$(HTML_STYLE)/cvt.xsl
HTMLPSTP	:= $(SHEETDIR)/xhtml5/style/$(HTML_STYLE)/pstp.xsl
ADOCFIXUP	:= $(SHEETDIR)/common/asciidoctor-fixup.xsl
BIBLIOID	:= $(SHEETDIR)/common/biblioid.xsl

FOP_CFG		:= /usr/share/doc/fop/fop.xconf

DBTOEPUB	:= dbtoepub
ASCIIDOC	:= asciidoctor
FOP			:= fop -q $(shell [ -e $(FOP_CFG) ] && printf -- "-c $(FOP_CFG)")

PAPER		?= letter

PROFARGS	:= --stringparam profile.condition 'default;pdf'
STYLEARGS	:= --param fop1.extensions 1
STYLEARGS	+= --param free.fonts.only 1
STYLEARGS	+= --stringparam paper.type $(PAPER)
STYLEARGS	+= --stringparam generate.toc "/ nop article nop book nop"

CUR			= $<

SRC_SUFFIXES	:= .dbx .adoc
TMP_SUFFIXES	:= .tmp .tmp2
GEN_SUFFIXES	:= .pdf .fo .rtf .txml .epub .html

DEPFILES	:= $(patsubst %.dbx,.%.d,$(wildcard *.dbx))
ALLSRCS		:= $(shell for i in $(SRC_SUFFIXES); do find -name "*$$i" | grep -v rsrc; done)

SRC_PATTERNS	:= $(subst .,%.,$(SRC_SUFFIXES))
ALLPDFS		:= $(foreach i,$(SRC_PATTERNS),$(patsubst $i,%.pdf,$(ALLSRCS)))

.SUFFIXES: $(SRC_SUFFIXES) $(GEN_SUFFIXES) .d

all:
	@echo "Nothing happens."

pdf: $(ALLPDFS)
epub: $(ALLPDFS:.pdf=.epub)

depend: $(DEPFILES)

.%.d: %.dbx
	perl -ne 'INIT { @x=qw/.dbx $(GEN_SUFFIXES)/; $$n="$^"; $$n=~s/.dbx$$//;' \
		-e '$$r = join(" ", map { "$$n$$_" } @x); }' \
		-e 'print "$$r: $$1\n" if /xi:include.*href="([^"]+)"/;' $^ > $@

.fo.pdf:
	$(FOP) -fo $(CUR) -pdfprofile PDF/A-1a -pdf $@.tmp
	bin/strip-date "$$(sha256sum $(CUR) | cut -d' ' -f1)" < $@.tmp >$@
	rm $@.tmp

.fo.rtf:
	$(FOP) -fo $(CUR) -rtf $@

.adoc.txml:
	bin/process-spaces -s 2 $(CUR) \
		| $(ASCIIDOC) -bdocbook5 -o - - \
		| xsltproc -o $@ --xinclude $(ADOCFIXUP) -

.dbx.fo .txml.fo:
	(str="$(CUR)"; if [ -z "$${str##*/*}" ]; \
	then \
		cat $(CUR); \
	else \
		xsltproc --xinclude $(PROFARGS) $(PROFILE) $(CUR); \
	fi) | xsltproc --xinclude $(STYLEARGS) $(STYLE) - | xsltproc -o $@ $(PSTP) -

.dbx.epub .txml.epub:
	(str="$(CUR)"; if [ -z "$${str##*/*}" ]; \
	then \
		cat $(CUR); \
	else \
		xsltproc --xinclude $(PROFARGS) $(PROFILE) $(CUR); \
	fi) > $@.tmp
	xsltproc -o $@.tmp2 \
		--stringparam hash-name sha-256 \
		--stringparam hash-base64 "$(bin/hash64 $@.tmp)" \
		$(BIBLIOID) $@.tmp
	$(DBTOEPUB) -v -o $@ -s $(EPUBSTYLE) $@.tmp2
	rm $@.tmp $@.tmp2
	# Repack the zip file for reproducibility.
	mkdir $@.dir
	(destpath="$$(pwd)/$(@F)" && cd $@.dir && unzip $$destpath)
	find $@.dir | xargs touch -d 1970-01-01T00:00:00Z
	rm $@
	(destpath="$$(pwd)/$(@F)" && cd $@.dir && \
		find . -print | sed 1d | sort | \
		xargs zip -oX $$destpath)
	$(RM) -fr $@.dir

.dbx.html .txml.html:
	(str="$(CUR)"; if [ -z "$${str##*/*}" ]; \
	then \
		cat $(CUR); \
	else \
		xsltproc --xinclude $(PROFARGS) $(PROFILE) $(CUR); \
	fi) | xsltproc --xinclude $(STYLEARGS) $(HTMLSTYLE) - | \
	xsltproc -o $@ $(HTMLPSTP) -

clean:
	for i in $(GEN_SUFFIXES); do find -name "*$$i" | xargs rm -f; done
	for i in $(TMP_SUFFIXES); do find -name "*$$i" | xargs rm -f; done
	for i in .dir; do find -name "*$$i" | xargs rm -fr; done
	find -name '*.d' | xargs -r rm

ifneq "$(MAKECMDGOALS)" "clean"
-include $(DEPFILES)
endif