From 625c441d4858880bba586e48512ce2959244063e Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Sat, 9 May 2015 19:41:57 +1000 Subject: [PATCH 1/6] * Text::Dokuwiki::Render : add default list level --- lib/Text/Dokuwiki/Render.pm | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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); } From 5bb0f5e229d8a396b8c7c7bdbcc16ab6f0796f40 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Sat, 9 May 2015 19:42:21 +1000 Subject: [PATCH 2/6] + Text::Dokuwiki::Render::Markdown --- lib/Text/Dokuwiki/Render/Markdown.pm | 75 ++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 lib/Text/Dokuwiki/Render/Markdown.pm diff --git a/lib/Text/Dokuwiki/Render/Markdown.pm b/lib/Text/Dokuwiki/Render/Markdown.pm new file mode 100644 index 0000000..9b476bc --- /dev/null +++ b/lib/Text/Dokuwiki/Render/Markdown.pm @@ -0,0 +1,75 @@ +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}) : ""; + + return sprintf("![%s](%s%s)", $attrs->{alt} // '', $attrs->{src}, $title); +} + +1; From 549758b23b9d02b19754e15ed302d8a61ddb3a74 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Sat, 9 May 2015 19:48:07 +1000 Subject: [PATCH 3/6] * Text::Dokuwiki::Parser->_parse_text : handle force newline --- lib/Text/Dokuwiki/Parser.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Text/Dokuwiki/Parser.pm b/lib/Text/Dokuwiki/Parser.pm index f7b3fc6..3cece54 100644 --- a/lib/Text/Dokuwiki/Parser.pm +++ b/lib/Text/Dokuwiki/Parser.pm @@ -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; @@ -178,7 +178,7 @@ sub _parse_text { } given ($match) { - when ('\\ ') { # force newline + when ('\\\\ ') { # force newline push @parts, [br => {}]; $line = $after; next; From 291014a7453e9645d263b3da9f8c4c749ad9bde3 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Sat, 9 May 2015 20:06:48 +1000 Subject: [PATCH 4/6] * Text::Dokuwiki::Parser : fix regex mod --- lib/Text/Dokuwiki/Parser.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Text/Dokuwiki/Parser.pm b/lib/Text/Dokuwiki/Parser.pm index 3cece54..e2fa3a0 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]; @@ -144,7 +144,7 @@ sub _parse_include { '' >ioe; - $src =~ s|:|/|o; + $src =~ s|:|/|go; $attrs{src} = $src; return [img => \%attrs]; From c55c2a1b5697f70b25c201077c50b4f682e569fe Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Sat, 9 May 2015 20:07:09 +1000 Subject: [PATCH 5/6] = Text::Dokuwiki::Parser->_parse_include --- lib/Text/Dokuwiki/Parser.pm | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/Text/Dokuwiki/Parser.pm b/lib/Text/Dokuwiki/Parser.pm index e2fa3a0..c91c794 100644 --- a/lib/Text/Dokuwiki/Parser.pm +++ b/lib/Text/Dokuwiki/Parser.pm @@ -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 '') From 187b4e2b6c0ee1601c4ea1bc2b10b933dc22cba1 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Sat, 9 May 2015 20:07:28 +1000 Subject: [PATCH 6/6] = Text::Dokuwiki::Render->img --- lib/Text/Dokuwiki/Render/Markdown.pm | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/Text/Dokuwiki/Render/Markdown.pm b/lib/Text/Dokuwiki/Render/Markdown.pm index 9b476bc..3c9df9f 100644 --- a/lib/Text/Dokuwiki/Render/Markdown.pm +++ b/lib/Text/Dokuwiki/Render/Markdown.pm @@ -67,9 +67,10 @@ sub p { sub img { my ($self, $attrs, $content) = @_; - my $title = ($attrs->{title}) ? qq( $attrs->{title}) : ""; + my $title = ($attrs->{title}) ? qq( "$attrs->{title}") : ""; + my $alt = $attrs->{alt} || $attrs->{title} || ''; - return sprintf("![%s](%s%s)", $attrs->{alt} // '', $attrs->{src}, $title); + return sprintf("![%s](%s%s)", $alt, $attrs->{src}, $title); } 1;