-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflattenPaper
More file actions
executable file
·283 lines (243 loc) · 9.85 KB
/
Copy pathflattenPaper
File metadata and controls
executable file
·283 lines (243 loc) · 9.85 KB
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
#/bin/bash
function help () {
echo " Usage: flattenPaper texFile pkgPrefix figsDir [figsDirAdditional]";
echo " Example usage: flattenPaper paper my figs images pics";
echo " Here, 'texFile' is a .tex file, 'pkgPrefix' the prefix for any private macro files and 'figsDir' a directory that contains the figures. 'figsDir' is also used in the flattened version of the paper and will contain the .pdf files of the figures. If additional directories for figure sources are used, they can be specified using the optional parameter 'figsDirAdditional'. The directory name 'build' is not allowed";
echo "";
echo "Options:";
echo " -f: output directory is flattened"
echo " -h: shows help";
echo " -i: adds IEEE copyright notice";
echo " -q: enables quiet output";
echo "";
echo " Supported source files:";
echo " - *.tex, *.sty, *.cls, *.clo, *.bib";
echo "";
echo " - output .tex file is written to paperFlattened/flattened.tex";
echo "";
echo " author: Thorben Casper";
echo " created in October 2016";
}
VERSION=0.0.1
QUIET=0
IEEE=0
FLATDIR=0
USAGE="'Usage: flattenPaper texFile pkgPrefix figsDir'. For more details, call 'flattenPaper -h'"
# get directory of this file (to follow symlinks, this complicated multi-line form is required)
# see https://stackoverflow.com/questions/59895/get-the-source-directory-of-a-bash-script-from-within-the-script-itself/246128#246128
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
BASEDIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
# --- Options -----------------------------------------------------------------------------------------------
if [ $# == 0 ] ; then
echo $USAGE
exit 1;
fi
# getopts does not support long options, however, getopt does not work on, e.g., BSD systems
while getopts "fhiq" flag; do
case $flag in
f) FLATDIR=1;;
h) help
exit 0
;;
i) IEEE=1;;
q) QUIET=1;;
\?) echo $USAGE
exit 1
;;
esac
done
# shift positional parameters depending on number of optional parameters
shift $(($OPTIND - 1))
# --- Compatibility -----------------------------------------------------------------------------------------
# check installed software
hash latexpand 2>/dev/null || { echo >&2 "I require latexpand but it's not installed. Aborting."; exit 1; }
hash de-macro 2>/dev/null || { echo >&2 "I require de-macro but it's not installed. Aborting."; exit 1; }
# --- Parsing input -----------------------------------------------------------------------------------------
TEXFILE=$1
TEXFILE="${TEXFILE%.*}" # stripping filename extension if present
if [ $2 ]; then
PKGPREFIX=$2
else
PKGPREFIX=### # dummy, must not be matched when renaming .sty-files
fi
# FIGS: all directories of figure sources
# FIGS4PDF: directory to store final PDF files of figures
if [ $# -lt 3 ]; then
FIGS=images
FIGS4PDF=images
else
shift 2
FIGS=$@
FIGS4PDF=$1
fi
# --- Body --------------------------------------------------------------------------------------------------
# define directory for source files
SRC="$(dirname "$(readlink -f "$0")")/src"
# define directory for destination files
DEST=paperFlattened
# settings
flattened='flattened' # main .tex file for the $DEST directory
# remove $DEST directory after receiving user confirmation
if [ -d $DEST ]; then
echo -n "Do you want to delete the entire '$DEST' directory [y/n]? "
read answer
if [ $answer == "y" ]; then
rm -r $DEST
else
echo "$0 requires to delete $DEST before execution. Please rename or move it to a different place."
exit 1;
fi
fi
# check whether any duplicate .sty files are present in the tree below the current directory
DUPLICATES=$(find . -type f -name "*.sty" | sed 's#.*/##' | sort | uniq -d)
if [ ! -z "$DUPLICATES" ]; then
printf "ERROR: There must be no duplicate .sty-files in the tree below the current directory. Duplicates found:\n $DUPLICATES\n"
printf "exiting ...\n"
exit 1;
fi
# copy all required source files from paper directory to new $DEST directory
mkdir $DEST
cp -t $DEST *.tex *.cls *.clo *.bib *.csv *.pdf 2>>/dev/null
rm $DEST/$TEXFILE.pdf 2>>/dev/null
cp -t $DEST $BASEDIR/flatPaper.bst
find . -name '*.sty' -print0 | xargs -0 cp -t $DEST 2>>/dev/null
for currentDir in $FIGS
do
mkdir $DEST/$currentDir
cp -r $currentDir/* $DEST/$currentDir/ 2>>/dev/null
done
# support for \loadglsentries
sed -i 's/\\loadglsentries/\\input/g' $TEXFILE.tex
# flatten the latex code using latexpand and keep comments to subsequently flatten bib
latexpand --keep-comments $TEXFILE.tex > $DEST/$flattened.tmp
cd $DEST
mv $GLSENTRIES.tex $GLSENTRIES.tmp 2>>/dev/null
rm *.tex 2>>/dev/null
mv $flattened.tmp $flattened.tex 2>>/dev/null
mv $GLSENTRIES.tmp $GLSENTRIES.tex 2>>/dev/null
# create build directory
mkdir build
mkdir build/$FIGS4PDF
mkdir build/build
mkdir build/build/$FIGS4PDF
# flatten bib
$SRC/flattenBib $flattened $QUIET
# apply latexpand again to remove comments
tmp=$(mktemp)
latexpand $flattened.tex > $tmp
mv $tmp $flattened.tex
# calls tikzternalize to produce .pdf files for all used tikzpictures in order to hide the source codes
$SRC/tikzternalize $flattened.tex $FIGS4PDF $FLATDIR $QUIET
if [ $? = 1 ]; then echo "something went wrong" && exit 1; fi
sed -i '/\\input{tudcolours\.def}/d' $flattened.tex
# add IEEE copyright notice
if [ "$IEEE" = 1 ]; then
$SRC/addIEEEcopyright $flattened.tex
fi
# expand macros (de-macro requires "-private" postfix to be added to .sty files first)
if [ $PKGPREFIX != "###" ]
then
for f in $PKGPREFIX-*.sty; do mv "$f" "$(echo "$f" | sed "s/$PKGPREFIX-\(.*\)\./$PKGPREFIX-\1-private\./")"; done
fi
sed -i "s/\\usepackage\(\[\?.*\]\?\){$PKGPREFIX-\(.*\)}/\\\\usepackage\1{$PKGPREFIX-\2-private}/g" $flattened.tex
sed -i 's/\\\\usepackage/\\usepackage/g' $flattened.tex # correct problem introduced by previous line
$SRC/rmLatexPackageOptions $flattened.tex
find . -name '*.sty' -print0 | xargs -0 sed -i 's/\\newcommand\*/\\newcommand/g' 2>>/dev/null # remove star from \newcommand since not supported by de-macro
find . -name '*.sty' -print0 | xargs -0 sed -i 's/\\renewcommand\*/\\renewcommand/g' 2>>/dev/null # remove star from \renewcommand since not supported by de-macro
$SRC/flattenUsepackage $flattened.tex
if [ -f "$PKGPREFIX-acronyms-private.sty" ]; then
$SRC/flattenGlossary $flattened.tex "$PKGPREFIX-acronyms-private.sty"
fi
$SRC/de-macroFITbow $flattened.tex
de-macro $flattened.tex >>/dev/null
mv $flattened-clean.tex $flattened.tex
rm $flattened # remove unused by de-macro created binary file
rm $PKGPREFIX-*-private.sty *-private.sty 2>>/dev/null
# flatten colors (not supported right now)
#flattenColors $flattened.tex
# remove not used standalone, todonotes, pgfplots, circui(tikz), graphicspath and tikzexternalize settings
sed -i '/\\usepackage\[\?.*\]\?{standalone}/d' $flattened.tex
sed -i '/\\usepackage\[\?.*\]\?{todonotes}/d' $flattened.tex
sed -i 's/\\todo.*{.*}//g' $flattened.tex
sed -i '/\\graphicspath{{/d' $flattened.tex
sed -i '/\\usepgfplotslibrary{external}/d' $flattened.tex
sed -i '/\\tikzexternalize\[/d' $flattened.tex
sed -i '/\\tikzset{external/d' $flattened.tex
# remove options from pgfplots and circui(tikz) to avoid option clash
sed -i 's/\\usepackage\[\?.*\]\?{pgfplots}/\\usepackage{pgfplots}/g' $flattened.tex
sed -i 's/\\usepackage\[\?.*\]\?{\(.*tikz\)}/\\usepackage{\1}/g' $flattened.tex
# remove any \let\xxx\undefined lines
sed -i '/\\let.*\\undefined/d' $flattened.tex
# remove any consecutive blank lines
tmp=$(mktemp)
cat -s $flattened.tex > $tmp
mv $tmp $flattened.tex
# remove build directory
rm -r build
# clean up figs directory
cd $FIGS4PDF
mkdir tmp
mv -t tmp fig*.pdf fig*.eps fig*.png fig*.jpg 2>>/dev/null
mv tmp ..
rm -r ./* 2>>/dev/null
mv ../tmp/* . 2>>/dev/null
rmdir ../tmp
rm *eps-converted-to.pdf 2>>/dev/null
cd ..
# clean up figsStandalone directory
if [ -d figsStandalone ]; then
cd figsStandalone 2>>/dev/null
mkdir tmp
mv -t tmp fig*.pdf fig*.eps fig*.png fig*.jpg 2>>/dev/null
rm ./* 2>>/dev/null
mv tmp/* . 2>>/dev/null
rmdir tmp
rm *eps-converted-to.pdf . 2>>/dev/null
cd ..
rmdir figsStandalone 2>>/dev/null
fi
# clean up $DEST directory
rm ${flattened}_bib.tex *.abs *.aux *.auxlock *.bcf *.bib *.blg *.bst *.glo *.ist *.log *.out *.run.xml *.thm *.spl *.suc *.syc *.upa *.upb 2>>/dev/null
for currentDir in $FIGS
do
if [ $currentDir != $FIGS4PDF ] || [ "$FLATDIR" = 1 ]; then
rm -r $currentDir
elif [ $currentDir = $FIGS4PDF ]; then
rmdir $FIGS4PDF 2>>/dev/null
fi
done
if [ "$FLATDIR" = 0 ]; then
rm *.pdf 2>>/dev/null
fi
rm -r tabs 2>>/dev/null
# write compile file
echo "#!/bin/bash" >> compile
echo "echo 'compiling PDF file ...'" >> compile
if [ "$QUIET" = 0 ]; then
echo "pdflatex "$flattened.tex"" >> compile
echo "pdflatex "$flattened.tex"" >> compile
else
echo "pdflatex "$flattened.tex" >>/dev/null" >> compile
echo "pdflatex "$flattened.tex" >>/dev/null" >> compile
fi
echo "rm *.abs *.aux *.auxlock *.bcf *.bib *.blg *.bst *.glo *.ist *.log *.out *.spl *.suc *.syc *.thm *.upa *.upb 2>>/dev/null" >> compile
echo "echo 'finished compiling PDF.'" >> compile
chmod +x compile
# execute compile
./compile
# write zipme file
echo "#!/bin/bash" >> zipme
echo "rm -v "$flattened.zip" 2>>/dev/null" >> zipme
echo "zipping flattened paper ..."
echo "zip" $flattened.zip $flattened.tex *.cls *.csv *.pdf $FIGS4PDF"/* >>/dev/null" >> zipme
echo "finished zipping flattened paper."
chmod +x zipme
# execute zipme
./zipme
echo "successfully flattened paper."
# -----------------------------------------------------------------------------------------------------------