summaryrefslogtreecommitdiff
path: root/noweb.mk
blob: f6207527ce329d29945c2ee10cdcf34ba38d4011 (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
# Author: Jose A Ortega Ruiz <jao@member.fsf.org>
# Start date: Mon Jan 06, 2003 00:47
#
# Copyright (C) 2003, 2009 by Jose Antonio Ortega Ruiz
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

NOTANGLE ?= notangle
NOWEAVE ?= noweave
NOROOTS ?= noroots
CPIF ?= cpif
NODEFS ?= nodefs
NOINDEX ?= noindex

NW_LINEFORMAT ?= -L"\# %L \"%F\" %N"
NW_MAIN ?= main
NW_DOCUMENT_CLASS ?= report
NW_CONTENTS ?= contents.tex
NW_HYPER ?= yes
NW_HYPER_OPTIONS ?= "colorlinks=true,linkcolor=blue"

f_notangle = $(NOTANGLE) $(NW_LINEFORMAT) -R$(2) $(1) | $(CPIF) $(2);
f_latex = $(1) '\scrollmode \input '"$(2)";\
          while grep -s 'Rerun to get cross-references right' $(2).log;\
          do $(1) '\scrollmode \input '"$(2)"; done
f_roots = $(shell $(NOROOTS) $(1) | sed -r 's/^(<<)(.*)>>$$/\2/')

f_extract_inputs = $(strip $(shell \
                     cat $(1) | grep '^\\input' | \
                     sed -r 's/\\\input\{(.*)\}.*/\1/'))

f_read_nw_files = $(foreach f, $(call f_extract_inputs, $(1)), $(dir $(1))$(f))

nw_contents_files = $(foreach d, $(NW_SUBDIRS), $d/$(NW_CONTENTS))
nw_files = $(foreach f, $(nw_contents_files), $(call f_read_nw_files, $(f)))

NW_FILES = $(strip $(nw_files))

nw_sources = $(NW_FILES:%=%.nw)
local_nw_files = $(call f_extract_inputs, $(NW_CONTENTS))
local_nw_sources = $(local_nw_files:%=%.nw)
code_targets = $(local_nw_sources:%.nw=.%.tangle)
main_tex = $(NW_MAIN).tex
main_html = $(NW_MAIN).html
main_dvi = $(NW_MAIN).dvi
main_pdf = $(NW_MAIN).pdf
main_defs = $(NW_MAIN).defs
tex_files = $(NW_FILES:%=%.tex)
defs_files = $(NW_FILES:%=%.defs)
code_files = $(foreach f, $(NW_FILES), $(call f_roots, $(f).nw))
code_dirs = $(sort $(filter-out ./, \
                       $(foreach src, $(code_files), $(dir $(src)))))

%.tex : %.nw ; $(NOWEAVE) -n -indexfrom $(main_defs) $< > $@

%.defs : %.nw ;	$(NODEFS) $< > $@

$(main_tex): $(nw_contents_files) $(main_defs) $(tex_files) \
             $(NW_PRE_TEX) $(NW_POST_TEX)
	@echo "\documentclass[a4paper]{$(NW_DOCUMENT_CLASS)}" > $(main_tex)
	@echo "\usepackage{noweb}" >> $(main_tex)
	@echo "\noweboptions{longchunks}"  >> $(main_tex)
	@echo "\noweboptions{externalindex}" >> $(main_tex)
	@if [ ! -z $(NW_LANGUAGES) ]; \
          then echo "\usepackage[$(NW_LANGUAGES)]{babel}" >> $(main_tex); fi
	@echo "\usepackage[utf8]{inputenc}" >> $(main_tex)
	@for f in $(NW_PACKAGES); do echo "\usepackage{$$f}" >> $(main_tex); \
           done
	@if [ "x$(HW_HYPER)" != "xno" ]; \
           then echo "\usepackage[$(NW_HYPER_OPTIONS)]{hyperref}" >> $(main_tex); fi
	@echo "\begin{document}" >> $(main_tex)
	@for f in $(NW_PRE_TEX); do echo "\input{$$f}" >> $(main_tex); done
	@for f in $(nw_contents_files); do \
         foo=`dirname $$f`; \
         cat $$f | grep -v "^%" | \
           sed -r "s/\\\input\{(.+)\}/\\\input\{$$foo\/\1\}/" \
           >> $(main_tex); done
	@for f in $(NW_POST_TEX); do echo "\input{$$f}" >> $(main_tex); done
	@echo "\\end{document}" >> $(main_tex)

$(code_dirs): $(nw_sources)
	mkdir -p $@

$(code_targets): .%.tangle: %.nw
	$(foreach root, $(call f_roots,$<), $(call f_notangle,$<,$(root)))
	touch $@

$(main_defs): $(defs_files)
	sort -u $(defs_files) | $(CPIF) $(main_defs)

$(main_dvi): $(main_defs) $(tex_files) $(main_tex) $(nw_sources)
	latex $(NW_MAIN)
	$(NOINDEX) $(NW_MAIN)
	$(call f_latex, latex, $(NW_MAIN))

$(main_pdf): $(main_defs) $(tex_files) $(main_tex) $(nw_sources)
	pdflatex $(NW_MAIN)
	$(NOINDEX) $(NW_MAIN)
	$(call f_latex, pdflatex, $(NW_MAIN))

$(main_html): $(nw_sources)
	cat $(nw_sources) > $(NW_MAIN).nw
	$(NOWEAVE) -filter l2h -index -html $(NW_MAIN).nw | htmltoc \
          > $(main_html)
	rm -f $(NW_MAIN).nw

tangle: t_subdirs = $(filter-out ., $(NW_SUBDIRS))
tangle: $(code_dirs) $(code_targets)
	for d in $(t_subdirs); do cd $$d && $(MAKE) tangle; done

weave:  $(main_defs) $(tex_files) $(main_tex)

debug:
	@echo "sources: $(nw_sources)"
	@echo "contents files: $(nw_contents_files)"
	@echo "noweb files: $(NW_FILES)"
	@echo "code targets: $(code_targets)"
	@echo "tex files: $(tex_files)"

dvi: $(main_dvi)

pdf: $(main_pdf)

html: $(main_html)

NW_TRASH_FILES = $(code_targets) $(code_files) $(tex_files) $(defs_files) \
                 $(main_html) $(main_defs) $(main_tex)  $(main_pdf) \
                 $(NW_MAIN).out $(NW_MAIN).nwi *.toc *.log *.aux *.dvi

clean:
	rm -rf $(NW_TRASH_FILES)

.PHONY: clean dvi html weave tangle