blah blah blah, without a
or something
$self->_envelop_children( $node, HTML::Element->new('p') );
}
} elsif( $node->tag eq '~text' ) {
$self->_escape_text($node);
# bug #43998
$self->_decode_entities_in_code($node)
if $node->parent->tag eq 'code' or $node->parent->tag eq 'code_block';
}
}
sub preprocess_tree {
my( $self, $root ) = @_;
foreach my $node ( $root->descendants ) {
# bug #43997 - multiline code blocks
if( $self->_text_is_within_code_pre($node) ) {
$self->_convert_to_code_block($node);
}
}
}
sub _text_is_within_code_pre {
my( $self, $node ) = @_;
return unless $node->parent->parent and $node->parent->parent->tag;
# Must be (or ...
...)
my $code_pre = $node->parent->tag eq 'code' && $node->parent->parent->tag eq 'pre';
my $pre_code = $node->parent->tag eq 'pre' && $node->parent->parent->tag eq 'code';
return unless $code_pre or $pre_code;
# Can't be any other nodes in a code block
return if $node->left or $node->right;
return if $node->parent->left or $node->parent->right;
return 1;
}
sub _convert_to_code_block {
my( $self, $node ) = @_;
$node->parent->parent->replace_with_content->delete;
$node->parent->tag( "code_block" );
}
sub _envelop_children {
my( $self, $node, $new_child ) = @_;
my @children = $node->detach_content;
$node->push_content($new_child);
$new_child->push_content(@children);
}
# special handling for: ` _ # . [ !
my @escapes = qw( \\ * { } _ ` );
my %backslash_escapes = (
'\\' => [ '0923fjhtml2wikiescapedbackslash', "\\\\" ],
'*' => [ '0923fjhtml2wikiescapedasterisk', "\\*" ],
'{' => [ '0923fjhtml2wikiescapedopenbrace', "\\{" ],
'}' => [ '0923fjhtml2wikiescapedclosebrace', "\\}" ],
'_' => [ '0923fjhtml2wikiescapedunderscore', "\\_" ],
'`' => [ '0923fjhtml2wikiescapedbacktick', "\\`" ],
);
sub _escape_text {
my( $self, $node ) = @_;
my $text = $node->attr('text') || '';
#
# (bug #43998)
# Only backslash-escape backticks that don't occur within
# tags. Those within tags are left alone and the backticks to
# signal a tag get upgraded to a double-backtick by
# _code_delim().
#
# (bug #43993)
# Likewise, only backslash-escape underscores that occur outside
# tags.
#
my $inside_code = $node->look_up( _tag => 'code' ) || $node->look_up( _tag => 'code_block' );
if( not $inside_code ) {
my $escapes = join '', @escapes;
$text =~ s/([\Q$escapes\E])/$backslash_escapes{$1}->[0]/g;
$text =~ s/^([\d]+)\./$1\\./;
$text =~ s/^\#/\\#/;
$text =~ s/\!\[/\\![/g;
$text =~ s/\]\[/]\\[/g;
$node->attr( text => $text );
}
}
# bug #43998
sub _code_delim {
my( $self, $node, $rules ) = @_;
my $contents = $self->get_elem_contents($node);
return $contents =~ /\`/ ? '``' : '`';
}
# bug #43996
sub _decode_entities_in_code {
my( $self, $node ) = @_;
my $text = $node->attr('text') || '';
return unless $text;
HTML::Entities::_decode_entities( $text, { 'amp' => '&', 'lt' => '<', 'gt' => '>' } );
$node->attr( text => $text );
}
sub postprocess_output {
my( $self, $outref ) = @_;
$$outref =~ s/\Q$code_block_prefix\E/ /gm;
$self->_unescape_text($outref);
$self->_add_references($outref);
}
sub _unescape_text {
my( $self, $outref ) = @_;
foreach my $escape ( values %backslash_escapes ) {
$$outref =~ s/$escape->[0]/$escape->[1]/g;
}
}
sub _add_references {
my( $self, $outref ) = @_;
my @links = @{ $self->_links || [] };
return unless @links;
my $links = '';
foreach my $link ( @links ) {
my $id = $link->{id} || '';
my $url = $link->{url} || '';
my $title = $link->{title} || '';
if( $title ) {
$links .= sprintf " [%s]: %s \"%s\"\n", $id, $url, $title;
} else {
$links .= sprintf " [%s]: %s\n", $id, $url;
}
}
$self->_links( [] );
$self->_last_link_id( 0 );
$$outref .= "\n\n$links";
$$outref =~ s/\s+$//gs;
}
sub _is_phrase_tag {
my $tag = pop || '';
return $HTML::Tagset::isPhraseMarkup{$tag} || $tag eq '~text';
}
sub _abs2rel {
my( $self, $uri ) = @_;
return $uri unless $self->base_uri;
return URI->new($uri)->rel($self->base_uri)->as_string;
}
=head1 AUTHOR
David J. Iberri, C<< >>
=head1 BUGS
Please report any bugs or feature requests to
C, or through the web interface at
L.
I will be notified, and then you'll automatically be notified of progress on
your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc HTML::WikiConverter::Markdown
You can also look for information at:
=over 4
=item * AnnoCPAN: Annotated CPAN documentation
L
=item * CPAN Ratings
L
=item * RT: CPAN's request tracker
L
=item * Search CPAN
L
=back
=head1 COPYRIGHT & LICENSE
Copyright 2006 David J. Iberri, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
1;