This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
fopub
executable file
·150 lines (128 loc) · 3.67 KB
/
fopub
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
#!/bin/bash
abspath() {
PATH_REL_DIRNAME=`dirname "$1"`
PATH_ABS_DIRNAME=`cd "$PATH_REL_DIRNAME"; pwd`
PATH_BASENAME=`basename "$1"`
if [ "$PATH_BASENAME" == "." ]; then
echo "$PATH_ABS_DIRNAME"
else
echo "$PATH_ABS_DIRNAME/$PATH_BASENAME"
fi
}
usage() {
echo "Usage: $0 [-t <path>] [-f FORMAT] [-H] FILE"
echo "Example: $0 -t /path/to/custom/docbook-xsl mydoc.xml"
}
sedi() {
sed --version >/dev/null 2>&1 && sed -i -- "$@" || sed -i "" "$@"
}
CONVERT_ARGS=""
while getopts ':f:hHt:' opt; do
case $opt in
f)
FORMAT=$OPTARG
;;
h)
usage
exit 0
;;
H)
CONVERT_ARGS="-param hyphenate true"
;;
t)
DOCBOOK_XSL_DIR=$OPTARG
;;
\?)
echo "$0: unrecognized option '$1'"
usage
exit 1
esac
done
shift $(( $OPTIND -1 ))
if [ ! -r "$1" ]; then
echo "$0: You must specify a DocBook v4.5 or DocBook v5 XML source file as the first command argument"
usage
exit 1
fi
SOURCE_FILE="$1"
SOURCE_EXTENSION="${SOURCE_FILE##*.}"
shift
if [ "$SOURCE_EXTENSION" != "xml" ] && [ "$SOURCE_EXTENSION" != "fo" ]; then
echo $0: Invalid source file name: "$SOURCE_FILE"
echo "$0: The source file name must end in .fo (XSL-FO) or .xml (DocBook)"
usage
echo "Try \`asciidoctor -b docbook $1\` to convert your AsciiDoc source file to DocBook"
exit 1
fi
if [ -z "$FORMAT" ]; then
FORMAT=pdf
fi
PRG_DIR=`dirname "$0"`
PRG_ABS_DIR=`abspath "$PRG_DIR"`
GRADLEW_CMD="$PRG_DIR/gradlew"
FOPUB_DIR="$PRG_DIR/build/fopub"
FOPUB_ABS_DIR="$PRG_ABS_DIR/build/fopub"
FOPUB_CMD="$FOPUB_DIR/bin/fopub"
# APP_DIR allows this script to invoke a distributed version of the application
if [ -z "$APP_DIR" ]; then
export APP_DIR="$FOPUB_DIR"
fi
if [ ! -e "$FOPUB_CMD" ]; then
echo Initializing application...
"$GRADLEW_CMD" -p "$PRG_DIR" -s -q -u installDist
if [ $? != 0 ]; then
echo Failed to initialize application
exit 1
fi
echo Application initialized!
fi
SOURCE_ROOTNAME="${SOURCE_FILE%.*}"
OUT_FILE="$SOURCE_ROOTNAME.$FORMAT"
DOCBOOK_DIR="$FOPUB_DIR/docbook"
if [ ! -z "$DOCBOOK_XSL_DIR" ]; then
DOCBOOK_XSL_ABS_DIR=`readlink -f "$DOCBOOK_XSL_DIR" 2>/dev/null`
if [ $? -ne 0 ]; then
DOCBOOK_XSL_ABS_DIR=`greadlink -f "$DOCBOOK_XSL_DIR" 2>/dev/null`
if [ $? -ne 0 ]; then
DOCBOOK_XSL_ABS_DIR="$DOCBOOK_XSL_DIR"
fi
fi
else
DOCBOOK_XSL_DIR="$FOPUB_DIR/docbook-xsl"
DOCBOOK_XSL_ABS_DIR="$FOPUB_ABS_DIR/docbook-xsl"
fi
XSLTHL_CONFIG_URI="file://$DOCBOOK_XSL_ABS_DIR/xslthl-config.xml"
CONVERT_ARGS="$CONVERT_ARGS -$SOURCE_EXTENSION \"$SOURCE_FILE\""
if [ "$SOURCE_EXTENSION" == "xml" ]; then
CONVERT_ARGS="$CONVERT_ARGS -xsl \"$DOCBOOK_XSL_DIR/fo-pdf.xsl\""
if [ `grep -c '^<!DOCTYPE \(book\|article\)' "$SOURCE_FILE"` == 0 ]; then
# QUESTION should we work on a copy?
sedi "s:<\(book\|article\):<!DOCTYPE \1 [<!ENTITY % db5ent PUBLIC \"-//FOPUB//ENTITIES Entities for DocBook 5\" \"db5.ent\"> %db5ent;]>\n<\1:" "$SOURCE_FILE"
fi
fi
case $FORMAT in
pdf|ps)
CONVERT_ARGS="$CONVERT_ARGS -$FORMAT \"$OUT_FILE\""
;;
fo)
CONVERT_ARGS="$CONVERT_ARGS -foout \"$OUT_FILE\""
;;
xhtml)
CONVERT_ARGS="${CONVERT_ARGS/fo-pdf/xhtml} -foout \"$OUT_FILE\""
;;
*)
echo Unexpected output format: "$FORMAT"
echo Valid options: pdf, ps, fo
usage
exit 1
;;
esac
eval \"$FOPUB_CMD\" -q -catalog \
-c \"$DOCBOOK_XSL_DIR/fop-config.xml\" \
$CONVERT_ARGS \
-param highlight.xslthl.config \"$XSLTHL_CONFIG_URI\" \
-param admon.graphics.path \"$DOCBOOK_DIR/images/\" \
-param callout.graphics.path \"$DOCBOOK_DIR/images/callouts/\" "$@"
# NOTE use ignore.image.scaling to disable the image scaling logic in fo-pdf.xsl
# -param ignore.image.scaling \"1\" \
exit 0