Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Generate link in markdown enabled tag

  • Loading branch information...
commit 417b02e71234f837198ba54ec86e0b6c3d392563 1 parent 6a3ae30
@keedi authored
Showing with 62 additions and 3 deletions.
  1. +22 −2 lib/Text/Markdown.pm
  2. +40 −1 t/05markdown_in_divs.t
View
24 lib/Text/Markdown.pm
@@ -235,8 +235,6 @@ sub _Markdown {
# Turn block-level HTML elements into hash entries, and interpret markdown in them if they have a 'markdown="1"' attribute
$text = $self->_HashHTMLBlocks($text, {interpret_markdown_on_attribute => 1});
- $text = $self->_StripLinkDefinitions($text);
-
$text = $self->_RunBlockGamut($text, {wrap_in_p_tags => 1});
$text = $self->_UnescapeSpecialChars($text);
@@ -380,6 +378,7 @@ sub _HashHTMLBlocks {
my @chunks;
# parse each line, looking for block-level HTML tags
+ my %interpret_markdowns;
while ($text =~ s{^(([ ]{0,$less_than_tab}<)?.*\n)}{}m) {
my $cur_line = $1;
if (defined $2) {
@@ -390,12 +389,21 @@ sub _HashHTMLBlocks {
if ($options->{interpret_markdown_on_attribute} and $opening_tag =~ s/$markdown_attr//i) {
my $markdown = $2;
if ($markdown =~ /^(1|on|yes)$/) {
+ $text_in_tag = $self->_StripLinkDefinitions($text_in_tag);
+
# interpret markdown and reconstruct $tag to include the interpreted $text_in_tag
my $wrap_in_p_tags = $opening_tag =~ /^<(div|iframe)/;
$tag = $prefix . $opening_tag . "\n"
. $self->_RunBlockGamut($text_in_tag, {wrap_in_p_tags => $wrap_in_p_tags})
. "\n" . $closing_tag
;
+ $interpret_markdowns{ _md5_utf8($tag) } = {
+ prefix => $prefix,
+ opening_tag => $opening_tag,
+ text_in_tag => $text_in_tag,
+ wrap_in_p_tags => $wrap_in_p_tags,
+ closing_tag => $closing_tag,
+ }
} else {
# just remove the markdown="0" attribute
$tag = $prefix . $opening_tag . $text_in_tag . $closing_tag;
@@ -421,6 +429,18 @@ sub _HashHTMLBlocks {
$text = join '', @chunks;
+ $text = $self->_StripLinkDefinitions($text);
+ for my $key ( keys %interpret_markdowns ) {
+ my $val = $interpret_markdowns{$key};
+ my $tag
+ = $val->{prefix} . $val->{opening_tag} . "\n"
+ . $self->_RunBlockGamut($val->{text_in_tag}, {wrap_in_p_tags => $val->{wrap_in_p_tags}})
+ . "\n" . $val->{closing_tag}
+ ;
+
+ $self->{_html_blocks}{$key} = $tag;
+ }
+
return $text;
}
View
41 t/05markdown_in_divs.t
@@ -1,6 +1,6 @@
use strict;
use warnings;
-use Test::More tests => 21;
+use Test::More tests => 23;
use Test::Differences;
use FindBin '$Bin';
use lib "$Bin/../lib";
@@ -409,3 +409,42 @@ eq_or_diff $html, <<'EOF', $test;
<p>Above was code</p>
EOF
+
+
+#-------------------------------------------------------------------------------
+$test = "without markdown on - cannot generate link";
+$html = $m->markdown(<<"EOF");
+<div style="margin-top: 50px; margin-left: 400px;">
+
+[Text::Markdown][cpan-text-markdown] is really cool!
+
+</div>
+
+[cpan-text-markdown]: https://metacpan.org/module/Text::Markdown
+
+EOF
+eq_or_diff $html, <<'EOF', $test;
+<div style="margin-top: 50px; margin-left: 400px;">
+
+[Text::Markdown][cpan-text-markdown] is really cool!
+
+</div>
+EOF
+
+
+#-------------------------------------------------------------------------------
+$test = "markdown on - generat link";
+$html = $m->markdown(<<"EOF");
+<div markdown="1" style="margin-top: 50px; margin-left: 400px;">
+
+[Text::Markdown][cpan-text-markdown] is really cool!
+
+</div>
+
+[cpan-text-markdown]: https://metacpan.org/module/Text::Markdown
+EOF
+eq_or_diff $html, <<'EOF', $test;
+<div style="margin-top: 50px; margin-left: 400px;">
+<p><a href="https://metacpan.org/module/Text::Markdown">Text::Markdown</a> is really cool!</p>
+</div>
+EOF
Please sign in to comment.
Something went wrong with that request. Please try again.