Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #32 from gisle/master

Improved handling of markup nesting for code
  • Loading branch information...
commit 009a0fc578eec864a3a20e2a946ee166b5f16c64 2 parents 80eec15 + d083631
@theory authored
Showing with 67 additions and 15 deletions.
  1. +34 −9 lib/Pod/Simple/XHTML.pm
  2. +6 −1 t/xhtml01.t
  3. +27 −5 t/xhtml20.t
View
43 lib/Pod/Simple/XHTML.pm
@@ -309,7 +309,17 @@ something like:
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> >>>.
+The base implementation just escapes the text.
+
+The callback methods C<start_code> and C<end_code> emits the C<code> tags
+before and after C<handle_code> is invoked, so you might want to override these
+together with C<handle_code> if this wrapping isn't suiteable.
+
+Note that the code might be broken into mulitple segments if there are
+nested formatting codes inside a C<< CE<lt>...> >> sequence. In between the
+calls to C<handle_code> other markup tags might have been emitted in that
+case. The same is true for verbatim sections if the C<codes_in_verbatim>
+option is turned on.
=head2 accept_targets_as_html
@@ -332,8 +342,8 @@ sub accept_targets_as_html {
}
sub handle_text {
- if ($_[0]{'in_code'}) {
- return $_[0]->handle_code( $_[1] );
+ if ($_[0]{'in_code'} && @{$_[0]{'in_code'}}) {
+ return $_[0]->handle_code( $_[1], $_[0]{'in_code'}[-1] );
}
# escape special characters in HTML (<, >, &, etc)
my $text = $_[0]->__in_literal_xhtml_region
@@ -344,12 +354,27 @@ sub handle_text {
$_[0]{htext} .= $text if $_[0]{'in_head'};
}
+sub start_code {
+ $_[0]{'scratch'} .= '<code>';
+}
+
+sub end_code {
+ $_[0]{'scratch'} .= '</code>';
+}
+
sub handle_code {
- $_[0]{'scratch'} .= '<code>' . $_[0]->encode_entities( $_[1] ) . '</code>';
+ $_[0]{'scratch'} .= $_[0]->encode_entities( $_[1] );
+}
+
+sub start_Para {
+ $_[0]{'scratch'} = '<p>';
}
-sub start_Para { $_[0]{'scratch'} = '<p>' }
-sub start_Verbatim { $_[0]{'scratch'} = '<pre>'; $_[0]{'in_code'} = 1; }
+sub start_Verbatim {
+ $_[0]{'scratch'} = '<pre>';
+ push(@{$_[0]{'in_code'}}, 'Verbatim');
+ $_[0]->start_code($_[0]{'in_code'}[-1]);
+}
sub start_head1 { $_[0]{'in_head'} = 1; $_[0]{htext} = ''; }
sub start_head2 { $_[0]{'in_head'} = 2; $_[0]{htext} = ''; }
@@ -412,8 +437,8 @@ sub end_over_text {
sub end_Para { $_[0]{'scratch'} .= '</p>'; $_[0]->emit }
sub end_Verbatim {
+ $_[0]->end_code(pop(@{$_[0]->{'in_code'}}));
$_[0]{'scratch'} .= '</pre>';
- delete $_[0]{'in_code'};
$_[0]->emit;
}
@@ -584,8 +609,8 @@ sub end_Document {
sub start_B { $_[0]{'scratch'} .= '<b>' }
sub end_B { $_[0]{'scratch'} .= '</b>' }
-sub start_C { $_[0]{'in_code'} = 1; }
-sub end_C { delete $_[0]{'in_code'}; }
+sub start_C { push(@{$_[0]{'in_code'}}, 'C'); $_[0]->start_code($_[0]{'in_code'}[-1]); }
+sub end_C { $_[0]->end_code(pop(@{$_[0]{'in_code'}})); }
sub start_F { $_[0]{'scratch'} .= '<i>' }
sub end_F { $_[0]{'scratch'} .= '</i>' }
View
7 t/xhtml01.t
@@ -474,10 +474,15 @@ $parser->parse_string_document(<<'EOPOD');
=pod
A plain paragraph with a C<functionname>.
+
+C<< This code is B<important> to E<lt>me>! >>
+
EOPOD
is($results, <<"EOHTML", "code entity in a paragraph");
<p>A plain paragraph with a <code>functionname</code>.</p>
+<p><code>This code is <b>important</b> to &lt;me&gt;!</code></p>
+
EOHTML
@@ -667,7 +672,7 @@ SKIP: for my $use_html_entities (0, 1) {
EOPOD
is($results, <<"EOHTML", "Verbatim text with markup and embedded formatting");
<pre><code> # this header is very important &amp; dont you forget it
- </code><b><code>my \$file = &lt;FILE&gt; || Blank!;</code></b><code>
+ <b>my \$file = &lt;FILE&gt; || Blank!;</b>
my \$text = &quot;File is: &quot; . &lt;FILE&gt;;</code></pre>
EOHTML
View
32 t/xhtml20.t
@@ -4,17 +4,27 @@
use strict;
use warnings;
-use Test::More tests => 2;
+use Test::More tests => 1;
BEGIN {
package MyXHTML;
use base 'Pod::Simple::XHTML';
sub handle_code {
- my($self, $code) = @_;
- $code = "[$code]";
+ my($self, $code, $kind) = @_;
+ $code = "${kind}[$code]";
$self->SUPER::handle_code($code);
}
+
+ sub start_code {
+ my($self, $kind) = @_;
+ $self->{scratch} .= "<code class=\"$kind\">";
+ }
+
+ sub end_code {
+ my($self, $kind) = @_;
+ $self->{scratch} .= "</code><!-- $kind -->";
+ }
}
@@ -28,14 +38,26 @@ $parser->parse_string_document(<<'EOT');
This is C<$code> and so is:
my $foo = 1;
+
+Code might even be C<<< nested( B<< C<1> >> ) >>>.
EOT
-like $results, qr/<code>\[\$code]<\/code>/;
-like $results , qr/<pre><code>\[ my \$foo = 1;/;
+is($results, <<'EOT');
+<h1 id="Foo">Foo</h1>
+
+<p>This is <code class="C">C[$code]</code><!-- C --> and so is:</p>
+
+<pre><code class="Verbatim">Verbatim[ my $foo = 1;]</code><!-- Verbatim --></pre>
+
+<p>Code might even be <code class="C">C[nested( ]<b><code class="C">C[1]</code><!-- C --></b>C[ )]</code><!-- C -->.</p>
+
+EOT
sub initialize {
$parser = MyXHTML->new;
+ $parser->html_header('');
+ $parser->html_footer('');
$parser->output_string( \$results );
$results = '';
}
Please sign in to comment.
Something went wrong with that request. Please try again.