f6c252c8a5c54e8c0e2fa68a7b03858dadb2428c
[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 sub _apply_table_body {
48   my ($self, $markup, $report) = @_;
49   my $index = 0;
50   my $rows  = $report->{rows};
51   my @cols  = map $_->{key}, @{$report->{columns}};
52   return $markup->repeat('.data-row', sub {
53     return HTML::Zoom::CodeStream->new({
54       code => sub {
55         return if $index > $#$rows;
56         my $row = $rows->[$index++];
57         return sub {
58           $_->repeat('td', [ map {
59             my $col_idx = $_;
60             my $col = $cols[$_];
61             my $value = $row->{$col};
62             sub {
63               $_->apply_if($col !~ m{^__}, sub {
64                   $_->add_to_attribute('td', class => "si-column-$col");
65                 })
66                 ->replace_content('td', defined($value) ? $value : '')
67                 ->apply_if($col_idx != $#cols, sub {
68                   $_->add_after('td', "\n    ");
69                 });
70             };
71           } 0 .. $#cols ]);
72         };
73       },
74     });
75   })->memoize;
76 }
77
78 my $_trim = sub {
79   my $string = shift;
80   $string =~ s{(?:^\s+|\s+$)}{}g;
81   return $string;
82 };
83
84 sub _apply_table_head {
85   my ($self, $markup, $report) = @_;
86   my @cols = @{$report->{columns}};
87   return $markup
88     ->repeat('th', [map {
89       my $col_idx = $_;
90       my $col = $cols[$_];
91       my $label = $col->{label};
92       sub {
93         $_->apply_if($col->{key} !~ m{^__}, sub {
94             $_->add_to_attribute('th', class => 'si-colhead-' . $col->{key});
95           })
96           ->replace_content('th', defined($label) ? $label->$_trim : 'Unnamed')
97           ->apply_if($col_idx != $#cols, sub {
98             $_->add_after('th', "\n    ");
99           });
100       };
101     } 0 .. $#cols ])
102     ->memoize;
103 }
104
105 my $_template = do {
106   local $/;
107   scalar <DATA>;
108 };
109
110 sub _load_markup {
111   return HTML::Zoom->from_html($_template);
112 }
113
114 1;
115
116 __DATA__
117 <table class="si-report">
118   <tr>
119     <th>Header</th>
120   </tr>
121   <tr class="data-row">
122     <td>Value</td>
123   </tr>
124 </table>