github的一些开源项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.9 KiB

  1. #! /usr/bin/perl -w
  2. # Script to take the output of nroff -man and remove all the backspacing and
  3. # the page footers and the screen commands etc so that it is more usefully
  4. # readable online. In fact, in the latest nroff, intermediate footers don't
  5. # seem to be generated any more.
  6. $blankcount = 0;
  7. $lastwascut = 0;
  8. $firstheader = 1;
  9. # Input on STDIN; output to STDOUT.
  10. while (<STDIN>)
  11. {
  12. s/\x1b\[\d+m//g; # Remove screen controls "ESC [ number m"
  13. s/.\x8//g; # Remove "char, backspace"
  14. # Handle header lines. Retain only the first one we encounter, but remove
  15. # the blank line that follows. Any others (e.g. at end of document) and the
  16. # following blank line are dropped.
  17. if (/^PCRE(\w*)\(([13])\)\s+PCRE\1\(\2\)$/)
  18. {
  19. if ($firstheader)
  20. {
  21. $firstheader = 0;
  22. print;
  23. $lastprinted = $_;
  24. $lastwascut = 0;
  25. }
  26. $_=<STDIN>; # Remove a blank that follows
  27. next;
  28. }
  29. # Count runs of empty lines
  30. if (/^\s*$/)
  31. {
  32. $blankcount++;
  33. $lastwascut = 0;
  34. next;
  35. }
  36. # If a chunk of lines has been cut out (page footer) and the next line
  37. # has a different indentation, put back one blank line.
  38. if ($lastwascut && $blankcount < 1 && defined($lastprinted))
  39. {
  40. ($a) = $lastprinted =~ /^(\s*)/;
  41. ($b) = $_ =~ /^(\s*)/;
  42. $blankcount++ if ($a ne $b);
  43. }
  44. # We get here only when we have a non-blank line in hand. If it was preceded
  45. # by 3 or more blank lines, read the next 3 lines and see if they are blank.
  46. # If so, remove all 7 lines, and remember that we have just done a cut.
  47. if ($blankcount >= 3)
  48. {
  49. for ($i = 0; $i < 3; $i++)
  50. {
  51. $next[$i] = <STDIN>;
  52. $next[$i] = "" if !defined $next[$i];
  53. $next[$i] =~ s/\x1b\[\d+m//g; # Remove screen controls "ESC [ number m"
  54. $next[$i] =~ s/.\x8//g; # Remove "char, backspace"
  55. }
  56. # Cut out chunks of the form <3 blanks><non-blank><3 blanks>
  57. if ($next[0] =~ /^\s*$/ &&
  58. $next[1] =~ /^\s*$/ &&
  59. $next[2] =~ /^\s*$/)
  60. {
  61. $blankcount -= 3;
  62. $lastwascut = 1;
  63. }
  64. # Otherwise output the saved blanks, the current, and the next three
  65. # lines. Remember the last printed line.
  66. else
  67. {
  68. for ($i = 0; $i < $blankcount; $i++) { print "\n"; }
  69. print;
  70. for ($i = 0; $i < 3; $i++)
  71. {
  72. $next[$i] =~ s/.\x8//g;
  73. print $next[$i];
  74. $lastprinted = $_;
  75. }
  76. $lastwascut = 0;
  77. $blankcount = 0;
  78. }
  79. }
  80. # This non-blank line is not preceded by 3 or more blank lines. Output
  81. # any blanks there are, and the line. Remember it. Force two blank lines
  82. # before headings.
  83. else
  84. {
  85. $blankcount = 2 if /^\S/ && !/^Last updated/ && !/^Copyright/ &&
  86. defined($lastprinted);
  87. for ($i = 0; $i < $blankcount; $i++) { print "\n"; }
  88. print;
  89. $lastprinted = $_;
  90. $lastwascut = 0;
  91. $blankcount = 0;
  92. }
  93. }
  94. # End