| 1 | package Markua::Parser; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Path::Tiny qw(path); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = 0.01; | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | my ($class) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | my $self = bless {}, $class; | |||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 12 | } | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 7 | ▶ | sub parse_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_file: called CC=16 SCAR=27.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 7 | my ($self, $filename) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 7 | my $path = path($filename); | |||||||||||||||||||||||||||||||||||||||||
| 17 | 7 | my $dir = $path->parent->stringify; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 7 | my @entries; | |||||||||||||||||||||||||||||||||||||||||
| 19 | my @errors; | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 7 | my $cnt = 0; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 7 | $self->{text} = ''; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 7 | for my $line ($path->lines_utf8) { | |||||||||||||||||||||||||||||||||||||||||
| 25 | 75 | $cnt++; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 75 | ▶ | if ($line =~ /^(#{1,6}) (\S.*)/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 7 | push @entries, { | |||||||||||||||||||||||||||||||||||||||||
| 28 | tag => 'h' . length($1), | ||||||||||||||||||||||||||||||||||||||||||
| 29 | text => $2, | ||||||||||||||||||||||||||||||||||||||||||
| 30 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 7 | next; | |||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | # numbered list | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 68 | ▶ | if ($line =~ m{\A(\d+)([.\)])( {1,4}|\t)(\S.*)}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 6 | my ($number, $sep, $space, $text) = ($1, $2, $3, $4); | |||||||||||||||||||||||||||||||||||||||||
| 37 | 6 | ▶ | if (not $self->{tag}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | $self->{tag} = 'numbered-list'; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 2 | $self->{list} = []; | |||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 6 | ▶ | if ($self->{tag} eq 'numbered-list') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 6 | push @{ $self->{list} }, { | |||||||||||||||||||||||||||||||||||||||||
| 44 | number => $number, | ||||||||||||||||||||||||||||||||||||||||||
| 45 | sep => $sep, | ||||||||||||||||||||||||||||||||||||||||||
| 46 | space => $space, | ||||||||||||||||||||||||||||||||||||||||||
| 47 | text => $text, | ||||||||||||||||||||||||||||||||||||||||||
| 48 | raw => $line, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 6 | next; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | die "What to do if a numbered list starts in the middle of another element?"; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | # bulleted list | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 62 | ▶ | if ($line =~ m{\A([\*-])( {1,4}|\t)(\S.*)}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 12 | my ($bullet, $space, $text) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||
| 59 | 12 | ▶ | if (not $self->{tag}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 8 | $self->{tag} = 'list'; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 8 | $self->{list}{type} = 'bulleted'; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 8 | $self->{list}{bullet} = $bullet; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 8 | $self->{list}{space} = $space; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 8 | $self->{list}{ok} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 8 | $self->{list}{items} = [$text]; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 8 | $self->{list}{raw} = [$line]; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 8 | next; | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 4 | ▶ | if ($self->{tag} eq 'list') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 4 | ▶ | if ($self->{list}{type} ne 'bulleted' or | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$self->{"list"}{"type"} ne "bulleted" or $self->{"list"}{"bullet"} ne $bullet or $self->{"list"}{"space"} ne $space
$self->{"list"}{"type"} ne "bulleted" or $self->{"list"}{"bullet"} ne $bullet
MC/DC33%1/3
$self->{"list"}{"type"} ne "bulleted" or $self->{"list"}{"bullet"} ne $bullet or $self->{"list"}{"space"} ne $space
$self->{"list"}{"type"} ne "bulleted"
$self->{"list"}{"bullet"} ne $bullet
$self->{"list"}{"space"} ne $space
Truth table50%2/4
A or B or C
A= $self->{"list"}{"type"} ne "bulleted"B= $self->{"list"}{"bullet"} ne $bulletC= $self->{"list"}{"space"} ne $space
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | $self->{list}{bullet} ne $bullet or | ||||||||||||||||||||||||||||||||||||||||||
| 73 | $self->{list}{space} ne $space) { | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 2 | $self->{list}{ok} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 4 | push @{ $self->{list}{raw} }, $line; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 4 | push @{ $self->{list}{items} }, $text; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 4 | next; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | die "What to do if a bulleted list starts in the middle of another element?"; | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | # I should remember to always use \A instead of ^ even thoygh here we are really parsing lines so those two are the same | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 50 | ▶ | if ($line =~ /\A ! \[([^\]]*)\] \(([^\)]+)\) \s* \Z/x) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 4 | my $title = $1; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 4 | my $file_to_include = $2; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 4 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 89 | 4 | my $text = path("$dir/$file_to_include")->slurp_utf8; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 3 | push @entries, { | |||||||||||||||||||||||||||||||||||||||||
| 91 | tag => 'code', | ||||||||||||||||||||||||||||||||||||||||||
| 92 | title => $title, | ||||||||||||||||||||||||||||||||||||||||||
| 93 | text => $text, | ||||||||||||||||||||||||||||||||||||||||||
| 94 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 95 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 4 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | push @errors, { | |||||||||||||||||||||||||||||||||||||||||
| 98 | row => $cnt, | ||||||||||||||||||||||||||||||||||||||||||
| 99 | line => $line, | ||||||||||||||||||||||||||||||||||||||||||
| 100 | error => "Could not read included file '$file_to_include'", | ||||||||||||||||||||||||||||||||||||||||||
| 101 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 4 | next; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | # anything else defaults to paragraph | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 46 | ▶ | if ($line =~ /\S/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 9 | $self->{tag} = 'p'; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 9 | $self->{text} .= $line; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 9 | next; | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 37 | ▶ | if ($line =~ /^\s*$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 37 | $self->save_tag(\@entries); | |||||||||||||||||||||||||||||||||||||||||
| 115 | 37 | next; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | push @errors, { | |||||||||||||||||||||||||||||||||||||||||
| 119 | row => $cnt, | ||||||||||||||||||||||||||||||||||||||||||
| 120 | line => $line, | ||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 7 | $self->save_tag(\@entries); | |||||||||||||||||||||||||||||||||||||||||
| 124 | 7 | return \@entries, \@errors; | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 44 | ▶ | sub save_tag { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
save_tag: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 44 | my ($self, $entries) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 44 | ▶ | if ($self->{tag} and $self->{tag} eq 'numbered-list') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"tag"} and $self->{"tag"} eq "numbered-list"
MC/DC100%2/2
$self->{"tag"} and $self->{"tag"} eq "numbered-list"
$self->{"tag"}
$self->{"tag"} eq "numbered-list"
Truth table100%3/3
A and B
A= $self->{"tag"}B= $self->{"tag"} eq "numbered-list"
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | # TODO: verify that it is a proper list | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 2 | for my $row (@{ $self->{list} }) { | |||||||||||||||||||||||||||||||||||||||||
| 133 | 6 | delete $row->{raw}; | |||||||||||||||||||||||||||||||||||||||||
| 134 | 6 | delete $row->{sep}; | |||||||||||||||||||||||||||||||||||||||||
| 135 | 6 | delete $row->{space}; | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | push @$entries, { | ||||||||||||||||||||||||||||||||||||||||||
| 138 | tag => $self->{tag}, | ||||||||||||||||||||||||||||||||||||||||||
| 139 | list => $self->{list}, | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||
| 141 | 2 | $self->{tag} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 2 | delete $self->{list}; | |||||||||||||||||||||||||||||||||||||||||
| 143 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 42 | ▶ | if ($self->{tag} and $self->{tag} eq 'list') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"tag"} and $self->{"tag"} eq "list"
MC/DC100%2/2
$self->{"tag"} and $self->{"tag"} eq "list"
$self->{"tag"}
$self->{"tag"} eq "list"
Truth table100%3/3
A and B
A= $self->{"tag"}B= $self->{"tag"} eq "list"
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 8 | ▶ | if ($self->{list}{ok}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 6 | delete $self->{list}{raw}; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 6 | delete $self->{list}{ok}; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 6 | delete $self->{list}{space}; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 6 | delete $self->{list}{bullet}; | |||||||||||||||||||||||||||||||||||||||||
| 153 | push @$entries, { | ||||||||||||||||||||||||||||||||||||||||||
| 154 | tag => $self->{tag}, | ||||||||||||||||||||||||||||||||||||||||||
| 155 | list => $self->{list}, | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 6 | }; | |||||||||||||||||||||||||||||||||||||||||
| 157 | 6 | $self->{tag} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 6 | delete $self->{list}; | |||||||||||||||||||||||||||||||||||||||||
| 159 | 6 | return; | |||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | # If it is a failed list, convert it to paragraph | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 2 | $self->{tag} = 'p'; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 2 | $self->{text} = join '', @{ $self->{list}{raw} }; | |||||||||||||||||||||||||||||||||||||||||
| 165 | 2 | delete $self->{list}; | |||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 36 | ▶ | if ($self->{tag}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 9 | $self->{text} =~ s/\n+\Z//; | |||||||||||||||||||||||||||||||||||||||||
| 170 | push @$entries, { | ||||||||||||||||||||||||||||||||||||||||||
| 171 | tag => $self->{tag}, | ||||||||||||||||||||||||||||||||||||||||||
| 172 | text => $self->{text}, | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 9 | }; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 9 | $self->{tag} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 175 | 9 | $self->{text} = ''; | |||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 36 | return; | |||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | __END__ | ||||||||||||||||||||||||||||||||||||||||||