| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use 5.022; | ||
| 4 | use feature qw/signatures postderef/; | ||
| 5 | no warnings qw/experimental uninitialized/; | ||
| 6 | use utf8::all; | ||
| 7 | use Try::Tiny; | ||
| 8 | # use Data::Printer; | ||
| 9 | |||
| 10 | =pod | ||
| 11 | |||
| 12 | =head1 buildpod.pl | ||
| 13 | |||
| 14 | This is a utility script for the Vote::Count Distribution. | ||
| 15 | Method Documentation is being written conventionally as inline POD, | ||
| 16 | Documentation files are being written in MarkDown. This utility | ||
| 17 | will convert markdown files to pod files, and insert pod into modules | ||
| 18 | that have a markdown file. The insertion will be between lines that | ||
| 19 | have #buildpod comments. | ||
| 20 | |||
| 21 | As an added convenience buildpod will read the version from dist.ini and | ||
| 22 | replace the version strings in modules. | ||
| 23 | |||
| 24 | Someday I may make a Dist::Zilla plugin out of this. | ||
| 25 | |||
| 26 | =head1 SYNAPSIS | ||
| 27 | |||
| 28 | ./buildpod.pl | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | |||
| 33 | # use Carp::Always; | ||
| 34 | use Carp; | ||
| 35 | use Path::Tiny; | ||
| 36 | use Markdown::Pod;; | ||
| 37 | # use Time::Piece; | ||
| 38 | # use Time::Moment; | ||
| 39 | # use Search::Tools::UTF8; | ||
| 40 | # use Unicode::Normalize; | ||
| 41 | # use List::Util qw(max); | ||
| 42 | # use Unicode::Collate::Locale; | ||
| 43 | # use feature 'unicode_strings'; | ||
| 44 | # use Encode qw (from_to decode_utf8 encode_utf8 decode encode); | ||
| 45 | # use Cpanel::JSON::XS; # qw( encode_json decode_json ); | ||
| 46 | use Data::Printer; | ||
| 47 | # use Data::Dumper; | ||
| 48 | # use charnames ':full'; | ||
| 49 | # use Unicode::UCD 'charinfo'; | ||
| 50 | # use Test::More; | ||
| 51 | # use Getopt::Long::Descriptive; | ||
| 52 | |||
| 53 | =pod | ||
| 54 | |||
| 55 | =head1 buildpod.pl | ||
| 56 | |||
| 57 | =head1 SYNOPSIS | ||
| 58 | |||
| 59 | =head1 VERSION 2019.0902 | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | our $VERSION='2019.0902'; | ||
| 64 | |||
| 65 | my $m2p = Markdown::Pod->new; | ||
| 66 | |||
| 67 | sub fix_version ( $text, $version ) { | ||
| 68 | $text =~ s/our \$VERSION(| )=.*;/our \$VERSION='$version';/g; | ||
| 69 | $text =~ s/=head1 VERSION.*\n/=head1 VERSION $version\n/; | ||
| 70 | return $text; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub add_pod ( $text, $markdown ) { | ||
| 74 | my $pod = $m2p->markdown_to_pod( | ||
| 75 | markdown => $markdown, | ||
| 76 | ); | ||
| 77 | my $markerstr = '#buildpod'; | ||
| 78 | my $num_markers = () = $text =~ /$markerstr/g; | ||
| 79 | say "Counted Markers: $num_markers"; | ||
| 80 | return $text unless $num_markers; | ||
| 81 | if ( $num_markers >2 ) { | ||
| 82 | die "There are too many $markerstr markers in the current file\\n" | ||
| 83 | } | ||
| 84 | my @beforepod = (); | ||
| 85 | my @afterpod = (); | ||
| 86 | my $aftermarker = 0; | ||
| 87 | for my $l ( split /\n/, $text ) { | ||
| 88 | if ($aftermarker == $num_markers ) { push @afterpod, $l } | ||
| 89 | elsif ($aftermarker == 1 ) { | ||
| 90 | $aftermarker++ if $l =~ /$markerstr/; | ||
| 91 | } else { | ||
| 92 | if ( $l =~ /$markerstr/ ) { | ||
| 93 | $aftermarker++ if $l =~ /$markerstr/; | ||
| 94 | } else { | ||
| 95 | push @beforepod, $l; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | return join( "\n", | ||
| 100 | ( @beforepod, $markerstr, "\n=pod\n$pod\n=cut\n", $markerstr, @afterpod ) | ||
| 101 | ) | ||
| 102 | } | ||
| 103 | |||
| 104 | my $footer = <<'FOOTER'; | ||
| 105 | |||
| 106 | #FOOTER | ||
| 107 | |||
| 108 | =pod | ||
| 109 | |||
| 110 | BUG TRACKER | ||
| 111 | |||
| 112 | L<https://github.com/brainbuz/Vote-Count/issues> | ||
| 113 | |||
| 114 | AUTHOR | ||
| 115 | |||
| 116 | John Karr (BRAINBUZ) brainbuz@cpan.org | ||
| 117 | |||
| 118 | CONTRIBUTORS | ||
| 119 | |||
| 120 | Copyright 2019-2020 by John Karr (BRAINBUZ) brainbuz@cpan.org. | ||
| 121 | |||
| 122 | LICENSE | ||
| 123 | |||
| 124 | This module is released under the GNU Public License Version 3. See license file for details. For more information on this license visit L<http://fsf.org>. | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | FOOTER | ||
| 129 | |||
| 130 | my $dist = path( './dist.ini')->slurp; | ||
| 131 | $dist =~ /version\s? =\s?(\d+\.\d+)/; | ||
| 132 | my $version = $1; | ||
| 133 | |||
| 134 | my @mdfiles = path("./md")->children( qr/md$/ ); | ||
| 135 | my @pmfiles1 = path("./lib/Vote/Count")->children( qr/pm$|pod$/); | ||
| 136 | my @pmfiles2 = path("./lib/Vote/Count/Method")->children( qr/pm$/); | ||
| 137 | my $countpm = path( "./lib/Vote/Count.pm"); | ||
| 138 | my @pmfiles = ( @pmfiles1, @pmfiles2, $countpm); | ||
| 139 | my %pmkeys = (); | ||
| 140 | for my $pm (@pmfiles ) { | ||
| 141 | $pm =~ /(.*)\.(pm$|pod$)/; # extract the part of the string before .pm | ||
| 142 | my @bits = split /\//, $1; # split extracted on /, llast bit is basename | ||
| 143 | $pmkeys{ $bits[-1] } = $pm ; # put the path object in the hash keyed on the basename. | ||
| 144 | } | ||
| 145 | FORMD: | ||
| 146 | for my $md ( @mdfiles ) { | ||
| 147 | my $name = $md->basename; | ||
| 148 | $name =~ s/\.md$//; | ||
| 149 | say "base name $name"; | ||
| 150 | if ( $pmkeys{ $name }) { | ||
| 151 | my $pm = delete $pmkeys{ $name }; # remove from keys, so we dont repeat it later. | ||
| 152 | my $mdtext = path($md)->slurp(); | ||
| 153 | my $pmtext = path($pm)->slurp(); | ||
| 154 | $pmtext = add_pod( $pmtext, $mdtext ); | ||
| 155 | $pmtext = fix_version( $pmtext, $version); | ||
| 156 | $pm->spew( $pmtext ); | ||
| 157 | say "updated $pm added pod from $md"; | ||
| 158 | } else { | ||
| 159 | next FORMD if $md =~ /README/ ; | ||
| 160 | my $mdtext = path($md)->slurp(); | ||
| 161 | $mdtext =~ s/(\# ABSTRACT.*)\n//; | ||
| 162 | my $abstract = $1; | ||
| 163 | my $pod = $m2p->markdown_to_pod( markdown => $mdtext ); | ||
| 164 | my $versionline = "=head1 VERSION $version"; | ||
| 165 | warn $versionline; | ||
| 166 | # $pod = "$abstract\n$versionline\n$pod\n$footer"; | ||
| 167 | $pod = "$abstract\n\n=pod\n\n$versionline\n$pod\n$footer\n=cut\n"; | ||
| 168 | path( "./lib/Vote/$name.pod")->spew($pod); | ||
| 169 | } | ||
| 170 | for my $pm ( values %pmkeys ) { | ||
| 171 | warn "fixing $pm" ; | ||
| 172 | my $pmtext = path($pm)->slurp; | ||
| 173 | unless ($pmtext =~ /\#FOOTER/) { $pmtext .= $footer ; } | ||
| 174 | path($pm)->spew( fix_version( $pmtext, $version) ); | ||
| 175 | say "updated version in $pm"; | ||
| 176 | } | ||
| 177 | } |