diff --git a/lib/Text/Dokuwiki/Parser.pm b/lib/Text/Dokuwiki/Parser.pm index e88880a..7cffbd0 100644 --- a/lib/Text/Dokuwiki/Parser.pm +++ b/lib/Text/Dokuwiki/Parser.pm @@ -110,7 +110,7 @@ sub _parse_link { } unless ($href =~ m!^[a-z]+://!io) { - $href =~ s{:}{/}oi; + $href =~ s{:}{/}go; } return [a => {href => $href}, @text]; @@ -119,18 +119,18 @@ sub _parse_link { sub _parse_include { my ($self, $content) = @_; - if ($content =~ m|{{([a-z]+)>(.*)}}|oi) { + if ($content =~ m|^([a-z]+)>(.*)|oi) { ... } my ($lpad, $rpad, $src, %attrs) = ('', '', ''); - if ($content =~ m!^{{(\s*)([^\s\|]+)(\s*)\|(.*)}}!oi) { + if ($content =~ m!^(\s*)([^\s\|]+)(\s*)[|](.*)!oi) { ($lpad, $src, $rpad) = ($1, $2, $3); $attrs{title} = $4; - } elsif ($content =~ m!^{{(\s*)(\S+)(\s*)}}!) { + } elsif ($content =~ m!^(\s*)(\S+)(\s*)!) { ($lpad, $src, $rpad) = ($1, $2, $3); } else { - ($src) = ($content =~ s/{{(.+)}}/$1/or); + ($src) = ($content =~ s/^(.+)/$1/or); } $attrs{align} = ($lpad ne '') @@ -144,7 +144,7 @@ sub _parse_include { '' >ioe; - $src =~ s|:|/|o; + $src =~ s|:|/|go; $attrs{src} = $src; return [img => \%attrs]; @@ -165,7 +165,7 @@ sub _parse_text { my @parts = (); while ($line) { - $line =~ m!^(?:(.*?)(__|//|''|\*\*|\[\[|\{\{|\(\())?(.*)!o; + $line =~ m!^(?:(.*?)(__|//|''|\*\*|\[\[|\{\{|\(\(|\\\\ ))?(.*)!o; my ($before, $match, $after) = ($1, $2, $3); if ($before) { push @parts, $before; @@ -177,7 +177,7 @@ sub _parse_text { last; } - if ($match eq '\\ ') { # force newline + if ($match eq '\\\\ ') { # force newline push @parts, [br => {}]; $line = $after; next; diff --git a/lib/Text/Dokuwiki/Render.pm b/lib/Text/Dokuwiki/Render.pm index 17c7272..16a1aab 100644 --- a/lib/Text/Dokuwiki/Render.pm +++ b/lib/Text/Dokuwiki/Render.pm @@ -6,7 +6,9 @@ use utf8; sub new { my ($class) = @_; - my $self = {}; + my $self = { + list_depth => 0, + }; return bless($self, $class); } diff --git a/lib/Text/Dokuwiki/Render/Markdown.pm b/lib/Text/Dokuwiki/Render/Markdown.pm new file mode 100644 index 0000000..3c9df9f --- /dev/null +++ b/lib/Text/Dokuwiki/Render/Markdown.pm @@ -0,0 +1,76 @@ +package Text::Dokuwiki::Render::Markdown; + +use strict; +use warnings; +use utf8; + +use base 'Text::Dokuwiki::Render'; +use Data::Dumper; + +sub _header { + my ($self, $tag, $attrs, $content) = @_; + my $title = $content; + utf8::decode($title); + + my $text = $content . "\n"; + $text .= (($tag eq 'h1') ? "=" : "-") x length($title); + $text .= "\n\n"; + + return $text; +} + +sub h1 { my $self = shift; $self->_header('h1', @_); }; +sub h2 { my $self = shift; $self->_header('h2', @_); }; +sub h3 { my $self = shift; $self->_header('h3', @_); }; +sub h4 { my $self = shift; $self->_header('h4', @_); }; +sub h5 { my $self = shift; $self->_header('h5', @_); }; +sub h6 { my $self = shift; $self->_header('h6', @_); }; + +sub div { + my ($self, $attrs, $content) = @_; + return $content; +} + +sub br { return "\n"; }; + +sub ul { + my ($self, $attrs, $content) = @_; + + $self->{list_depth} += 1; + my $text = $self->treewalk($content); + $self->{list_depth} -= 1; + + return $text; +} + +sub li { + my ($self, $attrs, $content) = @_; + my $text = " " x $self->{list_depth}; + $text .= "* "; + $text .= $self->treewalk($content); + $text .= "\n"; + + return $text; +} + +sub a { + my ($self, $attrs, $content) = @_; + + return sprintf("[%s](%s)", $self->treewalk($content), $attrs->{href}); +} + +sub p { + my ($self, $attrs, $content) = @_; + + return $self->treewalk($content) . "\n"; +} + +sub img { + my ($self, $attrs, $content) = @_; + my $title = ($attrs->{title}) ? qq( "$attrs->{title}") : ""; + my $alt = $attrs->{alt} || $attrs->{title} || ''; + + return sprintf("![%s](%s%s)", $alt, $attrs->{src}, $title); +} + +1;