handle newline and html break conversions in mediawiki publishing
[scpubgit/System-Introspector-Report.git] / lib / System / Introspector / Report / Publish / MediaWiki / Producer.pm
1 package System::Introspector::Report::Publish::MediaWiki::Producer;
2 use Moo;
3 use HTML::Zoom;
4
5 sub render {
6   my ($self, $stream) = @_;
7   return $self->_clear_body(join "\n", map {
8     ref($_) ? $self->_render_table($_) : $_;
9   } @$stream);
10 }
11
12 sub _clear_body {
13   my ($self, $string) = @_;
14   $string =~ s{\n\n+}{\n\n}g;
15   return $string;
16 }
17
18 sub _wrap {
19   my ($self, $type, $id, $body) = @_;
20   chomp $body;
21   return join "\n",
22     sprintf('<!-- SI:%s begin %s -->', uc($type), $id),
23     $body,
24     sprintf('<!-- SI:%s end %s -->', uc($type), $id),
25 }
26
27 sub _render_table {
28   my ($self, $report) = @_;
29   my $id     = $report->{id};
30   my $str_id = ref($id) ? join(':', @$id) : $id;
31   my $markup = $self->_load_markup;
32   $markup = $self->_apply_table_head($markup, $report);
33   $markup = $self->_apply_table_body($markup, $report);
34   my $description = $report->{description} || [''];
35   return join "\n",
36     $self->_wrap('title', $str_id, $self->_render_title($report)),
37     @$description,
38     $self->_wrap('table', $str_id, $markup->to_html),
39     '';
40 }
41
42 sub _render_title {
43   my ($self, $report) = @_;
44   return sprintf '== %s ==', $report->{title};
45 }
46
47 my $_prepare_column_content = sub {
48   my ($content) = @_;
49   return ''
50     unless defined $content;
51   my @lines = split m{\n}, $content;
52   if (@lines == 1) {
53     return $content;
54   }
55   else {
56     my ($first, @rest) = @lines;
57     return HTML::Zoom->from_events([
58       { type => 'TEXT', raw => $first },
59       map {
60         my $string = $_;
61         ( @{ HTML::Zoom->from_html('<br/>')->to_events },
62           { type => 'TEXT', raw => $string });
63       } @rest,
64     ]);
65   }
66 };
67
68 sub _apply_table_body {
69   my ($self, $markup, $report) = @_;
70   my $index = 0;
71   my $rows  = $report->{rows};
72   my @cols  = map $_->{key}, @{$report->{columns}};
73   return $markup->repeat('.data-row', sub {
74     return HTML::Zoom::CodeStream->new({
75       code => sub {
76         return if $index > $#$rows;
77         my $row = $rows->[$index++];
78         return sub {
79           $_->repeat('td', [ map {
80             my $col_idx = $_;
81             my $col = $cols[$_];
82             my $value = $row->{$col};
83             sub {
84               $_->apply_if($col !~ m{^__}, sub {
85                   $_->add_to_attribute('td', class => "si-column-$col");
86                 })
87                 ->replace_content('td', $value->$_prepare_column_content)
88                 ->apply_if($col_idx != $#cols, sub {
89                   $_->add_after('td', "\n    ");
90                 });
91             };
92           } 0 .. $#cols ]);
93         };
94       },
95     });
96   })->memoize;
97 }
98
99 my $_trim = sub {
100   my $string = shift;
101   $string =~ s{(?:^\s+|\s+$)}{}g;
102   return $string;
103 };
104
105 sub _apply_table_head {
106   my ($self, $markup, $report) = @_;
107   my @cols = @{$report->{columns}};
108   return $markup
109     ->repeat('th', [map {
110       my $col_idx = $_;
111       my $col = $cols[$_];
112       my $label = $col->{label};
113       sub {
114         $_->apply_if($col->{key} !~ m{^__}, sub {
115             $_->add_to_attribute('th', class => 'si-colhead-' . $col->{key});
116           })
117           ->replace_content('th', defined($label) ? $label->$_trim : 'Unnamed')
118           ->apply_if($col_idx != $#cols, sub {
119             $_->add_after('th', "\n    ");
120           });
121       };
122     } 0 .. $#cols ])
123     ->memoize;
124 }
125
126 my $_template = do {
127   local $/;
128   scalar <DATA>;
129 };
130
131 sub _load_markup {
132   return HTML::Zoom->from_html($_template);
133 }
134
135 1;
136
137 __DATA__
138 <table class="si-report">
139   <tr>
140     <th>Header</th>
141   </tr>
142   <tr class="data-row">
143     <td>Value</td>
144   </tr>
145 </table>