utils/prepare-configfile.pl: Automatically unbreak the forward header line
[privoxy.git] / utils / prepare-configfile.pl
1 #!/usr/local/bin/perl
2
3 # This script is used by the config-file target in GNUMakefile.
4 #
5 # It removes garbage in the w3m output and separates comments
6 # and active directives.
7
8 use strict;
9 use warnings;
10
11 sub main() {
12     my $hit_header = 0;
13     my $hit_option = 0;
14     my $header_len;
15     my $unfold_mode = 0;
16     my $unfolding_enabled = 0;
17     my $windows_section_reached = 0;
18
19     while (<>) {
20
21         if (!$unfolding_enabled and m/=========/) {
22             # We passed the table of contents
23             # and can try to unfold unintentional
24             # line breaks;
25             $unfolding_enabled = 1;
26         }
27         if (m/specific to the Windows GUI/) {
28             # The Windows section is formatted differently.
29             $windows_section_reached = 1;
30         }
31
32         s/^1\. \@\@TITLE\@\@/     /i;
33
34         if ($hit_header) {
35             $header_len += length($_);
36             $_ = " " . $_;
37         } elsif (m/^(\d*\.){1,3}\s/) {
38             # Remove the first digit as it's the
39             # config file section in the User Manual.
40             s/^(\d\.)//;
41
42             # If it's a section header, uppercase it.
43             $_ = uc() if (/^\d\.\s+/);
44
45             # Remember to underline it.
46             $hit_header = 1;
47             $header_len = length($_);
48         }
49
50         if ($unfold_mode) {
51             s/^\s+/ /;
52             $unfold_mode = 0;
53         } else {
54             s/^/#  /;
55         }
56         if ($unfolding_enabled and
57             (m/(\s+#)\s*$/ or m/forward-socks5 and$/)) {
58             $unfold_mode = 1;
59             chomp;
60         }
61
62         # XXX: someone should figure out what this stuff
63         # is supposed to do (and if it really does that).
64         s/^#  #/####/ if /^#  #{12,}/;
65         s/^.*$// if $hit_option;
66         $hit_option = 0;
67         s/^\n//;
68         s/^#\s*-{20,}//;
69         s/ *$//;
70         $hit_option = 1 if s/^#\s+@@//;
71
72         if ($windows_section_reached) {
73             # Do not drop empty lines in the Windows section
74             s/^\s*$/#\n/;
75         }
76
77         print unless (/^\s*$/);
78
79         if ($hit_header and !$unfold_mode) {
80             # The previous line was a section
81             # header so we better underline it.
82             die "Invalid header length" unless defined $header_len;
83             print "#  " . "=" x $header_len . "\n";
84             $hit_header = 0;
85             $header_len = 0;
86         };
87     }
88 }
89 main();