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 {
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} || [''];
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) = @_;
$_->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 ");
});
};
},
});
- });
+ })->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}};
->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;