-
Notifications
You must be signed in to change notification settings - Fork 5
/
Rakefile
153 lines (140 loc) · 3.61 KB
/
Rakefile
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
# Rakefile to collect all .tex files in a directory and run `pdflatex` and `bibtex` as needed to
# produce PDF output. If a .tex file is updated `pdflatex -draftmode` will be run to produce new
# .aux and .log files. These are used to determine whether `bibtex` needs to be run. If so `bibtex`
# will always need to be followed by `pdflatex -draftmode`. With fully updated .aux and .bbl in
# hand, a final `pdflatex` is run. The only hole in the logic I've found is that, when making a
# small revision, this will run `pdflatex -draftmode` then `pdflatex` when only `pdflatex` is
# required.
#
# Run `rake` to compile PDFs and `rake clean` to remove the intermediary cruft
basedir = Dir.getwd
TEX = FileList["**/*.tex"]
AUX = TEX.ext("aux")
BBL = TEX.ext("bbl")
BLG = TEX.ext("blg")
LOG = TEX.ext("log")
OUT = TEX.ext("out")
PDF = TEX.ext("pdf")
require 'rake/clean'
CLEAN.include(AUX)
CLEAN.include(BBL)
CLEAN.include(BLG)
CLEAN.include(LOG)
CLEAN.include(OUT)
CLOBBER.include(PDF)
desc "Full compile"
task :default => PDF
desc "LaTeX aux"
rule ".aux" => ".tex" do |t|
prefix = t.name.pathmap("%n")
dir = t.name.pathmap("%d")
Dir.chdir(dir)
puts "pdflatex -draftmode #{prefix}"
`pdflatex -draftmode #{prefix}`
Dir.chdir(basedir)
end
desc "LaTeX log"
rule ".log" => ".tex" do |t|
prefix = t.name.pathmap("%n")
dir = t.name.pathmap("%d")
Dir.chdir(dir)
puts "pdflatex -draftmode #{prefix}"
`pdflatex -draftmode #{prefix}`
Dir.chdir(basedir)
end
desc "LaTeX compile"
# require log file and proceed if references are incomplete
rule ".pdf" => [".aux", ".bbl", ".log", ".tex"] do |t|
prefix = t.name.pathmap("%n")
dir = t.name.pathmap("%d")
Dir.chdir(dir)
puts "pdflatex #{prefix}"
`pdflatex #{prefix}`
Dir.chdir(basedir)
end
desc "BibTex compile"
# look for .bib file in top-level directory
rule ".bbl" => [".aux", ".log", ".tex"] do |t|
prefix = t.name.pathmap("%n")
dir = t.name.pathmap("%d")
Dir.chdir(dir)
if cite?(prefix)
puts "bibtex #{prefix}"
`bibtex #{prefix}`
puts "pdflatex -draftmode #{prefix}"
`pdflatex -draftmode #{prefix}`
end
Dir.chdir(basedir)
end
desc "Look at log file and check if references are complete"
def ref?(string)
dirty = false
file = File.open("#{string}.log", "r")
m = file.read.match(/LaTeX Warning: There were undefined references|LaTeX Warning: Label(s) may have changed. Rerun to get|^LaTeX Warning: Reference/)
file.close
if m != nil
puts m
dirty = true
end
return dirty
end
desc "Are citations up to date?"
def cite?(string)
dirty = false
if File.exists?("#{string}.bbl")
if cite_missing(string) == true
dirty = true
else
aux_list = cite_aux(string)
bbl_list = cite_bbl(string)
extra = (aux_list - bbl_list).length
missing = (bbl_list - aux_list).length
if extra > 0 || missing > 0
dirty = true
end
end
else
dirty = true
end
return dirty
end
desc "Look at log file and check if citations are complete"
def cite_missing(string)
dirty = false
file = File.open("#{string}.log", "r")
m = file.read.match(/^LaTeX Warning: Citation/)
file.close
if m != nil
puts m
dirty = true
end
return dirty
end
desc "Find citations in .aux"
def cite_aux(string)
list = Array.new
file = File.open("#{string}.aux", "r")
cites = file.read.scan(/\\citation{([^\}]+)}/)
file.close
if cites != nil
cites.each {|m|
list += m[0].split(",")
}
end
list.uniq!
return list
end
desc "Find citations in .bbl"
def cite_bbl(string)
list = Array.new
file = File.open("#{string}.bbl", "r")
cites = file.read.scan(/\\bibitem{([^\}]+)}/)
file.close
if cites != nil
cites.each {|m|
list += m[0].split(",")
}
end
list.uniq!
return list
end