#!/usr/bin/perl # $Cambridge: exim/doc/doc-scripts/f2txt,v 1.1 2004/10/07 15:04:35 ph10 Exp $ # Script to turn the Exim FAQ into plain ASCII. use integer; # Function to do text conversions to display paragraphs sub process_display { my($s) = $_[0]; $s =~ s/^==>/ /; return $s; } # Function to do text conversions to paragraphs not in displays. sub process_non_display { my($s) = $_[0]; $s =~ s/@\\/@@backslash@@/g; # @\ temporarily hidden $s =~ s/\\#/ /g; # \# is a hard space $s =~ s/\\\*\*([^*]*)\*\*\\/$1/g; # \**...**\ => text $s =~ s/\\\*([^*]*)\*\\/"$1"/g; # \*.....*\ => "text" $s =~ s/\\"([^"]*)"\\/"$1"/g; # \"....."\ => "text" $s =~ s/\\\$([^\$]*)\$\\/\$$1/g; # \$.....$\ => $text $s =~ s/\\\\([^\\]*)\\\\/$1/g; # \\.....\\ => text $s =~ s/\\\(([^)]*)\)\\/$1/g; # \(.....)\ => text $s =~ s/\\-([^-]*)-\\/-$1/g; # \-.....-\ => -text $s =~ s/\\\[([^]]*)\]\\/<$1>/gx; # \[.....]\ => $s =~ s/\\\?(.*?)\?\\/$1/g; # \?.....?\ => text $s =~ s/\\\^\^([^^]*)\^\^\\/$1/g; # \^^...^^\ => text $s =~ s/\\\^([^^]*)\^\\/$1/g; # \^.....^\ => text $s =~ s/\\%([^%]*)%\\/"$1"/g; # \%.....%\ => "text" $s =~ s/\\\/([^\/]*)\/\\/$1/g; # \/...../\ => text $s =~ s/\\([^\\]+)\\/"$1"/g; # \.......\ => "text" $s =~ s"//([^/\"]*)//"$1"g; # //.....// => text $s =~ s/::([^:]*)::/$1:/g; # ::.....:: => text: $s =~ s/``(.*?)''/"$1"/g; # ``.....'' => "text" $s =~ s/\s*\[\[br\]\]\s*\n/\n/g; # Remove [[br]] $s =~ s/@@backslash@@/\\/g; # Put back single backslash return $s; } # Main program # We want to read the file paragraph by paragraph; Perl only does this if the # separating lines are truly blank. Having been caught by lines containing # whitespace before, do a detrailing pass first. open(IN, "$ARGV[0]") || die "can't open $ARGV[0] (preliminary)\n"; open(OUT, ">$ARGV[0]-$$") || die "can't open $ARGV[0]-$$\n"; while () { s/[ \t]+$//; print OUT; } close(IN); close(OUT); rename("$ARGV[0]-$$", "$ARGV[0]") || die "can't rename $ARGV[0]-$$ as $ARGV[0]\n"; # The second argument is the name of the output file. open(IN, "$ARGV[0]") || die "can't open $ARGV[0] (for real)\n"; open(OUT, ">$ARGV[1]") || die "can't open $ARGV[1]\n"; $/ = ""; while ($_ = ) { # Comment lines start with ## next if /^\#\#/; # If a paragraph begins ==> it is a display which must remain verbatin # and not be reformatted. The flag gets turned into spaces. if ($_ =~ /^==>/) { $_ = &process_display($_); } # Non-display paragraph else { $_ = &process_non_display($_); } print OUT; } close(IN); close(OUT); End