f4d27521ccf0dfbed94b6876b223c9b48a34cb63
[privoxy.git] / utils / changelog2doc.pl
1 #!/usr/bin/perl
2
3 ##########################################################################
4 #
5 # $Id: changelog2doc.pl,v 1.7 2010/10/31 13:29:31 fabiankeil Exp $
6 #
7 # Filter to parse the ChangeLog and translate the changes for
8 # the most recent version into something that looks like markup
9 # for the documentation but still needs fine-tuning.
10 #
11 # Copyright (c) 2008, 2010 Fabian Keil <fk@fabiankeil.de>
12 #
13 # Permission to use, copy, modify, and distribute this software for any
14 # purpose with or without fee is hereby granted, provided that the above
15 # copyright notice and this permission notice appear in all copies.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 ##########################################################################
25
26 use strict;
27 use warnings;
28
29 my @entries;
30
31 sub read_entries() {
32     my $section_reached = 0;
33     my $i = -1;
34
35     while (<>) {
36         if (/^\*{3} /) {
37             last if $section_reached;
38             $section_reached = 1;
39             next;
40         }
41         next unless $section_reached;
42         next if /^\s*$/;
43
44         if (/^(\s*)-/) {
45             my $indentation = length($1);
46             if ($i > 1 and $entries[$i]{indentation} > $indentation) {
47                 $entries[$i]{last_list_item} = 1;
48             }
49             $i++; 
50             $entries[$i]{description} = '';
51             $entries[$i]{indentation} = $indentation;
52         }
53         if (/:\s*$/) {
54             $entries[$i]{list_header} = 1;
55         }
56
57         s@^\s*-?\s*@@;
58
59         $entries[$i]{description} .= $_;
60     }
61     if ($entries[$i]{indentation} != 0) {
62         $entries[$i]{last_list_item} = 1;
63     }
64     print "Parsed " . @entries . " entries.\n";
65 }
66
67 sub create_listitem_markup($) {
68     my $entry = shift;
69     my $description = $entry->{description};
70     my $markup = '';
71     my $default_lws = '  ';
72     my $lws = $default_lws x ($entry->{indentation} ? 2 : 1);
73
74     chomp $description;
75
76     $description =~ s@\n@\n  ${lws}@g;
77
78     $markup .= $lws . "<listitem>\n" .
79                $lws . " <para>\n";
80
81     $markup .= $lws . "  " . $description . "\n";
82
83     if (defined $entry->{list_header}) {
84         $markup .= $lws . "  <itemizedlist>\n";
85
86     } else {
87         if (defined $entry->{last_list_item}) {
88             $markup .= $lws . " </para>\n";
89             $markup .= $lws . " </listitem>\n";
90             $markup .= $lws . "</itemizedlist>\n";
91             $lws = $default_lws;
92         }
93         $markup .= $lws . " </para>\n" .
94                    $lws . "</listitem>\n";
95     }
96
97     return $markup;
98 }
99
100 sub wrap_in_para_itemlist_markup($) {
101     my $content = shift;
102     my $markup = "<para>\n" .
103                  " <itemizedlist>\n" .
104                  "  $content" .
105                  " </itemizedlist>\n" .
106                  "</para>\n";
107     return $markup;
108 }
109
110 sub generate_markup() {
111     my $markup = '';
112
113     foreach my $entry (@entries) {
114         $markup .= create_listitem_markup(\%{$entry});
115     }
116
117     print wrap_in_para_itemlist_markup($markup);
118 }
119
120 sub main () {
121     read_entries();
122     generate_markup();
123 }
124
125 main();