usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / pcre / CleanTxt
blob1f42519c8d780dd7e163c8eec3057d270e9b675d
1 #! /usr/bin/perl -w
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.
8 $blankcount = 0;
9 $lastwascut = 0;
10 $firstheader = 1;
12 # Input on STDIN; output to STDOUT.
14 while (<STDIN>)
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\)$/)
25 if ($firstheader)
27 $firstheader = 0;
28 print;
29 $lastprinted = $_;
30 $lastwascut = 0;
32 $_=<STDIN>; # Remove a blank that follows
33 next;
36 # Count runs of empty lines
38 if (/^\s*$/)
40 $blankcount++;
41 $lastwascut = 0;
42 next;
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.
59 if ($blankcount >= 3)
61 for ($i = 0; $i < 3; $i++)
63 $next[$i] = <STDIN>;
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*$/ &&
73 $next[2] =~ /^\s*$/)
75 $blankcount -= 3;
76 $lastwascut = 1;
79 # Otherwise output the saved blanks, the current, and the next three
80 # lines. Remember the last printed line.
82 else
84 for ($i = 0; $i < $blankcount; $i++) { print "\n"; }
85 print;
86 for ($i = 0; $i < 3; $i++)
88 $next[$i] =~ s/.\x8//g;
89 print $next[$i];
90 $lastprinted = $_;
92 $lastwascut = 0;
93 $blankcount = 0;
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
99 # before headings.
101 else
103 $blankcount = 2 if /^\S/ && !/^Last updated/ && !/^Copyright/ &&
104 defined($lastprinted);
105 for ($i = 0; $i < $blankcount; $i++) { print "\n"; }
106 print;
107 $lastprinted = $_;
108 $lastwascut = 0;
109 $blankcount = 0;
113 # End