]> git.notmuchmail.org Git - notmuch/blob - devel/news2wiki.pl
debian: add changelog stanza for 0.18.2-1
[notmuch] / devel / news2wiki.pl
1 #!/usr/bin/perl
2 #
3 # Author: Tomi Ollila
4 # License: same as notmuch
5
6 # This program is used to split NEWS file to separate (mdwn) files
7 # for notmuch wiki. Example run:
8 #
9 # $ ./devel/news2wiki.pl NEWS ../notmuch-wiki/news
10 #
11 # In case taken into more generic use, modify these comments and examples.
12
13 use strict;
14 use warnings;
15
16 unless (@ARGV == 2) {
17     warn "\n$0 <source-file> <destination-directory>\n\n";
18     warn "Example: ./devel/news2wiki.pl NEWS ../notmuch-wiki/news\n\n";
19     exit 1;
20 }
21
22 die "'$ARGV[0]': no such file\n" unless -f $ARGV[0];
23 die "'$ARGV[1]': no such directory\n" unless -d $ARGV[1];
24
25 open I, '<', $ARGV[0] or die "Cannot open '$ARGV[0]': $!\n";
26
27 open O, '>', '/dev/null' or die $!;
28 my @emptylines = ();
29 my $cln;
30 print "\nWriting to $ARGV[1]:\n";
31 while (<I>)
32 {
33     warn "$ARGV[0]:$.: tab(s) in line!\n" if /\t/;
34     warn "$ARGV[0]:$.: trailing whitespace\n" if /\s\s$/;
35     # The date part in regex recognizes wip version dates like: (201x-xx-xx).
36     if (/^Notmuch\s+(\S+)\s+\((\w\w\w\w-\w\w-\w\w)\)\s*$/) {
37         # open O... autocloses previously opened file.
38         open O, '>', "$ARGV[1]/release-$1.mdwn" or die $!;
39         print "+ release-$1.mdwn...\n";
40         print O "[[!meta date=\"$2\"]]\n\n";
41         @emptylines = ();
42     }
43
44     last if /^<!--\s*$/; # Local variables block at the end (as of now).
45
46     # Buffer "trailing" empty lines -- dropped at end of file.
47     push(@emptylines, $_), next if s/^\s*$/\n/;
48     if (@emptylines) {
49         print O @emptylines;
50         @emptylines = ();
51     }
52
53     # Convert '*' to '`*`' and "*" to "`*`" so that * is not considered
54     # as starting emphasis character there. We're a bit opportunistic
55     # there -- some single * does not cause problems and, on the other
56     # hand, this would not regognize already 'secured' *:s.
57     s/'[*]'/'`*`'/g; s/"[*]"/"`*`"/g;
58
59     # Convert nonindented lines that aren't already headers or
60     # don't contain periods (.) or '!'s to level 4 header.
61     if ( /^[^\s-]/ ) {
62         my $tbc = ! /[.!]\s/;
63         chomp;
64         my @l = $_;
65         $cln = $.;
66         while (<I>) {
67             last if /^\s*$/;
68             #$cln = 0 if /^---/ or /^===/; # used for debugging.
69             $tbc = 0 if /[.!]\s/ or /^---/ or /^===/;
70             chomp; s/^\s+//;
71             push @l, $_;
72         }
73         if ($tbc) {
74             print O "### ", (join ' ', @l), "\n";
75         }
76         else {
77             #print "$ARGV[0]:$cln: skip level 4 header conversion\n" if $cln;
78             print O (join "\n", @l), "\n";
79         }
80         @emptylines = ( "\n" );
81         next;
82     }
83
84     # Markdown doc specifies that list item may have paragraphs if those
85     # are indented by 4 spaces (or a tab) from current list item marker
86     # indentation (paragraph meaning there is empty line in between).
87     # If there is empty line and next line is not indented 4 chars then
88     # that should end the above list. This doesn't happen in all markdown
89     # implementations.
90     # In our NEWS case this problem exists in release 0.6 documentation.
91     # It can be avoided by removing 2 leading spaces in lines that are not
92     # list items and requiring all that indents are 0, 2, and 4+ (to make
93     # regexp below work).
94     # Nested lists are supported but one needs to be more careful with
95     # markup there (as the hack below works only on first level).
96
97     s/^[ ][ ]// unless /^[ ][ ](?:[\s*+-]|\d+\.)\s/;
98
99     print O $_;
100 }
101 print "\ndone.\n";
102 close O;