-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate-docutils.sh
executable file
·498 lines (305 loc) · 11.9 KB
/
generate-docutils.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#!/bin/sh
# Copyright (C) 2010-2023 Olivier Boudeville
#
# This file is part of the Ceylan-Myriad library.
# Author: Olivier Boudeville [olivier (dot) boudeville (at) esperide (dot) com]
# This file is part of the Ceylan-Myriad library (although it does not depend on
# any of our own prerequisites), as it is used by this Myriad layer and the
# layers upward, and we do not want to introduce an extra dependency to
# Ceylan-Hull.
# Notes:
#
# - docutils has been finally preferred to txt2tags
#
# - beware of not running more than one instance of this script against the
# same RST source file: they would trigger at the same time (file saved) and
# each would remove the temporary files of the others, leading to spurious
# errors...
usage="Usage: $(basename $0) <target rst file> [--pdf|--all|<comma-separated path(s) to CSS file to be used, e.g. common/css/XXX.css,other.css>] [--icon-file ICON_FILENAME]
Generates a final document from specified docutils source file (*.rst).
By default, only the HTML output will be enabled (using any specified CSS file).
If the '--pdf' command-line option is specified, a PDF will be created instead (and thus no HTML output will be generated), whereas, if '--all' is specified, all output formats (i.e. HTML and PDF) will be created.
"
# Left out: --warnings=rst-warnings.txt --traceback --verbose --debug
# Can be removed for debugging: --quiet
docutils_common_opt="--report=error --tab-width=4 --no-generator --no-datestamp --no-source-link --strip-comments --syntax-highlight=short"
# Obtained from 'rst2html -h':
# (a real issue is to protect correctly parameters like '--math-output="aaa
# bbb"' - never working here with rst2html)
docutils_html_opt="${docutils_common_opt} --cloak-email-addresses --link-stylesheet --no-section-numbering"
# --math-output=MathML: (ERROR/3) Environment not supported! Supported
# --environment: "matrix".
# (ERROR/3) Unknown LaTeX command: overrightarrow
# --math-output=HTML: not the right matrices
# --math-output=MathJax: requires MathJax files or outbound accesses
# Adds:
# <script type="text/javascript"
# src="file:/usr/share/javascript/mathjax/MathJax.js?config=TeX-AMS_CHTML"></script>
# (not existing by default, corresponding to MathJax version 2, not version 3)
# --math-output="MathJax
# --https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js": works yet
# --performs a cross-scripting web access
#<script async src="https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-AMS-MML_CHTML"></script>
# --math-output=LaTeX: not interpreted
#one of "MathML", "HTML", "MathJax" or "LaTeX". Default: "HTML math.css"
#--math-output='MathJax https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'"
# Obtained from 'rst2latex -h':
doc_class=article
#doc_class=report
# Allows to directly add inlined graphics; \usepackage{amsmath} not strictly
# necessary apparently:
#
docutils_pdf_opt="${docutils_common_opt} --documentclass=${doc_class} --compound-enumerators --latex-preamble=\\usepackage{graphicx}"
latex_to_pdf_opt="-interaction nonstopmode"
begin_marker="---->"
# By default, generate HTML and not PDF:
do_generate_html=0
do_generate_pdf=1
css_base_opt="--link-stylesheet"
docutils_opt="${docutils_html_opt}"
docutils_html="$(which rst2html 2>/dev/null)"
#echo "arguments received: $*"
if [ -z "$1" ]; then
echo "${begin_marker} Error: no parameter given.
${usage}" 1>&2
exit 1
fi
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "${usage}"
exit 0
fi
rst_file="$1"
if [ -e "${rst_file}" ]; then
shift
if [ "$1" = "--pdf" ]; then
do_generate_html=1
do_generate_pdf=0
shift
elif [ "$1" = "--all" ]; then
do_generate_html=0
do_generate_pdf=0
shift
css_file_spec="$1"
# Might be for example "pygments-default.css,foobar.css":
if [ -n "${css_file_spec}" ]; then
shift
echo " Using CSS file spec ${css_file_spec}."
#css_opt="--stylesheet-path=${css_file_spec}"
css_opt="--stylesheet=${css_file_spec} ${css_base_opt}"
fi
docutils_html_opt="${docutils_html_opt} ${css_opt}"
else
css_file_spec="$1"
if [ -n "${css_file_spec}" ]; then
shift
#echo "Using CSS file spec ${css_file_spec}."
#css_opt="--stylesheet-path=${css_file_spec}"
css_opt="--stylesheet=${css_file_spec} ${css_base_opt}"
fi
docutils_html_opt="${docutils_html_opt} ${css_opt}"
fi
else
echo "${begin_marker} Error: file '$1' not found.
${usage}" 1>&2
exit 2
fi
if [ "$1" = "--icon-file" ]; then
shift
icon_file="$1"
shift
if [ ! -f "${icon_file}" ]; then
echo "${begin_marker} Error, specified icon file (${icon_file}) does not exist (current directory being $(pwd))." 1>&2
exit 55
else
echo " Using icon file '${icon_file}'."
fi
else
if [ -n "$1" ]; then
echo "${begin_marker} Error, unexpected parameter ($1).
${usage}" 1>&2
exit 60
fi
fi
if [ $do_generate_html -eq 0 ]; then
docutils_html="$(which rst2html 2>/dev/null)"
if [ -z "${docutils_html}" ]; then
echo "${begin_marker} Error: unable to find an executable tool to convert docutils files to HTML (rst2html)." 1>&2
exit 10
fi
fi
if [ $do_generate_pdf -eq 0 ]; then
docutils_latex="$(which rst2latex 2>/dev/null)"
if [ -z "${docutils_latex}" ]; then
echo "${begin_marker} Error: unable to find an executable tool to convert docutils files to LaTeX (rst2latex)." 1>&2
exit 11
fi
latex_to_pdf="$(which pdflatex 2>/dev/null)"
if [ -z "${latex_to_pdf}" ]; then
echo "${begin_marker} Error: unable to find an executable tool to convert LaTeX files to PDF (pdflatex)." 1>&2
exit 12
fi
fi
manage_rst_to_html()
# $1: name of the .rst file to convert to HTML.
# $2: name of the HTML target file
{
source="$1"
target="$2"
echo "${begin_marker} building HTML target ${target} from source"
# For some reason we have *never* been able to store the -math-output option
# in a separate variable. We have always:
# - either, if using ${test_opt}: 'rst2html: error: Maximum 2 arguments
# allowed.'
# - or if using "${test_opt}": '# (ERROR/3) math-output format ""mathjax"
# not supported falling back to "latex"
#test_opt='--math-output="MathJax file://hello"'
#test_opt="--math-output=\"MathJax file://hello\""
#${docutils_html} ${docutils_html_opt} ${test_opt} "${source}" "${target}"
# So finally it had to be specified literally:
#
# It supposes that MathJax is installed (for that refer to
# https://howtos.esperide.org/DocGeneration.html#rendering-mathematical-elements)
# and that typically a local symlink points to it (see the
# 'create-mathjax-symlink' make target): 'ln -s /usr/share/mathjax' or 'ln
# -s $HOME/Software/MathJax mathjax'; otherwise we default to a basic,
# non-MathJax output and the '* Unknown equation environment bmatrix' error
# is likely:
#
if [ -e "mathjax" ]; then
${docutils_html} ${docutils_html_opt} --math-output="MathJax mathjax/tex-mml-chtml.js" "${source}" "${target}"
else
echo "Warning: no local MathJax available, math formulas may be ugly." 1>&2
${docutils_html} ${docutils_html_opt} "${source}" "${target}"
fi
if [ ! $? -eq 0 ]; then
echo "${begin_marker} Error: HTML generation with ${docutils_html} failed for ${source}." 1>&2
exit 5
fi
if [ ! -f "${target}" ]; then
echo " Error, no target file '${target}' generated." 1>&2
exit 60
fi
# Better suffixed than prefixed, as a leading directory may be specified:
tmp_file="${target}.tmp"
if [ -n "${icon_file}" ]; then
icon_filename="$(basename ${icon_file})"
echo " Referencing the ${icon_filename} icon."
/bin/cat "${target}" | sed "s|</head>$|<link href=\"${icon_filename}\" rel=\"icon\">\n</head>|1" > "${tmp_file}"
if [ ! -f "${tmp_file}" ]; then
echo " Error, no temporary file '${tmp_file}' obtained." 1>&2
exit 61
fi
/bin/mv -f "${tmp_file}" "${target}"
# We also have to copy the referenced icon file in the target location:
target_dir="$(dirname ${target})"
target_icon_path="${target_dir}/${icon_filename}"
# As they can be the same path:
if [ ! -f "${target_icon_path}" ]; then
/bin/cp -f "${icon_file}" "${target_icon_path}"
fi
fi
echo " Adding the viewport settings."
#echo " Adding the viewport settings (from '${target}' to '${tmp_file}')."
# Apparently the viewport settings are strongly recommended in all cases,
# for mobile support:
#
/bin/cat "${target}" | sed 's|</head>$|<meta name="viewport" content="width=device-width, initial-scale=1.0">\n</head>|1' > "${tmp_file}"
if [ ! -f "${tmp_file}" ]; then
echo " Error, no temporary file '${tmp_file}' obtained." 1>&2
exit 62
fi
/bin/mv -f "${tmp_file}" "${target}"
echo " Generated file is: file://${PWD}/${target}"
}
manage_rst_to_pdf()
# $1: name of the .rst file to convert to PDF.
{
source="$1"
target="$2"
echo "${begin_marker} building PDF target ${target} corresponding to source ${source}"
# Never triggered:
if [ -f "${target}" ]; then
echo " (removing pre-existing target '${target}')"
/bin/rm -f "${target}"
fi
# Input extension is generally '.rst' (allows to remove only the final
# extension, even if there were dots in the base name):
#
tex_file=$(echo ${source} | sed 's|\.[^\.]*$|.tex|1')
#echo "Docutils command: ${docutils_latex} ${docutils_pdf_opt} ${source} ${tex_file}"
${docutils_latex} ${docutils_pdf_opt} ${source} ${tex_file}
res=$?
if [ ! ${res} -eq 0 ]; then
if [ ${res} -eq 1 ]; then
echo "${begin_marker} Warning: LaTeX generation returned code 1 for ${source}." 1>&2
else
echo "${begin_marker} Error: LaTeX generation failed for ${source}." 1>&2
exit 6
fi
fi
if [ ! -e "${tex_file}" ]; then
echo "${begin_marker} Error: generated TeX file '${tex_file}' could not be found, probably due to RST errors." 1>&2
exit 8
fi
rubber="$(which rubber 2>/dev/null)"
#prefer_rubber=0
prefer_rubber=1
if [ ${prefer_rubber} -eq 0 ] && [ -x "${rubber}" ]; then
# Our preferred build method; however it may enter infinite loops and/or
# output spurious errors like: '[depend] while making XX.pdf: file
# contents does not seem to settle':
#
echo "Using rubber for PDF generation"
${rubber} --pdf -v "${tex_file}"
res=$?
if [ ! ${res} -eq 0 ]; then
if [ ${res} -eq 1 ] || [ ${res} -eq 2 ]; then
echo "${begin_marker} Warning: PDF generation returned code ${res} for ${source}." 1>&2
else
echo "${begin_marker} Error: PDF generation failed for ${source} (error code: ${res})." 1>&2
exit 7
fi
fi
else
echo "Warning: using 'pdflatex' directly, instead of 'rubber'." 1>&2
# Run thrice on purpose, to fix links:
echo "LaTeX command: ${latex_to_pdf} ${latex_to_pdf_opt} ${tex_file}"
${latex_to_pdf} ${latex_to_pdf_opt} "${tex_file}" && \
${latex_to_pdf} ${latex_to_pdf_opt} "${tex_file}" && \
${latex_to_pdf} ${latex_to_pdf_opt} "${tex_file}"
res=$?
if [ ! ${res} -eq 0 ]; then
if [ ${res} -eq 1 ]; then
echo "${begin_marker} Warning: PDF generation returned code 1 for ${source}." 1>&2
else
echo "${begin_marker} Error: PDF generation failed for ${source} (error code: ${res})." 1>&2
exit 7
fi
fi
fi
#echo "target = ${target}"
if [ -f "${target}" ]; then
echo "PDF ready, in '$(pwd)/${target}'."
else
echo "Error, no PDF obtained (no '$(pwd)/${target}')."
fi
}
if [ ${do_generate_html} -eq 0 ]; then
target_html_file="$(echo ${rst_file} | sed 's|.rst$|.html|1')"
#echo "target_html_file = ${target_html_file}"
manage_rst_to_html "${rst_file}" "${target_html_file}"
fi
if [ ${do_generate_pdf} -eq 0 ]; then
target_pdf_file="$(echo ${rst_file} | sed 's|.rst$|.pdf|1')"
#echo "target_pdf_file = ${target_pdf_file}"
# PDF generator will not find includes (e.g. images) if not already in
# target dir:
#
current_dir="$(pwd)"
target_dir="$(dirname ${target_pdf_file})"
source_file="$(basename ${rst_file})"
target_file="$(basename ${target_pdf_file})"
cd "${target_dir}"
manage_rst_to_pdf "${source_file}" "${target_file}"
cd "${current_dir}"
fi