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