-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenlatexdoc
More file actions
executable file
·280 lines (248 loc) · 9.66 KB
/
Copy pathgenlatexdoc
File metadata and controls
executable file
·280 lines (248 loc) · 9.66 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
#!/bin/bash
usage() { echo "$(basename $0) [-f <a4|a5|b4|b5|c4|c5>] [-m <breaks|rightbreaks|cont>] [-j <book|dual>] [-i <header|footer|body>] <filename>.md
- opciones:
FORMATO
-f default: formato libro (6x9in)
-f a5: Formato de página A5
-f a4: Formato de página A4
-g geometry: listado de parámetros de geometría en formato latex. ej: 'margin=2.3cm, footskip=1.14cm, bmargin=2.67cm'
-s escala: Factor de ampliación.
-o offset vertical: valor decimal en cm
-l linespace: por defecto 1
METADATOS
-t título a incluir en los metadatos y nombre del pdf
-a autor a incluir en los metadatos del pdf
MODO
-m cont[inuous]: todo el texto contínuo, sin saltos de página
-m breaks: partes y capítulos con saltos de página (modo por defecto)
-m rightbreaks: partes y capítulos con saltos de página y comenzando siempre en la parte derecha
JUNTAR
-j dual: junta 2 páginas en cada A4 con ordenación estándar
-j book: pone 2 páginas en cada A4 en formato folleto
-b box: mostrar marco alrededor de cada página (sólo se muestra cuando hay 2 páginas por A4)
INCLUIR
-i especifica que partes se incluyen en el documento (se pueden escoger varias especificando varias veces el parámetro)
- plantillas .tex (a incluir en el mismo directorio que el fichero .md):
- header.tex: páginas iniciales del documento en formato latex
- footer.tex: páginas finales del documento en formato latex" 1>&2; exit 1; }
dependencies() {
if ! $(gem list | grep kramdown >/dev/null) ||
! dpkg -s texlive texlive-latex-recommended texlive-latex-extra texlive-lang-spanish texlive-extra-utils evince ruby-full &> /dev/null
then
echo -e "Some required dependencies were not found. Would you like to install them? (y/n) \c"
read
if [[ "$REPLY" = "y" ]]; then
sudo apt-get install -y texlive texlive-latex-recommended texlive-latex-extra texlive-lang-spanish
sudo apt-get install -y texlive-extra-utils
sudo apt-get install -y evince
sudo apt-get install -y ruby-full
sudo gem install kramdown
fi
fi
}
##### PARSEO DE PARÁMETROS RECIBIDOS #####
# definición de parámetros por defecto
mode=scrbook
f=default
j=default
m=default
s=default
included_args=()
included=()
included+=('header')
included+=('footer')
included+=('body')
# parámetros recibidos
while getopts ":f:i:j:m:s:t:a:o:g:l:b" option; do
case "${option}" in
f)
f=${OPTARG}
if [[ "$f" != 'a4' && "$f" != 'a5' && "$f" != 'b4' && "$f" != 'b5' && "$f" != 'c4' && "$f" != 'c5' ]]; then usage; fi
;;
g)
g=${OPTARG}
;;
m)
m=${OPTARG}
if [[ "$m" != 'cont' && "$m" != 'breaks' && "$m" != 'rightbreaks' ]]; then usage; fi
;;
j)
j=${OPTARG}
if [[ "$j" != 'book' && "$j" != 'dual' ]]; then usage; fi
;;
i)
i=${OPTARG}
if [[ "$i" != 'header' && "$i" != 'footer' && "$i" != 'body' ]]; then usage; fi
included_args+=($i)
;;
s)
s=${OPTARG}
if [[ ! "$s" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then usage; fi
;;
o)
o=${OPTARG}
# if [[ ! "$o" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then usage; fi
;;
l)
l=${OPTARG}
# if [[ ! "$o" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then usage; fi
;;
b)
b=true
;;
t)
t=${OPTARG}
if [[ -z "$t" ]]; then usage; fi
;;
a)
a=${OPTARG}
if [[ -z "$a" ]]; then usage; fi
;;
*)
usage
;;
esac
done
# si se han especificado por parametro partes a incluir sobreescrimos las que vienen por defecto
if [ ! -z $included_args ]; then
included=(${included_args[*]})
fi
# comprobamos que el fichero pasado por parámetro exista
shift $(( OPTIND - 1 ))
if [[ ! -f $@ || -z $@ || ! $(echo $@ | grep '.md') ]]; then
usage
fi
##### PREPARACIÓN ENTORNO #####
# comprobación de dependencias
dependencies
filename=`basename -s .md $@`
dirname=`dirname $@`
# cambio al directorio de trabajo
cd "$dirname"
# limpieza de ficheros generados en ejecuciones anteriores
rm "$filename".pdf "$filename".ps 2> /dev/null
##### PARSEO DE PARÁMETROS
if [[ ! -z "$t" ]]; then title="$t"; else title="$filename"; fi
if [[ ! -z "$a" ]]; then author="$a"; else author="-"; fi
if [[ ! -z "$g" ]]; then geometry="$g,"; else geometry=""; fi
if [[ ! -z "$l" ]]; then linespace="\renewcommand{\baselinestretch}{$l}\normalsize"; else linespace=""; fi
openany=''
paper=''
if [[ "$m" == 'cont' ]]; then
mode=scrartcl
else
if [[ "$f" == *"4"* ]]; then
mode=scrartcl
else
# komastyle="\addtokomafont{chapterentry}{\mdseries}\addtokomafont{chapterentry}{\rmfamily}"
komastyle=""
fi
fi
if [[ "$m" != 'rightbreaks' ]]; then
openany=',openany'
fi
##### CONVERSIÓN DE KRAMDOWN A LATEX #####
kramdown -o latex --template document "$filename".md > "$filename".tex
##### POSTPROCESO DEL FICHERO TEX #####
# se cambia regla horizontal por asteriscos
sed -i 's/\\rule{3in}{0.4pt}/* * */g' "$filename".tex
# puntos suspensivos sin espacios
sed -i 's/\\ldots{}/.../g' "$filename".tex
# guiones largos más largos
# sed -i 's/--/---/g' "$filename".tex
# espacio vertical
sed -i 's/\/\/\//\\bigskip\\bigskip\\bigskip/g' "$filename".tex
# markup for manual adjustment of vbox badness
sed -i 's/beginsloppy/\\begin{sloppypar}/g' "$filename".tex
sed -i 's/endsloppy/\\end{sloppypar}/g' "$filename".tex
##### INCLUIMOS PARTES SELECCIONADAS SEGÚN PARÁMETROS #####
sed -n '/\\begin{document}/,/\\end{document}/P' "$filename".tex | head -n-1 | tail -n+2 > body.tex
# añadimos cabecera (latex)
cat <<EOF > "$filename".tex
\documentclass
\usepackage[utf8x]{inputenc}
\usepackage{listings}
\usepackage{microtype}
\usepackage{pdfpages}
\usepackage[$geometry paperwidth=6in, paperheight=9in]{geometry}
$linespace
\usepackage[spanish]{babel}
\usepackage[pdftex,
pdfauthor={$author},
pdftitle={$title}]{hyperref}
\setcounter{footnote}{0}
\setlength{\emergencystretch}{2pt}
\makeatletter
\@ifundefined{date}{}{\date{}}
\makeatother
\renewcommand{\partname}{Parte}
\begin{document}
EOF
# \raggedbottom
# \renewcommand{\baselinestretch}{2}\normalsize
# \usepackage[T1]{fontenc}
if [[ " ${included[@]} " =~ " header " ]]; then cat header.tex >> "$filename".tex 2> /dev/null; fi
if [[ " ${included[@]} " =~ " body " ]]; then cat body.tex >> "$filename".tex 2> /dev/null; fi
if [[ " ${included[@]} " =~ " footer " ]]; then cat footer.tex >> "$filename".tex 2> /dev/null; fi
# añadimos marca de fin de documento (latex)
echo '\end{document}' >> "$filename".tex
##### FORMATO A4/A5/LIBRO/ARTICULO SEGÚN PARÁMETROS #####
bookstructure() {
# formato a5, si soporta capítulos a diferencia de scrartcl
sed -i 's/\\subsection{\([^}]*\)}.*$/\\chapter*{\1}\n\\addcontentsline{toc}{chapter}{\1}/g' "$filename".tex
# cambiamos section por parte
sed -i 's/\\section{\([^}]*\)}.*$/\\part*{\1}\n\\addcontentsline{toc}{part}{\1}/g' "$filename".tex
}
articlestructure() {
# borramos las partes, para ahorrar espacio
# sed -i 's/\\section{\([^}]*\)}.*$//g' "$filename".tex
sed -i 's/\\section{\([^}]*\)}.*$/\\section*{\1}\n\\addcontentsline{toc}{section}{\1}/g' "$filename".tex
# subsecciones, no capítulos
sed -i 's/\\subsection{\([^}]*\)}.*$/\\subsection*{\1}\n\\addcontentsline{toc}{subsection}{\1}/g' "$filename".tex
}
if [[ "$f" != 'default' ]]; then
paper=','"$f"'paper'
# si el formato no es 6x9, lo borramos de geometry
sed -i 's/paperwidth.*$/\]{geometry}/g' "$filename".tex
fi
# oneside puts number page on center, solution: better play with inner and outer margins..
# sed -i 's/\\documentclass.*$/\\documentclass[oneside,toc=chapterentrydotfill'"$openany"''"$paper"']{'"$mode"'}/g' "$filename".tex
sed -i 's/\\documentclass.*$/\\documentclass[twoside,toc=chapterentrydotfill'"$openany"''"$paper"']{'"$mode"'}/g' "$filename".tex
if [[ $mode == 'scrartcl' ]]; then
articlestructure
else
bookstructure
fi
##### CONVERSIÓN A PDF #####
notvalidparts() {
echo -e "Included parts not found or invalid syntax: $included\nPlease include your header.tex and/or footer.tex file in the same directory as your .md file"
exit 1
}
# se compila 2 veces para obtener TOC actualizada
pdflatex -draftmode "$filename".tex
pdflatex "$filename".tex
if [[ ! -f "$filename".pdf ]]; then notvalidparts; fi
##### MAQUETADO MEDIANTE PDFJAM #####
if [[ "$j" != 'default' ]]; then
if [[ ! -z "$b" ]]; then box=" --frame true "; else box=''; fi
landscape=' --landscape '
if [[ "$j" == 'dual' ]]; then
joinargs=' --nup 2x1 '
else
joinargs=' --signature 4 --preamble "\usepackage{everyshi}\makeatletter\EveryShipout{\ifodd\c@page\pdfpageattr{/Rotate 180}\fi}\makeatother" '
fi
eval "pdfjam --keepinfo $joinargs $box $landscape $filename.pdf"
mv "$filename"-pdfjam.pdf "$filename".pdf >/dev/null 2>&1
fi
if [[ ! -z $o || "$s" != 'default' ]]; then
if [[ "$s" != 'default' ]]; then scale="--noautoscale true --scale $s"; else scale=""; fi
if [[ ! -z "$o" ]]; then offset='--offset \"$s\"'; else offset=""; fi
if [[ "$f" == 'default' ]]; then fitpaper='--papersize {6in,9in}'; fi
if [[ "$f" != 'default' ]]; then fitpaper='--'"$f"'paper'; fi
if [[ "$j" != 'default' ]]; then fitpaper=''; fi
eval 'pdfjam --keepinfo $fitpaper $landscape $scale $offset "$filename".pdf'
mv "$filename"-pdfjam.pdf "$filename".pdf >/dev/null 2>&1
fi
nohup evince "$filename".pdf >/dev/null 2>&1 &
##### LIMPIEZA DE FICHEROS TEMPORALES #####
rm *.toc *.dvi *.aux *.out *.log "$filename"_temp.ps body.tex "$filename".tex "$filename"-pdfjam.pdf 2> /dev/null