fixed unquoted expr
[exim-website.git] / filter / mkfilter.pl
1 #!/usr/bin/perl
2 #
3 use strict;
4 use FileHandle;
5
6 \f
7
8 sub process_refile {
9     my $fn = shift;
10
11     my $re;
12     print STDERR "Opening $fn\n";
13     my $fh = FileHandle->new($fn, 'r') || die $!;
14     while (<$fh>) {
15         chomp();
16         # process includes
17         if (/^\#include\s/) {
18             my($junk, $nfn) = split;
19             $re .= process_refile($nfn);
20             next;
21         }
22         # ignore comments starting at the begining of the line
23         next if (/^\#/);
24         # dispose of comments with their leading spaces
25         s/\s+\#.*$//;
26         # recode \" -> "
27         s/\\\"/\"/g;
28         # double all \ (twice)
29         s/\\/\\\\/g;
30         s/\\/\\\\/g;
31         # escape " again
32         s/\"/\\\"/g;
33         # remove all space
34         s/\s+//g;
35         # add to re
36         $re .= $_;
37     }
38     return $re;
39 }
40
41 \f
42
43 sub process_recfile {
44     my $fn = shift;
45
46     my $re;
47     print STDERR "Opening $fn\n";
48     my $fh = FileHandle->new($fn, 'r') || die $!;
49     while (<$fh>) {
50         chomp();
51         # process includes
52         if (/^\#include\s/) {
53             my($junk, $nfn) = split;
54             $re .= process_recfile($nfn);
55             next;
56         }
57         # skip comment only and blank lines
58         next if (/^\#/);
59         next if (/^\s*$/);
60         $re .= "#\t$_\n";
61     }
62     return $re;
63 }
64
65 \f
66
67 # main
68 {
69     while(<>) {
70         s/\[\[([a-z0-9_]+)\]\]/process_refile($1)/ge;
71         s/\[\<([a-z0-9_]+)\>\]/process_recfile($1)/ge;
72         print;
73     }
74 }