Browse Source

Markdown.pl: apply -i and -r options to a and img tags

When a and img tags are generated using the normal Markdown syntax
any prefixes specified with the -i and -r options are inserted as
appropriate.

Extend this processing to explicit a and img tags as well.

This makes sense because they should be handled the same way the
Markdown syntax generated tags are for consistency.

It's still possible to "escape" from the prefixes by using an
explicit scheme+host+port or the commonly supported (but not a
standard) //+host+port mechanism.

And it only matters if prefixes have been set with the -i and/or
-r options (the default is no prefixes) anyway.

Signed-off-by: Kyle J. McKay <mackyle@gmail.com>
master
Kyle J. McKay 6 years ago
parent
commit
f2701d5638
  1. 41
      Markdown.pl

41
Markdown.pl

@ -1979,6 +1979,8 @@ sub _DoTag {
if (($tag =~ m{^<($g_possible_tag_name)(?:[\s>]|/>$)} || $tag =~ m{^</($g_possible_tag_name)\s*>}) &&
$ok_tag_name{lc($1)}) {
return _ProcessURLTag("href", $tag) if $tag =~ /^<a\s/i;
return _ProcessURLTag("src", $tag) if $tag =~ /^<img\s/i;
return $tag;
}
$tag =~ s/</&lt;/g;
@ -1986,6 +1988,45 @@ sub _DoTag {
}
sub _ProcessURLTag {
my $att = shift;
my $tag = shift;
$att = lc($att) . "=";
if ($tag =~ /^(<[^\s>]+\s+)/g) {
my $out = $1;
while ($tag =~ /\G([^\s\042\047>]+=)([\042\047])((?:(?!\2)(?!>).)*)(\2\s*)/gc) {
my ($p, $q, $v, $s) = ($1, $2, $3, $4);
if (lc($p) eq $att && $v ne "") {
$v = _HTMLEncode(_PrefixURL($v));
}
$out .= $p . $q . $v . $s;
}
$out .= substr($tag, pos($tag));
return $out;
}
return $tag;
}
sub _HTMLEncode {
my $text = shift;
# Ampersand-encoding based entirely on Nat Irons's Amputator MT plugin:
# http://bumppo.net/projects/amputator/
$text =~ s/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/&amp;/g;
# Remaining entities now
$text =~ s/\042/&quot;/g;
$text =~ s/\047/&apos;/g;
$text =~ s/</&lt;/g;
$text =~ s/>/&gt;/g;
return $text;
}
sub _EncodeAmps {
my $text = shift;

Loading…
Cancel
Save