-
Notifications
You must be signed in to change notification settings - Fork 0
/
docbook52pdf
executable file
·114 lines (84 loc) · 2.39 KB
/
docbook52pdf
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
#!/bin/bash
# Author: Jens Getreu
# apt install pandoc docbook5-xml docbook-xsl-ns xsltproc fop xmlto libxml2-utils xmlstarlet
#set -x
render () {
### parse args
#set -x
InPath="$1"
InFile="${InPath##*/}"
InFileExt="${InPath##*.}"
InBase="${InFile%.*}"
InDir="${InPath%/*}"
if [ "$InDir" = "$InPath" ] ; then
InDir="."
fi
OutPath="$2"
OutFile="${OutPath##*/}"
OutBase="${OutFile%.*}"
OutDir="${OutPath%/*}"
if [ "$OutDir" = "$OutPath" ] ; then
OutDir="."
fi
### Prepare
XmlFile="$OutBase.xml"
XmlPath="$OutDir/$XmlFile"
FoFile="$OutBase.fo"
FoPath="$OutDir/$FoFile"
PdfFile="$OutBase.pdf"
PdfPath="$OutDir/$PdfFile"
XslPath="$(realpath $InDir/docbook-xsl/fo-pdf.xsl)"
mkdir -p "$OutDir"
### Generate XML
# unfortunately the chain does not honor --number-section yet
if [ $InFileExt == 'xml' ]
then
cp "$InPath" "$XmlPath"
else
if ! pandoc -s -t docbook5 -o "$XmlPath" "$InPath"
then
echo Fatal error: Pandoc failed: \"$InPath\"
exit 1
fi
fi
# Workaround bug
# [Docbook5 Writer: produces invalid output when author is given ·
# Issue #6244 · jgm/pandoc](https://github.com/jgm/pandoc/issues/6244)
if grep -q '<author>' "$XmlPath" && ! grep -q '<personname>' "$XmlPath"
then
echo change
sed -i 's/<author>/<author><personname>/g' "$XmlPath"
sed -i 's/<\/author>/<\/personname><\/author>/g' "$XmlPath"
fi
### Validate
if ! xmlstarlet val --err \
--xsd /usr/share/xml/docbook/schema/xsd/5.0/docbook.xsd \
"$XmlPath"
then
echo Fatal error: docbook file \"$XmlFile\" is not valid.
exit 1
fi
### Generate .pdf
# xsltproc also take parameters e.g. --stringparam use.extensions 0\
cp -r "$InDir/images/" "$OutDir"
cd "$OutDir"
xsltproc --output "$FoFile" \
"$XslPath"\
"$XmlFile" && \
fop -fo "$FoFile" -pdf "$PdfFile" && \
rm "$FoFile" && \
if [ "$OutDir" != "$OutDir" ] ; then
rm "$XmlFile" && \
rm -r "images"
fi
}
### Main
# usage:
# render FILE [FILE]
# render report.md ./rendition/report.pdf
if [[ -n "${2/[ ]*\n/}" ]] ; then
OutPath="$2"
else
OutPath="${1%.*}.pdf" # $2 is empty
fi
render "$1" "$OutPath"