From: Robert 'phaylon' Sedlacek Date: Tue, 18 Sep 2012 20:35:38 +0000 (+0000) Subject: handle newline and html break conversions in mediawiki publishing X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a57e879096529eac82243630265c9ef8c91b6ed8;hp=d636bbefc8890517e8f567297e5e567eeb6e1cb8;p=scpubgit%2FSystem-Introspector-Report.git handle newline and html break conversions in mediawiki publishing --- diff --git a/lib/System/Introspector/Report/Publish/MediaWiki/Parser.pm b/lib/System/Introspector/Report/Publish/MediaWiki/Parser.pm index 4932353..d1e1359 100644 --- a/lib/System/Introspector/Report/Publish/MediaWiki/Parser.pm +++ b/lib/System/Introspector/Report/Publish/MediaWiki/Parser.pm @@ -64,7 +64,13 @@ my $_collect_content = sub { my ($stream, $type) = @_; my @events; until ($stream->$_is_type(close => $type)) { - push @events, $stream->next; + my $next = $stream->next; + if ($next->{type} eq 'OPEN' and $next->{name} eq 'br') { + push @events, { type => 'TEXT', raw => "\n" }; + } + else { + push @events, $next; + } } return HTML::Zoom->from_events(\@events)->to_html; }; diff --git a/lib/System/Introspector/Report/Publish/MediaWiki/Producer.pm b/lib/System/Introspector/Report/Publish/MediaWiki/Producer.pm index f6c252c..db40895 100644 --- a/lib/System/Introspector/Report/Publish/MediaWiki/Producer.pm +++ b/lib/System/Introspector/Report/Publish/MediaWiki/Producer.pm @@ -44,6 +44,27 @@ sub _render_title { return sprintf '== %s ==', $report->{title}; } +my $_prepare_column_content = sub { + my ($content) = @_; + return '' + unless defined $content; + my @lines = split m{\n}, $content; + if (@lines == 1) { + return $content; + } + else { + my ($first, @rest) = @lines; + return HTML::Zoom->from_events([ + { type => 'TEXT', raw => $first }, + map { + my $string = $_; + ( @{ HTML::Zoom->from_html('
')->to_events }, + { type => 'TEXT', raw => $string }); + } @rest, + ]); + } +}; + sub _apply_table_body { my ($self, $markup, $report) = @_; my $index = 0; @@ -63,7 +84,7 @@ sub _apply_table_body { $_->apply_if($col !~ m{^__}, sub { $_->add_to_attribute('td', class => "si-column-$col"); }) - ->replace_content('td', defined($value) ? $value : '') + ->replace_content('td', $value->$_prepare_column_content) ->apply_if($col_idx != $#cols, sub { $_->add_after('td', "\n "); }); diff --git a/t/data/result/foo.txt b/t/data/result/foo.txt index a75fa43..61419aa 100644 --- a/t/data/result/foo.txt +++ b/t/data/result/foo.txt @@ -75,6 +75,11 @@ More text. 784 + + 989 + foo
bar + + diff --git a/t/publish_mediawiki.t b/t/publish_mediawiki.t index 97035ae..a328e8e 100644 --- a/t/publish_mediawiki.t +++ b/t/publish_mediawiki.t @@ -77,6 +77,7 @@ ok $wiki->publish([ rows => [ { bar => 232, baz => 884 }, { bar => 332, baz => 784 }, + { bar => 989, baz => "foo\nbar" }, ], }, ]), 'publish ok'; diff --git a/t/publish_mediawiki_parser.t b/t/publish_mediawiki_parser.t index e02c6b2..e795e6c 100644 --- a/t/publish_mediawiki_parser.t +++ b/t/publish_mediawiki_parser.t @@ -34,6 +34,11 @@ User description 172 173 + + foo
bar + bar
baz + 199 + @@ -74,6 +79,9 @@ do { is_deeply $report->{rows}[1], { bar => 232, baz => 422, __usercol_0 => 172, __usercol_orph_0 => 173, }, 'second row'; + is_deeply $report->{rows}[2], { + bar => "foo\nbar", baz => "bar\nbaz", __usercol_0 => 199, + }, 'third row'; }; is shift(@$stream), '', 'empty line';