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

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

cd "${root}/man"
mkdir "${out}"

for m in *.[1-8]
do
    n="${m##*.}"
    echo "opening ${m}"

    if grep "\.so man${n}" "${m}"
    then
        sed -i "s/man${n}/man/" "${m}"
        man2html -r -H "${hostname}" "${m}" > "${ftmp}"
        sed -i "s/man\//man${n}\//g" "${m}"
    else
        man2html -r -H "${hostname}" "${m}" > "${ftmp}"
    fi

    # 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 top page heading with page info, remove superflous info
    name="$(basename ${m})"
    name="${name%.*}"
    version_info="$(head -n1 ${ftmp} | cut -f3 -d: | cut -f1 -d\<)"
    tail -n+2 "${ftmp}" > tmp; mv tmp "${ftmp}"
    sed -i "1s/^/<p class=\"info left\">${name}(${n})<\/p><p class=\"info center\">bofc manual pages<\/p><p class=\"info right\">${name}(${n})<\/p>\n<br><P> /" "${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 (page 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 '/<DL>/,$!d;H;/<\/DL>/!d;s/.*//;x;s/\n//' "${ftmp}")"

    toc="$(echo "${toc}" | sed 's/<DL>/<UL class="man-toc">/')"
    toc="$(echo "${toc}" | sed 's/<\/DL>/<\/UL>/')"
    toc="$(echo "${toc}" | sed 's/<DT>/<LI>/')"
    toc="$(echo "${toc}" | sed 's/<DD>/<\/LI>/')"

    ## 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}"

    # change deprecated name in <a> into id
    sed -i 's/A NAME="/A ID="/g' "${ftmp}"

    # generate page info at bottom of page
    echo "<p class=\"info left\"><a href=\"http://en.bofc.pl\">bofc.pl</a></p><p class=\"info center\">${version_info}</p><p class=\"info right\">${name}(${n})</p>" >> "${ftmp}"

    # convert all h2 into h1 headings, man2html generates .SH as h2 and we want
    # it as h1
    sed -i 's/H2>/H1>/g' "${ftmp}"

    # convert all h3 into h2 headings, man2html generates .SS as h3 and we want
    # it as h2
    sed -i 's/H3>/H2>/g' "${ftmp}"

    # remove obsolete COMPACT from dl
    sed -i 's/DL COMPACT/DL/g' "${ftmp}"

    # change ../man# to ./ as we have all mans in single directory
    sed -i 's/\.\.\/man.\//.\//g' "${ftmp}"

    #remove <P> right before <PRE>
    perl -i -0pe 's/<P>\n\n<PRE>/<PRE>/g' "${ftmp}"

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