Commit | Line | Data |
499ebcdd |
1 | package System::Introspector::Report::Publish::MediaWiki::Producer; |
2 | use Moo; |
3 | use HTML::Zoom; |
4 | |
5 | sub 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 | |
12 | sub _clear_body { |
13 | my ($self, $string) = @_; |
14 | $string =~ s{\n\n+}{\n\n}g; |
15 | return $string; |
499ebcdd |
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; |
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 | |
42 | sub _render_title { |
43 | my ($self, $report) = @_; |
44 | return sprintf '== %s ==', $report->{title}; |
45 | } |
46 | |
499ebcdd |
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 | }); |
21e7cc98 |
75 | })->memoize; |
499ebcdd |
76 | } |
77 | |
21e7cc98 |
78 | my $_trim = sub { |
79 | my $string = shift; |
80 | $string =~ s{(?:^\s+|\s+$)}{}g; |
81 | return $string; |
82 | }; |
83 | |
499ebcdd |
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[$_]; |
21e7cc98 |
91 | my $label = $col->{label}; |
499ebcdd |
92 | sub { |
93 | $_->apply_if($col->{key} !~ m{^__}, sub { |
94 | $_->add_to_attribute('th', class => 'si-colhead-' . $col->{key}); |
95 | }) |
21e7cc98 |
96 | ->replace_content('th', defined($label) ? $label->$_trim : 'Unnamed') |
499ebcdd |
97 | ->apply_if($col_idx != $#cols, sub { |
98 | $_->add_after('th', "\n "); |
99 | }); |
100 | }; |
101 | } 0 .. $#cols ]) |
21e7cc98 |
102 | ->memoize; |
499ebcdd |
103 | } |
104 | |
21e7cc98 |
105 | my $_template = do { |
106 | local $/; |
107 | scalar <DATA>; |
108 | }; |
109 | |
499ebcdd |
110 | sub _load_markup { |
21e7cc98 |
111 | return HTML::Zoom->from_html($_template); |
499ebcdd |
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> |