From f8607a9d9c13087014b5a9e4d86f389f63c1626e Mon Sep 17 00:00:00 2001 From: Zubrikhin Alexey Date: Tue, 14 Apr 2015 13:11:47 +1000 Subject: [PATCH] + Text::Dokuwiki->_parse_table --- lib/Text/Dokuwiki.pm | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/lib/Text/Dokuwiki.pm b/lib/Text/Dokuwiki.pm index 0e7b229..6e67f15 100644 --- a/lib/Text/Dokuwiki.pm +++ b/lib/Text/Dokuwiki.pm @@ -52,6 +52,53 @@ sub _parse_list { 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 { my ($self, $text) = @_; my ($tree, $mode, $attrs, $buf) = ([], 'text', '', '');