3 # Script to convert a SGML file into some other format
4 # Send any comments to Eric Bischoff <eric@caldera.de>
5 # This program is under GPL license. See LICENSE file for details.
8 SGML_HELP_MESSAGE="Usage: `basename $0` [<options>] <sgml_file>\n\
9 where <options> are:\n\
10 \040 -f|--frontend <frontend>: \t Specify the frontend (source format)\n\
11 \040 \t\t\t\t (default is docbook)\n\
12 \040 -b|--backend <backend>: \t Specify the backend (destination format)\n\
13 \040 \t\t\t\t (default is html)\n\
14 \040 -c|--cat <file>: \t\t Specify an extra SGML open catalog\n\
15 \040 -n|--nostd: \t\t\t Do not use the standard SGML open catalogs\n\
16 \040 -d|--dsl <file>|default|none:\t Specify an alternate style sheet\n\
17 \040 \t\t\t\t (default is to use the default stylesheet)\n\
18 \040 -l|--dcl <file>: \t\t Specify an alternate SGML declaration\n\
19 \040 \t\t\t\t (usual ones like xml.dcl get detected automatically)\n\
20 \040 -s|--sgmlbase <path>: \t Change base directory for SGML distribution\n\
21 \040 \t\t\t\t (usually @prefix@/share/sgml)\n\
22 \040 -p|--parser <program>: \t Specify the parser if several are installed\n\
23 \040 \t\t\t\t (jade or openjade)\n\
24 \040 -o|--output <directory>: \t Set output directory\n\
25 \040 -u|--nochunks: \t\t Output only one big file\n\
26 \040 \t\t\t\t (overrides the stylesheet settings)\n\
27 \040 -i|--include <section>: \t Specify a SGML marked section to include\n\
28 \040 \t\t\t\t (should be marked as \"ignore\" in the SGML text)\n\
29 \040 -w|--warning <warning_type>|list: Control warnings or display the allowed warning types\n\
30 \040 -e|--errors <error_type>|list: Control errors or display the allowed error types\n\
31 \040 -h|--help: \t\t\t Print this help message and exit\n\
32 \040 -V <variable[=value]>: \t Set a variable\n\
33 \040 -v|--version: \t\t Print the version and exit"
35 # Set list of warning types
36 SGML_WARNINGS_LIST="\n\
37 \040 xml \t\t Warn about constructs that are not allowed by XML\n\
38 \040 mixed \t Warn about mixed content models that do not allow #pcdata anywhere\n\
39 \040 sgmldecl \t Warn about various dubious constructions in the SGML declaration\n\
40 \040 should \t Warn about various recommendations made in ISO 8879 that the document does not comply with\n\
41 \040 default \t Warn about defaulted references\n\
42 \040 duplicate \t Warn about duplicate entity declarations\n\
43 \040 undefined \t Warn about undefined elements: elements used in the DTD but not defined\n\
44 \040 unclosed \t Warn about unclosed start and end-tags\n\
45 \040 empty \t Warn about empty start and end-tags\n\
46 \040 net \t\t Warn about net-enabling start-tags and null end-tags\n\
47 \040 min-tag \t Warn about minimized start and end-tags. Equivalent to: unclosed, empty and net\n\
48 \040 unused-map \t Warn about unused short reference maps\n\
49 \040 unused-param \t Warn about parameter entities that are defined but not used in a DTD\n\
50 \040 notation-sysid Warn about notations for which no system identifier could be generated\n\
51 \040 all \t\t Equivalent to: mixed, should, default, undefined, sgmldecl, unused-map, unused-param, empty and unclosed\n\n\
52 \040 A warning can be disabled by using its name prefixed with no-. Example: -w all -w no-duplicate"
54 # Set list of error types
56 \040 no-idref \t No error for an ID reference value which no element has as its ID\n\
57 \040 no-significant No errors for characters not significant in the reference concrete syntax\n\
58 \040 no-valid \t Do not require the document to be type-valid"
60 # Get name of main SGML configuration file
61 which sgmlwhich >/dev/null 2>/dev/null
66 SGML_CONF="/etc/sgml/sgml.conf"
70 SGML_VERSION_MESSAGE="DocBook-utils version @VERSION@ (jw version 1.1)"
72 # Set option: use standard catalogs
73 SGML_STANDARD_CATALOGS="yes"
75 # Set path to user SGML catalogs
76 SGML_EXTRA_CATALOGS=""
78 # Set SGML base directory and centralized catalogs directory
79 SGML_BASE_DIR="@prefix@/share/sgml"
80 SGML_CATALOGS_DIR="/etc/sgml"
81 if [ -f "$SGML_CONF" ]
83 RE='^[:space:]*SGML_BASE_DIR[:space:]*=[:space:]*'
84 SGML_BASE_DIR=`grep $RE $SGML_CONF | sed "s/$RE//"`
85 RE='^[:space:]*SGML_CATALOGS_DIR[:space:]*=[:space:]*'
86 SGML_CATALOGS_DIR=`grep $RE $SGML_CONF | sed "s/$RE//"`
90 SGML_FRONTEND="$SGML_BASE_DIR/docbook/utils-@VERSION@/frontends/docbook"
93 SGML_BACKEND="$SGML_BASE_DIR/docbook/utils-@VERSION@/backends/html"
96 SGML_STYLESHEET="default"
98 # Set flavour of jade parser to use
101 # Set SGML declaration and file to process
105 # Set output and current directories
106 SGML_OUTPUT_DIRECTORY="."
107 SGML_CURRENT_DIRECTORY=`pwd`
109 # Set of options to set
112 # Set list of "include" marked sections
115 # Set list of warning types and errors types
121 -f|--frontend) case "$2" in
122 /*) SGML_FRONTEND="$2" ;;
123 *) d=$SGML_BASE_DIR/docbook/utils-@VERSION@/frontends
124 SGML_FRONTEND="$d/$2" ;;
128 -b|--backend) case "$2" in
129 /*) SGML_BACKEND="$2" ;;
130 *) d=$SGML_BASE_DIR/docbook/utils-@VERSION@/backends
131 SGML_BACKEND="$d/$2" ;;
135 -c|--cat) case "$2" in
136 /*) SGML_EXTRA_CATALOGS="$SGML_EXTRA_CATALOGS:$2" ;;
137 *) SGML_EXTRA_CATALOGS="$SGML_EXTRA_CATALOGS:$SGML_CURRENT_DIRECTORY/$2" ;;
141 -d|--dsl) case "$2" in
142 /*|none|default) SGML_STYLESHEET="$2" ;;
143 *) SGML_STYLESHEET="$SGML_CURRENT_DIRECTORY/$2" ;;
147 -l|--dcl) case "$2" in
148 /*) SGML_DECL="$2" ;;
149 *) SGML_DECL="$SGML_CURRENT_DIRECTORY/$2" ;;
153 -n|--nostd) SGML_STANDARD_CATALOGS="no"
156 -s|--sgmlbase) case "$2" in
157 /*) SGML_BASE_DIR="$2" ;;
158 *) SGML_BASE_DIR="$SGML_CURRENT_DIRECTORY/$2" ;;
162 -p|--parser) case "$2" in
163 /*) SGML_JADE="$2" ;;
164 *) SGML_JADE="$SGML_CURRENT_DIRECTORY/$2" ;;
168 -o|--output) case "$2" in
169 /*) SGML_OUTPUT_DIRECTORY="$2" ;;
170 *) SGML_OUTPUT_DIRECTORY="$SGML_CURRENT_DIRECTORY/$2" ;;
174 -u|--nochunks) SGML_OPTIONS="$SGML_OPTIONS -V nochunks"
177 -V) SGML_OPTIONS="$SGML_OPTIONS -V $2"
180 -i|--include) case $2 in
181 print|html) echo "The marked section named \"$2\" is reserved for internal use by `basename $0`." >&2
185 SGML_INCLUDE="$SGML_INCLUDE -i $2"
188 -w|--warning) case $2 in
189 list) echo -e "List of allowed warning types (multiple -w options are allowed):\n"
190 echo -e $SGML_WARNINGS_LIST
193 xml|mixed|sgmldecl|should|default|duplicate|undefined|unclosed|empty|net|min-tag|unused-map|unused-param|notation-sysid|all|no-xml|no-mixed|no-sgmldecl|no-should|no-default|no-duplicate|no-undefined|no-unclosed|no-empty|no-net|no-min-tag|no-unused-map|no-unused-param|no-notation-sysid) \
194 SGML_WARNINGS="$SGML_WARNINGS -w $2"
197 *) echo "Unknown warning type \"$2\". Allowed warning types are:" >&2
198 echo -e $SGML_WARNINGS_LIST >&2
203 -e|--error) case $2 in
204 list) echo -e "List of allowed error types (multiple -e options are allowed):\n"
205 echo -e $SGML_ERRORS_LIST
208 no-idref|no-significant|no-valid) \
209 SGML_WARNINGS="$SGML_WARNINGS -w $2"
212 *) echo "Unknown error type \"$2\". Allowed error types are:" >&2
213 echo -e $SGML_ERRORS_LIST >&2
218 -h|--help) echo -e "`basename $0` - Jade Wrapper\n"
219 echo -e "$SGML_HELP_MESSAGE"
222 -v|--version) echo -e "$SGML_VERSION_MESSAGE"
225 -*) echo -e "$SGML_HELP_MESSAGE" >&2
228 *) if [ -z "$SGML_FILE" ]
231 /*) SGML_FILE="$1" ;;
232 *) SGML_FILE="$SGML_CURRENT_DIRECTORY/$1" ;;
236 echo -e "$SGML_HELP_MESSAGE" >&2
243 # Check that we have a frontend
244 if [ -z "$SGML_FRONTEND" ]
246 echo -e $SGML_HELP_MESSAGE >&2
249 if [ ! -s $SGML_FRONTEND ]
251 echo "`basename $0`: There is no frontend called \"$SGML_FRONTEND\"." >&2
255 # Check that we have a backend
256 if [ -z "$SGML_BACKEND" ]
258 echo -e $SGML_HELP_MESSAGE >&2
261 if [ ! -s $SGML_BACKEND ]
263 echo "`basename $0`: There is no backend called \"$SGML_BACKEND\"." >&2
267 # Check that we have a file to process
268 if [ -z "$SGML_FILE" ]
270 echo -e $SGML_HELP_MESSAGE >&2
273 if [ ! -s $SGML_FILE ]
275 echo "`basename $0`: Cannot open \"$SGML_FILE\"." >&2
279 # Separate the file name and the extension (if any)
280 SGML_FILE_NAME=`basename $SGML_FILE`
281 SGML_FILE_NAME=${SGML_FILE_NAME%.*}
283 # Determine if we are in an XML file
284 SGML_XML=`head "$SGML_FILE" | grep "<?xml.*?>" | wc -l`
285 if [ $SGML_XML -eq 1 ]; then
291 # Use SGML declaration for XML if needed
292 if [ "$SGML_DECL" = "" -a "$SGML_XML" = "xml" ]
293 then SGML_DECL=${SGML_BASE_DIR}/xml.dcl
296 # Try to find the SGML normalizer
297 if [ -z "$SGML_NORM" ]
299 SGML_NORM=`which sgmlnorm 2>/dev/null`
300 if [ -z "SGML_NORM" ]
302 SGML_NORM=`which osgmlnorm 2>/dev/null`
306 # Set path to SGML catalogs (first try centralized catalog)
307 case $SGML_STANDARD_CATALOGS in
308 yes) export SGML_CATALOGS_DIR SGML_FILE SGML_XML SGML_NORM
309 SGML_CENTRALIZED_CATALOG=`sh $SGML_FRONTEND centralized-catalog`
310 if [ -s $SGML_CENTRALIZED_CATALOG ]
312 SGML_CATALOG_FILES=$SGML_CENTRALIZED_CATALOG
314 SGML_CATALOG_FILES=`find $SGML_BASE_DIR -name catalog`
315 SGML_CATALOG_FILES=`echo "$SGML_CATALOG_FILES" | tr [:space:] :`
318 no) SGML_CATALOG_FILES=""
321 SGML_CATALOG_FILES=${SGML_CATALOG_FILES}${SGML_EXTRA_CATALOGS}
322 SGML_CATALOG_FILES=`echo "$SGML_CATALOG_FILES" | sed 's/^://;s/:$//'`
323 if [ -z "$SGML_CATALOG_FILES" ]
325 echo "`basename $0`: Please specify at least one catalog" >&2
329 # Determine the backend type
330 case $SGML_BACKEND in
338 # Determine which stylesheet to use
339 # (run the frontend to know it if the default stylesheet or no custom stylesheet is desired)
340 if [ "$SGML_STYLESHEET" = "default" -o "$SGML_STYLESHEET" = "none" ]
342 export SGML_BASE_DIR SGML_TYPE SGML_STYLESHEET
343 SGML_STYLESHEET=`sh $SGML_FRONTEND style-sheet`
345 if [ $SGML_RETURN -gt 0 ]
346 then exit `expr 7 + $SGML_RETURN`
349 if [ -z "$SGML_STYLESHEET" ]
351 echo "`basename $0`: Please specify at least one stylesheet" >&2
355 case "$SGML_OPTIONS" in
359 # Set the paper size to A4 if the locale suggests so
361 if [ -x /usr/bin/locale ]
363 h=$(/usr/bin/locale LC_PAPER 2>/dev/null | head -n 1)
369 if [ -n "$papersize" ]
371 SGML_OPTIONS="-V paper-type=$papersize $SGML_OPTIONS"
377 if [ -z "$SGML_JADE" ]
379 which jade >/dev/null 2>/dev/null
384 which openjade >/dev/null 2>/dev/null
389 echo "`basename $0`: No parser available" >&2
394 which $SGML_JADE >/dev/null 2>/dev/null
397 echo "`basename $0`: parser $SGML_JADE is not available" >&2
402 # Create output directory if not available
403 if [ -z "$SGML_OUTPUT_DIRECTORY" ]
405 echo -e $SGML_HELP_MESSAGE >&2
408 if [ ! -d $SGML_OUTPUT_DIRECTORY ]
410 mkdir $SGML_OUTPUT_DIRECTORY 2>/dev/null
413 echo "`basename $0`: Could not create \"$SGML_OUTPUT_DIRECTORY\" output directory" >&2
418 # Prepare the parser's arguments
419 SGML_ARGUMENTS="$SGML_INCLUDE -i $SGML_TYPE -d $SGML_STYLESHEET $SGML_OPTIONS $SGML_WARNINGS $SGML_DECL $SGML_FILE"
422 echo "Using catalogs: `echo $SGML_CATALOG_FILES | sed 's/:/, /g'`"
423 echo "Using stylesheet: $SGML_STYLESHEET"
424 echo "Working on: $SGML_FILE"
425 cd $SGML_OUTPUT_DIRECTORY
426 export SGML_JADE SGML_FILE_NAME SGML_ARGUMENTS
427 export SGML_CATALOG_FILES SGML_BASE_DIR SGML_FILE SGML_STYLESHEET
428 NOCHUNKS=`echo $SGML_OPTIONS | grep nochunks`
429 if [ -z "$NOCHUNKS" ]
433 sh $SGML_BACKEND >$SGML_FILE_NAME.html
436 cd $SGML_CURRENT_DIRECTORY
438 if [ $SGML_RETURN -gt 0 ]
439 then exit `expr 7 + $SGML_RETURN`