-
Notifications
You must be signed in to change notification settings - Fork 1
/
texprune
executable file
·328 lines (298 loc) · 9.12 KB
/
texprune
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env bash
#
# NAME
#
# texprune - clean up and make a copy of a LaTeX source directory
#
# SYNOPSIS
#
# texprune [-b|-m] [-cfsz] <src_dir> <dst_dir>
#
# OPTIONS
#
# -b keep .bbl but not .bib
# -c optimize (compress) PDF figures before copying
# -f force overwrite destination files
# -m merge the .bbl file with the main TeX file
# -s scrub .tex files by removing comments
# -z make a .zip archive as well
#
# The above options can also be set in an environment variable
# TEXPRUNE_OPTS, e.g.,
#
# export TEXPRUNE_OPTS="-b -c"
#
# DESCRIPTION
#
# While working on a LaTeX document, it is not uncommon to have
# several nonessential files in the working tree, e.g., notes, image
# files that aren't included, etc. If we want to email the working
# tree to a colleague or upload it to arXiv, including these files
# would be superflous. texprune is an attempt to solve this
# problem.
#
# texprune takes two directories -- the source directory <src_dir>
# and the destination directory <dst_dir> -- as arguments.
# texprune assumes that *all* the required files for compilation,
# except the figures, are in the source directory at the first level,
# i.e., <src_dir>/paper.tex and not <src_dir>/level/paper.tex.
# Figure files can be scattered within the source directory, and the
# directory structure would also be copied. texprune then copies all
# the essential files from <src_dir> into <dst_dir>.
#
# Obviously, texprune does not cover all use cases, and is heavily
# tailored to suit my workflow. For this reason, it should not be
# used without extensive testing.
#
# DEPENDENCIES
#
# GNU coreutils, pdflatex(1), rsync(1), zip(1), and optionally
# latexmk(1), and pdfsizeopt(1) or gs(1).
#
# SEE ALSO
#
# arXiv LaTeX Cleaner
# <https://github.com/google-research/arxiv-latex-cleaner/>
#
die() {
echo >&2 "$@"
exit 1
}
# Load options from environment if available.
[[ "$TEXPRUNE_OPTS" ]] && set -- $TEXPRUNE_OPTS "$@"
# First round of option processing.
archive="no"
bbl="no"
force="no"
optimize="no"
scrub="no"
merge_bbl="no"
while getopts ":fszbcm" opt
do
case "$opt" in
b) bbl="yes" ;;
m) merge_bbl="yes" ;;
c) optimize="yes" ;;
f) force="yes" ;;
s) scrub="yes" ;;
z) archive="yes" ;;
:) die "${0##*/}: -$OPTARG requires an argument" ;;
\?) die "${0##*/}: -$OPTARG is not a valid option" ;;
esac
done
shift $(( OPTIND - 1 ))
pushd() {
command pushd "$@" >/dev/null
}
popd() {
command popd "$@" >/dev/null
}
# Get real path of <file> after descending into <dir>.
# Synopsis: get_path <dir> <file>
get_path() {
pushd "$1"
readlink -e -- "$2"
popd
}
# Scrub .tex files by removing comments.
# Synopsis: scrub_tex <file>
scrub_tex() {
sed -i -e '
# What are the chances that your .tex file contains the word
# __TEXSTRIP_PERCENT?
s/%[ \t]*$/__TEXSTRIP_PERCENT_EOL/
s/\\%/__TEXSTRIP_PERCENT/g
/^[ \t]*%/d
s/%.*//
s/[ \t]\\{1,\}$//
s/__TEXSTRIP_PERCENT_EOL/%/
s/__TEXSTRIP_PERCENT/\\%/g
' "$@"
}
# Get main .tex file from a source directory.
# Synopsis: get_main_tex <dir>
get_main_tex() {
while IFS= read -r name
do
grep -q '\\documentclass' "$name" && {
echo "$name"
return
}
done < <(find "$1" -maxdepth 1 -type f -name '*.tex')
die "${0##*/}: no main .tex file found"
}
# Get included figures.
# Synopsis: get_figures <dir>
get_figures() {
pushd "$1"
while IFS= read -r log
do
# Ignore log files not associated with a .tex file.
[ -f "${log%.*}.tex" ] || continue
# Regex may need to be tweaked.
# This seems to work right now.
sed -n 's|^File: \(.*\) Graphic file.*|\1|p' "$log"
done < <(find . -maxdepth 1 -type f -name '*.log')
popd
}
# Compare the sizes of two files.
# Synopsis: compare_size <file1> <file2>
compare_size() {
a=$(stat -c%s "$1")
b=$(stat -c%s "$2")
printf >&2 "${0##*/}: reduced size of '${1##*/}' by %s%%\n" \
$(echo "scale=2; ($a - $b)*100/$a" | bc)
}
# Optimize PDFs using GhostScript or pdfsizeopt.
# Synopsis: pdf_optimize <input> <output>
pdf_optimize() {
if command -v pdfsizeopt &>/dev/null
then
pdfsizeopt "$1" "$2" &>/dev/null && compare_size "$1" "$2"
elif command -v gs &>/dev/null
then
gs 2>/dev/null \
-sDEVICE=pdfwrite \
-dBATCH \
-dNOPAUSE \
-dQUIET \
-dSAFER \
-dAutoRotatePages=/None \
-dEmbedAllFonts=true \
-dSubsetFonts=true \
-dCompatibilityLevel=1.4 \
-dPDFA=2 \
-dPDFACompatibilityPolicy=1 \
-sOutputFile="$2" "$1" && compare_size "$1" "$2"
else
cp "$1" "$2"
fi
return $?
}
# Copy figures from <src_dir> to <dst_dir>
# Synopsis: cp_figures <src_dir> <dst_dir>
cp_figures() {
while IFS= read -r name
do
src=$(get_path "$1" "$name")
outdir="$2/$(dirname "$name")"
tar="$outdir/$(basename "$name")"
mkdir -p "$outdir"
if [[ "${src##*.}" == "pdf" ]] && [[ "$optimize" == "yes" ]]
then
echo >&2 "${0##*/}: optimizing and copying '${src##*/}'"
pdf_optimize "$src" "$tar" || cp "$src" "$tar"
else
cp "$src" "$tar"
fi
done < <(get_figures "$1")
}
# Main function.
# Synopsis: cp_tree <src_dir> <dst_dir>
cp_tree() {
pushd "$1"
main=$(get_main_tex .)
# Start with a "clean" tree.
rm -rf *.log *.aux *.bbl *.blg *.out latex.out
# Compile main LaTeX to ensure the presence of *.log and *.bbl files.
if command -v latexmk &>/dev/null
then
echo >&2 "${0##*/}: compiling main .tex file '$main' using latexmk"
latexmk -norc -pdf -ps- -dvi- -C &>/dev/null
latexmk -norc -pdf -ps- -dvi- -pdflatex='pdflatex -interaction=nonstopmode' "$main" &>/dev/null
else
echo >&2 "${0##*/}: compiling main .tex file '$main' using pdflatex"
rm -f *.log *.aux *.bbl
pdflatex -interaction=nonstopmode "$main" &>/dev/null
bibtex -terse "${main%.*}" &>/dev/null
pdflatex -interaction=nonstopmode "$main" &>/dev/null
pdflatex -interaction=nonstopmode "$main" &>/dev/null
fi
popd
args=(
--archive
--copy-links
--prune-empty-dirs
# Descent to a maximum depth of 1 using --exclude='/*/'
--exclude='/*/'
)
if [[ "$bbl" = "yes" ]] || [[ "$merge_bbl" = "yes" ]]
then
args+=( --exclude='*.bib' --include='*.bbl' )
else
args+=( --exclude='*Notes.bib' --include='*.bib' )
fi
args+=(
--include='*.bbx'
--include='*.bst'
--include='*.cbx'
--include='*.cls'
--include='*.def'
--include='*.dtx'
--include='*.fd'
--include='*.ins'
--include='*.sty'
--include='*.tex'
--include='*/'
--exclude='*'
)
# Trailing slash after source is important to copy
# just the contents.
rsync "${args[@]}" -- "$1/" "$2/"
if [[ "$scrub" = "yes" ]]
then
pushd "$2"
while IFS= read -r name
do
scrub_tex "$name"
done < <(find -type f -name '*.tex')
popd
fi
cp_figures "$1" "$2"
if [[ "$merge_bbl" = "yes" ]]
then
pushd "$2"
main=$(get_main_tex .)
bbl="${main%.*}.bbl"
if [[ -f "$bbl" ]]
then
tmp="texprune-merged-$$.tex"
sed '/\\bibliography{[^}]*}/Q' "$main" >>"$tmp"
echo -e '\n% --- bibliography --- %\n' >>"$tmp"
cat "$bbl" | sed '/^%/d' >>"$tmp"
echo -e '\n% -------------------- %\n' >>"$tmp"
sed '1,/\\bibliography{[^}]*}/d' "$main" >>"$tmp"
mv -f "$tmp" "$main"
rm -f "$bbl"
else
echo >&2 "${0##*/}: .bbl file not found; not merging"
fi
popd
fi
}
if [[ $# -lt 2 ]]
then
die "usage: ${0##*/} [-bcfsz] <src_dir> <dst_dir>"
else
[[ -d "$1" ]] || die "${0##*/}: source '$1' is not a valid directory"
if [[ -e "$2" ]]
then
if [[ "$force" = "no" ]]
then
die "${0##*/}: target '$2' exists; refusing to overwrite"
else
rm -rf "$2"
fi
fi
cp_tree "$1" "$2"
echo >&2 "${0##*/}: '$1' -> '$2'"
if [[ "$archive" = "yes" ]]
then
out="${2%/}.zip"
[[ -f "$out" ]] && rm -f "$out"
pushd "$(dirname "$out")"
zip -q "$out" -r "$(basename "$2")"
popd
echo >&2 "${0##*/}: '$2' -> '$out'"
fi
fi