Pod-Elemental-Transformer-SynHi
view release on metacpan or search on metacpan
lib/Pod/Elemental/Transformer/SynHi.pm view on Meta::CPAN
#pod =cut
sub parse_synhi_param {
my ($self, $str) = @_;
confess "don't know how to parse synhi parameter '$str'" if $str =~ /\S/;
return {};
}
#pod =method build_html_para
#pod
#pod Whenever the C<synhi_params_for_para> method returns true, this method is
#pod called with the result (array-dereferenced) and the result of I<this> method is
#pod used to replace the original paragraph. The default implementation of this
#pod method is probably suitable for everyone: it passes its parameters along to the
#pod C<build_html> method, constructs a C<html> region containing the resultant
#pod string, and returns that.
#pod
#pod =cut
sub build_html_para {
my ($self, $content, $param) = @_;
my $html = $self->build_html($content, $param);
$html = $self->standard_code_block($html) if $self->use_standard_wrapper;
my $new = Pod::Elemental::Element::Pod5::Region->new({
format_name => 'html',
is_pod => 0,
content => '',
children => [
Pod::Elemental::Element::Pod5::Data->new({ content => $html }),
],
});
return $new;
}
#pod =method standard_code_block
#pod
#pod my $html = $xform->standard_code_block( $in_html );
#pod
#pod Given a hunk of HTML representing the syntax highlighted code, this rips the
#pod HTML apart and re-wraps it in a table with line numbers. It assumes the code's
#pod actual lines are broken by newlines or C<< <br> >> elements.
#pod
#pod The standard code block emitted by this role is table with the class
#pod C<code-listing>. It will have one row with two cells; the first has class
#pod C<line-numbers> and the second has class C<code>. The table is used to make
#pod it easy to copy only the code without the line numbers.
#pod
#pod Some other minor changes are made, and these may change over time, to make the
#pod code blocks "better" displayed. If your needs are very specific, replace this
#pod method.
#pod
#pod =cut
sub standard_code_block {
my ($self, $html) = @_;
my @lines = split m{<br(?:\s*/)>|\n}, $html;
# The leading nbsp below, in generating $code, is to try to get indentation
# to appear in feed readers, which to not respect white-space:pre or the pre
# element. The use of <br> instead of newlines is for the same reason.
# -- rjbs, 2009-12-10
my $nums = join "<br />", map {; "$_: " } (1 .. @lines);
my $code = join "<br />",
map {; s/^(\s+)/' ' x length $1/me; $_ }
@lines;
# Another stupid hack: the <code> blocks below force monospace font. It
# can't wrap the whole table, though, because it would cause styling issues
# in the rendered XHTML. -- rjbs, 2009-12-10
$html = "<table class='code-listing'><tr>"
. "<td class='line-numbers'><br /><code>$nums</code><br /> </td>"
. "<td class='code'><br /><code>$code</code><br /> </td>"
. "</table>";
return $html;
}
sub transform_node {
my ($self, $node) = @_;
for my $i (0 .. (@{ $node->children } - 1)) {
my $para = $node->children->[ $i ];
next unless my $arg = $self->synhi_params_for_para($para);
my $new = $self->build_html_para(@$arg);
die "couldn't produce new html" unless $new;
$node->children->[ $i ] = $new;
}
return $node;
}
#pod =head1 SEE ALSO
#pod
#pod =for :list
#pod * L<Pod::Elemental::Transformer::SynMux>
#pod
#pod =cut
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Pod::Elemental::Transformer::SynHi - a role for transforming code into syntax highlighted HTML regions
=head1 VERSION
version 0.101001
( run in 1.141 second using v1.01-cache-2.11-cpan-71847e10f99 )