Browse Source

* update main loop

master
Zubrikhin Alexey 10 years ago
parent
commit
3f9d37f56a
  1. 62
      lib/Text/Dokuwiki.pm

62
lib/Text/Dokuwiki.pm

@ -111,49 +111,60 @@ sub parse {
given ($mode) { given ($mode) {
when (m!block/(file|code|nowiki)!o) { when (m!block/(file|code|nowiki)!o) {
$buf //= [];
if ($line =~ m{^\s*</$1>}o) { if ($line =~ m{^\s*</$1>}o) {
$buf = join("\n", @{ $buf });
if ($1 eq 'file') { if ($1 eq 'file') {
my $dt = [dt => {}, 0 => $attrs->{file}]; my $dt = [dt => {}, $attrs->{file}];
my $dd = [dt => {}, 0 => [pre => {class => $attrs->{class}}, 0 => $buf]]; my $dd = [dt => {}, [pre => {class => $attrs->{class}}, $buf]];
push @{ $tree }, [dl => {class => 'file'}, [$dt, $dd]]; push @{ $tree }, [dl => {class => 'file'}, [$dt, $dd]];
} elsif ($1 eq 'nowiki') { } elsif ($1 eq 'nowiki') {
push @{ $tree }, [pre => {}, 0 => $buf]; push @{ $tree }, [pre => {}, $buf];
} else { } else {
push @{ $tree }, [code => {class => $attrs->{class}}, 0 => $buf]; push @{ $tree }, [code => {class => $attrs->{class}}, $buf];
} }
($buf, $mode, $attrs) = ('', '', {}); next; ($buf, $mode, $attrs) = ('', '', {}); next;
} }
$buf .= $line . "\n"; push @{ $buf }, $line;
next;
} }
when ("code") { when ("code") {
if ($line =~ m/^\s{2}(.+)/o) { if ($line =~ m/^\s{2}(.+)/o) {
$buf .= $line . "\n"; next; $buf .= $line . "\n";
} else { next;
push @{ $tree }, [pre => {}, 0 => $buf];
($buf, $mode, $attrs) = ('', '', {}); continue;
} }
push @{ $tree }, [pre => {}, $buf];
($buf, $mode, $attrs) = ('', '', {});
} }
when ("list") { when ("list") {
if ($line =~ m/^\s{2}([\*-])\s+(.+)/o) { if ($line =~ m/^(\s{2})+([\*-])\s+(.+)/o) {
push @{ $buf }, [li => {}, 0 => $2]; push @{ $buf }, $line;
next; next;
} else {
push @{ $tree }, [ul => {}, @$buf]; # TODO: lost list type
($buf, $mode, $attrs) = ('', '', {}); continue;
} }
push @{ $tree }, $self->_parse_list($buf);
($buf, $mode, $attrs) = ('', '', {});
} }
when ("table") { when ("table") {
... if ($line =~ m/^\s?[\|\^]/o) {
push @{ $buf }, $line;
next;
}
push @{ $tree }, $self->_parse_table($buf);
($buf, $mode, $attrs) = ('', '', {});
} }
} }
given ($line) { given ($line) {
# macro
when (m/~~NO(TOC|CACHE)~~/) {
next; # ignore
}
# header # header
when (m/^\s?(={2,6}) (.+) \g{1}\s*/o) { when (m/^\s?(={2,6}) (.+) \g{1}\s*/o) {
my $level = $1 =~ tr/=/=/; my $level = $1 =~ tr/=/=/;
$level = 7 - $level; # invert $level = 7 - $level; # invert
push @{ $tree }, ["h$level" => {}, 0 => $2]; push @{ $tree }, ["h$level" => {}, $2];
next; next;
} }
# code/file block # code/file block
@ -168,22 +179,23 @@ sub parse {
next; next;
} }
# lists # lists
when (m/^\s{2}([\*-])\s+(.+)/o) { when (m/^(\s{2})+([\*-])\s+(.+)/o) {
$mode = 'list'; $mode = 'list';
$buf = []; $buf = [];
push @{ $buf }, [li => {}, 0 => $2]; push @{ $buf }, $line;
next; next;
} }
# quotes # quotes
when (m/^\s?(>)+\s*(.+)/o) { when (m/^\s?(>)+\s*(.+)/o) {
my $level = $1 =~ tr/>/>/; my $level = $1 =~ tr/>/>/;
push @{ $tree }, [blockquote => {level => $level}, 0 => $2]; push @{ $tree }, [blockquote => {level => $level}, $2];
next; next;
} }
# table # table
when (m/^\s?[\|\^]/o) { when (m/^\s?[\|\^]/o) {
$mode = 'table'; $mode = 'table';
$buf = $line . "\n"; # render later $buf = [];
push @{ $buf }, $line;
next; next;
} }
# code idented with two spaces # code idented with two spaces
@ -194,7 +206,7 @@ sub parse {
} }
# nonempty line # nonempty line
when (m/^\s?(\S.+)/o) { when (m/^\s?(\S.+)/o) {
push @{ $tree }, [p => {}, 0 => $1]; push @{ $tree }, [p => {}, $1];
next; next;
} }
# empty lines; # empty lines;
@ -219,9 +231,9 @@ sub parse {
__END__ __END__
[div => {class => 'block'}, # <div class='block'> [div => {class => 'block'}, # <div class='block'>
0 => 'text1', # Hello! 'Hello!', # Hello!
[strong => {}, 0 => 'bold text'], # <strong>user</strong> [strong => {}, 'user'], # <strong>user</strong>
0 => ', this is converted text.', # , this is converted text. ', this is converted text.', # , this is converted text.
[br => {}], # <br/> [br => {}], # <br/>
[p => {}, 0 => 'Second paragraph'] # <p>Second paragraph</p> [p => {}, 'Second paragraph'] # <p>Second paragraph</p>
] # </div> ] # </div>

Loading…
Cancel
Save