Tidy up "make" output along the lines of a 2.6 kernel make (just a short
[exim.git] / doc / doc-scripts / f2txt
CommitLineData
495ae4b0
PH
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
6use integer;
7
8
9# Function to do text conversions to display paragraphs
10
11sub process_display {
12my($s) = $_[0];
13$s =~ s/^==>/ /;
14return $s;
15}
16
17
18# Function to do text conversions to paragraphs not in displays.
19
20sub process_non_display {
21my($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
51return $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
61open(IN, "$ARGV[0]") || die "can't open $ARGV[0] (preliminary)\n";
62open(OUT, ">$ARGV[0]-$$") || die "can't open $ARGV[0]-$$\n";
63while (<IN>)
64 {
65 s/[ \t]+$//;
66 print OUT;
67 }
68close(IN);
69close(OUT);
70rename("$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
75open(IN, "$ARGV[0]") || die "can't open $ARGV[0] (for real)\n";
76open(OUT, ">$ARGV[1]") || die "can't open $ARGV[1]\n";
77
78$/ = "";
79
80while ($_ = <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
104close(IN);
105close(OUT);
106
107End