]> git.notmuchmail.org Git - notmuch/blob - devel/news2wiki.pl
Merge tag '0.18.2_rc1'
[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     if (/^Notmuch\s+(\S+)\s+\((\d\d\d\d-\d\d-\d\d|UNRELEASED)\)\s*$/) {
36         # open O... autocloses previously opened file.
37         open O, '>', "$ARGV[1]/release-$1.mdwn" or die $!;
38         print "+ release-$1.mdwn...\n";
39         print O "[[!meta date=\"$2\"]]\n\n";
40         @emptylines = ();
41     }
42
43     last if /^<!--\s*$/; # Local variables block at the end (as of now).
44
45     # Buffer "trailing" empty lines -- dropped at end of file.
46     push(@emptylines, $_), next if s/^\s*$/\n/;
47     if (@emptylines) {
48         print O @emptylines;
49         @emptylines = ();
50     }
51
52     # Convert '*' to '`*`' and "*" to "`*`" so that * is not considered
53     # as starting emphasis character there. We're a bit opportunistic
54     # there -- some single * does not cause problems and, on the other
55     # hand, this would not regognize already 'secured' *:s.
56     s/'[*]'/'`*`'/g; s/"[*]"/"`*`"/g;
57
58     # Convert nonindented lines that aren't already headers or
59     # don't contain periods (.) or '!'s to level 4 header.
60     if ( /^[^\s-]/ ) {
61         my $tbc = ! /[.!]\s/;
62         chomp;
63         my @l = $_;
64         $cln = $.;
65         while (<I>) {
66             last if /^\s*$/;
67             #$cln = 0 if /^---/ or /^===/; # used for debugging.
68             $tbc = 0 if /[.!]\s/ or /^---/ or /^===/;
69             chomp; s/^\s+//;
70             push @l, $_;
71         }
72         if ($tbc) {
73             print O "### ", (join ' ', @l), "\n";
74         }
75         else {
76             #print "$ARGV[0]:$cln: skip level 4 header conversion\n" if $cln;
77             print O (join "\n", @l), "\n";
78         }
79         @emptylines = ( "\n" );
80         next;
81     }
82
83     # Markdown doc specifies that list item may have paragraphs if those
84     # are indented by 4 spaces (or a tab) from current list item marker
85     # indentation (paragraph meaning there is empty line in between).
86     # If there is empty line and next line is not indented 4 chars then
87     # that should end the above list. This doesn't happen in all markdown
88     # implementations.
89     # In our NEWS case this problem exists in release 0.6 documentation.
90     # It can be avoided by removing 2 leading spaces in lines that are not
91     # list items and requiring all that indents are 0, 2, and 4+ (to make
92     # regexp below work).
93     # Nested lists are supported but one needs to be more careful with
94     # markup there (as the hack below works only on first level).
95
96     s/^[ ][ ]// unless /^[ ][ ](?:[\s*+-]|\d+\.)\s/;
97
98     print O $_;
99 }
100 print "\ndone.\n";
101 close O;