aboutsummaryrefslogtreecommitdiffstats
path: root/man2html.sh
blob: 45632d6c1f0c7742dfd6f643d7842b62e51fefdc (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
#!/bin/sh

hostname="http://mtest.kurwinet.pl"
out="$(pwd)/www/manuals"
root="$(pwd)"
ftmp="/tmp/mtest-man2html"

for n in {3,7}
do
    mkdir -p "${out}/man${n}"
    cd "${root}/man/man${n}"

    for m in *
    do
        man2html -r -H "${hostname}" "${m}" > "${ftmp}"

        # get only body part of the file
        body_only="$(sed -n '/<BODY>/,/<\/BODY>/p' "${ftmp}")"
        echo "$body_only" > "${ftmp}"

        # remove leftover <body> and <h1>man</h1> tags from beginning
        tail -n+3 "${ftmp}" > tmp; mv tmp "${ftmp}"

        # construct own h1 tag
        name="$(basename ${m})"
        name="${name%.*}"
        sed -i "1s/^/<H1>${name}(${n})<\/H1>\n /" "${ftmp}"

        # remove uneeded links to non-existing index
        sed -i 's/<A HREF="\.\.\/index.html">Return to Main Contents<\/A><HR>//' "${ftmp}"
        sed -i 's/<A HREF="#index">Index<\/A>//g' "${ftmp}"

        # extract table of content and put it in the beginning of file
        ## cache first two lines (h1 and info) and remove them from file
        tmp="$(head -n2 ${ftmp})"
        tail -n+3 "${ftmp}" > tmp; mv tmp "${ftmp}"

        ## get table of content from file
        toc="$(sed -n '/<DL>/,/<\/DL>/p' "${ftmp}")"

        ## put table of content and first two lines into file and append hr
        { echo -e "${tmp}\n${toc}\n<HR>"; cat "${ftmp}"; } > tmp; mv tmp "${ftmp}"

        ## remove table of content and some uneeded info from bottom of file
        sed -i '/^<A NAME="index">&nbsp;<\/A><H2>Index<\/H2>$/,$d' "${ftmp}"
        head -n-3 "${ftmp}" > tmp; mv tmp "${ftmp}"

        # move generated file into output directory for further processing
        cp "${ftmp}" "${out}/man${n}/${m}.html"
    done
done