Markdent/Parser.pm

filter: Statementstmt 100% 130 / 130 Branchbran 89.2% 25 / 28 Conditioncond 66.6% 4 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 24 / 24 Podpod 50.0% 1 / 2 totaltotal 95.3% 184 / 193 CC 21 SCAR 14.8
CC
21
Cov
97%
_set_classes_for_dialects
11.2
BUILD
5.0
__ANON__
4.0
CRAP 4.4
« Markdent/Role/DebugPrinter.pm ↑ Summary Markdent/Role/BalancedEvent.pm »
1 package Markdent::Parser;
2  
3 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use namespace::autoclean 0.09;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 35 our $VERSION = '0.39';
8  
9 35 ▶use Markdent::Parser::BlockParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 35 ▶use Markdent::Parser::SpanParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use Markdent::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use Module::Runtime qw( require_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 35 ▶use Moose::Meta::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 35 ▶use Params::ValidationCompiler 0.14 qw( validation_for );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 35 ▶use Specio::Declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 35 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 35 ▶use Moose 0.92;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 35 ▶use MooseX::SemiAffordanceAccessor 0.05;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 35 ▶use MooseX::StrictConstructor 0.08;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 35 with 'Markdent::Role::AnyParser';
23  
24 35 has _block_parser_class => (
25 is => 'rw',
26 isa => t('BlockParserClass'),
27 init_arg => 'block_parser_class',
28 default => 'Markdent::Parser::BlockParser',
29 );
30  
31 35 has _block_parser => (
32 is => 'rw',
33 does => object_does_type('Markdent::Role::BlockParser'),
34 lazy => 1,
35 init_arg => undef,
36 builder => '_build_block_parser',
37 );
38  
39 35 has _block_parser_args => (
40 is => 'rw',
41 isa => t('HashRef'),
42 init_arg => undef,
43 );
44  
45 35 has _span_parser_class => (
46 is => 'rw',
47 isa => t('SpanParserClass'),
48 init_arg => 'span_parser_class',
49 default => 'Markdent::Parser::SpanParser',
50 );
51  
52 35 has _span_parser => (
53 is => 'ro',
54 does => object_does_type('Markdent::Role::SpanParser'),
55 lazy => 1,
56 init_arg => undef,
57 builder => '_build_span_parser',
58 );
59  
60 35 has _span_parser_args => (
61 is => 'rw',
62 isa => t('HashRef'),
63 init_arg => undef,
64 );
65  
66 override BUILDARGS => sub {
67 184 ▶ my $class = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
68  
69 184 my $args = super();
70  
71 184 ▶ if ( exists $args->{dialect} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args->{'dialect'}) { } 1 183 184
elsif (exists $args->{'dialects'}) { } 50 133 183
72  
73 # XXX - deprecation warning
74 1 $args->{dialects} = [ delete $args->{dialect} ];
75 }
76 elsif ( exists $args->{dialects} ) {
77 $args->{dialects} = [ $args->{dialects} ]
78 50 ▶ unless ref $args->{dialects};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args->{"dialects"} 43 7 50
79 }
80  
81 184 return $args;
82 35 };
83  
84 sub BUILD {
85 184 ▶ my $self = shift;
Subroutine BUILD: called CC=5 SCAR=16.1
Pod undocumented
86 184 my $args = shift;
87  
88 184 $self->_set_classes_for_dialects($args);
89  
90 184 my %sp_args;
91 184 for my $key (
92 1656 grep {defined}
93 1656 map { $_->init_arg }
94 $self->_span_parser_class->meta->get_all_attributes
95 ) {
96  
97 $sp_args{$key} = $args->{$key}
98 552 ▶ if exists $args->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{$key} 184 368 552
99 }
100  
101 184 $sp_args{handler} = $self->handler;
102  
103 184 $self->_set_span_parser_args( \%sp_args );
104  
105 184 my %bp_args;
106 184 for my $key (
107 1130 grep {defined}
108 1130 map { $_->init_arg }
109 $self->_block_parser_class->meta->get_all_attributes
110 ) {
111  
112 $bp_args{$key} = $args->{$key}
113 552 ▶ if exists $args->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{$key} 184 368 552
114 }
115  
116 184 $bp_args{handler} = $self->handler;
117 184 $bp_args{span_parser} = $self->_span_parser;
118  
119 184 $self->_set_block_parser_args( \%bp_args );
120 }
121  
122 sub _set_classes_for_dialects {
123 184 ▶ my $self = shift;
Subroutine _set_classes_for_dialects: called CC=11 SCAR=24.1
124 184 my $args = shift;
125  
126 184 my $dialects = delete $args->{dialects};
127  
128 184 ▶ return unless @{ $dialects || [] };
Branch: 2 branches
BranchTrueFalseTotal
unless @{$dialects or [];} 136 48 184
$dialects or [] 133 51 184
129  
130 48 for my $thing (qw( block_parser span_parser )) {
131 96 my @roles;
132  
133 96 for my $dialect ( @{$dialects} ) {
134 98 ▶ next if $dialect eq 'Standard';
Branch: 1 branch
BranchTrueFalseTotal
if $dialect eq "Standard" 0 98 98
135  
136 98 my $role = $self->_role_name_for_dialect( $dialect, $thing );
137  
138 my $found = try {
139 98 ▶ require_module($role);
Subroutine __ANON__: called CC=1 SCAR=0.0
140 }
141 catch {
142 1 ▶ die $_ unless $_ =~ /Can't locate/;
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /Can't locate/ 0 1 1
143 1 0;
144 98 };
145 98 ▶ next unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 1 97 98
146  
147 97 my $specified_class = $args->{ $thing . '_class' };
148  
149 next
150 97 ▶ if $specified_class
Branch: 1 branch
BranchTrueFalseTotal
if $specified_class and $specified_class->can("meta") and $specified_class->meta->does_role($role) 0 97 97
Condition66%4/6
$specified_class and $specified_class->can("meta") and $specified_class->meta->does_role($role)
!ll&&!rl&&r
9610
$specified_class and $specified_class->can("meta")
!ll&&!rl&&r
9601
MC/DC0%0/3
$specified_class and $specified_class->can("meta") and $specified_class->meta->does_role($role)
$specified_class $specified_class->can("meta") $specified_class->meta->does_role($role)
Truth table50%2/4
A and B and C
A= $specified_classB= $specified_class->can("meta")C= $specified_class->meta->does_role($role)
ABCresult
0XX0
10X0
1100
1111
151 && $specified_class->can('meta')
152 && $specified_class->meta->does_role($role);
153  
154 97 push @roles, $role;
155 }
156  
157 96 ▶ next unless @roles;
Branch: 1 branch
BranchTrueFalseTotal
unless @roles 1 95 96
158  
159 95 my $class_meth = q{_} . $thing . '_class';
160  
161 95 my $class = Moose::Meta::Class->create_anon_class(
162 superclasses => [ $self->$class_meth() ],
163 roles => \@roles,
164 cache => 1,
165 )->name;
166  
167 95 my $set_meth = '_set' . $class_meth;
168 95 $self->$set_meth($class);
169 }
170 }
171  
172 sub _role_name_for_dialect {
173 98 ▶ my $self = shift;
Subroutine _role_name_for_dialect: called CC=2 SCAR=6.9
174 98 my $dialect = shift;
175 98 my $type = shift;
176  
177 98 my $suffix = join q{}, map {ucfirst} split /_/, $type;
178  
179 98 ▶ if ( $dialect =~ /::/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dialect =~ /::/) { } 4 94 98
180 4 return join '::', $dialect, $suffix;
181 }
182 else {
183 94 return join '::', 'Markdent::Dialect', $dialect, $suffix;
184 }
185 }
186  
187 sub _build_block_parser {
188 182 ▶ my $self = shift;
Subroutine _build_block_parser: called CC=1 SCAR=0.0
189  
190 182 return $self->_block_parser_class->new( $self->_block_parser_args );
191 }
192  
193 sub _build_span_parser {
194 184 ▶ my $self = shift;
Subroutine _build_span_parser: called CC=1 SCAR=0.0
195  
196 184 return $self->_span_parser_class->new( $self->_span_parser_args );
197 }
198  
199 {
200 35 my $validator = validation_for(
201 params => [ markdown => { type => t('Str') } ],
202 named_to_list => 1,
203 );
204  
205 sub parse {
206 178 ▶ my $self = shift;
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
207 178 my ($text) = $validator->(@_);
208  
209 178 $self->_clean_text( \$text );
210  
211 178 $self->_send_event('StartDocument');
212  
213 178 $self->_block_parser->parse_document( \$text );
214  
215 176 $self->_send_event('EndDocument');
216  
217 176 return;
218 }
219 }
220  
221 35 sub _clean_text {
222 178 ▶ my $self = shift;
Subroutine _clean_text: called CC=2 SCAR=6.9
223 178 my $text = shift;
224  
225 178 ${$text} =~ s/\r\n?/\n/g;
226 4 ${$text} .= "\n"
227 178 ▶ unless substr( ${$text}, -1, 1 ) eq "\n";
Branch: 1 branch
BranchTrueFalseTotal
unless substr(${$text;}, -1, 1) eq "\n" 4 174 178
228  
229 178 return;
230 }
231  
232 35 __PACKAGE__->meta->make_immutable;
233  
234 35 1;
235  
236 # ABSTRACT: A markdown parser
237  
238 __END__
239  
240 =pod
241  
242 =encoding UTF-8
243  
244 =head1 NAME
245  
246 Markdent::Parser - A markdown parser
247  
248 =head1 VERSION
249  
250 version 0.39
251  
252 =head1 SYNOPSIS
253  
254 my $handler = Markdent::Handler::HTMLStream->new( ... );
255  
256 my $parser = Markdent::Parser->new(
257 dialect => ...,
258 handler => $handler,
259 );
260  
261 $parser->parse( markdown => $markdown );
262  
263 =head1 DESCRIPTION
264  
265 This class provides the primary interface for creating a parser. It ties a
266 block and span parser together with a handler.
267  
268 By default, it will parse the standard Markdown dialect, but you can provide
269 alternate block or span parser classes.
270  
271 =head1 METHODS
272  
273 This class provides the following methods:
274  
275 =head2 Markdent::Parser->new(...)
276  
277 This method creates a new parser. It accepts the following parameters:
278  
279 =over 4
280  
281 =item * dialects => $name or [ $name1, $name2 ]
282  
283 You can use this to apply dialect roles to the standard parser class.
284  
285 If a dialect name does not contain a namespace separator (::), the constructor
286 looks for roles named C<Markdent::Dialect::${dialect}::BlockParser> and
287 C<Markdent::Dialect::${dialect}::SpanParser>.
288  
289 If a dialect name does contain a namespace separator, it is used a prefix -
290 C<$dialect::BlockParser> and C<$dialect::SpanParser>.
291  
292 If any relevant roles are found, they will be used by the parser.
293  
294 It is okay if a given dialect only provides a block or span parser, but not
295 both.
296  
297 =item * block_parser_class => $class
298  
299 This defaults to L<Markdent::Parser::BlockParser>, but can be any class which
300 implements the L<Markdent::Role::BlockParser> role.
301  
302 =item * span_parser_class => $class
303  
304 This defaults to L<Markdent::Parser::SpanParser>, but can be any class which
305 implements the L<Markdent::Role::SpanParser> role.
306  
307 =item * handler => $handler
308  
309 This can be any object which implements the L<Markdent::Role::Handler>
310 role. It is required.
311  
312 =back
313  
314 =head2 $parser->parse( markdown => $markdown )
315  
316 This method parses the given document. The parsing will cause events to be
317 fired which will be passed to the parser's handler.
318  
319 =head1 ROLES
320  
321 This class does the L<Markdent::Role::EventsAsMethods> and
322 L<Markdent::Role::Handler> roles.
323  
324 =head1 BUGS
325  
326 See L<Markdent> for bug reporting details.
327  
328 Bugs may be submitted at L<https://github.com/houseabsolute/Markdent/issues>.
329  
330 I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
331  
332 =head1 SOURCE
333  
334 The source code repository for Markdent can be found at L<https://github.com/houseabsolute/Markdent>.
335  
336 =head1 AUTHOR
337  
338 Dave Rolsky <autarch@urth.org>
339  
340 =head1 COPYRIGHT AND LICENSE
341  
342 This software is copyright (c) 2021 by Dave Rolsky.
343  
344 This is free software; you can redistribute it and/or modify it under
345 the same terms as the Perl 5 programming language system itself.
346  
347 The full text of the license can be found in the
348 F<LICENSE> file included with this distribution.
349  
350 =cut
« Markdent/Role/DebugPrinter.pm ↑ Summary Markdent/Role/BalancedEvent.pm »