use
5.006_001;
our
$VERSION
=
'2.16'
;
our
$AUTHORITY
=
'cpan:NIGELM'
;
sub
default_values {
(
shift
->SUPER::default_values(),
lm
=> 0,
rm
=> 70,
);
}
sub
configure {
my
(
$self
,
$hash
) =
@_
;
my
$lm
=
$self
->{lm};
my
$rm
=
$self
->{rm};
$lm
=
delete
$hash
->{lm}
if
exists
$hash
->{lm};
$lm
=
delete
$hash
->{leftmargin}
if
exists
$hash
->{leftmargin};
$rm
=
delete
$hash
->{rm}
if
exists
$hash
->{rm};
$rm
=
delete
$hash
->{rightmargin}
if
exists
$hash
->{rightmargin};
my
$width
=
$rm
-
$lm
;
if
(
$width
< 1 ) {
warn
"Bad margins, ignored"
if
$^W;
return
;
}
if
(
$width
< 20 ) {
warn
"Page probably too narrow"
if
$^W;
}
for
(
keys
%$hash
) {
warn
"Unknown configure option '$_'"
if
$^W;
}
$self
->{lm} =
$lm
;
$self
->{rm} =
$rm
;
$self
;
}
sub
begin {
my
$self
=
shift
;
$self
->SUPER::begin();
$self
->{maxpos} = 0;
$self
->{curpos} = 0;
}
sub
end {
shift
->collect(
"\n"
);
}
sub
header_start {
my
(
$self
,
$level
) =
@_
;
$self
->vspace(1);
$self
->out(
'#'
x
$level
.
' '
);
1;
}
sub
header_end {
my
(
$self
,
$level
) =
@_
;
$self
->out(
' '
.
'#'
x
$level
);
$self
->vspace(1);
}
sub
bullet {
my
$self
=
shift
;
$self
->SUPER::bullet(
$_
[0] .
' '
);
}
sub
hr_start {
my
$self
=
shift
;
$self
->vspace(1);
$self
->out(
'- - -'
);
$self
->vspace(1);
}
sub
img_start {
my
(
$self
,
$node
) =
@_
;
my
$alt
=
$node
->attr(
'alt'
) ||
''
;
my
$src
=
$node
->attr(
'src'
);
$self
->out(
""
);
}
sub
a_start {
my
(
$self
,
$node
) =
@_
;
if
(
$node
->attr(
'name'
) ) {
1;
}
elsif
(
$node
->attr(
'href'
) =~ /^
1;
}
else
{
$self
->out(
"["
);
1;
}
}
sub
a_end {
my
(
$self
,
$node
) =
@_
;
if
(
$node
->attr(
'name'
) ) {
return
;
}
elsif
(
my
$href
=
$node
->attr(
'href'
) ) {
if
(
$href
=~ /^
return
;
}
$self
->out(
"]($href)"
);
}
}
sub
b_start {
shift
->out(
"**"
) }
sub
b_end {
shift
->out(
"**"
) }
sub
i_start {
shift
->out(
"*"
) }
sub
i_end {
shift
->out(
"*"
) }
sub
tt_start {
my
$self
=
shift
;
if
(
$self
->{pre} ) {
return
1;
}
else
{
$self
->out(
"`"
);
}
}
sub
tt_end {
my
$self
=
shift
;
if
(
$self
->{pre} ) {
return
;
}
else
{
$self
->out(
"`"
);
}
}
sub
blockquote_start {
my
$self
=
shift
;
$self
->{blockquote}++;
$self
->vspace(1);
$self
->adjust_rm(-4);
1;
}
sub
blockquote_end {
my
$self
=
shift
;
$self
->{blockquote}--;
$self
->vspace(1);
$self
->adjust_rm(+4);
}
sub
blockquote_out {
my
(
$self
,
$text
) =
@_
;
$self
->nl;
$self
->goto_lm;
my
$line
=
"> "
;
$self
->{curpos} += 2;
foreach
my
$word
(
split
/\s/,
$text
) {
$line
.=
"$word "
;
if
( (
$self
->{curpos} +
length
(
$line
) ) >
$self
->{rm} ) {
$self
->collect(
$line
);
$self
->nl;
$self
->goto_lm;
$line
=
"> "
;
$self
->{curpos} += 2;
}
}
$self
->collect(
$line
);
$self
->nl;
}
sub
pre_out {
my
$self
=
shift
;
if
(
defined
$self
->{vspace} ) {
if
(
$self
->{out} ) {
$self
->nl()
while
$self
->{vspace}-- >= 0;
$self
->{vspace} =
undef
;
}
}
my
$indent
=
' '
x
$self
->{lm};
$indent
.=
' '
x 4;
my
$pre
=
shift
;
$pre
=~ s/^/
$indent
/mg;
$self
->collect(
$pre
);
$self
->{out}++;
}
sub
out {
my
$self
=
shift
;
my
$text
=
shift
;
if
(
$text
=~ /^\s*$/ ) {
$self
->{hspace} = 1;
return
;
}
if
(
defined
$self
->{vspace} ) {
if
(
$self
->{out} ) {
$self
->nl
while
$self
->{vspace}-- >= 0;
}
$self
->goto_lm;
$self
->{vspace} =
undef
;
$self
->{hspace} = 0;
}
if
(
$self
->{hspace} ) {
if
(
$self
->{curpos} +
length
(
$text
) >
$self
->{rm} ) {
$self
->nl;
$self
->goto_lm;
}
else
{
$self
->collect(
' '
);
++
$self
->{curpos};
}
$self
->{hspace} = 0;
}
$self
->collect(
$text
);
my
$pos
=
$self
->{curpos} +=
length
$text
;
$self
->{maxpos} =
$pos
if
$self
->{maxpos} <
$pos
;
$self
->{
'out'
}++;
}
sub
goto_lm {
my
$self
=
shift
;
my
$pos
=
$self
->{curpos};
my
$lm
=
$self
->{lm};
if
(
$pos
<
$lm
) {
$self
->{curpos} =
$lm
;
$self
->collect(
" "
x (
$lm
-
$pos
) );
}
}
sub
nl {
my
$self
=
shift
;
$self
->{
'out'
}++;
$self
->{curpos} = 0;
$self
->collect(
"\n"
);
}
sub
adjust_lm {
my
$self
=
shift
;
$self
->{lm} +=
$_
[0];
$self
->goto_lm;
}
sub
adjust_rm {
shift
->{rm} +=
$_
[0];
}
1;