Skip to content

Commit

Permalink
Tidy and style.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkende committed Oct 30, 2024
1 parent 724851e commit f3c7a19
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .aspelldict
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
personal_ws-1.1 en 184
personal_ws-1.1 en 185
Babelmark
BlockTree
CDATA
Expand Down Expand Up @@ -43,6 +43,7 @@ accessor
accessors
apos
argv
arrayref
atx
autolink
autolinks
Expand Down
13 changes: 9 additions & 4 deletions lib/Markdown/Perl.pm
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,15 @@ will replace whatever would have been used for the link content.
=item *
C<yaml_metadata>: this hook will trigger if there is valid (!) YAML metadata in
the file and will give you a hashref as an argument. If the hook throws a die(),
the Markdown parsing will stop as the die() needs to be handled by your code.
This allows for conditional parsing based on values in the metadata section.
C<yaml_metadata>: this hook will trigger if there is B<valid> YAML metadata at
the beginning of the file. This also requires that the
L<parse_file_metadata option|Markdown::Perl::Options/parse_file_metadata> is set
to C<yaml> (which is the default in our default mode). The hook will receive a
hashref or an arrayref with the content of the YAML document.
To allow conditional parsing based on some metadata, if the hook dies, the
exception will interrupt the processing and be propagated as-is to the initial
caller.
=back
Expand Down
8 changes: 4 additions & 4 deletions lib/Markdown/Perl/BlockParser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -371,14 +371,14 @@ sub _parse_yaml_metadata {
my $metadata = eval { YAML::Tiny->read_string($+{YAML}) };
if ($EVAL_ERROR) {
pos($this->{md}) = 0;
carp 'YAML Metadata (Markdown frontmatter) is invalid.';
return 1;
carp 'YAML Metadata (Markdown frontmatter) is invalid' if $this->get_warn_for_unused_input();
return;
}
if(exists($this->{pmarkdown}{hooks}{yaml_metadata})) {
if (exists($this->{pmarkdown}{hooks}{yaml_metadata})) {
$this->{pmarkdown}{hooks}{yaml_metadata}->($metadata->[0]);
}
}
return 1;
return;
}

# https://spec.commonmark.org/0.30/#atx-headings
Expand Down
4 changes: 2 additions & 2 deletions lib/Markdown/Perl/Options.pm
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ sub _word_list {
=head3 B<parse_file_metadata> I<(enum, default: yaml)>
This option controls whether the parser accepts optional metadata at the
beginning of the file. The module does nothing with the metadata itself but you
can configure a hook to intercept the YAML.
beginning of the file. The module currently does nothing with the metadata
itself but you can configure a hook to receive the YAML content.
The possible values are:
Expand Down
47 changes: 28 additions & 19 deletions t/501-hooks-yaml-metadata.t → t/500-hooks-yaml_metadata.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use warnings;
use utf8;

use Markdown::Perl 'convert', 'set_hooks';
use Test::More;
use Test2::Tools::Warnings;
use Test2::Tools::Exception;
use Test2::V0;

my $p = Markdown::Perl->new();
my $page = <<EOF;
Expand All @@ -19,6 +17,16 @@ number: 42
I repeat: "Mark is down!"
EOF

my $list_yaml = <<EOF;
---
- abc
- def
---
# Mark is down!
I repeat: "Mark is down!"
EOF

my $invalid_page = <<EOF;
---
name: Mark is down
Expand All @@ -30,36 +38,37 @@ name: Mark is down
I repeat: "Mark is down!"
EOF

# Test 1: Check if we can get a string value
# Check that we can read a hash-map for map document.
{
sub hook_is_name_mark {
my $x = shift;
ok(exists($x->{name}) && $x->{name} eq 'Mark is down', "key 'name' was retrieved and validated as being 'Mark is down'");
sub hook_hash {
is($_[0], {name => 'Mark is down', draft => 'false', number => 42}, 'read map YAML');
}
$p->set_hooks(yaml_metadata => \&hook_is_name_mark);
$p->set_hooks(yaml_metadata => \&hook_hash);
$p->convert($page);
}

# Test 2: Validate that hook is not called if yaml is invalid
# Check if we can get a string value
{
my $hook_called = 0;
sub hook_called {
$hook_called = 1;
sub hook_list {
is($_[0], [qw(abc def)], 'read list YAML');
}
$p->set_hooks(yaml_metadata => \&hook_called);
ok(!$hook_called, "Hook was not called because metadata was invalid.");
$p->convert($invalid_page);
$p->set_hooks(yaml_metadata => \&hook_list);
$p->convert($list_yaml);
}

# Test 3: Validate that invalid yaml causes a carp()
# Validate that hook is not called if yaml is invalid and that this causes a
# call to carp().
{
sub hook {
my $hook_called = 0;
sub hook_called {
$hook_called = 1;
}
$p->set_hooks(yaml_metadata => \&hook);
$p->set_hooks(yaml_metadata => \&hook_called);
like(warning { $p->convert($invalid_page) }, qr/invalid/, "Got expected warning");
ok(!$hook_called, "Hook was not called because metadata was invalid.");
}

# Test 4: What happens if inside the hook we die()
# Hook exceptions are propagated
{
sub hook_die {
die "last words";
Expand Down

0 comments on commit f3c7a19

Please sign in to comment.