-
Notifications
You must be signed in to change notification settings - Fork 1
/
autonomize.sh
executable file
·294 lines (269 loc) · 11.7 KB
/
autonomize.sh
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
#!/usr/bin/env bash
# By: Lilian BESSON
# Email: Lilian.BESSON[AT]ens-cachan[DOT]fr
# Date: 09-12-2014
#
# autonomize.sh, a small tool to be used with autonomize.sh to produce autonomous latex document from autotex-powered document (it adds all the necessary headers)
#
# Online: https://bitbucket.org/lbesson/bin/src/master/autonomize.sh
#
# More informations on autotex here: https://bitbucket.org/lbesson/bin/src/master/autotex
#
# Example of a LaTeX2e template to use with autonomize.sh :
# (http://perso.crans.org/besson/publis/latex/template_minimalist.tex)
#
# Licence: [GPLv3](http://perso.crans.org/besson/LICENCE.html)
#
version='0.4'
justClean="false"
doNothing="false"
batch="false"
compile="false"
clear
# echo -e "# Arguments are: ${magenta}\n$@${white}\n\n"
oridir="$(pwd)"
# Options parsing
for arg in "$@"; do
case "$arg" in
-v|-version|--version)
echo -e "autonomize.sh $version"
exit 0
;;
-h|-help|--help)
echo -e "${green}autonomize.sh${white} -help | [options]"
echo -e ""
echo -e "Produce autonomous latex document from autotex-powered document (it adds all the necessary headers)."
echo -e ""
echo -e "Help:"
echo -e " ${yellow}-h${white} to print this help message (and quit)."
echo -e " ${yellow}-v${white} to print just the version of strapdown2pdf, and quit."
echo -e ""
echo -e "Options:"
echo -e " ${yellow}-c|-clean${white} just cleaning the source."
echo -e " ${yellow}-b|-batch${white} run without interactivity from the user."
echo -e " ${yellow}-p|-compile${white} after making the file autonomous, compile it with two uses of pdflatex."
echo -e " ${yellow}-a|-noaction${white} do nothing (just showing what will be done ?). FIXME"
echo -e ""
echo -e "autonomize.sh v$version : Copyrights: (c) Lilian Besson 2011-2014."
echo -e "Released under the term of the GPL v3 Licence (more details on http://perso.crans.org/besson/LICENSE.html)."
echo -e "In particular, autonomize.sh is provided WITHOUT ANY WARANTY."
exit 0
;;
-a|-noaction|--noaction)
echo -e "${cyan}Option '-a'${white}: do nothing (just showing)." # FIXME
doNothing="true"
shift
;;
-c|-clean|--clean)
echo -e "${cyan}Option '-c'${white}: Just cleaning the source."
justClean="true"
shift
;;
-b|-batch|--batch)
echo -e "${cyan}Option '-b'${white}: Running without interactivity from the user."
batch="true"
shift
;;
-p|-compile|--compile)
echo -e "${cyan}Option '-p'${white}: After making the file autonomous, compile it with two uses of pdflatex."
compile="true"
shift
;;
esac
done
template="${HOME}/template_minimalist.tex"
autonomize() {
arg="${1}"
if [ ! -f "$arg" ]; then
echo -e "${red}The file ${cyan}${u}'${arg}'${U}${red} is NOT HERE : I skip it ...${white}"
return 3
fi
if [ "$(grep "\\documentclass" "$arg")" ]; then
echo -e "${red}The file ${cyan}${u}'${arg}'${U}${red} seems to be autonomous already (it contains a \\documentclass) : I skip it ...${white}"
return 4
fi
f="$(basename "$1")"
echo -e "${cyan}f = ${f}${white}"
dossier="$(dirname "$1")"
echo -e "${cyan}dossier = ${dossier}${white}"
dossier="${dossier#./}"
name="${arg%.en.tex}"
name="${name%.fr.tex}"
name="${name%.tex}"
name="${name%__autonomize}" # better !
# Try to change the template regarding the language
echo -e "We have f='${f}', and {f%.fr.tex}.fr.tex='${f%.fr.tex}.fr.tex', and {f%.en.tex}.en.tex='${f%.en.tex}.en.tex'..."
langf="en"
if [ "${f%.fr.tex}.fr.tex" = "$f" ]; then
langf="fr"
echo -e "Switching language from default (en) to ${green}fr (French)${white}..."
elif [ "${f%.en.tex}.en.tex" = "$f" ]; then
langf="en"
echo -e "Switching language from default (en) to ${green}en (English)${white}..."
fi
out="${name}__autonomize.${langf}.tex"
echo -e "${cyan}out = ${out}${white}"
outf="$(basename "$out")"
echo -e "${cyan}outf = ${outf}${white}"
mv -vf "${out}" /tmp/
##
## Looking for Title, Size, PoliceSize
##
# If PDF, find the appropriate TeX source
f="${f//.pdf/.tex}"
# If .tex was not there, add it
f="${f%.tex}.tex"
# Special hack. DURTY : FIXME !
if [ "${dossier:0:6}" = "/home/" ]; then
p="$(pwd)/"
else
p="$(pwd)/${dossier}"
fi
echo -e "Working with $u'$f'$U on $blue'$p'$white."
cd "${p}"
title="$(grep "%autotex% Titre: " "${p}/${f}")"
title="${title#%autotex% Titre: }"
if [ "X$title" = "X" ]; then
title="$(grep "%autotex% Title: " "${p}/${f}")"
title="${title#%autotex% Title: }"
fi
echo -e "Title: $green${title}$white"
scale="$(grep "%autotex% Scale: " "${p}/${f}")"
scale="${scale#%autotex% Scale: }"
scale="${scale:-0.70}"
echo -e "Scale: $green${scale}$white"
policesize="$(grep "%autotex% PoliceSize: " "${p}/${f}")"
policesize="${policesize#%autotex% PoliceSize: }"
policesize="${policesize:-11pt}"
echo -e "PoliceSize: $green${policesize}$white"
##
## End of detection for title, police size, scale
##
##
## First n lines of the template
##
if [ "${justClean}" = "false" ]; then
firstNlines=$(grep -n 'input{MyFileNameToChangeWithAutonomizeSh.tex}' "${template}" | grep -o "^[0-9]*")
firstNlines=$(( firstNlines - 2 ))
echo -e "${yellow}Using the first ${firstNlines} lines of the template ${template}."
# cat "${template}" > "${out}"
## From autotex, change title, police size, scale
if [ "${doNothing}" = "false" ]; then # FIXME
head -n ${firstNlines} "${template}" \
| grep -v "^%%%%.*$" \
| sed s/"scale=[0-9\.]*\]{geometry}"/"scale=${scale}]{geometry}"/ \
| sed s/"11pt\]{article}"/"${policesize}]{article}"/ \
| sed s#MyTitleToChangeWithAutonomizeSh#"${title}"# \
> "${outf}"
fi
fi
##
## FIXME: + Tikz, + theorem, lemma, remark, remarque, demoBox, demoEn, etc
echo -e "${yellow}From the file ${arg}, we change it a little bit, to make it more standard and more autonomous"
##
## Edit the input file
## From naereen.sty, slowly change every macros, clean up macro and non conventional files
if [ "${doNothing}" = "false" ]; then # FIXME
cat "${p}/${f}" \
| sed s/'\\begin{arab}'/'\\begin{enumerate}[label=(\\arabic*)]'/g \
| sed s/'\\begin{roma}'/'\\begin{enumerate}[label=(\\roman*)]'/g \
| sed s/'\\begin{Roma}'/'\\begin{enumerate}[label=(\\Roman*)]'/g \
| sed s/'\\begin{enumalph}'/'\\begin{enumerate}[label=(\\alph*)]'/g \
| sed s/'\\begin{enumAlph}'/'\\begin{enumerate}[label=(\\Alph*)]'/g \
| sed s/'\\end{\(arab\|roma\|Roma\|enumalph\|enumAlph\)}'/'\\end{enumerate}'/g \
| sed s/'\\arccosh'/'\\mathop{\\mathrm{arccosh}}'/g \
| sed s/'\\arcsinh'/'\\mathop{\\mathrm{arcsinh}}'/g \
| sed s/'\\arctanh'/'\\mathop{\\mathrm{arctanh}}'/g \
| sed s/'=='/'\\mathrel{\\stackrel{\\smash{\\scriptscriptstyle\\mathrm{def}}}{=}}'/g \
| sed s/'\\prof\(vijay\|arya\|satya\)\({}\)\?'/''/g \
| sed s/'<==>'/'\\mathrel{\\Longleftrightarrow}'/g \
| sed s/'--->'/'\\mathrel{\\Longleftrightarrow}'/g \
| sed s/'-->'/'\\mathrel{\\longleftrightarrow}'/g \
| sed s/'<=>'/'\\mathrel{\\Leftrightarrow}'/g \
| sed s/'==>'/'\\mathrel{\\Rightarrow}'/g \
| sed s/'<='/'\\leq'/g \
| sed s/'>='/'\\geq'/g \
| sed s/'\\geqlatex'/'>=latex'/g \
| sed s/'\\leqlatex'/'<=latex'/g \
| sed s/'|->'/'\\mathrel{\\mapsto}'/g \
| sed s/'+oo'/'+\\infty'/g \
| sed s/'-oo'/'-\\infty'/g \
| sed s/'\\DUtransition\({}\)\?'/'\\hspace*{\\fill}\\hrulefill\\hspace*{\\fill}\\vskip 0.5\\baselineskip %% Horizontal line'/g \
| grep -v "^%autotex%.*$" \
| sed s/'\\ie'/'\\emph{i.e.}~'/g \
| sed s/'\\eg'/'\\emph{e.g.}~'/g \
| sed s/'\\ssi'/'\\textbf{ssi}~'/g \
| sed s/'\\iff'/'\\textbf{iff}~'/g \
| sed s/'\\it{'/'\\textit{'/g \
| sed s/'\\tt{'/'\\texttt{'/g \
| sed s/'\\bf{'/'\\textbf{'/g \
| sed s/'\\sc{'/'\\textsc{'/g \
| sed s/'\\todo{'/'\\textcolor{red}{'/g \
| sed s/'\\inv{\(.*\)}'/'\\frac{1}{\1}'/g \
| sed s/'\\inv{\(.*\)}'/'\\frac{1}{\1}'/g \
| sed s/'\\inv{\(.*\)}'/'\\frac{1}{\1}'/g \
| sed s/'\\e^'/'\\mathrm{e}^'/g \
| sed s/'\\e '/'\\mathrm{e} '/g \
| sed s/'\\dx'/'\\mathrm{d}x'/g \
| sed s/'\\dy'/'\\mathrm{d}y'/g \
| sed s/'\\dz'/'\\mathrm{d}z'/g \
| sed s/'\\dt'/'\\mathrm{d}t'/g \
| sed s/'\\E '/'\\exists '/g \
| sed s/'\\V '/'\\forall '/g \
| sed s/'\\Z'/'\\mathbb{Z}'/g \
| sed s/'\\N'/'\\mathbb{N}'/g \
| sed s/'\\R'/'\\mathbb{R}'/g \
| sed s/'\\Q'/'\\mathbb{Q}'/g \
| sed s/'\\C'/'\\mathbb{C}'/g \
| sed s/'\\D'/'\\mathbb{D}'/g \
| sed s/'\\K'/'\\mathbb{K}'/g \
| sed s/'\\epsilon'/'\\varepsilon'/g \
| sed s/'\\IR'/'\\mathbb{R}'/g \
| sed s/'\\IC'/'\\mathbb{C}'/g \
| sed s/'\\IQ'/'\\mathbb{Q}'/g \
| sed s/'\\IZ'/'\\mathbb{Z}'/g \
| sed s/'\\IN'/'\\mathbb{N}'/g \
| sed s/'\\IF'/'\\mathbb{F}'/g \
| sed s/'\\score{\([0-9\.]*\)}'/''/g \
| sed s/'\\exer '/'\\item '/g \
| sed s/'\\bonus '/'\\item (\textbf{Bonus:})'/g \
>> "${outf}"
# | sed s/'\\cosh'/'\\mathop{\\mathrm{cosh}}'/g \
# | sed s/'\\sinh'/'\\mathop{\\mathrm{sinh}}'/g \
# | sed s/'\\tanh'/'\\mathop{\\mathrm{tanh}}'/g \
## Closing documents
if [ "${justClean}" = "false" ]; then
echo -e "\n\\\\end{document}\n%% End of the document ${arg}" >> "${outf}"
fi
echo -e "${green}Done: new file is ${out}.${white}"
if [ "${batch}" = "false" ]; then
colordiff "${p}/${f}" "${outf}"
subl "${outf}"
fi
if [ "${compile}" = "true" ]; then
# cd "${p}"
echo -e "${green}Compiling ${outf}${white}... (in pwd=${u}$(pwd)${U})${white}"
head -n1 "${outf}"
( pdflatex -interaction=batchmode "${outf}" && pdflatex -interaction=batchmode "${outf}" &>/dev/null ) \
|| echo -e "${red} Problem with the file ${arg} converted to ${outf}"
if [ -f "${outf%.tex}.pdf" -a "${batch}" = "false" ]; then
evince "${outf%.tex}.pdf" &> /dev/null &
fi
if [ -f "${outf%.tex}.pdf" ]; then
PDFCompress --no-notify "${outf%.tex}.pdf"
fi
mv -vf "${outf%.tex}.aux" "${outf%.tex}.out" "${outf%.tex}".synctex.gz* "${outf%.tex}.log" /tmp/
fi
notify-send "autonomize.sh" "Done for the file <b>${f}</b> --> <b>${out}</b>. $([ "${compile}" = "true" ] && echo "\nIt has been compiled to <b>${out%.tex}.pdf</b>") "
fi
cd "${oridir}"
}
autonomize "${1}"
shift
for finput in "$@"; do
echo -e "\n\n---------------------------"
# read # debug
echo -e "${blue}Generating the next file..."
autonomize "${finput}"
done
## End of autonomize.sh