aboutsummaryrefslogtreecommitdiff
path: root/common/Makefile.common
blob: c3f69597cdee5df0d6d71c0234a712a531bc8dee (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
TEX=$(shell command -v lualatex xelatex | head -n 1)
SOURCE=.
MODEL ?= .

SRC  =$(wildcard *.rst)
SVG  =$(wildcard content/*.svg)

PDFS = $(SRC:.rst=.pdf)
INCLUDED_PDF = $(SVG:.svg=.pdf)

all: $(PDFS)

BIBS =$(wildcard *.bib)

DIR = $(sort $(shell find $(SOURCE) -name '*.rst' -print))

# Pick the first file for the given name, either in the path from the model, or
# in the local path from the source.
select_file = \
  $(firstword $(wildcard ./resources/$(1)) $(wildcard $(MODEL)/resources/$1))


# Select the files to use in the styles.
ifeq ($(MODEL),.)
  $(info Modèle de base)

  SOURCES = $(sort $(filter-out $(FILTERS), $(wildcard $(MODEL)/resources/*.tex) ))

else
  $(info Modèle surchargé)

  # Remove from the list the elements given in the variable FILTERS
  NEW_STYLES = $(wildcard resources/*.tex)
  MODEL_STYLES = $(wildcard $(MODEL)/resources/*.tex)
  # For each file, extract the name, and sort the list by order.
  STYLE_NAME = $(sort $(foreach fic,$(NEW_STYLES) $(filter-out $(FILTERS),$(MODEL_STYLES)),$(notdir $(fic))))
  # Pick the file either in the current directory, or in the model path
  SOURCES = $(foreach fic,$(STYLE_NAME),$(call select_file,$(fic)))

endif

null  :=
space := $(null) #
comma := ,
TEX_STYLE := $(subst $(space),$(comma),$(strip $(SOURCES)))
TEX_BIBS := $(subst $(space),$(comma),$(strip $(BIBS)))

# Add a variable for specific options to add in the rst2latex command
# 
EXTRA_RST_OPTIONS ?=

EXTRA_STYLES ?=

# Remove the comments and the empty lines from the module list file
PACK = $(shell sed -e 's/\#.*$$//' -e '/^\s$$/d' $(MODEL)/resources/modules)
PACKAGES = $(subst $(space),$(comma),$(strip $(PACK)))

tmp:
	mkdir tmp

# Hack used to identify the path to the shared configuration in latex.
tmp/model.tex: | tmp
	# Transform the path to the model into an absolute path, because this path
	# will be used in the lualatex cache to references the fonts, and using
	# relative path can cause issues if we need to references the font from
	# another location.
	$(file >$@,\def\commonPath{$(abspath $(MODEL)/../common/)})

# Generate the latex file from rst
tmp/%.tex: %.rst $(INCLUDED_PDF) $(SOURCES) | tmp
	$$(command -v rst2latex rst2latex.py | head -n 1)  \
		--no-section-subtitles \
		--table-style=borderless \
		--use-latex-citations \
		--use-latex-docinfo \
		--documentclass=extarticle \
		--documentoption=12pt,A4,table \
		--syntax-highlight=short \
		$(EXTRA_RST_OPTIONS) \
		--stylesheet=tmp/model.tex,$(PACKAGES),$(subst $(space),$(comma),\$(EXTRA_STYLES)),$(TEX_STYLE) $< $@
	sed -i -e 's/continued on next page/suite sur la page suivante/;s|^%$$||' $@
	sed -i -e 's/admonition-/admonition/g' $@

# Generate each pdf with latex
tmp/%.pdf: tmp/%.tex tmp/model.tex $(BIBS) | tmp
	# Make the first run in batch mode (no pdf produced)
	TEXMFHOME=$(MODEL)/../common/classes $(TEX) -output-directory tmp --halt-on-error --draftmode $<
	TEXMFHOME=$(MODEL)/../common/classes $(TEX) -output-directory tmp $<
	#bibtex tmp/$*.aux && $(TEX) -output-directory tmp $< || echo "no bib"
	while grep 'Rerun to get ' tmp/$*.log ; do \
		$(TEX) -output-directory tmp $< ;\
	done

# Put the pdf in the right place
%.pdf: tmp/%.pdf | tmp
	cp $< $@
	# Update mupdf if the process exist, or do nothing.
	pkill -HUP mupdf || true

.PHONY: clean
clean:
	rm -r tmp

# Generate the final PDF document
# Send a notification to mupdf in order to refresh the document
%.png: %.pdf
	gs -dTextAlphaBits=4 -dFirstPage=1 -dLastPage=1 -dBATCH -dNOPAUSE -sDEVICE=png16m -r150 -dUseCropBox -sOutputFile=$@  $<

# Watch mode, which regenerate the documents as soon there is a change in a
# file.,
# requires inotifywait (package inotify-tools)
.PHONY: watch
watch:
	while true; do \
		$(MAKE) all; \
		inotifywait -qre close_write . ; \
	done

# Format the pages in order to make them printable in a book
book: $(PDFS)

	pdfbook2 --no-crop $<