~ubuntu-branches/ubuntu/raring/maradns/raring

« back to all changes in this revision

Viewing changes to deadwood-2.4.10/tools/ej/ej2man

  • Committer: Bazaar Package Importer
  • Author(s): Kai Hendry
  • Date: 2010-01-24 12:17:40 UTC
  • mfrom: (1.1.13 upstream) (10.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100124121740-a4e1fjobwaouz443
Tags: 1.4.02-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
 
 
3
# This is a shell script that calls a perl script;
 
4
if [ -e /usr/local/bin/perl ] ; then
 
5
        exec /usr/local/bin/perl -x $0 ${1+"$@"}
 
6
else 
 
7
        exec perl -x $0 ${1+"$@"}
 
8
fi
 
9
 
 
10
# The reason for this is because MaraDNS is built on Centos 3.X
 
11
# (RHEL 3 clone) and the Perl Centos comes with takes over 30 seconds to make
 
12
# the changelog; Perl 5.8.7 does it in under 4 seconds.
 
13
 
 
14
# /usr/local/bin/perl is a local compile of Perl which, on MaraDNS' build
 
15
# system, is able to quickly process the changelog.  The script looks for
 
16
# Perl at /usr/local/bin/perl then anywhere else in the user's path.
 
17
 
 
18
#!/usr/local/bin/perl
 
19
# Convert an ej-formatted doc in to a man page
 
20
# Input: First argument or standard input
 
21
# Output: Standard output
 
22
 
 
23
$FILENAME = shift || "/////";
 
24
 
 
25
# Tmp dir (used for running iconv on non-8859-1 pages)
 
26
$TMP = $ENV{'HOME'} . "/tmp";
 
27
# Make this just /tmp at your own risk.  You have been warned.
 
28
 
 
29
if(! -d $TMP ) {
 
30
    die "Fatal: Please create a directory entitled " . $TMP . "\n";
 
31
    }
 
32
 
 
33
# Read in the doc
 
34
 
 
35
# This makes the script happy when run with both Perl 5.8.0 and
 
36
# Perl 5.8.8; basically 5.8.0 had a lot of unhappy Unicode bugs
 
37
# and so they changed the behavior for later releases.  It is possible
 
38
# to make a Unicode-happy script that runs unchanged Unicode-happy
 
39
# on both 5.8.0 and 5.8.8, but the contortions I had to do were
 
40
# amazing.
 
41
 
 
42
# I would like to thank all of the helpful people in the newsgroup
 
43
# comp.lang.perl.misc for their assistance; I couldn't have done it
 
44
# without them.
 
45
 
 
46
use utf8;
 
47
if($FILENAME ne "/////") {
 
48
    close(STDIN);
 
49
    open(STDIN,"< $FILENAME");
 
50
    }
 
51
 
 
52
binmode(STDIN,":utf8");
 
53
 
 
54
while(<STDIN>){$doc .= $_}
 
55
 
 
56
#$* = 1; # Match multiple lines
 
57
 
 
58
# Get rid of <!-- ... --> comments
 
59
$doc =~ s|<\!\-\-.*?\-\->||msg;
 
60
 
 
61
# Grab the header
 
62
if($doc =~ m|<head>(.*?)</head>|ims) {
 
63
    $header = $1;
 
64
    }
 
65
else {
 
66
    die "Fatal: Document must have a heading section\n";
 
67
    }
 
68
 
 
69
# Make sure the header has 
 
70
# <meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=XXX">
 
71
# Where XXX is any character set
 
72
if($header !~ 
 
73
m|meta\s+http\-equiv\=\"content\-type\"\s+content\=\"text\/html\;\s+charset=|i) 
 
74
  {
 
75
  print "Please have somthing like this:\n";
 
76
  print
 
77
    '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=utf-8">';
 
78
  print "\n";
 
79
  die "Fatal: Header must declare charset\n";
 
80
  }
 
81
else {
 
82
   if($header =~ /charset=([^"]+)/i) {
 
83
       $charset = $1;
 
84
       #print "Charset: $charset\n";
 
85
       }
 
86
   else {
 
87
       die "Fatal: Error determining charset of document\n";
 
88
       }
 
89
   }
 
90
 
 
91
# Sanitize charset; only allow letters, numbers, and the dash
 
92
$charset =~ s/[^A-Za-z0-9\-]//g;
 
93
 
 
94
#$doc = conv_to_latin1($doc); # Not in Fedora core 2
 
95
 
 
96
# OK, see if we have a DTWIDTH header in the ej document.  If so, use that
 
97
# value to determine how wide to make "dt" values in the formatted man page
 
98
 
 
99
if($header =~ m|<dtwidth>(.*?)</dtwidth>|ims) {
 
100
    $width = $1;
 
101
    if($width =~ /\D/) {
 
102
        die "Fatal: DTWIDTH tag can only have a numeric argument.\n";
 
103
        }
 
104
    $DTROFF = ".TP $1";
 
105
    }
 
106
else {
 
107
    # The nroff to convert a DT tag in to
 
108
    $DTROFF = ".TP 4";
 
109
    }
 
110
 
 
111
# OK, the header looks kosher.  Start generating nroff
 
112
 
 
113
print '.\" Do *not* edit this file; it was automatically generated by ej2man';
 
114
print "\n";
 
115
print '.\" Look for a name.ej file with the same name as this filename';
 
116
print "\n";
 
117
print '.\"' . "\n";
 
118
#print '.\" Process this file with the following on iso-8859-1 terminals:'."\n";
 
119
#print '.\" nroff -man -Tlatin1 maradns.8' . "\n";
 
120
print '.\" Process this file with the following' . "\n";
 
121
print '.\" nroff -man -Tutf8 maradns.8 | tr \'\020\' \' \'' . "\n";
 
122
print '.\"' . "\n";
 
123
# Timestamp
 
124
$ts = localtime(time());
 
125
print '.\" Last updated ' . $ts . "\n";
 
126
print '.\"' . "\n";
 
127
 
 
128
# OK, see if we have a TH header in the ej document.  If so, add that to the
 
129
# man page.  If not, generate a generic TH
 
130
 
 
131
if($header =~ m|<th>(.*?)</th>|ims) {
 
132
    print ".TH $1\n";
 
133
    }
 
134
else {
 
135
    print ".TH \n";
 
136
    }
 
137
 
 
138
print '.\" We don\'t want hyphenation (it\'s too ugly)' . "\n";
 
139
print '.\" We also disable justification when using nroff' . "\n";
 
140
print '.\" Due to the way the -mandoc macro works, this needs to be placed'
 
141
      . "\n";
 
142
print '.\" after the .TH heading' . "\n";
 
143
print ".hy 0\n";
 
144
print ".if n .na\n";
 
145
print '.\"' . "\n";
 
146
print '.\" We need the following stuff so that we can have single quotes' .
 
147
      "\n";
 
148
print '.\" In both groff and other UNIX *roff processors' . "\n";
 
149
print '.if \n(.g .mso www.tmac' . "\n";
 
150
print '.ds aq \(aq' . "\n";
 
151
print '.if !\\n(.g .if \'\\(aq\'\' .ds aq \\\'' . "\n";
 
152
 
 
153
 
 
154
# Enough of header processing; let's get to the body of the document
 
155
 
 
156
# Grab the body
 
157
if($doc =~ m|<body>(.*?)</body>|ims) {
 
158
    $body = $1;
 
159
    }
 
160
else {
 
161
    die "Fatal: Document must have a body section\n";
 
162
    }
 
163
 
 
164
$body = process_body($body,0);
 
165
 
 
166
print($body);
 
167
 
 
168
print "\n";
 
169
 
 
170
exit(0);
 
171
 
 
172
# And this processes the body (we do this way so we can recursively handle 
 
173
# those pesky PRE flags)
 
174
sub process_body {
 
175
   my($body,$inrecurse) = @_;
 
176
   my($hack,$rest,$filename);
 
177
   my(@parts);
 
178
 
 
179
   # The INCLUDE tag
 
180
   while($body =~ m|\<include\s+\"([^"]+)\"\s*\>|ims) {
 
181
       $filename = $1;
 
182
       open(FILE,"< $filename") || die "Can not find file $filename\n";
 
183
       $hack = "";
 
184
       while(<FILE>) {$hack .= $_}
 
185
       close(FILE);
 
186
       #$hack = conv_to_latin1($hack);
 
187
       #$hack = process_body($hack);
 
188
       $body =~ s|\<include\s+\"([^"]+)\"\s*\>|$hack|ims;
 
189
       }
 
190
 
 
191
   # Get rid of any </?BLOCKQUOTE> tags in bulletted lists; the NROFF macros
 
192
   # can not handle nesting 
 
193
   if($inrecurse == 0) {
 
194
       @parts = split(m|</?ul>|im,$body);
 
195
       if($#parts > 0) {
 
196
           $body = "";
 
197
           for($hack = 0; $hack <= $#parts; $hack++) {
 
198
               if($hack % 2 == 0) { # If we are not in a bulleted list
 
199
                   $body .= $parts[$hack];
 
200
                   if($hack < $#parts) {
 
201
                      $body .= "\n<ul>\n";
 
202
                      }
 
203
                   }
 
204
               else {
 
205
                   $parts[$hack] =~ s|</?blockquote>||g;
 
206
                   $body .= $parts[$hack];
 
207
                   $body .= "\n</ul>\n";
 
208
                   }
 
209
               }
 
210
           }
 
211
       }
 
212
 
 
213
   # &nbsp; is made a simple whitespace
 
214
   while($body =~ m|\&nbsp\;|ims) {
 
215
       $body =~ s|\&nbsp\;| |ims;
 
216
       }
 
217
 
 
218
   # The HIBIT tag (replace with ALT text then remove)
 
219
   while($body =~ m|<hibit alt=\"([^"]*)\">(.*?)</hibit>|ims) {
 
220
       $body =~ s|<hibit alt=\"([^"]*)\">(.*?)</hibit>|$1|ims;
 
221
       }
 
222
   $body =~ s|<hibit[^>]*>(.*?)</hibit>||imsg;
 
223
   # Because of how PRE is handled, we need to delete anything starting
 
224
   # with <hibit> to the end of string and anything from the beginning
 
225
   # of the string to </hibit>, since we may only be processing a subpart
 
226
   # of the whole text. (Disabled because this is above the PRE processing
 
227
   #$body =~ s|<hibit alt=\"([^"]*)\">.*$|$1|is;
 
228
   #$body =~ s|<hibit[^>]*>.*$||is;
 
229
   #$body =~ s|^.*</hibit>||is;
 
230
 
 
231
   # The PRE tag
 
232
   @parts = split(m|</?pre>|im,$body);
 
233
   if($#parts > 0) {
 
234
        $body = "";
 
235
        for($hack=0;$hack <= $#parts; $hack++) {
 
236
           if($hack %2 == 0) { # If we are not in a <pre> section
 
237
               $body .= process_body($parts[$hack],1);
 
238
               }
 
239
           else {
 
240
               $body .= "\n.nf";
 
241
               # Deal with back slashes
 
242
               $parts[$hack] =~ s/\\/\\\\/g;
 
243
               # Make single quotes literal single quotes ('\(aq' in troff)
 
244
               $parts[$hack] =~ s/\'/\\\(aq/g;
 
245
               # Handle the á character (Debian's lint complains if the 
 
246
               # man page has raw hi-bit characters)
 
247
               $parts[$hack] =~ s|\xc3\xa1|\\\(\'a|g;
 
248
               $body .= $parts[$hack];
 
249
               $body .= ".fi\n";
 
250
               }
 
251
           }
 
252
 
 
253
       # Make á \('a so *roff can digest this.  We have to put this here
 
254
       # so the sequence remains a *roff command
 
255
       $body =~ s|á|\\\(\'a|g;
 
256
 
 
257
       return($body);
 
258
       }
 
259
 
 
260
   # Backslashes need to be escaped in *roff source
 
261
   $body =~ s/\\/\\\\/g;
 
262
   # As do single quotes
 
263
   $body =~ s/\'/\\\(aq/g;
 
264
 
 
265
   # The H1 tag
 
266
   while($body =~ m|<h1>(.*?)</h1>|ims) {
 
267
       $hack = $1;
 
268
       $hack =~ s/\s+/ /g;
 
269
       $hack =~ s/\"\'//g;
 
270
       $body =~ s|<h1>(.*?)</h1>|\n.SH "$hack"\n.PP\n|ims;
 
271
       }
 
272
 
 
273
   # The H2 tag
 
274
   while($body =~ m|<h2>(.*?)</h2>|ims) {
 
275
       $hack = $1;
 
276
       $hack =~ s/\s+/ /g;
 
277
       $hack =~ s/\"\'//g;
 
278
       $body =~ s|<h2>(.*?)</h2>|\n.PP\n.in -3\n\\fB$hack\\fR\n.PP\n|ims;
 
279
       }
 
280
 
 
281
   # The A tag (and /A closer)
 
282
   $body =~ s|</?a[^>]+>||img;
 
283
   $body =~ s|</?a>||img;
 
284
 
 
285
   # The TT tag (and /TT closer)
 
286
   $body =~ s|</?tt>||img;
 
287
 
 
288
   # The HR tag
 
289
   $body =~ s|<hr>|\n.PP\n.RS 28\n* * *\n.RE\n.PP\n|img;
 
290
 
 
291
   # The HINCLUDE tag
 
292
   $body =~ s|<hinclude[^>]+>||img;
 
293
 
 
294
   # The BLOCKQUOTE tag
 
295
   $body =~ s|<blockquote>|\n.PP\n.RS 4\n|img;
 
296
   $body =~ s|</blockquote>|\n.RE\n.PP\n|img;
 
297
 
 
298
   # The B tag
 
299
   while($body =~ m|<b>(.*?)</b>(\S+)?|ims) {
 
300
      $hack = $1;
 
301
      $rest = $2;
 
302
      if($rest =~ /[<>]/) {
 
303
         die "ej2man can't handle a tag immediately after a B tag\nthe offending text is $rest\n";
 
304
         }
 
305
      if($hack =~ m|<\?i>|) {
 
306
         die "No I tags are allowed inside B tags\n";
 
307
         }
 
308
      $hack =~ s/\s+/ /g;
 
309
      $hack =~ s/\"\'//g;
 
310
      $rest =~ s/\"\'//g;
 
311
      if($rest) {
 
312
        $body =~ s|<b>(.*?)</b>\S+|\n.BR "$hack" "$rest"\n|ims;
 
313
        }
 
314
      else {
 
315
        $body =~ s|<b>(.*?)</b>|\n.B "$hack"\n|ims;
 
316
        }
 
317
      }
 
318
 
 
319
   # The I tag
 
320
   while($body =~ m|<i>(.*?)</i>(\S+)?|ims) {
 
321
      $hack = $1;
 
322
      $rest = $2;
 
323
      if($rest =~ /[<>]/) {
 
324
         print "The stuff in the I: $hack\n";
 
325
         die "ej2man can't handle a tag immediately after a I tag\nthe offending text is $rest\n";
 
326
         }
 
327
      if($hack =~ m|<\?b>|) {
 
328
         die "No B tags are allowed inside I tags\n";
 
329
         }
 
330
      $hack =~ s/\s+/ /g;
 
331
      $hack =~ s/\"\'//g;
 
332
      $rest =~ s/\"\'//g;
 
333
      if($rest) {
 
334
        $body =~ s|<i>(.*?)</i>\S+|\n.IR "$hack" "$rest"\n|ims;
 
335
        }
 
336
      else {
 
337
        $body =~ s|<i>(.*?)</i>|\n.I "$hack"\n|ims;
 
338
        }
 
339
      }
 
340
 
 
341
   # Get rid of any multiple newlines
 
342
   $body =~ s/\n(\s*)\n/\n/msg;
 
343
 
 
344
   # The P tag
 
345
   $body =~ s/<p>\s*/\n.PP\n/img;
 
346
 
 
347
   # The UL and tags (just nuke them)
 
348
   $body =~ s/<[du]l>//img;
 
349
 
 
350
   # The LI tag
 
351
   #$body =~ s/<li>\n?/\n.TP 2\n•\n/img; # Can't do because of groff bug
 
352
   #$body =~ s/<li>\n?/\n.TP 2\nʘ\n/img; # World isn't ready for unicode
 
353
   $body =~ s/<li>\n?/\n.TP 2\n*\n/img;
 
354
 
 
355
   # The DT and DD tag
 
356
   while($body =~ /<dt>(.*?)<dd>\n?/sim) {
 
357
       $hack = $1;
 
358
       $hack =~ s/\s+/ /g;
 
359
       $body =~ s/<dt>(.*?)<dd>\n?/\n$DTROFF\n$hack\n/sim;
 
360
       }
 
361
 
 
362
   # The /DT and /DD tags (just nuke them)
 
363
   $body =~ s|</d[td]>||img;
 
364
 
 
365
   # The /UL and /DL tag (which we don't ignore)
 
366
   $body =~ s|</[du]l>|\n.PP\n|img;
 
367
 
 
368
   # Get rid of leading space; this confuses nroff
 
369
   $body =~ s/\n[ \t]+/\n/msg;
 
370
 
 
371
   # Get rid of empty lines before a .TP or .PP flag; this never looks nice
 
372
   $body =~ s/\n+(\n\.[TP]P)/$1/msg;
 
373
 
 
374
   # Get rid of empty lines at the beginning of the segment which come
 
375
   # before a .TP; this covers the case of a </pre> before a <li> in the
 
376
   # EJ source
 
377
   $body =~ s/^\n+(\.[TP]P)/$1/ms;
 
378
 
 
379
   # Same with empty lines before an .RE flag; this does not look nice
 
380
   $body =~ s/^\n+(\.RE)/$1/ms;
 
381
 
 
382
   # Get rid of multiple empty lines together; this never looks nice
 
383
   # when formatted by Nroff
 
384
   $body =~ s/\n\n\n+/\n\n/msg;
 
385
 
 
386
   # Get rid of empty lines at the end of a segment after a .TP or .PP
 
387
   # flag to work around how <pre> tags are handled
 
388
   $body =~ s/(\n\.[TP]P)\s*$/$1/ms;
 
389
 
 
390
   # Put a newline before the .RE flag; this looks nicer
 
391
   $body =~ s/(\n\.RE)/\n$1/msg;
 
392
   $body =~ s/^(\.RE)/\n$1/msg;
 
393
 
 
394
   # Get rid of empty lines after a .RE flag; this does not look nice either
 
395
   $body =~ s/(\n\.RE.*?\n)\n+/$1/msg;
 
396
 
 
397
   # Put a newline before the .in flag; this looks nicer
 
398
   $body =~ s/(\n\.in)/\n$1/msg;
 
399
 
 
400
   # Get rid of empty lines after a .TP or .PP flag; this never looks nice
 
401
   $body =~ s/(\n\.[TP]P.*?\n)\n+/$1/msg;
 
402
 
 
403
   # Get rid of multiple spaces; nroff (unlike EJ) honors them
 
404
   $body =~ s/[ \t]+/ /sg;
 
405
 
 
406
   # The TABLE tags (TABLE, TD, TR, /TABLE)
 
407
   $body =~ s|<table>|.ta +5 +7 +7|ig;
 
408
   $body =~ s|<td>|\t|ig;
 
409
   # We also process .br tags
 
410
   $body =~ s|<[tb]r>\n?|\n.br\n|ig;
 
411
   $body =~ s|</table>||ig;
 
412
 
 
413
   # Break long lines so the nroff source is more legible
 
414
   $body = fmt($body);
 
415
 
 
416
   $body;
 
417
   }
 
418
 
 
419
# This takes a string, and braks any lines longer than 75 columns; otherwise
 
420
# it performs no other formatting
 
421
# Input: The string to format
 
422
# Output: The formatted string
 
423
 
 
424
sub fmt {
 
425
   my($input) = @_;
 
426
   my($place,$lastspace,$column,$linebegin);
 
427
  
 
428
   $place = $lastspace = $column = $linebegin = 0;
 
429
 
 
430
   # Get rid of trailing white space, which confuses this algorithm
 
431
   $input =~ s/[ \t]+\n/\n/sg;
 
432
 
 
433
   # The core algorithm
 
434
   while($place < length($input)) {
 
435
       # If we hit a whitespace, remember that this is where the last
 
436
       # (previous) space character is
 
437
       if(substr($input,$place,1) =~ /[ \t]/) {
 
438
           $lastspace = $place;
 
439
           }
 
440
       # If we hit the end of a line reset the counters which tell us when
 
441
       # to break a line
 
442
       if(substr($input,$place,1) =~ /\n/) {
 
443
           $column = -1;
 
444
           $linebegin = $lastspace = $place + 1;
 
445
           }
 
446
       # This adds the newline as needed.  Note that we do not break
 
447
       # lines which start with a .; this means the line has a man macro 
 
448
       # and breaking the line will change the formatting of the page
 
449
       if($column > 70 && $linebegin != $lastspace && 
 
450
          substr($input,$linebegin,1) !~ /\./) {
 
451
           substr($input,$lastspace,1,"\n");
 
452
           $place = $lastspace;
 
453
           $column = -1;
 
454
           $linebegin = $lastspace = $place + 1;
 
455
           }
 
456
       $column++;
 
457
       $place++;
 
458
       }
 
459
 
 
460
   $input;
 
461
   } 
 
462
 
 
463
# Convert a given string from whatever encoding to latin1, since groff is
 
464
# still in the dark ages and can not handle utf-8 input (not true in
 
465
# Fedoa core 2)
 
466
# Input: String to convert
 
467
# Output: Converted string
 
468
# Global variables used: $charset (assumed to already be sanitized)
 
469
# $TMP (also assumed to be sanitized)
 
470
sub conv_to_latin1 {
 
471
    my($string) = @_;
 
472
    if($charset =~ /latin1/i || $charset =~ /8859.1/) {
 
473
        return $string;
 
474
        }
 
475
    open(TFILE,"> $TMP/conv.$$") || die "Can not open $TMP/conv.$$: $!\n";
 
476
    print TFILE $string;
 
477
    close(TFILE);
 
478
#   if($charset !~ /utf.?8/i) {
 
479
        system("iconv -f $charset -t latin1 $TMP/conv.$$ > $TMP/converted.$$");
 
480
#       }
 
481
#   else {
 
482
#      system("utf8tol1 < $TMP/conv.$$ > $TMP/converted.$$");
 
483
#      }
 
484
    open(TFILE,"< $TMP/converted.$$") || 
 
485
        die "Can not open $TMP/converted.$$: $!\n";
 
486
    $string = "";
 
487
    while(<TFILE>) {
 
488
        $string .= $_;
 
489
        }
 
490
    close(TFILE);
 
491
 
 
492
    # Delete the trash
 
493
    #unlink("$TMP/conv.$$") || die "Can not erase $TMP/conv.$$: $!\n";
 
494
    #unlink("$TMP/converted.$$") ||die "Can not erase $TMP/convierted.$$: $!\n";
 
495
    return $string;
 
496
    }
 
497