aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/extras/rman/contrib/rman_html_split
blob: aafb023c5989912bfa06887b69363186c29749b2 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/bin/sh
#
# Script to break up rman html output into separate sections in a directory.
#
# Author: Robert Moniot <moniot@fordham.edu>
# Date:   4 Aug 1998
#

version="rman_html_split V1.0 (c) 1998 Robert K. Moniot"

	    # Usage function prints synopsis and exits
Usage(){
  myname=`echo $0 | sed -e 's,.*/,,'`
cat <<EOF

Usage: $myname [ options ] [ file ]

Breaks up rman HTML output in file (or standard input) into separate
section files, placing them into a directory.

Options:
    -d | --html-dir dir   Place output into directory dir (default = html).
    -m | --merge #        Merge sections 0 thru # (default 1) into one file.
    -t | --toc-first      Link index.html to table of contents instead
                          of to sect0.html.
    -T | --title "title"  Use "title" as title on top of each page
                          instead of rman's title.  "title" may be empty.
    -h | --help           Print synopsis and exit.
    -V | --version        Print version information and exit.
EOF
  exit 0
}

	    # Version function prints version number and exits
Version(){
  echo "$version"
  exit 0
}

	    # This function puts out header needed by all but 1st page.
do_header(){
cat <<EOF
<HTML>
<HEAD>
<TITLE>$*</TITLE>
</HEAD>
<BODY>
EOF
do_title
}

	    # This function puts out the title with name of manpage
do_title(){
  if [ -n "$title" ]
  then
    echo "<H2>$title</H2>"
  fi
}

	    # This function puts out ref to table of contents that
	    # follows header for all but 1st and toc page.
do_href_toc(){
cat <<EOF
<A HREF="toc.html">Table of Contents</A><P>
EOF
}

	    # This function puts out ref to previous section
do_href_prev(){
  echo "<P>Previous: <A HREF=\"$1.html\">$2</A><HR><P>"
}

	    # This function puts out footer needed by all but last page.
do_footer(){
cat <<EOF
</BODY></HTML>
EOF
}

	    # This function changes the trailing NAME anchor
	    # of a section into an href to the next section.
	    # The first edit is for sections.  The second edit
	    # handles the toc anchor, which has no href and so
	    # is missed by the first edit.
do_href_next(){
 sed -e '$s,^.*<A NAME="\([^"]*\)" HREF=[^>]*>\([^<]*\).*$,<P><HR><P>Next: <A HREF="\1.html">\2</A>,' \
     -e '$s,<A NAME="\(toc[^"]*\)",Next: <A HREF="\1.html",'
}

	    # This function adapts internal refs from one-big-file
	    # form into multi-file format.
	    # First edit changes "#tocn" into "toc.html#tocn"
	    # Second edit changes "#sectn" into "sectn.html#sectn"
	    # Third edit changes "#toc" into "toc.html"
change_hrefs(){
    sed -e 's,HREF="#toc\([0-9][0-9]*\)",HREF="toc.html#toc\1",g' \
	-e 's,HREF="#sect\([0-9][0-9]*\)",HREF="sect\1.html#sect\1",g' \
        -e 's,HREF="#\(toc\)",HREF="\1.html",g'
}


#  Execution begins here.

				# Process the command-line options.
htmldir="html"
mergesect="1"

while [ $# != 0 ]
do
  case $1 in

    -d | --html-dir)
	shift
	if [ $# != 0 ]
	then
	    htmldir="$1"
	    shift
	fi
	;;

    -m | --merge)
	shift
	if [ $# != 0 ]
	then
	    mergesect="$1"
	    shift
	fi
	case $mergesect in
	    [0-9])
		;;
	    *)
		echo "ERROR: merge section must be a digit from 0 to 9."
		Usage
		;;
	esac
	;;

    -t | --toc-first)
	shift
	tocfirst="t"
	;;

    -T | --title)
	shift
	if [ $# != 0 ]
	then
	    title="$1"
	    shift
	fi
	if [ -z "$title" ]	# If title is the null string, set notitle
	then
	    notitle="t"
	fi
	;;

    -h | --help)
	Usage
	;;

    -V | --version)
	Version
	;;

				# Unrecognized flag.
    -*)
	echo "Unrecognized option: $1"
	Usage
	;;

				# Non-flag: go to process file.
    *)
	break
	;;
  esac
done

				# At this point we should have at most one
				# argument, the file name.  Check it.
if [ $# -gt 1 ]
then
  Usage
fi

if [ $# = 1 ]
then
  if [ ! -r "$1" ]
  then
    echo "ERROR: Cannot open file $1."
    exit 1
  fi
fi

		# Filter the input (file arg or stdin) thru a sed script
		# that fixes cases where an anchor for a section has
		# had its section title split across two or more lines.
		# Put the result in a tmp file so it can be re-read.

filename="/tmp/rman_html_split_$$.html"
trap "rm -f $filename" 0
sed -e ':top' \
    -e '/^<H[1-9]><A NAME="sect.*[^>]$/N' \
    -e '/^<LI><A NAME="toc.*[^>]$/N' \
    -e 's/\n/ /' \
    -e 't top' $1 > $filename

		# If user did not supply title, get title for top of
		# each page from rman-generated <TITLE> line.  If user
		# gave a blank title then leave it blank.
if [ -z "$title" -a -z "$notitle" ]
then
  title=`sed -e '/^<TITLE>/q'  $filename |
         sed -n -e 's,^<TITLE>\([^<]*\).*$,\1,p'`
fi

		# Get a list of all the sections.  Separate it into a
		# list of merged sections and a list of all other sections.
		# Merged sects are combined and get special treatment besides.
allsects=`sed -n 's,^.*<A NAME="\(sect[0-9][0-9]*\)".*$,\1,p' $filename`

mergesects=`echo $allsects | \
	    awk '{for(i=1; i<=NF && i<'"$mergesect"+2'; i++) print \$i;}'`

sectlist=`echo $allsects |
	    awk '{for(i='"$mergesect"'+2; i<=NF; i++) print \$i;}'`


# This little bit, copied from GNU configure, sets ac_n and ac_c such
# that echo $ac_n "stuff $ac_c" yields "stuff " w/o following newline.
if (echo "testing\c"; echo 1,2,3) | grep c >/dev/null; then
  # Stardent Vistra SVR4 grep lacks -e, says ghazi@caip.rutgers.edu.
  if (echo -n testing; echo 1,2,3) | sed s/-n/xn/ | grep xn >/dev/null; then
    ac_n= ac_c='
' ac_t='	'
  else
    ac_n=-n ac_c= ac_t=
  fi
else
  ac_n= ac_c='\c' ac_t=
fi

		# Create html directory if it does not exist.
if [ -d $htmldir ]
then
  echo "Re-using directory $htmldir."
else
  echo "Creating directory $htmldir..."
  if mkdir $htmldir
  then
    true
  else
    echo "Failed!"
    exit 1
  fi
fi

echo "Creating section pages..."

		# Produce index page.  It is special, since it combines
		# merged sections and uses the rman-generated header.
nextsect=`echo $sectlist | awk '{print $1;}'`
echo $ac_n $mergesects "$ac_c"
(do_title ;
 sed -n -e "1,/^<H[23]><A NAME=\"$nextsect\"/p"  $filename | \
 do_href_next | \
 change_hrefs ;
 do_footer) > $htmldir/sect0.html

		# Produce pages for all other sections except toc.
prevsect="sect$mergesect"
prevtext=`sed -n 's,^<H[23]><A NAME="'$prevsect'" HREF="#toc[0-9][0-9]*">\([^<]*\).*$,\1,p' $filename | sed -e 's/ *$//'`
for sect in $sectlist;
do
    echo $ac_n "$sect $ac_c"
    headtext=`sed -n 's,^<H[23]><A NAME="'$sect'" HREF="#toc[0-9][0-9]*">\([^<]*\).*$,\1,p' $filename | sed -e 's/ *$//'`
    (do_header $headtext ;
     do_href_toc ;
     do_href_prev "$prevsect" "$prevtext" ;
     sed -n -e '/<A NAME="'$sect'"/,/<A NAME=/p' $filename |
     do_href_next |
     change_hrefs ;
     do_footer) > $htmldir/$sect.html
     prevsect="$sect"
     prevtext="$headtext"
done


		# Produce table of contents
echo "toc"
(do_header Table of Contents ;
 sed -n -e '/<A NAME="toc">/,$p' $filename | \
 change_hrefs ;
) > $htmldir/toc.html


		# Finally, make sure the symlinks index.html and
		# sect1.html -> sect0.html are in place, and if not,
		# create them.  If --tocfirst is not specified, then
		# link index.html to section 0, otherwise link it to
		# index.html

echo "Doing symlinks..."
cd $htmldir

rm -f index.html
if [ -z "$tocfirst" ]
then
    echo "Linking index.html -> sect0.html"
    ln -s sect0.html index.html
else
    echo "Linking index.html -> toc.html"
    ln -s toc.html index.html
fi

for sect in $mergesects
do
  if [ "$sect" != "sect0" ]
  then
    echo "Linking $sect.html -> sect0.html"
    rm -f $sect.html
    ln -fs sect0.html $sect.html
  fi
done

echo "Done."