-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a (first) hook, to resolve otherwise unresolved link reference.
- Loading branch information
Showing
5 changed files
with
100 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,17 +9,19 @@ use Carp; | |
use English; | ||
use Exporter 'import'; | ||
use Hash::Util 'lock_keys'; | ||
use List::Util 'none'; | ||
use List::MoreUtils 'pairwise'; | ||
use Markdown::Perl::BlockParser; | ||
use Markdown::Perl::Inlines; | ||
use Markdown::Perl::HTML 'html_escape', 'decode_entities'; | ||
use Readonly; | ||
use Scalar::Util 'blessed'; | ||
|
||
use parent 'Markdown::Perl::Options'; | ||
|
||
our $VERSION = '1.05'; # Remember to also set the App::pmarkdown version. | ||
|
||
our @EXPORT_OK = qw(convert set_options); | ||
our @EXPORT_OK = qw(convert set_options set_mode set_hooks); | ||
our %EXPORT_TAGS = (all => \@EXPORT_OK); | ||
|
||
sub new { | ||
|
@@ -28,7 +30,8 @@ sub new { | |
my $this = $class->SUPER::new( | ||
mode => undef, | ||
options => {}, | ||
local_options => {}); | ||
local_options => {}, | ||
hooks => {}); | ||
$this->SUPER::set_options(options => @options); | ||
lock_keys(%{$this}); | ||
|
||
|
@@ -47,6 +50,24 @@ sub set_mode { | |
return; | ||
} | ||
|
||
Readonly::Array my @VALID_HOOKS => qw(resolve_link_ref); | ||
|
||
sub set_hooks { | ||
my ($this, %hooks) = &_get_this_and_args; ## no critic (ProhibitAmpersandSigils) | ||
while (my ($k, $v) = each %hooks) { | ||
if (none { $_ eq $k } @VALID_HOOKS) { | ||
croak "Invalid hook name: ${k}"; | ||
} elsif (!defined $v) { | ||
delete $this->{hooks}{$k}; | ||
} elsif (ref $v ne 'CODE') { | ||
carp 'Hook target must be a CODE reference'; | ||
} else { | ||
$this->{hooks}{$k} = $v; | ||
} | ||
} | ||
return; | ||
} | ||
|
||
# Returns @_, unless the first argument is not blessed as a Markdown::Perl | ||
# object, in which case it returns a default object. | ||
my $default_this = Markdown::Perl->new(); | ||
|
@@ -280,6 +301,29 @@ Converts the given $md string into HTML. The input string must be a decoded | |
Unicode string (or an ASCII string) and the output is similarly a decoded | ||
Unicode string. | ||
=head2 set_hooks | ||
$pmarkdown->set_hooks(hook_name => sub { ... }, ...); | ||
Markdown::Perl::set_hooks(hook_name => sub { ... }, ...); | ||
Registers hooks to be called during the processing of a Markdown document. Each | ||
hook must point to a code reference. You can remove a hook by passing C<undef> | ||
as the value associated with a hook. | ||
There is currently a single hook: | ||
=over 4 | ||
=item * | ||
C<resolve_link_ref>: this hook will receive a single string an argument, | ||
containing the label of a link reference in case where there was no matching | ||
link definition in the document and it must returns either C<undef> or a | ||
hash-reference containing a C<target> key, pointing to the link destination and | ||
optionally a C<title> key containing the title of the key. | ||
=back | ||
=head1 AUTHOR | ||
Mathias Kende <[email protected]> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use strict; | ||
use warnings; | ||
use utf8; | ||
|
||
use Markdown::Perl 'convert', 'set_hooks'; | ||
use Test2::V0; | ||
|
||
sub hook { | ||
my ($ref) = @_; | ||
if ($ref eq 'foo') { | ||
return { target => 'http://foo' }; | ||
} elsif ($ref eq 'bar') { | ||
return { target => 'http://bar', title => 'BAR' }; | ||
} | ||
return; | ||
} | ||
|
||
set_hooks(resolve_link_ref => \&hook); | ||
is(convert("[text][foo]"), "<p><a href=\"http://foo\">text</a></p>\n", 'resolved in full link reference'); | ||
is(convert("[foo][]"), "<p><a href=\"http://foo\">foo</a></p>\n", 'resolved in collapsed link reference'); | ||
is(convert("[foo]"), "<p><a href=\"http://foo\">foo</a></p>\n", 'resolved in shortcut link reference'); | ||
|
||
is(convert("[bar]"), "<p><a href=\"http://bar\" title=\"BAR\">bar</a></p>\n", 'resolved with title'); | ||
is(convert("[none]"), "<p>[none]</p>\n", 'not resolved'); | ||
|
||
is(convert("[foo]\n\n[foo]: http://other"), "<p><a href=\"http://other\">foo</a></p>\n", 'source has precedence'); | ||
|
||
{ | ||
my $p = Markdown::Perl->new(); | ||
is($p->convert("[foo]"), "<p>[foo]</p>\n", 'no default hook'); | ||
$p->set_hooks(resolve_link_ref => \&hook); | ||
is($p->convert("[foo]"), "<p><a href=\"http://foo\">foo</a></p>\n", 'set_hooks object-oriented'); | ||
} | ||
|
||
set_hooks(resolve_link_ref => undef); | ||
is(convert("[foo]"), "<p>[foo]</p>\n", 'hook can be removed'); | ||
|
||
done_testing; |