render () {
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
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"
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
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
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
cp -r "$InDir/images/" "$OutDir"
cd "$OutDir"
xsltproc --output "$FoFile" \
--stringparam section.autolabel 1 \
"$XslPath"\
"$XmlFile" && \
fop -fo "$FoFile" -pdf "$PdfFile"
rm "$XmlFile" && \
rm -r "images" && \
rm "$FoFile"
}
if [[ -n "${2/[ ]*\n/}" ]] ; then
OutPath="$2"
else
OutPath="${1%.*}.pdf" fi
render "$1" "$OutPath"