aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/config/docbook/docbookconv.sh
blob: e58f05b78ddc18ecfd696e88176bc025e42de75a (plain)
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
#!/bin/sh

#set -x

fatal_error()
{
    echo "$1" 1>&2
    exit 1
}

debug_echo()
{
    echo "$1" 1>&2
}

verbose_echo()
{
    echo "$1" 1>&2
}

which_tool()
{
    echo "${PATH}" | tr ":" "\n" | while read i ;
    do ls -1ad "${i}/${1}" 2>/dev/null ; done | sort | uniq
}

which_program()
{
    echo "${1}" | tr ":" "\n" | while read i ;
    do 
        which_tool "${i}"
    done | sort | uniq
}

which_xsl()
{
    ls -1ad ${1}/docbook-xsl-stylesheets*/ 2>/dev/null | head -1
}

# Fix HTML generated by the DocBook XSL stylesheets
# In many cases <nsxyz:p> is used instead of <p>, screwing-up
# display (this stuff only works for XHTML)
fix_docbook_html()
{
  sed "s/<ns[0-9]*:p>/<p>/g;s/<\/ns[0-9]*:p>/<\/p>/g"
}

# main
infile="${1}"
outputformat="${2}"
outputfile="${3}"

# xsl processing
case "`uname -s`" in
    FreeBSD)
        PATH="${PATH}:/usr/local/bin:/usr/local/sbin"
        export PATH
        stylesheetbase="/usr/local/share/xsl/"
        ;;
    *)
        stylesheetbase="/usr/share/sgml/docbook/"
        ;;
esac
xsltproc="`which_tool xsltproc`"
stylesheetdir="`which_xsl ${stylesheetbase}`"
docbook2man="`which_tool docbook2man`"
verbose_echo "# Using xsltproc=${xsltproc}."
verbose_echo "# Using stylesheetdir=${stylesheetdir}."
verbose_echo "# Using docbook2man=${docbook2man}."

# Prechecks
[ "${infile}" = "" ]         && fatal_error "$0: No input file."
[ "${outputfile}" = "" ]     && fatal_error "$0: No output file."
[ "${outputformat}" = "" ]   && fatal_error "$0: No format given."
[ ! -r "${infile}" ]         && fatal_error "$0: Input file not found or readable."
[ ! -x "${xsltproc}" ]       && fatal_error "$0: No xsltproc found."
[ ! -r "${stylesheetdir}" ]  && fatal_error "$0: No DocBook/XSL style sheets found."
[ ! -x "${docbook2man}" ]    && fatal_error "$0: No docbook2man found."


# this is hack style to work around the problem that "docbook2man"
# writes lots of files into the current dir
MYTMPDIR="/tmp/docbookconv_${RANDOM}"
MYCURRDIR="${PWD}"

(
  mkdir "${MYTMPDIR}"
  cd "${MYTMPDIR}"

  case "${outputformat}" in
    "html")
        cp "${MYCURRDIR}/${infile}" "${infile}.tmp"
        if [ "${infile}" != "${infile%.sgml}" ] ; then
          verbose_echo "# processing as SGML document"
          ${xsltproc} --docbook ${stylesheetdir}/html/docbook.xsl "${infile}.tmp" | fix_docbook_html >"${MYCURRDIR}/${outputfile}"
        else
          verbose_echo "# processing as XML document"
          ${xsltproc}           ${stylesheetdir}/html/docbook.xsl "${infile}.tmp" | fix_docbook_html >"${MYCURRDIR}/${outputfile}"
        fi
        ;;
    "man")
        if [ "${infile}" != "${infile%.sgml}" ] ; then
          #cp "${MYCURRDIR}/${infile}" "${infile%.sgml}.xml"
          #${docbook2man} --network "${infile%.sgml}.xml"
          cp "${MYCURRDIR}/${infile}" "${infile}.tmp"
          ${docbook2man} --network "${infile}.tmp"
        else
          cp "${MYCURRDIR}/${infile}" "${infile}.tmp"
          ${docbook2man} --network "${infile}.tmp"
        fi
        manfile="$(ls -1 ${infile%.*}.__*)"

        verbose_echo "manfile=${manfile}"
        [ ! -r "${manfile}" ] && fatal_error "$0: manfile not found."
        cp "${manfile}" "${MYCURRDIR}/${outputfile}"
        ;;
    *)
        fatal_error "Unsupported output format ${outputformat}."
        ;;
  esac
)

# EOF.