Matrix/Utils.pm

filter: Statementstmt 100% 45 / 45 Branchbran 92.8% 13 / 14 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 91.1% 72 / 79 CC 11 SCAR 20.7
CC
11
Cov
94%
parse_markdownlike
11.1
BEGIN
1.0
BEGIN
1.0
CRAP 7.9
« Matrix/Room.pm ↑ Summary Matrix.pm »
1 # You may distribute under the terms of the GNU General Public License
2 #
3 # (C) Paul Evans, 2017 -- leonerd@leonerd.org.uk
4  
5 package Circle::Net::Matrix::Utils;
6  
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '0.03';
11  
12 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 our @EXPORT_OK = qw(
16 parse_markdownlike
17 );
18  
19 2 ▶use String::Tagged;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 my %DEFAULT_PATTERNS = (
22 '*' => 'italic',
23 '**' => 'bold',
24 '`' => 'monospace',
25 );
26  
27 =head1 FUNCTIONS
28  
29 =cut
30  
31 =head2 parse_markdownlike
32  
33 $st = parse_markdownlike( $str )
34  
35 Attempts to parse some markdown-like formatting tags from the input C<$str>,
36 returning a L<String::Tagged::Formatting> instance representing it.
37 In particular, the following markup is recognised
38  
39 *italic*
40 **bold**
41 `monospace`
42  
43 This parser does not attempt to be a complete, nor a fully-compatible Markdown
44 parser, but simply tries to provide a useful function for entering formatted
45 messages.
46  
47 =cut
48  
49 sub parse_markdownlike
50 4 ▶{
Subroutine parse_markdownlike: called CC=11 SCAR=24.0
Pod documented
51 4 my ( $str, $patterns ) = @_;
52  
53 4 ▶ $patterns //= \%DEFAULT_PATTERNS;
Condition50%1/2
$patterns //= \%DEFAULT_PATTERNS
l!l
04
MC/DC0%0/1
$patterns //= \%DEFAULT_PATTERNS
$patterns
Truth table50%1/2
A
A= $patterns
Aresult
11
00
54 4 ▶ keys %$patterns or croak "Cannot parse_markdownlike with no marker patterns";
Branch: 1 branch
BranchTrueFalseTotal
keys %$patterns or croak("Cannot parse_markdownlike with no marker patterns") 4 0 4
55  
56 4 my %tags = ();
57  
58 12 my $match_marker = join "|", map { quotemeta }
59 4 sort { length $b <=> length $a } # longest first so ** wins over *
60 keys %$patterns;
61 4 $match_marker = qr/$match_marker/;
62  
63 4 my $ret = String::Tagged->new;
64  
65 4 ▶ while( length $str ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str) 10 1 11
66 10 ▶ if( $str =~ m/(\w?)($match_marker)(\w?)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /(\w?)($match_marker)(\w?)/) { } 7 3 10
67 7 my $word_before = length $1;
68 7 my $marker = $2;
69 7 my $markstart = $-[2];
70 7 my $markend = $+[2];
71 7 my $word_after = length $3;
72  
73 # prefix before marker
74 7 ▶ $ret->append_tagged( substr( $str, 0, $markstart ), %tags ) if $markstart > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $markstart > 0 6 1 7
75  
76 7 ▶ if( $word_after and !$word_before ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($word_after and not $word_before) { } 3 4 7
elsif ($word_before and not $word_after) { } 3 1 4
Condition66%2/3
$word_after and not $word_before
!ll&&!rl&&r
403
MC/DC50%1/2
$word_after and not $word_before
$word_after not $word_before
Truth table66%2/3
A and not(B)
A= $word_afterB= not $word_before
ABresult
0X0
100
111
77 3 $tags{ $patterns->{$marker} } = 1;
78 }
79 ▶ elsif( $word_before and !$word_after ) {
Condition66%2/3
$word_before and not $word_after
!ll&&!rl&&r
103
MC/DC50%1/2
$word_before and not $word_after
$word_before not $word_after
Truth table66%2/3
A and not(B)
A= $word_beforeB= not $word_after
ABresult
0X0
100
111
80 3 delete $tags{ $patterns->{$marker} };
81 }
82 else {
83 # This isn't actually a tag start/stop so ignore it and continue
84 1 $ret->append_tagged( substr( $str, $markstart, $markend - $markstart ), %tags );
85 }
86  
87 7 substr( $str, 0, $markend ) = "";
88 }
89 else {
90 3 $ret->append_tagged( $str, %tags );
91 3 last;
92 }
93 }
94  
95 4 ▶ return $ret if $ret->tagnames;
Branch: 1 branch
BranchTrueFalseTotal
if $ret->tagnames 2 2 4
96 2 return $ret->str;
97 }
98  
99 2 0x55AA;
« Matrix/Room.pm ↑ Summary Matrix.pm »