| 1 |
#!/usr/bin/env perl |
| 2 |
use strict; |
| 3 |
use subs 'quit'; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
my $file = shift || quit "no file specified"; |
| 7 |
-f $file || quit "$file does not exist or is not a file"; |
| 8 |
eval 'use Pod::Markdown; 1' || quit "no Pod::Markdown"; |
| 9 |
|
| 10 |
# Parse the documentation |
| 11 |
my $parser = Local::Pod::Markdown->new (markdown_fragment_format => sub { |
| 12 |
# remove non-word characters, along with non-hypens and non-spaces |
| 13 |
s/[^\w\-\s]//gi; |
| 14 |
# replace spaces with hyphens |
| 15 |
s/\s/-/g; |
| 16 |
# return the lowercase version of the string |
| 17 |
lc; |
| 18 |
}); |
| 19 |
open my $outfh, ">", "README.md" || die "can't open README.md for writing: $!"; |
| 20 |
$parser->output_fh ($outfh); |
| 21 |
$parser->parse_file ($file); |
| 22 |
|
| 23 |
print "README.md generated.", $/; |
| 24 |
|
| 25 |
sub quit |
| 26 |
{ |
| 27 |
print STDERR "skipping $0: ", shift, $/; |
| 28 |
exit 0 # we didn't fail |
| 29 |
} |
| 30 |
|
| 31 |
# Apply some fixes to Pod::Markdown |
| 32 |
package Local::Pod::Markdown; |
| 33 |
use strict; |
| 34 |
use warnings; |
| 35 |
BEGIN { |
| 36 |
# Too lazy to touch @ISA |
| 37 |
eval 'use parent "Pod::Markdown"'; |
| 38 |
} |
| 39 |
|
| 40 |
# Highlight Verbatim blocks. |
| 41 |
sub end_Verbatim |
| 42 |
{ |
| 43 |
my ($self) = @_; |
| 44 |
my $text = $self->_pop_stack_text; |
| 45 |
# Find the smallest indentation. (Pod::Markdown::_indent_verbatim) |
| 46 |
my $indent = ' ' x 4; |
| 47 |
foreach my $line (split /\n/, $text) |
| 48 |
{ |
| 49 |
next unless $line =~ /^(\s+)/; |
| 50 |
$indent = $1 if length ($1) < length ($indent); |
| 51 |
} |
| 52 |
# Remove it. |
| 53 |
$text =~ s/^$indent//mg; |
| 54 |
$self->_private->{no_escape} = 0; |
| 55 |
# Add the syntax highlighting block. |
| 56 |
$self->_save_block (join '', '```perl', $/, $text, $/, '```'); |
| 57 |
} |
| 58 |
|
| 59 |
# Normalize heading names, and fix normalization errors |
| 60 |
sub _end_head |
| 61 |
{ |
| 62 |
my ($self, $num) = @_; |
| 63 |
my $h = '#' x $num; |
| 64 |
my $text = $self->_pop_stack_text; |
| 65 |
$self->_private->{search_header} = |
| 66 |
$text =~ /NAME/ ? 'Title' |
| 67 |
: $text =~ /AUTHOR/ ? 'Author' |
| 68 |
: undef; |
| 69 |
# Normalize the heading name. |
| 70 |
$text = ucfirst lc $text if lc $text ne $text and $text ne 'AUTOLOAD'; |
| 71 |
# Fix lowercase names that shouldn't be lowercase. |
| 72 |
$text =~ s/([$@%][^\s]+)/uc $1/e; |
| 73 |
$self->_save_block (join ' ', $h, $text); |
| 74 |
} |
| 75 |
|
| 76 |
1; |