Add support for the new ChangeLog format.
[privoxy.git] / utils / changelog2doc.pl
1 #!/usr/bin/perl
2
3 # $Id: changelog2doc.pl,v 1.5 2010/10/31 13:27:03 fabiankeil Exp $
4 # $Source: /cvsroot/ijbswa/current/utils/changelog2doc.pl,v $
5
6 # Filter to parse the ChangeLog and translate the changes for
7 # the most recent version into something that looks like markup
8 # for the documentation but still needs fine-tuning.
9
10 use strict;
11 use warnings;
12
13 my @entries;
14
15 sub read_entries() {
16     my $section_reached = 0;
17     my $i = -1;
18
19     while (<>) {
20         if (/^\*{3} /) {
21             last if $section_reached;
22             $section_reached = 1;
23             next;
24         }
25         next unless $section_reached;
26         next if /^\s*$/;
27
28         if (/^(\s*)-/) {
29             my $indentation = length($1);
30             if ($i > 1 and $entries[$i]{indentation} > $indentation) {
31                 $entries[$i]{last_list_item} = 1;
32             }
33             $i++; 
34             $entries[$i]{description} = '';
35             $entries[$i]{indentation} = $indentation;
36         }
37         if (/:\s*$/) {
38             $entries[$i]{list_header} = 1;
39         }
40
41         s@^\s*-?\s*@@;
42
43         $entries[$i]{description} .= $_;
44     }
45     if ($entries[$i]{indentation} != 0) {
46         $entries[$i]{last_list_item} = 1;
47     }
48     print "Parsed " . @entries . " entries.\n";
49 }
50
51 sub create_listitem_markup($) {
52     my $entry = shift;
53     my $description = $entry->{description};
54     my $markup = '';
55     my $default_lws = '  ';
56     my $lws = $default_lws x ($entry->{indentation} ? 2 : 1);
57
58     chomp $description;
59
60     $description =~ s@\n@\n  ${lws}@g;
61
62     $markup .= $lws . "<listitem>\n" .
63                $lws . " <para>\n";
64
65     $markup .= $lws . "  " . $description . "\n";
66
67     if (defined $entry->{list_header}) {
68         $markup .= $lws . "  <itemizedlist>\n";
69
70     } else {
71         if (defined $entry->{last_list_item}) {
72             $markup .= $lws . " </para>\n";
73             $markup .= $lws . "</itemizedlist>\n";
74             $lws = $default_lws;
75         }
76         $markup .= $lws . " </para>\n" .
77                    $lws . "</listitem>\n";
78     }
79
80     return $markup;
81 }
82
83 sub wrap_in_para_itemlist_markup($) {
84     my $content = shift;
85     my $markup = "<para>\n" .
86                  " <itemizedlist>\n" .
87                  "  $content" .
88                  " </itemizedlist>\n" .
89                  "</para>\n";
90     return $markup;
91 }
92
93 sub generate_markup() {
94     my $markup = '';
95
96     foreach my $entry (@entries) {
97         $markup .= create_listitem_markup(\%{$entry});
98     }
99
100     print wrap_in_para_itemlist_markup($markup);
101 }
102
103 sub main () {
104     read_entries();
105     generate_markup();
106 }
107
108 main();