fixed line-break transformation
[scpubgit/System-Introspector-Report.git] / lib / System / Introspector / Report / Publish / MediaWiki / Producer.pm
index fa43a43..e5861ab 100644 (file)
@@ -4,9 +4,15 @@ use HTML::Zoom;
 
 sub render {
   my ($self, $stream) = @_;
-  return join "\n", map {
+  return $self->_clear_body(join "\n", map {
     ref($_) ? $self->_render_table($_) : $_;
-  } @$stream;
+  } @$stream);
+}
+
+sub _clear_body {
+  my ($self, $string) = @_;
+  $string =~ s{\n\n+}{\n\n}g;
+  return $string;
 }
 
 sub _wrap {
@@ -23,7 +29,6 @@ sub _render_table {
   my $id     = $report->{id};
   my $str_id = ref($id) ? join(':', @$id) : $id;
   my $markup = $self->_load_markup;
-  $markup = $self->_apply_identity($markup, $report);
   $markup = $self->_apply_table_head($markup, $report);
   $markup = $self->_apply_table_body($markup, $report);
   my $description = $report->{description} || [''];
@@ -39,15 +44,28 @@ sub _render_title {
   return sprintf '== %s ==', $report->{title};
 }
 
-sub _apply_identity {
-  my ($self, $markup, $report) = @_;
-  my $id = $report->{id};
-  return $markup
-    ->select('table')
-    ->set_attribute(id => 'si-id-' . join '__',
-      ref($id) ? @$id : $id,
-    );
-}
+my $_prepare_column_content = sub {
+  my ($content) = @_;
+  return ''
+    unless defined $content;
+  my @lines = split qr{\n}, $content;
+  return ''
+    unless @lines;
+  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('<br/>')->to_events },
+          { type => 'TEXT', raw => $string });
+      } @rest,
+    ]);
+  }
+};
 
 sub _apply_table_body {
   my ($self, $markup, $report) = @_;
@@ -68,7 +86,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    ");
                 });
@@ -77,9 +95,15 @@ sub _apply_table_body {
         };
       },
     });
-  });
+  })->memoize;
 }
 
+my $_trim = sub {
+  my $string = shift;
+  $string =~ s{(?:^\s+|\s+$)}{}g;
+  return $string;
+};
+
 sub _apply_table_head {
   my ($self, $markup, $report) = @_;
   my @cols = @{$report->{columns}};
@@ -87,23 +111,27 @@ sub _apply_table_head {
     ->repeat('th', [map {
       my $col_idx = $_;
       my $col = $cols[$_];
+      my $label = $col->{label};
       sub {
         $_->apply_if($col->{key} !~ m{^__}, sub {
             $_->add_to_attribute('th', class => 'si-colhead-' . $col->{key});
           })
-          ->replace_content('th', $col->{label})
+          ->replace_content('th', defined($label) ? $label->$_trim : 'Unnamed')
           ->apply_if($col_idx != $#cols, sub {
             $_->add_after('th', "\n    ");
           });
       };
     } 0 .. $#cols ])
+    ->memoize;
 }
 
+my $_template = do {
+  local $/;
+  scalar <DATA>;
+};
+
 sub _load_markup {
-  return HTML::Zoom->from_html(do {
-    local $/;
-    scalar <DATA>;
-  });
+  return HTML::Zoom->from_html($_template);
 }
 
 1;