From 5bb0f5e229d8a396b8c7c7bdbcc16ab6f0796f40 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Sat, 9 May 2015 19:42:21 +1000 Subject: [PATCH] + 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;