|
| 1 | +#!/usr/bin/perl -w |
| 2 | +=pod |
| 3 | + /*PGR-GNU***************************************************************** |
| 4 | + File: notes2news.pl |
| 5 | + Copyright (c) 2017 pgRouting developers |
| 6 | + Mail: project@pgrouting.org |
| 7 | + ------ |
| 8 | + This program is free software; you can redistribute it and/or modify |
| 9 | + it under the terms of the GNU General Public License as published by |
| 10 | + the Free Software Foundation; either version 2 of the License, or |
| 11 | + (at your option) any later version. |
| 12 | + This program is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + GNU General Public License for more details. |
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with this program; if not, write to the Free Software |
| 18 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | + ********************************************************************PGR-GNU*/ |
| 20 | +=cut |
| 21 | +use strict; |
| 22 | +use warnings; |
| 23 | +use File::Find; |
| 24 | +use Data::Dumper; |
| 25 | + |
| 26 | +sub Usage { |
| 27 | + die "Usage: notes2news.pl (from the root of the repository or pre-commit hook)\n"; |
| 28 | +} |
| 29 | + |
| 30 | +my $DEBUG = ''; |
| 31 | +my $in_file = "doc/general/release_notes.rst"; |
| 32 | +my $out_file = "NEWS.md"; |
| 33 | + |
| 34 | +my $ofh; |
| 35 | +open($ofh, ">$out_file") || die "ERROR: failed to open '$out_file' for write! : $!\n"; |
| 36 | + |
| 37 | +my $ifh; |
| 38 | +open($ifh, "$in_file") || die "ERROR: failed to open '$in_file' for read! : $!\n"; |
| 39 | + |
| 40 | +my %conversions = get_substitutions(); |
| 41 | +my $check = join '|', keys %conversions; |
| 42 | +print Dumper(\%conversions) if $DEBUG; |
| 43 | + |
| 44 | + |
| 45 | +my $skipping = 1; |
| 46 | +my $file = ''; |
| 47 | +my $start = ''; |
| 48 | +my $end = ''; |
| 49 | +while (my $line = <$ifh>) { |
| 50 | + next if $skipping and $line !~ /^pgvroom/; |
| 51 | + $skipping = 0; |
| 52 | + |
| 53 | + next if $line =~ /contents|:local:|:depth:|\*\*\*\*\*\*\*|\=\=\=\=\=\=\=|\-\-\-\-\-\-\-|\+\+\+\+\+\+\+\+/; |
| 54 | + |
| 55 | + $line =~ s/[\|]+//g; |
| 56 | + $line =~ s/($check)/$conversions{$1}/go; |
| 57 | + |
| 58 | + # Handling the headers |
| 59 | + if ($line =~ m/^pgvroom [0-9]$/i) { |
| 60 | + print $ofh "# $line"; |
| 61 | + next; |
| 62 | + }; |
| 63 | + if ($line =~ m/^pgvroom [0-9].[0-9]$/i) { |
| 64 | + print $ofh "## $line"; |
| 65 | + next; |
| 66 | + }; |
| 67 | + if ($line =~ m/^pgvroom [0-9].[0-9].[0-9] Release Notes$/i) { |
| 68 | + print $ofh "### $line"; |
| 69 | + next; |
| 70 | + }; |
| 71 | + |
| 72 | + # get include filename |
| 73 | + if ($line =~ /include/) { |
| 74 | + $line =~ s/^.*include\:\: (.*)/$1/; |
| 75 | + chomp $line; |
| 76 | + $line =~ s/^\s+//; |
| 77 | + $file = $line; |
| 78 | + my @wanted_files; |
| 79 | + find( |
| 80 | + sub{ |
| 81 | + -f $_ && $_ =~ /$file/ |
| 82 | + && push @wanted_files,$File::Find::name |
| 83 | + }, "doc" |
| 84 | + ); |
| 85 | + foreach(@wanted_files){ |
| 86 | + print "wanted: $_\n" if $DEBUG; |
| 87 | + } |
| 88 | + $file = $wanted_files[0]; |
| 89 | + print "rewanted: $file\n" if $DEBUG; |
| 90 | + next; |
| 91 | + }; |
| 92 | + |
| 93 | + if ($line =~ /start\-after/) { |
| 94 | + $line =~ s/start\-after\:\ (.*)/$1/; |
| 95 | + $line =~ tr/://d; |
| 96 | + chomp $line; |
| 97 | + $line =~ s/^\s+//; |
| 98 | + $start = $line; |
| 99 | + next; |
| 100 | + }; |
| 101 | + |
| 102 | + if ($line =~ /end\-before/) { |
| 103 | + $line =~ s/end\-before\:\ (.*)/$1/; |
| 104 | + $line =~ tr/://d; |
| 105 | + chomp $line; |
| 106 | + $line =~ s/^\s+//; |
| 107 | + $end = $line; |
| 108 | + print "---> $file from $start to $end \n" if $DEBUG; |
| 109 | + find_stuff($file, $start, $end); |
| 110 | + next; |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + # convert urls to markdown |
| 116 | + $line =~ s/`([^<]+?)\s*<([^>]+)>`_*/\[$1\]($2)/g; |
| 117 | + |
| 118 | + $line =~ s/`(Git closed)/\[$1/g; |
| 119 | + $line =~ s/<([^>]+)>`_*/\]($1)/g; |
| 120 | + |
| 121 | + # convert rubric to bold |
| 122 | + $line =~ s/^\.\. rubric::\s*(.+)$/**$1**/; |
| 123 | + |
| 124 | + next if $line =~ /^\.\. _changelog/; |
| 125 | + |
| 126 | + print $ofh $line; |
| 127 | +} |
| 128 | + |
| 129 | + |
| 130 | +close($ifh); |
| 131 | +close($ofh); |
| 132 | + |
| 133 | +sub find_stuff { |
| 134 | + my ($file, $mstart, $mend) = @_; |
| 135 | + print "find_stuff $file from $mstart to $mend \n" if $DEBUG; |
| 136 | + my $fh; |
| 137 | + open($fh, "$file") || die "ERROR: failed to open '$file' for read! : $!\n"; |
| 138 | + |
| 139 | + my $skipping = 1; |
| 140 | + while (my $line = <$fh>) { |
| 141 | + next if $skipping and $line !~ /$mstart/; |
| 142 | + $skipping = 0; |
| 143 | + next if $line =~ /$mstart/; |
| 144 | + $line =~ s/[\|]+//g; |
| 145 | + $line =~ s/($check)/$conversions{$1}/go; |
| 146 | + print $ofh " $line" if $line !~ /$mend/; |
| 147 | + last if $line =~ /$mend/; |
| 148 | + } |
| 149 | + close($fh); |
| 150 | +} |
| 151 | + |
| 152 | +sub get_substitutions { |
| 153 | + my $file = "doc/conf.py.in"; |
| 154 | + my $mstart = "rst_epilog"; |
| 155 | + my $mend = "epilog_end"; |
| 156 | + print "get_substitutions $file from $mstart to $mend \n" if $DEBUG; |
| 157 | + my $fh; |
| 158 | + open($fh, "$file") || die "ERROR: failed to open '$file' for read! : $!\n"; |
| 159 | + my %data; |
| 160 | + |
| 161 | + my $skipping = 1; |
| 162 | + while (my $line = <$fh>) { |
| 163 | + next if $skipping and $line !~ /$mstart/; |
| 164 | + last if $line =~ /\|br\|/; |
| 165 | + $skipping = 0; |
| 166 | + next if $line =~ /$mstart/; |
| 167 | + last if $line =~ /$mend/; |
| 168 | + my ($key) = substr($line, 4, index(substr($line, 4), "|")); |
| 169 | + my ($value) = substr($line, index($line,"`")); |
| 170 | + $value =~ s/\R//g; |
| 171 | + $data{$key} = $value; |
| 172 | + print "$key $data{$key} \n" if $DEBUG; |
| 173 | + } |
| 174 | + close($fh); |
| 175 | + return %data; |
| 176 | +} |
0 commit comments