From c5a0a068d16b61d373a4599652f92217b138b305 Mon Sep 17 00:00:00 2001 From: Zubrikhin Alexey Date: Tue, 14 Apr 2015 13:11:34 +1000 Subject: [PATCH] + Text::Dokuwiki->_parse_list --- lib/Text/Dokuwiki.pm | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/lib/Text/Dokuwiki.pm b/lib/Text/Dokuwiki.pm index 0ee0617..0e7b229 100644 --- a/lib/Text/Dokuwiki.pm +++ b/lib/Text/Dokuwiki.pm @@ -14,6 +14,44 @@ sub new { return bless($self, $class); } +sub _parse_list { + my ($self, $lines) = @_; + my @lists = (); + my @stack = ( \@lists ); + my $types = {curr => '', last => ''}; + my $level = {curr => 0, last => 0}; + + foreach my $line (@{ $lines }) { + $line =~ m/^(\s+)/o; + my ($ident, $dot, $rest) = ($line =~ m/^((?:\s{2})+)([\*-])\s*(.+)/); + $level->{last} = $level->{curr}; + $level->{curr} = $ident =~ tr/ / /; + + $types->{last} = $types->{curr}; + $types->{curr} = ($dot eq '-') ? 'ol' : 'ul'; + + if ($level->{curr} == $level->{last} and + $types->{curr} ne $types->{last}) { + pop @stack; + my $list = [$types->{curr} => {}]; + push @{ $stack[-1] }, (@stack > 1) ? [li => {}, $list] : $list; + push @stack, $list; + } + if ($level->{curr} > $level->{last}) { + my $list = [$types->{curr} => {}]; + push @{ $stack[-1] }, (@stack > 1) ? [li => {}, $list] : $list; + push @stack, $list; + } + if ($level->{curr} < $level->{last}) { + pop @stack; + } + push @{ $stack[-1] }, [li => {}, $rest]; + } + pop @stack while @stack; + + return @lists; +} + sub parse { my ($self, $text) = @_; my ($tree, $mode, $attrs, $buf) = ([], 'text', '', '');