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