summaryrefslogtreecommitdiff
path: root/autoopts/tpl/texi2mdoc.sh
blob: 6c1325e8f1da18d391d2b448dbbd10d69bda31f8 (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
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
#! /bin/sh

## texi2mdoc.sh -- script to convert texi-isms to mdoc-isms
##
## Time-stamp:      "2012-05-13 15:45:06 bkorb"
##
##  This file is part of AutoOpts, a companion to AutoGen.
##  AutoOpts is free software.
##  AutoOpts is Copyright (c) 1992-2012 Bruce Korb - all rights reserved
##
##  AutoOpts is available under any one of two licenses.  The license
##  in use must be one of these two and the choice is under the control
##  of the user of the license.
##
##   The GNU Lesser General Public License, version 3 or later
##      See the files "COPYING.lgplv3" and "COPYING.gplv3"
##
##   The Modified Berkeley Software Distribution License
##      See the file "COPYING.mbsd"
##
##  These files have the following md5sums:
##
##  43b91e8ca915626ed3818ffb1b71248b COPYING.gplv3
##  06a1a2e4760c90ea5e1dad8dfaac4d39 COPYING.lgplv3
##  66a5cedaf62c4b2637025f049f9b826f COPYING.mbsd

## This "library" converts texi-isms into man-isms.  It gets included
## by the man page template at the point where texi-isms might start appearing
## and then "emit-man-text" is invoked when all the text has been assembled.
##
## Display the command line prototype,
## based only on the argument processing type.
##
## And run the entire output through "sed" to convert texi-isms

# /bin/sh on Solaris is too horrible for words
#
case "$0" in
/bin/sh ) test -x /usr/xpg4/bin/sh && exec /usr/xpg4/bin/sh ${1+"$@"} ;;
esac

parent_pid=$$
prog=`basename $0 .sh`

die() {
    echo "$prog error:  $*" >&2
    kill -TERM $parent_pid
    sleep 1
    kill -9 $parent_pid
    sleep 1
    exit 1
}

do_example() {
    echo '.Bd -literal -offset indent'
    res=0

    while :
    do
        IFS='' read -r line || die "incomplete example"
        case "$line" in
        '@end '*example ) break ;;
        esac

        do_line
    done
    echo '.Ed'
    return $res
}

do_noindent() {
    return 0
}

do_enumerate() {
    echo '.Bl -enum -compact'

    while :
    do
        IFS='' read -r line || die "incomplete enumerate"
        case "$line" in
        '@end '*enumerate ) break ;;
        esac

        do_line
    done
    echo '.El'

    return $res
}

do_end() {
    die "Improper ending:  $line"
}

do_table() {
    echo '.Bl -tag -width 8n'

    while :
    do
        IFS='' read -r line || die "incomplete table"
        case "$line" in
        '@end '*table ) break ;;
        esac

        do_line
    done
    echo '.El'

    return $res
}

do_itemize() {
    echo '.Bl -bullet -compact'

    while :
    do
        IFS='' read -r line || die "incomplete itemize"
        case "$line" in
        '@end '*itemize ) break ;;
        esac

        do_line
    done
    echo '.El'

    return $res
}

do_item() {
    echo "$line" | sed 's/@item/.It/'
}

do_line() {
    case "${line}" in
    '@subheading'* ) echo "$line" | sed 's/@subheading/.SS /' ;;
    '@*' ) echo .br ;;
    ''   ) echo .sp ;;
    '@'[{}]* ) echo "${line}" | sed 's/@\([{}]\)/\1/g' ;;
    '@'* )
        typ=`echo "$line" | egrep '@[a-z]*\{'`
        test ${#typ} -gt 0 && echo "$line" && return 0
        typ=`echo "$line" | sed 's/@ *//;s/[^a-z].*//'`
        eval do_${typ} || die "do_${typ} failed"
        ;;

    * )
        echo "$line"
        ;;
    esac
    return 0
}


nl='
'
sedcmd=
bracket='{\([^}]*\)}'
replB='%BACKSLASH%fB\1%BACKSLASH%fP'
replI='%BACKSLASH%fI\1%BACKSLASH%fP'

for f in code command var env dvn samp option strong
do
    sedcmd="${sedcmd}s;@${f}${bracket};${replB};g${nl}"
done

for f in i file emph kbd key abbr acronym email
do
    sedcmd="${sedcmd}s;@${f}${bracket};${replI};g${nl}"
done

fixfont="${sedcmd}"'
	s;@pxref{\([^}]*\)};see: \1;g
	s;@xref{\([^}]*\)};see: \1;g
	s/@\([{@}]\)/\1/g
	s,^[@$]\*$,.br,
	s/\*\([a-zA-Z0-9:~=_ -]*\)\*/\\fB\1\\fP/g
	s/``\([a-zA-Z0-9:~+=_ -]*\)'\'\''/\\(lq\1\\(rq/g
	s/\([^\\]\)-/\1\\-/g
	s/\([^\\]\)-/\1\\-/g
	/^\.Bl /s/ \\-/ -/g
	/^\.Bd /s/ \\-/ -/g
	/^\.in /s/ \\-/ -/g
	s#%BACKSLASH%#\\#g'"
	s/^'/\\\\'/
	/^\$/d"
readonly fixfont

{
    while IFS='' read -r line
    do
        do_line
    done
} | sed "${fixfont}"

exit 0