#!perl
my
(
$source_fn
,
$dest_fn
);
my
$format
=
'md'
;
GetOptions(
"i|input=s"
=> \
$source_fn
,
"o|output=s"
=> \
$dest_fn
,
"f|format=s"
=> \
$format
)
or
die
"Error in arguments. Usage:\nreadme_md.pl -i input -o output [-f format]\nFormat = md (default) or text."
;
die
"Need an input file"
unless
$source_fn
;
die
"Need an output file"
unless
$dest_fn
;
my
$parser
;
if
(
$format
eq
'md'
) {
$parser
= Pod::Markdown->new;
}
elsif
(
$format
eq
'text'
) {
$parser
= Pod::Text->new(
sentence
=> 1,
width
=> 78);
}
else
{
die
"Invalid format $format (I understand 'md' and 'text')"
}
my
$parsed
=
''
;
$parser
->output_string(\
$parsed
);
my
$pod
= file(
$source_fn
)->slurp;
$parser
->parse_string_document(
$pod
);
open
my
$fh
,
'<'
, \
$parsed
;
my
$saw_name
= 0;
my
$tweak_name
= (
$format
eq
'md'
);
my
$force_conventions
= (
$format
eq
'md'
);
while
(
my
$line
= <
$fh
>) {
if
(
$tweak_name
&& !
$saw_name
&&
$line
=~ /NAME/) {
$saw_name
= 1;
next
;
}
elsif
(
$tweak_name
&&
$saw_name
&&
$line
=~ m{\H\h*$/}) {
$output
.= (
$format
eq
'md'
?
'# '
:
''
) .
"$line\n"
;
$saw_name
= 0;
next
;
}
elsif
(
$tweak_name
&&
$saw_name
) {
next
;
}
next
if
$line
=~ /SYNOPSIS/;
$output
.=
$line
if
$line
=~ /SUPPORT/;
next
if
(
$line
=~ /VARIABLES/)..(
$line
=~ /SUPPORT/);
$output
.=
$line
;
}
file(
$dest_fn
)->spew(
$output
);