Skip to content

Commit

Permalink
Merge pull request perl-pod#28 from gisle/handle_code
Browse files Browse the repository at this point in the history
Add handle_code as overrideable method on Pod::Simple::XHTML
  • Loading branch information
theory committed May 23, 2012
2 parents c3a30b3 + 678687d commit fd41e0d
Show file tree
Hide file tree
Showing 28 changed files with 737 additions and 667 deletions.
6 changes: 4 additions & 2 deletions lib/Pod/Simple/BlackBox.pm
Expand Up @@ -1480,10 +1480,12 @@ sub _traverse_treelet_bit { # for use only by the routine above
my $scratch;
$self->_handle_element_start(($scratch=$name), shift @_);

foreach my $x (@_) {
if(ref($x)) {
while (@_) {
my $x = shift;
if (ref($x)) {
&_traverse_treelet_bit($self, @$x);
} else {
$x .= shift while @_ && !ref($_[0]);
$self->_handle_text($x);
}
}
Expand Down
12 changes: 3 additions & 9 deletions lib/Pod/Simple/DumpAsXML.pm
Expand Up @@ -8,6 +8,7 @@ BEGIN {@ISA = ('Pod::Simple')}
use strict;

use Carp ();
use Text::Wrap qw(wrap);

BEGIN { *DEBUG = \&Pod::Simple::DEBUG unless defined &DEBUG }

Expand Down Expand Up @@ -49,15 +50,8 @@ sub _handle_text {
my $indent = ' ' x $_[0]{'indent'};
my $text = $_[1];
_xml_escape($text);
$text =~ # A not-totally-brilliant wrapping algorithm:
s/(
[^\n]{55} # Snare some characters from a line
[^\n\ ]{0,50} # and finish any current word
)
\x20{1,10}(?!\n) # capture some spaces not at line-end
/$1\n$indent/gx # => line-break here
;

local $Text::Wrap::huge = 'overflow';
$text = wrap('', $indent, $text);
print {$_[0]{'output_fh'}} $indent, $text, "\n";
}
return;
Expand Down
26 changes: 20 additions & 6 deletions lib/Pod/Simple/XHTML.pm
Expand Up @@ -300,11 +300,17 @@ something like:
my ($self, $text) = @_;
if ($self->{'in_foo'}) {
$self->{'scratch'} .= build_foo_html($text);
} else {
$self->{'scratch'} .= $text;
return;
}
$self->SUPER::handle_text($text);
}
=head2 handle_code
This method handles the body of text that is marked up to be code.
You might for instance override this to plug in a syntax highlighter.
The base implementation just escapes the text and wraps it in C<<< <code>...</code> >>>.
=head2 accept_targets_as_html
This method behaves like C<accept_targets_as_text>, but also marks the region
Expand All @@ -326,14 +332,21 @@ sub accept_targets_as_html {
}

sub handle_text {
if ($_[0]{'in_code'}) {
return $_[0]->handle_code( $_[1] );
}
# escape special characters in HTML (<, >, &, etc)
$_[0]{'scratch'} .= $_[0]->__in_literal_xhtml_region
? $_[1]
: $_[0]->encode_entities( $_[1] );
}

sub handle_code {
$_[0]{'scratch'} .= '<code>' . $_[0]->encode_entities( $_[1] ) . '</code>';
}

sub start_Para { $_[0]{'scratch'} = '<p>' }
sub start_Verbatim { $_[0]{'scratch'} = '<pre><code>' }
sub start_Verbatim { $_[0]{'scratch'} = '<pre>'; $_[0]{'in_code'} = 1; }

sub start_head1 { $_[0]{'in_head'} = 1 }
sub start_head2 { $_[0]{'in_head'} = 2 }
Expand Down Expand Up @@ -396,7 +409,8 @@ sub end_over_text {

sub end_Para { $_[0]{'scratch'} .= '</p>'; $_[0]->emit }
sub end_Verbatim {
$_[0]{'scratch'} .= '</code></pre>';
$_[0]{'scratch'} .= '</pre>';
delete $_[0]{'in_code'};
$_[0]->emit;
}

Expand Down Expand Up @@ -567,8 +581,8 @@ sub end_Document {
sub start_B { $_[0]{'scratch'} .= '<b>' }
sub end_B { $_[0]{'scratch'} .= '</b>' }

sub start_C { $_[0]{'scratch'} .= '<code>' }
sub end_C { $_[0]{'scratch'} .= '</code>' }
sub start_C { $_[0]{'in_code'} = 1; }
sub end_C { delete $_[0]{'in_code'}; }

sub start_F { $_[0]{'scratch'} .= '<i>' }
sub end_F { $_[0]{'scratch'} .= '</i>' }
Expand Down
34 changes: 20 additions & 14 deletions t/corpus/2202jp.xml
Expand Up @@ -9,12 +9,11 @@
DESCRIPTION
</head1>
<Para start_line="11">
This is a test Pod document in ISO-2202-JP. Its content
is some Japanese haiku by famous poets.
This is a test Pod document in ISO-2202-JP. Its content is some Japanese
haiku by famous poets.
</Para>
<head2 start_line="15">
MATSUO BASHO (&#26494;&#23614;&#33453;&#34121; 1644 - 1694)
:
MATSUO BASHO (&#26494;&#23614;&#33453;&#34121; 1644 - 1694) :
</head2>
<Para start_line="17">
&#21476;&#27744;&#12420;&#34521;&#12392;&#12403;&#36796;&#12416;&#27700;&#12398;&#38899;
Expand All @@ -26,7 +25,8 @@
As verbatim:
</Para>
<VerbatimFormatted start_line="23" xml:space="preserve">
&#21476;&#27744;&#12420;&#34521;&#12392;&#12403;&#36796;&#12416;&#27700;&#12398;&#38899;

&#21476;&#27744;&#12420;&#34521;&#12392;&#12403;&#36796;&#12416;&#27700;&#12398;&#38899;
</VerbatimFormatted>
<head2 start_line="27">
YOSA BUSON (&#19982;&#35613;&#34154;&#26449;1716 - 1783)
Expand All @@ -41,7 +41,8 @@
As verbatim:
</Para>
<VerbatimFormatted start_line="35" xml:space="preserve">
&#26041;&#20843;&#37324;&#38632;&#38642;&#12424;&#12379;&#12396;&#29281;&#20025;&#12363;&#12394;

&#26041;&#20843;&#37324;&#38632;&#38642;&#12424;&#12379;&#12396;&#29281;&#20025;&#12363;&#12394;
</VerbatimFormatted>
<head2 start_line="37">
MASAOKA SHIKI (&#27491;&#23713;&#23376;&#35215; 1867 - 1902)
Expand All @@ -56,7 +57,8 @@
As verbatim:
</Para>
<VerbatimFormatted start_line="45" xml:space="preserve">
&#12356;&#12385;&#12399;&#12388;&#12398;&#19968;&#36650;&#30333;&#12375;&#26149;&#12398;&#26286;

&#12356;&#12385;&#12399;&#12388;&#12398;&#19968;&#36650;&#30333;&#12375;&#26149;&#12398;&#26286;
</VerbatimFormatted>
<Para start_line="47">
&#20313;&#21629;&#12356;&#12367;&#12400;&#12367;&#12363;&#12354;&#12427;&#22812;&#30701;&#12375;
Expand All @@ -65,15 +67,15 @@
(yomei / ikubakuka aru / yo mijikashi)
</Para>
<VerbatimFormatted start_line="51" xml:space="preserve">
&#20313;&#21629;&#12356;&#12367;&#12400;&#12367;&#12363;&#12354;&#12427;&#22812;&#30701;&#12375;

&#20313;&#21629;&#12356;&#12367;&#12400;&#12367;&#12363;&#12354;&#12427;&#22812;&#30701;&#12375;
</VerbatimFormatted>
<head1 start_line="53">
AS A LIST
</head1>
<over-text indent="4" start_line="55">
<item-text start_line="57">
MATSUO BASHO (&#26494;&#23614;&#33453;&#34121; 1644 - 1694)
:
MATSUO BASHO (&#26494;&#23614;&#33453;&#34121; 1644 - 1694) :
</item-text>
<Para start_line="59">
&#21476;&#27744;&#12420;&#34521;&#12392;&#12403;&#36796;&#12416;&#27700;&#12398;&#38899;
Expand All @@ -85,7 +87,8 @@
As verbatim:
</Para>
<VerbatimFormatted start_line="65" xml:space="preserve">
&#21476;&#27744;&#12420;&#34521;&#12392;&#12403;&#36796;&#12416;&#27700;&#12398;&#38899;

&#21476;&#27744;&#12420;&#34521;&#12392;&#12403;&#36796;&#12416;&#27700;&#12398;&#38899;
</VerbatimFormatted>
<item-text start_line="69">
YOSA BUSON (&#19982;&#35613;&#34154;&#26449;1716 - 1783)
Expand All @@ -100,7 +103,8 @@
As verbatim:
</Para>
<VerbatimFormatted start_line="77" xml:space="preserve">
&#26041;&#20843;&#37324;&#38632;&#38642;&#12424;&#12379;&#12396;&#29281;&#20025;&#12363;&#12394;

&#26041;&#20843;&#37324;&#38632;&#38642;&#12424;&#12379;&#12396;&#29281;&#20025;&#12363;&#12394;
</VerbatimFormatted>
<item-text start_line="79">
MASAOKA SHIKI (&#27491;&#23713;&#23376;&#35215; 1867 - 1902)
Expand All @@ -115,7 +119,8 @@
As verbatim:
</Para>
<VerbatimFormatted start_line="87" xml:space="preserve">
&#12356;&#12385;&#12399;&#12388;&#12398;&#19968;&#36650;&#30333;&#12375;&#26149;&#12398;&#26286;

&#12356;&#12385;&#12399;&#12388;&#12398;&#19968;&#36650;&#30333;&#12375;&#26149;&#12398;&#26286;
</VerbatimFormatted>
<Para start_line="89">
&#20313;&#21629;&#12356;&#12367;&#12400;&#12367;&#12363;&#12354;&#12427;&#22812;&#30701;&#12375;
Expand All @@ -124,7 +129,8 @@
(yomei / ikubakuka aru / yo mijikashi)
</Para>
<VerbatimFormatted start_line="93" xml:space="preserve">
&#20313;&#21629;&#12356;&#12367;&#12400;&#12367;&#12363;&#12354;&#12427;&#22812;&#30701;&#12375;

&#20313;&#21629;&#12356;&#12367;&#12400;&#12367;&#12363;&#12354;&#12427;&#22812;&#30701;&#12375;
</VerbatimFormatted>
</over-text>
<Para start_line="97">
Expand Down
34 changes: 20 additions & 14 deletions t/corpus/2202jpx.xml
Expand Up @@ -9,12 +9,11 @@
DESCRIPTION
</head1>
<Para start_line="11">
This is a test Pod document in ISO-2202-JP. Its content
is some Japanese haiku by famous poets.
This is a test Pod document in ISO-2202-JP. Its content is some Japanese
haiku by famous poets.
</Para>
<head2 start_line="15">
MATSUO BASHO (&#26494;&#23614;&#33453;&#34121; 1644 - 1694)
:
MATSUO BASHO (&#26494;&#23614;&#33453;&#34121; 1644 - 1694) :
</head2>
<Para start_line="17">
&#21476;&#27744;&#12420;&#34521;&#12392;&#12403;&#36796;&#12416;&#27700;&#12398;&#38899;
Expand All @@ -26,7 +25,8 @@
As verbatim:
</Para>
<VerbatimFormatted start_line="23" xml:space="preserve">
&#21476;&#27744;&#12420;&#34521;&#12392;&#12403;&#36796;&#12416;&#27700;&#12398;&#38899;

&#21476;&#27744;&#12420;&#34521;&#12392;&#12403;&#36796;&#12416;&#27700;&#12398;&#38899;
</VerbatimFormatted>
<head2 start_line="27">
YOSA BUSON (&#19982;&#35613;&#34154;&#26449;1716 - 1783)
Expand All @@ -41,7 +41,8 @@
As verbatim:
</Para>
<VerbatimFormatted start_line="35" xml:space="preserve">
&#26041;&#20843;&#37324;&#38632;&#38642;&#12424;&#12379;&#12396;&#29281;&#20025;&#12363;&#12394;

&#26041;&#20843;&#37324;&#38632;&#38642;&#12424;&#12379;&#12396;&#29281;&#20025;&#12363;&#12394;
</VerbatimFormatted>
<head2 start_line="37">
MASAOKA SHIKI (&#27491;&#23713;&#23376;&#35215; 1867 - 1902)
Expand All @@ -56,7 +57,8 @@
As verbatim:
</Para>
<VerbatimFormatted start_line="45" xml:space="preserve">
&#12356;&#12385;&#12399;&#12388;&#12398;&#19968;&#36650;&#30333;&#12375;&#26149;&#12398;&#26286;

&#12356;&#12385;&#12399;&#12388;&#12398;&#19968;&#36650;&#30333;&#12375;&#26149;&#12398;&#26286;
</VerbatimFormatted>
<Para start_line="47">
&#20313;&#21629;&#12356;&#12367;&#12400;&#12367;&#12363;&#12354;&#12427;&#22812;&#30701;&#12375;
Expand All @@ -65,15 +67,15 @@
(yomei / ikubakuka aru / yo mijikashi)
</Para>
<VerbatimFormatted start_line="51" xml:space="preserve">
&#20313;&#21629;&#12356;&#12367;&#12400;&#12367;&#12363;&#12354;&#12427;&#22812;&#30701;&#12375;

&#20313;&#21629;&#12356;&#12367;&#12400;&#12367;&#12363;&#12354;&#12427;&#22812;&#30701;&#12375;
</VerbatimFormatted>
<head1 start_line="53">
AS A LIST
</head1>
<over-text indent="4" start_line="55">
<item-text start_line="57">
MATSUO BASHO (&#26494;&#23614;&#33453;&#34121; 1644 - 1694)
:
MATSUO BASHO (&#26494;&#23614;&#33453;&#34121; 1644 - 1694) :
</item-text>
<Para start_line="59">
&#21476;&#27744;&#12420;&#34521;&#12392;&#12403;&#36796;&#12416;&#27700;&#12398;&#38899;
Expand All @@ -85,7 +87,8 @@
As verbatim:
</Para>
<VerbatimFormatted start_line="65" xml:space="preserve">
&#21476;&#27744;&#12420;&#34521;&#12392;&#12403;&#36796;&#12416;&#27700;&#12398;&#38899;

&#21476;&#27744;&#12420;&#34521;&#12392;&#12403;&#36796;&#12416;&#27700;&#12398;&#38899;
</VerbatimFormatted>
<item-text start_line="69">
YOSA BUSON (&#19982;&#35613;&#34154;&#26449;1716 - 1783)
Expand All @@ -100,7 +103,8 @@
As verbatim:
</Para>
<VerbatimFormatted start_line="77" xml:space="preserve">
&#26041;&#20843;&#37324;&#38632;&#38642;&#12424;&#12379;&#12396;&#29281;&#20025;&#12363;&#12394;

&#26041;&#20843;&#37324;&#38632;&#38642;&#12424;&#12379;&#12396;&#29281;&#20025;&#12363;&#12394;
</VerbatimFormatted>
<item-text start_line="79">
MASAOKA SHIKI (&#27491;&#23713;&#23376;&#35215; 1867 - 1902)
Expand All @@ -115,7 +119,8 @@
As verbatim:
</Para>
<VerbatimFormatted start_line="87" xml:space="preserve">
&#12356;&#12385;&#12399;&#12388;&#12398;&#19968;&#36650;&#30333;&#12375;&#26149;&#12398;&#26286;

&#12356;&#12385;&#12399;&#12388;&#12398;&#19968;&#36650;&#30333;&#12375;&#26149;&#12398;&#26286;
</VerbatimFormatted>
<Para start_line="89">
&#20313;&#21629;&#12356;&#12367;&#12400;&#12367;&#12363;&#12354;&#12427;&#22812;&#30701;&#12375;
Expand All @@ -124,7 +129,8 @@
(yomei / ikubakuka aru / yo mijikashi)
</Para>
<VerbatimFormatted start_line="93" xml:space="preserve">
&#20313;&#21629;&#12356;&#12367;&#12400;&#12367;&#12363;&#12354;&#12427;&#22812;&#30701;&#12375;

&#20313;&#21629;&#12356;&#12367;&#12400;&#12367;&#12363;&#12354;&#12427;&#22812;&#30701;&#12375;
</VerbatimFormatted>
</over-text>
<Para start_line="97">
Expand Down

0 comments on commit fd41e0d

Please sign in to comment.