Skip to content

Commit

Permalink
Add an option to render paragraphs of text without enclosing <p> tags.
Browse files Browse the repository at this point in the history
This allows to render just the "inline" part of Markdown.
  • Loading branch information
mkende committed Apr 10, 2024
1 parent 0ee304d commit a0cbf51
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/Markdown/Perl.pm
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ sub _emit_html { ## no critic (ProhibitExcessComplexity)
$html .= $this->_render_inlines($linkrefs, @{$bl->{content}});
if ($tight_block) {
$out .= $html;
} elsif ($this->get_render_naked_paragraphs) {
$out .= "${html}\n";
} else {
$out .= "<p>${html}</p>\n";
}
Expand Down
28 changes: 21 additions & 7 deletions lib/Markdown/Perl/Options.pm
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ my %validation;
our @valid_modes = (qw(default cmark github markdown));
my %valid_modes = map { $_ => 1 } @valid_modes;

my $err_str;

sub set_options {
my ($this, $dest, @options) = @_;
# We don’t put the options into a hash, to preserve the order in which they
Expand All @@ -72,7 +74,7 @@ sub set_options {
} else {
carp "Unknown option ignored: ${k}" unless exists $validation{$k};
my $validated_value = $validation{$k}($v);
croak "Invalid value for option '${k}': ${ERRNO}" unless defined $validated_value;
croak "Invalid value for option '${k}': ${err_str}" unless defined $validated_value;
$this->{$dest}{$k} = $validated_value;
}
}
Expand All @@ -90,7 +92,7 @@ sub validate_options {
} else {
die "Unknown option: ${k}\n" unless exists $validation{$k};
my $validated = $validation{$k}($v);
die "Invalid value for option '${k}': ${ERRNO}\n" unless defined $validated;
die "Invalid value for option '${k}': ${err_str}\n" unless defined $validated;
}
}
return;
Expand Down Expand Up @@ -139,9 +141,9 @@ sub _make_option {

sub _boolean {
return sub {
return 0 if $_[0] eq 'false' || $_[0] eq '0';
return 0 if $_[0] eq 'false' || $_[0] eq '' || $_[0] eq '0';
return 1 if $_[0] eq 'true' || $_[0] eq '1';
$ERRNO = 'must be a boolean value (0 or 1)';
$err_str = 'must be a boolean value (0 or 1)';
return;
};
}
Expand All @@ -150,7 +152,7 @@ sub _enum {
my @valid = @_;
return sub {
return $_[0] if any { $_ eq $_[0] } @valid;
$ERRNO = "must be one of '".join("', '", @valid)."'";
$err_str = "must be one of '".join("', '", @valid)."'";
return;
};
}
Expand All @@ -159,7 +161,7 @@ sub _regex {
return sub {
my $re = eval { qr/$_[0]/ };
return $re if defined $re;
$ERRNO = 'cannot be parsed as a Perl regex ($@)';
$err_str = 'cannot be parsed as a Perl regex ($@)';
return;
};
}
Expand Down Expand Up @@ -452,6 +454,18 @@ _make_option(

=pod
=head3 B<render_naked_paragraphs> I<(boolean, default: false)>
When this is set to true, the C<E<lt>pE<gt>> tag is always skipped around a
paragraph. This is mostly meant to render short amount of text as pure Markdown
inline content, without a surrounding block structure.
=cut

_make_option(render_naked_paragraphs => 0, _boolean);

=pod
=head2 Options controlling which inline elements are used
=head3 B<use_extended_autolinks> I<(boolean, default: true)>
Expand Down Expand Up @@ -628,7 +642,7 @@ C<&>, C<E<lt>>, and C<E<gt>>.
sub _escaped_characters {
return sub {
return $_[0] if $_[0] =~ m/^["'&<>]*$/;
$ERRNO = "must only contains the following characters: \", ', &, <, and >";
$err_str = "must only contains the following characters: \", ', &, <, and >";
return;
};
}
Expand Down
13 changes: 13 additions & 0 deletions t/400-opt-render_naked_paragraphs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use strict;
use warnings;
use utf8;

use Markdown::Perl 'convert';
use Test2::V0;

is(convert("foo\nbar", render_naked_paragraphs => 0), "<p>foo\nbar</p>\n", 'normal');
is(convert("foo\nbar", render_naked_paragraphs => 1), "foo\nbar\n", 'naked');
is(convert("foo\n\nbar", render_naked_paragraphs => 1), "foo\nbar\n", 'two_naked_paragraphs');
is(convert("- foo\n\n- bar", render_naked_paragraphs => 1), "<ul>\n<li>foo\n</li>\n<li>bar\n</li>\n</ul>\n", 'naked_loose_list');

done_testing;

0 comments on commit a0cbf51

Please sign in to comment.