Fix #338: re.sub() flag argument at wrong position.
[docutils.git] / sandbox / docbook / scripts / docutils_to_docbook.sh
blobf8ea9d08637fc58323ee1b384dcd5953e2e82d7a
1 if [ "$DOCBOOK_XSL" == "" ];then
2 echo Please set DOCBOOK_XSL to the directory with the stylesheets
3 echo script now quiting
4 exit 1
5 fi
6 if [ "$FOPCONF" == "" ];then
7 FOPCONF=
8 fi
9 if [ "$XSLFO_PDF" != "" ]; then
10 PDF='true'
11 else
12 PDF='false'
14 set -e
15 set -u
16 Usage() {
17 echo Converts a restructured text document to Docbook, then to PDF \(if appropriate\)
18 echo "docutils_to_docbook.sh [options] <RST file>"
19 echo --format: format resulting files
20 echo --noclean: no not remove files created
21 echo --pdf: create a PDF document
22 echo --valid: validate the FO document
23 echo --strict: quit when a template does not match \(or other error\)
24 echo --asciiml: convert math in the math element to mathml
25 echo --fo: convert to FO
26 echo --html: convert to HTML
27 echo "-s | --stylesheet <stylesheet> : the stylesheet to use"
28 echo "-o | --out: file to output to"
30 CLEAN='true'
31 FORMAT=
32 TEST=
33 TESTQ=
34 VALID='false'
35 STYLESHEET=
36 OUT=''
37 STRICT=''
38 ASCIIML='false'
39 FO='false'
40 HTML='false'
41 while [ $# -gt 0 ]
43 case "$1" in
45 -) STDIN='true'; shift; break;; # ====> standard in
46 -h) Usage;exit 0;; # ==> Help message.
47 --help) Usage;exit 0;;
48 --verbose) VERBOSE='true';;
49 --format) FORMAT='true';;
50 --test) CLEAN='false';FORMAT='true';VALID='true';TEST='true';STRICT='true';PDF='true';;
51 --testq) CLEAN='false';VALID='true';FORMAT='true';;
52 --noclean) CLEAN='false';;
53 --pdf) PDF='true';;
54 --valid) VALID='true';;
55 --strict) STRICT='true';;
56 --asciiml) ASCIIML='true';;
57 --fo) FO='true';;
58 --html) HTML='true';;
59 --out) shift;OUT=$1;;
60 -o) shift;OUT=$1;;
61 --stylesheet) shift;STYLESHEET=$1;;
62 -s) shift;STYLESHEET=$1;;
63 -*) echo "Illegal command $1"; Usage; exit 1;; # ===> illegal help
64 *) break;; # first argument
65 esac
66 shift
67 done
69 if [ "$STYLESHEET" == "" ]; then
70 MAIN_XSL=${DOCBOOK_XSL}/docutils_to_docbook.xsl
71 else
72 MAIN_XSL=$STYLESHEET
75 EXT=`echo "$1"|awk -F . '{print $NF}'`
76 BASENAME=`basename $1 .${EXT}`
77 DIR=`dirname $1`
78 if [ "$DIR" == "$1" ]; then
79 DIRNAME="."
80 else
81 DIRNAME=$DIR
84 if [ ! -w "$DIRNAME" ]; then
85 echo "has write permission"
88 if [ "${OUT}" != "" ];then
89 touch ${OUT}
90 rm -f ${OUT}
91 EXT=`echo "${OUT}"|awk -F . '{print $NF}'`
92 if [ ${OUT} == ${EXT} ]; then
93 OUT_BASE=${OUT}
94 else
95 OUT_BASE=`basename ${OUT} .${EXT}`
99 if [ "${OUT}" != "" ]; then
100 RAW_XML=${OUT_BASE}.xml
101 else
102 RAW_XML=${DIRNAME}/${BASENAME}.xml
105 if [ "${OUT}" != "" ]; then
106 DOC_FILE=${OUT_BASE}_docbook.xml
107 else
108 DOC_FILE=${DIRNAME}/${BASENAME}_docbook.xml
111 if [ "$ASCIIML" == 'true' ]; then
112 rst2xml.py --strip-comments --trim-footnote-reference-space --no-doctype $1\
113 | rstxml2xml.py > $RAW_XML
114 else
115 rst2xml.py --strip-comments --trim-footnote-reference-space --no-doctype $1 > $RAW_XML
118 if [ "$FORMAT" == "true" ]; then
119 xmlformat.pl -i $RAW_XML
122 if [ "$STRICT" == "" ]; then
123 xsltproc --output $DOC_FILE "$MAIN_XSL" $RAW_XML
124 else
125 xsltproc --stringparam strict true --output $DOC_FILE "$MAIN_XSL" $RAW_XML
129 if [ "$FORMAT" == "true" ]; then
130 xmlformat.pl -i $DOC_FILE
133 if [ "$VALID" == 'true' ]; then
134 java -jar $JING_DIR/bin/jing.jar -i ${VALIDATE_HOME}/relax/docbook.rng $DOC_FILE
137 FO_FILE=${DIRNAME}/${BASENAME}.fo
139 if [ "$PDF" == 'true' ]; then
140 PDF_FILE=${DIRNAME}/${BASENAME}.pdf
141 if [ "$FOPCONF" != '' ]; then
142 fop -c $FOPCONF -fo $FO_FILE -pdf ${PDF_FILE}
143 else
144 fop -fo $FO_FILE -pdf ${PDF_FILE}
148 if [ "$OUT" == "" ] && [ "$PDF" == 'false' ] && [ "$FO" == "true" ];then
149 cat $FO_FILE
152 if [ "$OUT" == "" ] && [ "$PDF" == 'false' ] && [ "$HTML" == "true" ];then
153 xsltproc http://50.56.245.89/xsl-ns/xhtml/docbook.xsl $DOC_FILE
156 if [ "$CLEAN" == "true" ]; then
157 rm -f $RAW_XML
158 if [ "$TEST" == 'true' ]; then
159 rm $PDF_FILE