#!/usr/bin/env perl
our
$VERSION
= 0.027_000;
sub
pod2cpanhtml_preprocess {
{
my
string_arrayref
$RETURN_TYPE
};
(
my
string_arrayref
$command_line_arguments
) =
@_
;
my
string
$input_file
=
$command_line_arguments
->[0];
if
( not( -e
$input_file
) ) {
croak
'ERROR: File '
.
$input_file
.
' does not exist, croaking'
;
}
if
( not( -r
$input_file
) ) {
croak
'ERROR: File '
.
$input_file
.
' is not readable, croaking'
;
}
if
( not( -f
$input_file
) ) {
croak
'ERROR: File '
.
$input_file
.
' is not a regular file, croaking'
;
}
if
( not( -T
$input_file
) ) {
croak
'ERROR: File '
.
$input_file
.
' is (probably) not text, croaking'
;
}
my
integer
$open_success
=
open
my
filehandleref
$INPUT_FILEHANDLE
,
'<'
,
$input_file
;
if
( not
$open_success
) {
croak
'ERROR: Failed to open file '
.
$input_file
.
' for reading, croaking'
;
}
my
string_arrayref
$file_lines
= [];
my
string
$file_line_previous
=
undef
;
my
string
$file_line_next
=
undef
;
my
integer
$file_line_number
= 0;
while
(
my
string
$file_line
= <
$INPUT_FILEHANDLE
> ) {
$file_line_number
++;
if
(
$file_line
=~ s/X<br>/<br>/gxms) {
$file_line_next
= <
$INPUT_FILEHANDLE
>;
$file_line_number
++;
if
(
$file_line_next
ne
"\n"
) { croak
'ERROR: X<br> tag followed by non-blank line '
.
q{'}
.
$file_line_next
.
q{'}
.
' on input file line '
.
$file_line_number
.
', croaking'
; }
$file_line_next
=
undef
;
}
if
(
$file_line
=~ s/=
for
\ rperl\ X<noncode>/<noncode>/gxms) {
$file_line_next
= <
$INPUT_FILEHANDLE
>;
$file_line_number
++;
if
(
$file_line_next
ne
"\n"
) { croak
'ERROR: X<noncode> tag followed by non-blank line '
.
q{'}
.
$file_line_next
.
q{'}
.
' on input file line '
.
$file_line_number
.
', croaking'
; }
}
if
(
$file_line
=~ s/=
for
\ rperl\ X<\/noncode>/<\/noncode>/gxms) {
if
(
$file_line_previous
ne
"\n"
) { croak
'ERROR: X</noncode> tag preceeded by non-blank line '
.
q{'}
.
$file_line_previous
.
q{'}
.
' on input file line '
.
$file_line_number
.
', croaking'
; }
}
push
@{
$file_lines
},
$file_line
;
if
(
defined
$file_line_next
) {
push
@{
$file_lines
},
$file_line_next
;
$file_line_next
=
undef
;
}
$file_line_previous
=
$file_line
;
}
if
( not
close
$INPUT_FILEHANDLE
) {
croak
'ERROR: Failed to close file '
.
$input_file
.
' after reading, croaking'
;
}
return
$file_lines
;
}
sub
pod2cpanhtml_process {
{
my
string_arrayref
$RETURN_TYPE
};
(
my
string_arrayref
$file_lines
) =
@_
;
my
filehandleref
$TEMP_FILE_HANDLE
;
my
string
$temp_file_name
;
(
$TEMP_FILE_HANDLE
,
$temp_file_name
) = tempfile(
'tempfileXXXX'
,
SUFFIX
=>
'.tmp'
,
UNLINK
=> 1,
TMPDIR
=> 1 );
my
string
$file_lines_joined
=
join
q{}
, @{
$file_lines
};
print
{
$TEMP_FILE_HANDLE
}
$file_lines_joined
or croak(
'Attempting to save new file '
.
$temp_file_name
.
', cannot write to file, croaking:'
.
$OS_ERROR
);
close
$TEMP_FILE_HANDLE
or croak(
'Attempting to save new file '
.
$temp_file_name
.
', cannot close file, croaking:'
.
$OS_ERROR
);
my
App::Pod2CpanHtml
$parser
= App::Pod2CpanHtml->new();
my
string
$file_lines_modified
;
$parser
->output_string( \
$file_lines_modified
);
$parser
->parse_file(
$temp_file_name
);
$file_lines
= [ (
split
/\n/xms,
$file_lines_modified
) ];
return
$file_lines
;
}
sub
pod2cpanhtml_postprocess {
{
my
string_arrayref
$RETURN_TYPE
};
(
my
string_arrayref
$file_lines
) =
@_
;
my
string_arrayref
$file_lines_modified
= [];
my
boolean
$inside_edition
= 0;
my
boolean
$inside_toc
= 0;
my
boolean
$need_check_close_ul
= 0;
my
string
$file_line_saved
=
undef
;
my
boolean
$inside_noncode
= 0;
push
@{
$file_lines_modified
},
'<!DOCTYPE html>'
;
foreach
my
string
$file_line
( @{
$file_lines
} ) {
if
(
$file_line
eq
q{</head>}
) {
push
@{
$file_lines_modified
},
q{<style>.wait_for_javascript { display: none; }
</style>},
q{}
; }
if
(
$file_line
eq
q{<a name='___top' class='dummyTopAnchor' ></a>}
) {
push
@{
$file_lines_modified
},
q{<div id="full_table_of_contents" class="hide_full_table_of_contents wait_for_javascript">}
,
q{}
; }
if
(
$file_line
eq
q{<div id="scoped-content"><style type="text/css" scoped>}
) {
push
@{
$file_lines_modified
},
q{}
,
q{</div> <!-- id="full_table_of_contents" -->}
,
q{}
; }
if
(
$file_line
eq
q{</body></html>}
) {
push
@{
$file_lines_modified
},
q{}
,
q{<script> document.getElementById('full_table_of_contents').className = 'hide_full_table_of_contents'; </script>}
,
q{}
; }
if
(
$file_line
eq
'name="EDITION"'
) {
$inside_edition
= 1;
push
@{
$file_lines_modified
},
$file_line
;
next
;
}
elsif
(
$inside_edition
) {
if
(
$file_line
eq
'<p><br></p>'
) {
$file_line
=
'<p><br></p>'
;
push
@{
$file_lines_modified
}, (time2str(
'On %A, %B %o, %Y at %l:%M%P %Z'
,
time
) .
'<br>'
.
"\n"
);
$inside_edition
= 0;
}
push
@{
$file_lines_modified
},
$file_line
;
next
;
}
if
(
$file_line
eq
'<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" >'
) {
push
@{
$file_lines_modified
},
q{<meta name="viewport" content="width=device-width, initial-scale=1">}
;
push
@{
$file_lines_modified
},
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
;
next
;
}
push
@{
$file_lines_modified
},
'<link href="stylesheets/metacpan_rperl.css" rel="stylesheet" type="text/css">'
;
push
@{
$file_lines_modified
},
'<script src="javascripts/metacpan_rperl.js" type="text/javascript"></script>'
;
next
;
}
if
(
$file_line
=~ m/<body class=
'pod'
>/) {
push
@{
$file_lines_modified
},
'<body>'
;
push
@{
$file_lines_modified
},
'<div class="pod content anchors">'
;
next
;
}
elsif
(
$file_line
=~ m/<\/body>/) {
push
@{
$file_lines_modified
},
'</div> <!-- END class="pod content anchors": SyntaxHighlighter applied to code within this div -->'
;
push
@{
$file_lines_modified
},
$file_line
;
next
;
}
if
(
$file_line
=~ m/^\s+generated\sby\sPod::Simple::HTML/) {
push
@{
$file_lines_modified
},
' re-generated by pod2rperlhtml.pl v'
. number_to_string(
$VERSION
) .
','
;
}
if
(
$file_line
eq
q{<div class='indexgroup'>}
) {
$inside_toc
= 1;
next
;
}
elsif
(
$inside_toc
) {
if
(
$file_line
eq
q{<ul class='indexList indexList1'>}
) {
push
@{
$file_lines_modified
},
'<ul id="index">'
;
next
;
}
elsif
(
$file_line
eq
'</div>'
) {
$inside_toc
= 0;
next
;
}
$file_line
=~ s/<li\s+class\=\'.*\'><a\s+href/<li><a href/gxms;
$file_line
=~ s/<\/ul>/<\/ul><\/li>/gxms;
$file_line
=~ s/<ul.+>/<ul>/gxms;
Hide Show 17 lines of Pod
}
$file_line
=~ s/&\
if
(
$file_line
eq
'<p><noncode></p>'
) {
$inside_noncode
= 1;
next
;
}
elsif
(
$inside_noncode
) {
if
(
$file_line
eq
'<p></noncode></p>'
) {
$inside_noncode
= 0;
next
;
}
}
else
{
$file_line
=~ s/<pre>/<pre><code>/gxms;
$file_line
=~ s/<\/pre>/<\/code><\/pre>/gxms;
}
$file_line
=~ s/<title><u>(.*)<\/u><\/title>/<title>$1<\/title>/gxms;
$file_line
=~ s/<li><p>(.*)<\/p>/<li>$1/gxms;
push
@{
$file_lines_modified
},
$file_line
;
}
return
$file_lines_modified
;
}
my
string_arrayref
$file_lines
;
$file_lines
= pod2cpanhtml_preprocess( [
@ARGV
] );
$file_lines
= pod2cpanhtml_process(
$file_lines
);
$file_lines
= pod2cpanhtml_postprocess(
$file_lines
);
print
join
"\n"
, @{
$file_lines
};