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