-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenepub
More file actions
executable file
·153 lines (124 loc) · 5.48 KB
/
Copy pathgenepub
File metadata and controls
executable file
·153 lines (124 loc) · 5.48 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
#!/bin/bash
usage() { echo "$(basename $0) [-i <header|footer|body>] <filename>.md
- opciones:
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 ruby-full latexml calibre &> /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 latexml calibre
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
included_args=()
included=()
included+=('header')
included+=('footer')
included+=('body')
# parámetros recibidos
while getopts ":i:" o; do
case "${o}" in
i)
i=${OPTARG}
if [[ "$i" != 'header' && "$i" != 'footer' && "$i" != 'body' ]]; then usage; fi
included_args+=($i)
;;
*)
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".epub 2> /dev/null
##### CONVERSIÓN DE KRAMDOWN A LATEX #####
sed 's/^#\ .*$//g' "$filename".md > "$filename"_temp.md
kramdown -o latex --template document "$filename"_temp.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
##### 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[a5paper]{scrbook}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage[spanish]{babel}
\usepackage{ragged2e}
\setcounter{footnote}{0}
\makeatletter
\@ifundefined{date}{}{\date{}}
\makeatother
\begin{document}
EOF
# \renewcommand{\contentsname}{{ÍNDICE}}
# \renewcommand{\partname}{Parte}
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 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
##### CONVERSIÓN A FICHERO FINAL #####
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
}
# sloppy solo sirve para el pdf, se quita
sed -i 's/beginsloppy//g' "$filename".tex
sed -i 's/endsloppy//g' "$filename".tex
latexml --dest="$filename".xml "$filename".tex >/dev/null 2>&1
sed -i 's/.*resource.*$/<resource src="myepub_styles.css" type="text\/css"\/>/g' "$filename".xml
sed -i 's/<para\ xml.*//g' "$filename".xml
sed -i 's/<\/para>//g' "$filename".xml
latexmlpost -dest="$filename".xhtml "$filename".xml >/dev/null 2>&1
# -splitpath="//div[contains(@class, 'ltx_role_newpage')]"
sed -i 's/.*ltx_page_footer.*$//g' "$filename".xhtml
sed -i 's/.*ltx_page_logo.*$//g' "$filename".xhtml
# add page breaks
sed -i 's/<div class="ltx_pagination ltx_role_newpage"\/>/<div style="page-break-before:always;"><\/div>/g' "$filename".xhtml
# add cover
# sed -i 's/<div class="ltx_document ltx_authors_1line">/<div class="ltx_document ltx_authors_1line"><div class="imgdivcont"><img src="..\/PD_cover\/PD_front_epub.png"\/><\/div><div style="page-break-before:always;"><\/div>/g' "$filename".xhtml
ebook-convert "$filename".xhtml "$filename".epub --cover='../PD_cover/PD_front_epub.png' --preserve-cover-aspect-ratio --language es --no-default-epub-cover --use-auto-toc --epub-toc-at-end --remove-paragraph-spacing \
--title 'Pensamiento divergente' --authors 'Jorge Hervás'
# todo: --page-breaks-before h5
( ebook-viewer "$filename".epub & )
##### LIMPIEZA DE FICHEROS TEMPORALES #####
rm "$filename"_temp.md body.tex "$filename".tex "$filename".xhtml "$filename".xml 2> /dev/null