| 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # Script to turn the Exim FAQ into plain ASCII. |
| 4 | |
| 5 | use integer; |
| 6 | |
| 7 | |
| 8 | # Function to do text conversions to display paragraphs |
| 9 | |
| 10 | sub process_display { |
| 11 | my($s) = $_[0]; |
| 12 | $s =~ s/^==>/ /; |
| 13 | return $s; |
| 14 | } |
| 15 | |
| 16 | |
| 17 | # Function to do text conversions to paragraphs not in displays. |
| 18 | |
| 19 | sub process_non_display { |
| 20 | my($s) = $_[0]; |
| 21 | |
| 22 | $s =~ s/@\\/@@backslash@@/g; # @\ temporarily hidden |
| 23 | |
| 24 | $s =~ s/\\#/ /g; # \# is a hard space |
| 25 | |
| 26 | $s =~ s/\\\*\*([^*]*)\*\*\\/$1/g; # \**...**\ => text |
| 27 | $s =~ s/\\\*([^*]*)\*\\/"$1"/g; # \*.....*\ => "text" |
| 28 | $s =~ s/\\"([^"]*)"\\/"$1"/g; # \"....."\ => "text" |
| 29 | $s =~ s/\\\$([^\$]*)\$\\/\$$1/g; # \$.....$\ => $text |
| 30 | $s =~ s/\\\\([^\\]*)\\\\/$1/g; # \\.....\\ => text |
| 31 | $s =~ s/\\\(([^)]*)\)\\/$1/g; # \(.....)\ => text |
| 32 | $s =~ s/\\-([^-]*)-\\/-$1/g; # \-.....-\ => -text |
| 33 | $s =~ s/\\\[([^]]*)\]\\/<$1>/gx; # \[.....]\ => <text> |
| 34 | $s =~ s/\\\?(.*?)\?\\/$1/g; # \?.....?\ => text |
| 35 | $s =~ s/\\\^\^([^^]*)\^\^\\/$1/g; # \^^...^^\ => text |
| 36 | $s =~ s/\\\^([^^]*)\^\\/$1/g; # \^.....^\ => text |
| 37 | $s =~ s/\\%([^%]*)%\\/"$1"/g; # \%.....%\ => "text" |
| 38 | $s =~ s/\\\/([^\/]*)\/\\/$1/g; # \/...../\ => text |
| 39 | $s =~ s/\\([^\\]+)\\/"$1"/g; # \.......\ => "text" |
| 40 | |
| 41 | $s =~ s"//([^/\"]*)//"$1"g; # //.....// => text |
| 42 | $s =~ s/::([^:]*)::/$1:/g; # ::.....:: => text: |
| 43 | |
| 44 | $s =~ s/``(.*?)''/"$1"/g; # ``.....'' => "text" |
| 45 | |
| 46 | $s =~ s/\s*\[\[br\]\]\s*\n/\n/g; # Remove [[br]] |
| 47 | |
| 48 | $s =~ s/@@backslash@@/\\/g; # Put back single backslash |
| 49 | |
| 50 | return $s; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | # Main program |
| 55 | |
| 56 | # We want to read the file paragraph by paragraph; Perl only does this if the |
| 57 | # separating lines are truly blank. Having been caught by lines containing |
| 58 | # whitespace before, do a de-trailing pass first. |
| 59 | |
| 60 | open(IN, "$ARGV[0]") || die "can't open $ARGV[0] (preliminary)\n"; |
| 61 | open(OUT, ">$ARGV[0]-$$") || die "can't open $ARGV[0]-$$\n"; |
| 62 | while (<IN>) |
| 63 | { |
| 64 | s/[ \t]+$//; |
| 65 | print OUT; |
| 66 | } |
| 67 | close(IN); |
| 68 | close(OUT); |
| 69 | rename("$ARGV[0]-$$", "$ARGV[0]") || |
| 70 | die "can't rename $ARGV[0]-$$ as $ARGV[0]\n"; |
| 71 | |
| 72 | # The second argument is the name of the output file. |
| 73 | |
| 74 | open(IN, "$ARGV[0]") || die "can't open $ARGV[0] (for real)\n"; |
| 75 | open(OUT, ">$ARGV[1]") || die "can't open $ARGV[1]\n"; |
| 76 | |
| 77 | $/ = ""; |
| 78 | |
| 79 | while ($_ = <IN>) |
| 80 | { |
| 81 | # Comment lines start with ## |
| 82 | |
| 83 | next if /^\#\#/; |
| 84 | |
| 85 | # If a paragraph begins ==> it is a display which must remain verbatim |
| 86 | # and not be reformatted. The flag gets turned into spaces. |
| 87 | |
| 88 | if ($_ =~ /^==>/) |
| 89 | { |
| 90 | $_ = &process_display($_); |
| 91 | } |
| 92 | |
| 93 | # Non-display paragraph |
| 94 | |
| 95 | else |
| 96 | { |
| 97 | $_ = &process_non_display($_); |
| 98 | } |
| 99 | |
| 100 | print OUT; |
| 101 | } |
| 102 | |
| 103 | close(IN); |
| 104 | close(OUT); |
| 105 | |
| 106 | End |