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
|
#!/bin/bash
#
# Copyright (C) 2012-2020 SUSE Software Solutions Germany GmbH
#
# Author:
# Thomas Schraitle <toms at opensuse dot org>
# Frank Sundermeyer <fsundermeyer at opensuse dot org>
#
# Script to change DocBook 4 source to version 5
#
# Name of this script:
ME="$(basename "$0")"
# TODO: Make it configurable by ~/.daps/config or ~/.config/daps/dapsrc
# [[ -f ~/.daps/config ]] && source ~/.daps/config
# [[ -f ~/.config/daps/dapsrc ]] && source ~/.config/daps/dapsrc
# Contains the stylesheet to migrate DocBook 4 -> 5:
MIGRATE_XSLT=${MIGRATE_XSLT:-/usr/share/xml/docbook/stylesheet/upgrade/db4-upgrade.xsl}
# File extension
EXT=${EXT:-.xml}
# Where to write all xsl:message outputs from xsltproc:
XSLTPROCLOG=${XSLTPROCLOG:-/tmp/daps-migrate.log}
# Holds XSLT parameters which is passed to the XSLT processor
declare -A PARAMS
PARAMS=()
# Verbosity
# VERBOSE=0
# replacement string for root element in header file
ROOTREPLACE=@RE@
## Functions
function exit_on_error {
echo -e "ERROR: $1" >&2
exit 1;
}
function dump {
if [[ -n "$VERBOSE" ]]; then
echo "$1"
fi
echo "$1" >> "$XSLTPROCLOG" 2>&1
}
function get_rootelement {
#
# input: XML file name
# output: root element
#
local FILE
FILE=$1
if [[ -x /usr/bin/xmlstarlet ]]; then
XMLSTARLET=/usr/bin/xmlstarlet
elif [[ -x /usr/bin/xml ]]; then
XMLSTARLET=/usr/bin/xml
else
exit_on_error "Cannot find xmlstarlet"
fi
ROOTELEMENT=$($XMLSTARLET sel -t -v "local-name(/*)" "$FILE") || exit_on_error "Cannot get root element from $FILE"
}
# Help function to preserve readability of the while loop
function usage {
cat <<EOF_helptext
Usage: $ME [OPTIONS] INPUTDIR OUTPUTDIR
Wrapper script to convert a set of DocBook documents
Options:
--header Path to a file containing a DocBook header. If specified,
this header will be written to each file that is generated.
The header needs to contain the placeholder "$ROOTREPLACE" in
place of the root element
-l LOGFILE, --log=LOGFILE
Write all outputs of xsltproc to LOGFILE
(default: $XSLTPROCLOG)
-p, --param="XSLPARAM=VALUE"
Defines parameter-value pair for the XSLT processor;
can be used more than once
-s XSLT, --stylesheet=XSLT
Use stylesheet for transformation
(default: $MIGRATE_XSLT)
-h, --help Print this help
-v, --verbose Be verbose
Mandatory arguments:
INPUTDIR Searches for all XML file in this directory
OUTPUTDIR Write all XML file into the OUT directory
EOF_helptext
}
function parse_param {
# Syntax: parse_param "PARAMETER_AND_VALUE"
# Extract parameter and value, syntax $1="param=value"
local _PARAM=$1
[[ "$_PARAM" == *"="* ]] || exit_on_error "Option -p must adhere to the syntax PARAMETER=VALUE"
p=${_PARAM%=*}
v=${_PARAM#*=}
# Define associative array:
PARAMS["$p"]="$v"
}
## START
#
# TODO: getopts
export POSIXLY_CORRECT=1
ARGS=$(getopt -o "hvl:s:p:" -l "header:,help,verbose,log:,stylesheet:,param:" -n "$ME" -- "$@")
eval set -- "$ARGS"
while true; do
case "$1" in
--header)
if [[ -f $2 ]]; then
if grep -q "$ROOTREPLACE" "$2" >/dev/null 2>&1; then
HEADERFILE="$2"
else
exit_on_error "Header file does not contain the replacement string $ROOTREPLACE for the root element"
fi
else
exit_on_error "$2 is not a regular file"
fi
shift 2
;;
-h|--help)
usage
exit 1
shift
;;
-l|--log)
XSLTPROCLOG="$2"
shift 2
;;
-p|--param)
parse_param "$2"
shift 2
;;
-s|--stylesheet)
MIGRATE_XSLT="$2"
shift 2
;;
-v|--verbose)
VERBOSE=1
shift
;;
--)
shift
break;;
*) exit_on_error "Internal error!" ;;
esac
done
unset POSIXLY_CORRECT
## Check for errors
if [[ $# -eq 0 ]]; then
usage
exit 1
fi
INPUTDIR=$1
OUTPUTDIR=$2
[[ $# -lt 2 ]] && exit_on_error "Missing arguments: Need both input and output directory!"
[[ -d $INPUTDIR ]] || exit_on_error "First argument '$1' is not a directory"
if [[ ! -d $OUTPUTDIR ]]; then
mkdir -p "$OUTPUTDIR"
fi
[[ -e $XSLTPROCLOG ]] && rm "$XSLTPROCLOG"
dump "** Starting DocBook Transformation **"
dump "** Using stylesheet $MIGRATE_XSLT"
p=""
for i in "${!PARAMS[@]}"; do
p="$i=${PARAMS[$i]} $p"
done
dump "** Parameters: $p"
p=""
for i in "${!PARAMS[@]}"; do
p="--stringparam $i ${PARAMS[$i]} $p"
done
dump "** Command line: xsltproc --output FILE ${p} $MIGRATE_XSLT XMLINPUT"
for xmlin in ${INPUTDIR}/*$EXT; do
xmlout="$OUTPUTDIR/$(basename "$xmlin")"
dump "$xmlin -> $xmlout"
if [[ -n $HEADERFILE ]]; then
cp "$HEADERFILE" "$xmlout"
get_rootelement "$xmlin"
sed -i "s/$ROOTREPLACE/$ROOTELEMENT/" "$xmlout"
xsltproc --nonet ${p} "$MIGRATE_XSLT" "$xmlin" >> "$xmlout" 2>> "$XSLTPROCLOG"
else
xsltproc --nonet --output "$xmlout" ${p} "$MIGRATE_XSLT" "$xmlin" 2>> "$XSLTPROCLOG"
fi
# ERRCODE=$?
dump "--------------------------------------------------"
done
dump "** Finished, see $XSLTPROCLOG for more information **"
dump ""
# EOF
|