|
|
@ -52,6 +52,53 @@ sub _parse_list { |
|
|
|
return @lists; |
|
|
|
return @lists; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub _parse_table { |
|
|
|
|
|
|
|
my ($self, $lines) = @_; |
|
|
|
|
|
|
|
my ($i, $j, $colspan) = (0, 0, 0); |
|
|
|
|
|
|
|
my @rows = (); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach my $line (@{ $lines }) { |
|
|
|
|
|
|
|
my @row = (); |
|
|
|
|
|
|
|
for ($j = 0; ;$j++) { |
|
|
|
|
|
|
|
last if ($line eq '|' or $line eq '^'); |
|
|
|
|
|
|
|
next if ($line !~ m/^(([\|\^])([^\|\^]*))/o); |
|
|
|
|
|
|
|
my $attrs = {}; |
|
|
|
|
|
|
|
my ($all, $key, $value) = ($1, $2, $3); |
|
|
|
|
|
|
|
$line = substr($line, length($all)); |
|
|
|
|
|
|
|
my $type = ($key eq '^') ? 'th' : 'td'; |
|
|
|
|
|
|
|
my ($lpad, $content, $rpad) = ($value =~ m/^(\s*)(.*?)(\s*)$/o); |
|
|
|
|
|
|
|
if ($line =~ m/^([\|\^]{2,})/o) { |
|
|
|
|
|
|
|
# colspan detected; |
|
|
|
|
|
|
|
$attrs->{colspan} = $colspan = length($1); |
|
|
|
|
|
|
|
} elsif (index($content, ":::") >= 0) { |
|
|
|
|
|
|
|
# rowspan detected |
|
|
|
|
|
|
|
for (my $k = $i - 1; $k >= 0; $k--) { # k is idx of prev rows |
|
|
|
|
|
|
|
my $cellptr = $rows[$k][$j + 2]; # +2 for (tr => {}) |
|
|
|
|
|
|
|
next if (index($cellptr->[2], ":::") >= 0); # also rowspan |
|
|
|
|
|
|
|
$cellptr->[1]->{rowspan} //= 1; # init attr, if missing |
|
|
|
|
|
|
|
$cellptr->[1]->{rowspan} += 1; # incr value |
|
|
|
|
|
|
|
last; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$attrs->{skip} = 1; |
|
|
|
|
|
|
|
} elsif ($colspan > 1) { |
|
|
|
|
|
|
|
# colspan-eaten column |
|
|
|
|
|
|
|
$attrs->{skip} = 1; |
|
|
|
|
|
|
|
$colspan -= 1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if ($lpad eq '') { |
|
|
|
|
|
|
|
$attrs->{align} = ($rpad eq '') ? 'center' : 'left'; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$attrs->{align} = ($rpad eq '') ? 'right' : 'center'; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
push @row, [$type => $attrs, $content]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
push @rows, [tr => {}, @row]; |
|
|
|
|
|
|
|
$i++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return [table => {}, @rows]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sub parse { |
|
|
|
sub parse { |
|
|
|
my ($self, $text) = @_; |
|
|
|
my ($self, $text) = @_; |
|
|
|
my ($tree, $mode, $attrs, $buf) = ([], 'text', '', ''); |
|
|
|
my ($tree, $mode, $attrs, $buf) = ([], 'text', '', ''); |
|
|
|