3 # Script to take the output of nroff -man and remove all the backspacing and
4 # the page footers and the screen commands etc so that it is more usefully
5 # readable online. In fact, in the latest nroff, intermediate footers don't
6 # seem to be generated any more.
12 # Input on STDIN; output to STDOUT.
16 s/\x1b\[\d+m//g; # Remove screen controls "ESC [ number m"
17 s/.\x8//g; # Remove "char, backspace"
19 # Handle header lines. Retain only the first one we encounter, but remove
20 # the blank line that follows. Any others (e.g. at end of document) and the
21 # following blank line are dropped.
23 if (/^PCRE(\w*)\(([13])\)\s+PCRE\1\(\2\)$/)
32 $_=<STDIN>; # Remove a blank that follows
36 # Count runs of empty lines
45 # If a chunk of lines has been cut out (page footer) and the next line
46 # has a different indentation, put back one blank line.
48 if ($lastwascut && $blankcount < 1 && defined($lastprinted))
50 ($a) = $lastprinted =~ /^(\s*)/;
51 ($b) = $_ =~ /^(\s*)/;
52 $blankcount++ if ($a ne $b);
55 # We get here only when we have a non-blank line in hand. If it was preceded
56 # by 3 or more blank lines, read the next 3 lines and see if they are blank.
57 # If so, remove all 7 lines, and remember that we have just done a cut.
61 for ($i = 0; $i < 3; $i++)
64 $next[$i] = "" if !defined $next[$i];
65 $next[$i] =~ s/\x1b\[\d+m//g; # Remove screen controls "ESC [ number m"
66 $next[$i] =~ s/.\x8//g; # Remove "char, backspace"
69 # Cut out chunks of the form <3 blanks><non-blank><3 blanks>
71 if ($next[0] =~ /^\s*$/ &&
72 $next[1] =~ /^\s*$/ &&
79 # Otherwise output the saved blanks, the current, and the next three
80 # lines. Remember the last printed line.
84 for ($i = 0; $i < $blankcount; $i++) { print "\n"; }
86 for ($i = 0; $i < 3; $i++)
88 $next[$i] =~ s/.\x8//g;
97 # This non-blank line is not preceded by 3 or more blank lines. Output
98 # any blanks there are, and the line. Remember it. Force two blank lines
103 $blankcount = 2 if /^\S/ && !/^Last updated/ && !/^Copyright/ &&
104 defined($lastprinted);
105 for ($i = 0; $i < $blankcount; $i++) { print "\n"; }