| 1 | package Markdent::Dialect::Theory::BlockParser; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | our $VERSION = '0.39'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use List::AllUtils qw( insert_after_string sum ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Markdent::Event::StartTable; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Markdent::Event::EndTable; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Markdent::Event::StartTableHeader; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Markdent::Event::EndTableHeader; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Markdent::Event::StartTableBody; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use Markdent::Event::EndTableBody; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use Markdent::Event::StartTableRow; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use Markdent::Event::EndTableRow; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use Markdent::Event::StartTableCell; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use Markdent::Event::EndTableCell; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 4 | ▶ | use Markdent::Regexes qw( $HorizontalWS $EmptyLine $BlockStart $BlockEnd ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 4 | ▶ | use Markdent::Types; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 4 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 4 | with 'Markdent::Role::Dialect::BlockParser'; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 4 | has _in_table => ( | |||||||||||||||||||||||||||
| 28 | traits => ['Bool'], | ||||||||||||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||||||||||
| 30 | isa => t('Bool'), | ||||||||||||||||||||||||||||
| 31 | default => 0, | ||||||||||||||||||||||||||||
| 32 | init_arg => undef, | ||||||||||||||||||||||||||||
| 33 | handles => { | ||||||||||||||||||||||||||||
| 34 | _enter_table => 'set', | ||||||||||||||||||||||||||||
| 35 | _leave_table => 'unset', | ||||||||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | around _possible_block_matches => sub { | ||||||||||||||||||||||||||||
| 40 | 658 | ▶ | my $orig = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 41 | 658 | my $self = shift; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 658 | my @look_for = $self->$orig(); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 658 | ▶ | return @look_for if $self->_list_level; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 655 | ▶ | if ( $self->_in_table ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 626 | insert_after_string 'list', 'table_cell', @look_for; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | else { | ||||||||||||||||||||||||||||
| 51 | 29 | insert_after_string 'list', 'table', @look_for; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 655 | return @look_for; | |||||||||||||||||||||||||||
| 55 | 4 | }; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 4 | my $TableCaption = qr{ ^ | |||||||||||||||||||||||||||
| 58 | $HorizontalWS* | ||||||||||||||||||||||||||||
| 59 | \[ | ||||||||||||||||||||||||||||
| 60 | (.*) | ||||||||||||||||||||||||||||
| 61 | \] | ||||||||||||||||||||||||||||
| 62 | $HorizontalWS* | ||||||||||||||||||||||||||||
| 63 | \n | ||||||||||||||||||||||||||||
| 64 | }xm; | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # The use of (?> ... ) in the various regexes below forces the regex engine | ||||||||||||||||||||||||||||
| 67 | # not to backtrack once it matches the relevant subsection. Using this where | ||||||||||||||||||||||||||||
| 68 | # possible _hugely_ speeds up matching, and seems to be safe. At least, the | ||||||||||||||||||||||||||||
| 69 | # tests pass. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 4 | my $PipeRow = qr{ ^ | |||||||||||||||||||||||||||
| 72 | [|]? # optional starting pipe | ||||||||||||||||||||||||||||
| 73 | (?: | ||||||||||||||||||||||||||||
| 74 | (?: | ||||||||||||||||||||||||||||
| 75 | (?>[^\|\\\n]*) # safe chars (not pipe or escape or newline) | ||||||||||||||||||||||||||||
| 76 | | | ||||||||||||||||||||||||||||
| 77 | \\[|] # an escaped newline | ||||||||||||||||||||||||||||
| 78 | )+ | ||||||||||||||||||||||||||||
| 79 | [|] # must have at least one pipe | ||||||||||||||||||||||||||||
| 80 | )+ | ||||||||||||||||||||||||||||
| 81 | .* # can have a final cell after the last pipe | ||||||||||||||||||||||||||||
| 82 | }xm; | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 4 | my $ColonRow = qr{ ^ | |||||||||||||||||||||||||||
| 85 | :? | ||||||||||||||||||||||||||||
| 86 | (?: | ||||||||||||||||||||||||||||
| 87 | (?: | ||||||||||||||||||||||||||||
| 88 | (?>[^:\\\n]*) | ||||||||||||||||||||||||||||
| 89 | | | ||||||||||||||||||||||||||||
| 90 | \\: | ||||||||||||||||||||||||||||
| 91 | )+ | ||||||||||||||||||||||||||||
| 92 | : | ||||||||||||||||||||||||||||
| 93 | )+ | ||||||||||||||||||||||||||||
| 94 | .* | ||||||||||||||||||||||||||||
| 95 | }xm; | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 4 | my $TableRow = qr{ (?>$PipeRow) # must have at least one starting row | |||||||||||||||||||||||||||
| 98 | \n | ||||||||||||||||||||||||||||
| 99 | (?> | ||||||||||||||||||||||||||||
| 100 | (?: | ||||||||||||||||||||||||||||
| 101 | $ColonRow | ||||||||||||||||||||||||||||
| 102 | \n | ||||||||||||||||||||||||||||
| 103 | )* # ... can have 0+ continuation lines | ||||||||||||||||||||||||||||
| 104 | ) | ||||||||||||||||||||||||||||
| 105 | }xm; | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 4 | my $HeaderMarkerLine = qr/^[\-\+=]+\n/xm; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 4 | my $TableHeader = qr{ $TableRow | |||||||||||||||||||||||||||
| 110 | $HeaderMarkerLine | ||||||||||||||||||||||||||||
| 111 | }xm; | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) | ||||||||||||||||||||||||||||
| 114 | sub _match_table { | ||||||||||||||||||||||||||||
| 115 | 29 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_match_table: called CC=16 SCAR=27.9
| |||||||||||||||||||||||||||||
| 116 | 29 | my $text = shift; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 29 | ▶ | return unless ${$text} =~ / \G | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | $BlockStart | ||||||||||||||||||||||||||||
| 120 | ( | ||||||||||||||||||||||||||||
| 121 | $TableCaption? | ||||||||||||||||||||||||||||
| 122 | $HeaderMarkerLine? | ||||||||||||||||||||||||||||
| 123 | ($TableHeader+)? | ||||||||||||||||||||||||||||
| 124 | ( | ||||||||||||||||||||||||||||
| 125 | $TableRow | ||||||||||||||||||||||||||||
| 126 | (?: | ||||||||||||||||||||||||||||
| 127 | $TableRow | ||||||||||||||||||||||||||||
| 128 | | | ||||||||||||||||||||||||||||
| 129 | $EmptyLine | ||||||||||||||||||||||||||||
| 130 | )* | ||||||||||||||||||||||||||||
| 131 | ) | ||||||||||||||||||||||||||||
| 132 | $HeaderMarkerLine? | ||||||||||||||||||||||||||||
| 133 | $TableCaption? | ||||||||||||||||||||||||||||
| 134 | ) | ||||||||||||||||||||||||||||
| 135 | $BlockEnd | ||||||||||||||||||||||||||||
| 136 | /xmgc; | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 21 | ▶ | $self->_debug_parse_result( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | $1, | ||||||||||||||||||||||||||||
| 140 | 'table', | ||||||||||||||||||||||||||||
| 141 | ) if $self->debug; | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 21 | ▶ | my $caption = defined $2 ? $2 : $5; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 21 | ▶ | $self->_debug_parse_result( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $caption and $self->debug
MC/DC0%0/2
defined $caption and $self->debug
defined $caption
$self->debug
Truth table66%2/3
A and B
A= defined $captionB= $self->debug
| |||||||||||||||||||||||||||||
| 146 | $caption, | ||||||||||||||||||||||||||||
| 147 | 'table caption', | ||||||||||||||||||||||||||||
| 148 | ) if defined $caption && $self->debug; | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 21 | my $header = $3; | |||||||||||||||||||||||||||
| 151 | 21 | my $body = $4; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 21 | ▶ | $self->_debug_parse_result( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | $header, | ||||||||||||||||||||||||||||
| 155 | 'table header', | ||||||||||||||||||||||||||||
| 156 | ) if $self->debug; | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 21 | ▶ | $self->_debug_parse_result( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | $body, | ||||||||||||||||||||||||||||
| 160 | 'table body', | ||||||||||||||||||||||||||||
| 161 | ) if $self->debug; | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 21 | my @header; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 21 | ▶ | if ( defined $header ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 17 | @header = $self->_parse_rows( qr/$HeaderMarkerLine/m, $header ); | |||||||||||||||||||||||||||
| 167 | 17 | $_->{is_header_cell} = 1 for map { @{$_} } @header; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 21 | my @body = $self->_parse_rows( qr/\n/, $body ); | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 21 | $self->_normalize_cell_count_and_alignments( @header, @body ); | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 21 | ▶ | if (@header) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 17 | my $first_header_cell_content = $header[0][0]{content}; | |||||||||||||||||||||||||||
| 176 | 17 | ▶ | unless ( defined $first_header_cell_content | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $first_header_cell_content and $first_header_cell_content =~ /\S/
MC/DC50%1/2
defined $first_header_cell_content and $first_header_cell_content =~ /\S/
defined $first_header_cell_content
$first_header_cell_content =~ /\S/
Truth table66%2/3
A and B
A= defined $first_header_cell_contentB= $first_header_cell_content =~ /\S/
| |||||||||||||||||||||||||||||
| 177 | && $first_header_cell_content =~ /\S/ ) { | ||||||||||||||||||||||||||||
| 178 | 1 | $_->[0]{is_header_cell} = 1 for @body; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 21 | $self->_enter_table; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 21 | ▶ | my %caption = defined $caption ? ( caption => $caption ) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 21 | $self->_send_event( 'StartTable', %caption ); | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 21 | ▶ | $self->_events_for_rows( \@header, 'Header' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | if @header; | ||||||||||||||||||||||||||||
| 189 | 21 | $self->_events_for_rows( \@body, 'Body' ); | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 21 | $self->_send_event('EndTable'); | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 21 | $self->_leave_table; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 21 | return 1; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | ## use critic | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | sub _parse_rows { | ||||||||||||||||||||||||||||
| 200 | 38 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_parse_rows: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||
| 201 | 38 | my $split_re = shift; | |||||||||||||||||||||||||||
| 202 | 38 | my $rows = shift; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 38 | my @rows; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 38 | for my $chunk ( split $split_re, $rows ) { | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | # Splitting on an empty string returns nothing, so we need to | ||||||||||||||||||||||||||||
| 209 | # special-case that, as we want to preserve empty lines. | ||||||||||||||||||||||||||||
| 210 | 168 | ▶ | for my $line ( length $chunk ? ( split /\n/, $chunk ) : $chunk ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 168 | ▶ | if ( $line =~ /^$HorizontalWS*$/ ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 212 | 2 | push @rows, undef; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | elsif ( $self->_is_continuation_line($line) ) { | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | # If the $TableRow regex is correct, this shouldn't be | ||||||||||||||||||||||||||||
| 217 | # possible. | ||||||||||||||||||||||||||||
| 218 | 7 | ▶ | die q{Continuation of a row before we've seen a row start?!} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | unless @rows; | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 7 | my $cells = $self->_cells_from_line( $line, ':' ); | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 7 | for my $i ( 0 .. $#{$cells} ) { | |||||||||||||||||||||||||||
| 224 | 15 | ▶ | if ( defined $cells->[$i]{content} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $cells->[$i]{"content"} and $cells->[$i]{"content"} =~ /\S/
MC/DC50%1/2
defined $cells->[$i]{"content"} and $cells->[$i]{"content"} =~ /\S/
defined $cells->[$i]{"content"}
$cells->[$i]{"content"} =~ /\S/
Truth table66%2/3
A and B
A= defined $cells->[$i]{"content"}B= $cells->[$i]{"content"} =~ /\S/
| |||||||||||||||||||||||||||||
| 225 | && $cells->[$i]{content} =~ /\S/ ) { | ||||||||||||||||||||||||||||
| 226 | $rows[-1][$i]{content} | ||||||||||||||||||||||||||||
| 227 | 9 | .= "\n" . $cells->[$i]{content}; | |||||||||||||||||||||||||||
| 228 | 9 | ▶ | $rows[-1][$i]{colspan} ||= 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$rows[-1][$i]{"colspan"} ||= 1
MC/DC100%1/1
$rows[-1][$i]{"colspan"} ||= 1
$rows[-1][$i]{"colspan"}
Truth table100%2/2
A
A= $rows[-1][$i]{"colspan"}
| |||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | else { | ||||||||||||||||||||||||||||
| 233 | 159 | push @rows, $self->_cells_from_line( $line, '|' ); | |||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 38 | return @rows; | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | sub _is_continuation_line { | ||||||||||||||||||||||||||||
| 242 | 166 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_is_continuation_line: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 243 | 166 | my $line = shift; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 166 | ▶ | return 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | if $line =~ /(?<!\\)[|]/x; | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 7 | ▶ | return 1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 249 | if $line =~ /(^|\p{SpaceSeparator}+)(?<!\\):(\p{SpaceSeparator}|$)/x; | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | # a blank line, presumably | ||||||||||||||||||||||||||||
| 252 | 0 | return 0; | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | sub _cells_from_line { | ||||||||||||||||||||||||||||
| 256 | 166 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_cells_from_line: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 257 | 166 | my $line = shift; | |||||||||||||||||||||||||||
| 258 | 166 | my $div = shift; | |||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 166 | my @row; | |||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 166 | for my $cell ( $self->_split_cells( $line, $div ) ) { | |||||||||||||||||||||||||||
| 263 | 657 | ▶ | if ( length $cell ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 264 | 642 | push @row, $self->_cell_params($cell); | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | # If the first cell is empty, that means the line started with a | ||||||||||||||||||||||||||||
| 268 | # divider, and we can ignore the "cell". If we already have cells in | ||||||||||||||||||||||||||||
| 269 | # the row, that means we just saw a repeated divider, which means the | ||||||||||||||||||||||||||||
| 270 | # most recent cell has a colspan+1. | ||||||||||||||||||||||||||||
| 271 | elsif (@row) { | ||||||||||||||||||||||||||||
| 272 | 15 | $row[-1]{colspan}++; | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 166 | return \@row; | |||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | sub _split_cells { | ||||||||||||||||||||||||||||
| 280 | 166 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_split_cells: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 281 | 166 | my $line = shift; | |||||||||||||||||||||||||||
| 282 | 166 | my $div = shift; | |||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 166 | $line =~ s/^\Q$div//; | |||||||||||||||||||||||||||
| 285 | 166 | $line =~ s/\Q$div\E$HorizontalWS*$/$div/; | |||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | # We don't want to split on a backslash-escaped divider, thus the | ||||||||||||||||||||||||||||
| 288 | # lookbehind. The -1 ensures that Perl gives us the trailing empty fields. | ||||||||||||||||||||||||||||
| 289 | 166 | my @cells = split /(?<!\\)\Q$div/, $line, -1; | |||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | # If the line has just one divider as the line-ending, it should not be | ||||||||||||||||||||||||||||
| 292 | # treated as marking an empty cell. | ||||||||||||||||||||||||||||
| 293 | 166 | ▶ | if ( $cells[-1] eq q{} && $line =~ /\Q$div\E$HorizontalWS*$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$cells[-1] eq "" and $line =~ /\Q$div\E$Markdent::Dialect::Theory::BlockParser::HorizontalWS*$/
MC/DC50%1/2
$cells[-1] eq "" and $line =~ /\Q$div\E$Markdent::Dialect::Theory::BlockParser::HorizontalWS*$/
$cells[-1] eq ""
$line =~ /\Q$div\E$Markdent::Dialect::Theory::BlockParser::HorizontalWS*$/
Truth table66%2/3
A and B
A= $cells[-1] eq ""B= $line =~ /\Q$div\E$Markdent::Dialect::Theory::BlockParser::HorizontalWS*$/
| |||||||||||||||||||||||||||||
| 294 | 156 | pop @cells; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 166 | return @cells; | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | sub _cell_params { | ||||||||||||||||||||||||||||
| 301 | 642 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_cell_params: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 302 | 642 | my $cell = shift; | |||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 642 | my $alignment; | |||||||||||||||||||||||||||
| 305 | my $content; | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 642 | ▶ | if ( defined $cell && $cell =~ /\S/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $cell and $cell =~ /\S/
MC/DC50%1/2
defined $cell and $cell =~ /\S/
defined $cell
$cell =~ /\S/
Truth table66%2/3
A and B
A= defined $cellB= $cell =~ /\S/
| |||||||||||||||||||||||||||||
| 308 | 633 | $alignment = $self->_alignment_for_cell($cell); | |||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | 633 | ( $content = $cell ) =~ s/^$HorizontalWS+|$HorizontalWS+$//g; | |||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 642 | my %p = ( | |||||||||||||||||||||||||||
| 314 | colspan => 1, | ||||||||||||||||||||||||||||
| 315 | content => $content, | ||||||||||||||||||||||||||||
| 316 | ); | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 642 | ▶ | $p{alignment} = $alignment | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 319 | if defined $alignment; | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 642 | return \%p; | |||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | sub _alignment_for_cell { | ||||||||||||||||||||||||||||
| 325 | 633 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_alignment_for_cell: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 326 | 633 | my $cell = shift; | |||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | 633 | ▶ | return 'center' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 329 | if $cell =~ /^\p{SpaceSeparator}{2,}.+?\p{SpaceSeparator}{2,}$/; | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 632 | ▶ | return 'left' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 332 | if $cell =~ /\p{SpaceSeparator}{2,}$/; | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 151 | ▶ | return 'right' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | if $cell =~ /^\p{SpaceSeparator}{2,}/; | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | 148 | return; | |||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | sub _normalize_cell_count_and_alignments { | ||||||||||||||||||||||||||||
| 341 | 21 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_normalize_cell_count_and_alignments: called CC=7 SCAR=19.7
| |||||||||||||||||||||||||||||
| 342 | 21 | my @rows = @_; | |||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | # We use the first header row as an indicator for how many cells we expect | ||||||||||||||||||||||||||||
| 345 | # on each line. | ||||||||||||||||||||||||||||
| 346 | 21 | my $default_cells = sum( map { $_->{colspan} } @{ $rows[0] } ); | |||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | # Alignments are inherited from the cell above, or they default to | ||||||||||||||||||||||||||||
| 349 | # "left". We loop through all the rules and set alignments accordingly. | ||||||||||||||||||||||||||||
| 350 | 21 | my %alignments; | |||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | 21 | for my $row ( grep {defined} @rows ) { | |||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | # If we have one extra column and the final cell has a colspan > 1 it | ||||||||||||||||||||||||||||
| 355 | # means we misinterpreted a trailing divider as indicating that the | ||||||||||||||||||||||||||||
| 356 | # prior cell had a colspan > 1. We adjust for that by comparing it to | ||||||||||||||||||||||||||||
| 357 | # the number of columns in the first row. | ||||||||||||||||||||||||||||
| 358 | 159 | ▶ | if ( sum( map { $_->{colspan} } @{$row} ) == $default_cells + 1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
&sum(map({$_->{"colspan"};} @{$row;})) == $default_cells + 1 and $row->[-1]{"colspan"} > 1
MC/DC0%0/2
&sum(map({$_->{"colspan"};} @{$row;})) == $default_cells + 1 and $row->[-1]{"colspan"} > 1
&sum(map({$_->{"colspan"};} @{$row;})) == $default_cells + 1
$row->[-1]{"colspan"} > 1
Truth table33%1/3
A and B
A= &sum(map({$_->{"colspan"};} @{$row;})) == $default_cells + 1B= $row->[-1]{"colspan"} > 1
| |||||||||||||||||||||||||||||
| 359 | && $row->[-1]{colspan} > 1 ) { | ||||||||||||||||||||||||||||
| 360 | 0 | $row->[-1]{colspan}--; | |||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 159 | my $i = 0; | |||||||||||||||||||||||||||
| 364 | 159 | for my $cell ( @{$row} ) { | |||||||||||||||||||||||||||
| 365 | 628 | ▶ | if ( $cell->{alignment} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | 480 | $alignments{$i} = $cell->{alignment}; | |||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||
| 368 | else { | ||||||||||||||||||||||||||||
| 369 | 148 | ▶ | $cell->{alignment} = $alignments{$i} || 'left'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$alignments{$i} || 'left'
MC/DC100%1/1
$alignments{$i} || 'left'
$alignments{$i}
Truth table100%2/2
A
A= $alignments{$i}
| |||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | 628 | $i += $cell->{colspan}; | |||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | sub _events_for_rows { | ||||||||||||||||||||||||||||
| 378 | 38 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_events_for_rows: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 379 | 38 | my $rows = shift; | |||||||||||||||||||||||||||
| 380 | 38 | my $type = shift; | |||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | 38 | my $start = 'StartTable' . $type; | |||||||||||||||||||||||||||
| 383 | 38 | my $end = 'EndTable' . $type; | |||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | 38 | $self->_send_event($start); | |||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | 38 | for my $row ( @{$rows} ) { | |||||||||||||||||||||||||||
| 388 | 161 | ▶ | if ( !defined $row ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 389 | 2 | $self->_send_event($end); | |||||||||||||||||||||||||||
| 390 | 2 | $self->_send_event($start); | |||||||||||||||||||||||||||
| 391 | 2 | next; | |||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | 159 | $self->_send_event('StartTableRow'); | |||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | 159 | for my $cell ( @{$row} ) { | |||||||||||||||||||||||||||
| 397 | 628 | my $content = delete $cell->{content}; | |||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | $self->_send_event( | ||||||||||||||||||||||||||||
| 400 | 'StartTableCell', | ||||||||||||||||||||||||||||
| 401 | 628 | %{$cell} | |||||||||||||||||||||||||||
| 402 | ); | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 628 | ▶ | if ( defined $content ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | # If the content has newlines, it should be matched as a | ||||||||||||||||||||||||||||
| 407 | # block-level construct (blockquote, list, etc), but to make | ||||||||||||||||||||||||||||
| 408 | # that work, it has to have a trailing newline. | ||||||||||||||||||||||||||||
| 409 | 626 | ▶ | $content .= "\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 410 | if $content =~ /\n/; | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | 626 | $self->_parse_text( \$content ); | |||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | $self->_send_event( | ||||||||||||||||||||||||||||
| 416 | 'EndTableCell', | ||||||||||||||||||||||||||||
| 417 | is_header_cell => $cell->{is_header_cell}, | ||||||||||||||||||||||||||||
| 418 | 628 | ); | |||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | 159 | $self->_send_event('EndTableRow'); | |||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | 38 | $self->_send_event($end); | |||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | # A table cell's contents can be a single line _not_ terminated by a | ||||||||||||||||||||||||||||
| 428 | # newline. If that's the case, it won't match as a paragraph. | ||||||||||||||||||||||||||||
| 429 | # | ||||||||||||||||||||||||||||
| 430 | ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) | ||||||||||||||||||||||||||||
| 431 | sub _match_table_cell { | ||||||||||||||||||||||||||||
| 432 | 625 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_match_table_cell: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 433 | 625 | my $text = shift; | |||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 625 | ▶ | return unless ${$text} =~ / \G | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 436 | ( | ||||||||||||||||||||||||||||
| 437 | ^ | ||||||||||||||||||||||||||||
| 438 | \p{SpaceSeparator}* | ||||||||||||||||||||||||||||
| 439 | \S | ||||||||||||||||||||||||||||
| 440 | .* | ||||||||||||||||||||||||||||
| 441 | ) | ||||||||||||||||||||||||||||
| 442 | \z | ||||||||||||||||||||||||||||
| 443 | /xmgc; | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | 620 | ▶ | $self->_debug_parse_result( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 446 | $1, | ||||||||||||||||||||||||||||
| 447 | 'table cell', | ||||||||||||||||||||||||||||
| 448 | ) if $self->debug; | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | 620 | $self->_span_parser->parse_block($1); | |||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||
| 452 | ## use critic | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | 4 | 1; | |||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | # ABSTRACT: Block parser for Theory's proposed Markdown extensions | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | __END__ | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | =pod | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | =head1 NAME | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | Markdent::Dialect::Theory::BlockParser - Block parser for Theory's proposed Markdown extensions | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | =head1 VERSION | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | version 0.39 | ||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | This role adds parsing for Markdown extensions proposed by David Wheeler (aka | ||||||||||||||||||||||||||||
| 475 | Theory). See | ||||||||||||||||||||||||||||
| 476 | L<http://justatheory.com/computers/markup/markdown-table-rfc.html> and | ||||||||||||||||||||||||||||
| 477 | L<http://justatheory.com/computers/markup/modest-markdown-proposal.html> for | ||||||||||||||||||||||||||||
| 478 | details. | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | For now, this role handles tables only. | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | This role should be applied to L<Markdent::Parser::BlockParser> class or a | ||||||||||||||||||||||||||||
| 483 | subclass of that class. | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | =head1 ROLES | ||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | This role does the L<Markdent::Role::Dialect::BlockParser> role. | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | =head1 BUGS | ||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | See L<Markdent> for bug reporting details. | ||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | Bugs may be submitted at L<https://github.com/houseabsolute/Markdent/issues>. | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>. | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | =head1 SOURCE | ||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | The source code repository for Markdent can be found at L<https://github.com/houseabsolute/Markdent>. | ||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | Dave Rolsky <autarch@urth.org> | ||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | This software is copyright (c) 2021 by Dave Rolsky. | ||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 510 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | The full text of the license can be found in the | ||||||||||||||||||||||||||||
| 513 | F<LICENSE> file included with this distribution. | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | =cut | ||||||||||||||||||||||||||||