aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Youri/Check/Output/File/Format/HTML.pm
blob: c498bbd3e7a0135fc6d66fd0fe9ef8e6a97b8783 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# $Id: HTML.pm 1179 2006-08-05 08:30:57Z warly $
package Youri::Check::Output::File::Format::HTML;

=head1 NAME

Youri::Check::Output::File::Format::HTML - File HTML format support

=head1 DESCRIPTION

This format plugin for L<Youri::Check::Output::File> provides HTML format
support.

=cut

use warnings;
use strict;
use Carp;
use CGI;
use base 'Youri::Check::Output::File::Format';

sub extension {
    return 'html';
}

sub _init {
    my $self = shift;
    my %options = (
        style => <<EOF, # css style
h1 {
    text-align:center;
}
table {
    border-style:solid; 
    border-width:1px; 
    border-color:black;
    width:100%;
}
tr.odd { 
    background-color:white;
}
tr.even { 
    background-color:silver;
}
p.footer {
    font-size:smaller;
    text-align:center;
}
EOF
        @_
    );

    $self->{_style} = $options{style};
    $self->{_cgi}   = CGI->new();
}

sub get_report {
    my ($self, $time, $title, $iterator, $type, $columns, $links, $maintainer) = @_;

    my $content;
    my $lead_columns = [ 
        $maintainer ?
            qw/package media/ :
            qw/package media maintainer/
        ];
    my $line;
    my @results;
    $content .= $self->{_cgi}->start_table();
    $content .= $self->{_cgi}->Tr([
        $self->{_cgi}->th([ 
            @$lead_columns,
            @$columns
        ])
    ]);
    while (my $result = $iterator->get_result()) {
        if (@results && $result->{package} ne $results[0]->{package}) {
            $content .= $self->_get_formated_results(
                $lead_columns,
                $columns,
                $links,
                $line++ % 2 ? 'odd' : 'even',
                \@results
            );
            @results = ();
        }
        push(@results, $result);
    }
    $content .= $self->_get_formated_results(
        $lead_columns,
        $columns,
        $links,
        $line++ % 2 ? 'odd' : 'even',
        \@results
    );
    $content .= $self->{_cgi}->end_table();

    return $self->_get_html_page($time, $title, \$content);
}

sub get_index {
    my ($self, $time, $title, $reports, $maintainers) = @_;

    my $content;

    if ($reports) {
        $content .= $self->{_cgi}->h2("Reports");
        my @types = keys %{$reports};

        $content .= $self->{_cgi}->start_ul();
        foreach my $type (sort @types) {
            my $item;
            $item = $self->{_cgi}->a(
                { href => "$type.html" },
                $type
            );
            foreach my $extension (@{$reports->{$type}}) {
                next if ($extension eq extension());
                $item .= " ".$self->{_cgi}->a(
                        { href => "$type.$extension" },
                        "[$extension]"
                );
            }
            $content .= $self->{_cgi}->li($item);
        }
        $content .= $self->{_cgi}->end_ul();
    }

    if ($maintainers) {
        $content .= $self->{_cgi}->h2("Individual reports");

        $content .= $self->{_cgi}->start_ul();
        foreach my $maintainer (sort @{$maintainers}) {
            $content .= $self->{_cgi}->li(
                $self->{_cgi}->a(
                    { href => "$maintainer/index.html" },
                    _obfuscate($maintainer)
                )
            );
        }
        $content .= $self->{_cgi}->end_ul();
    }

    return $self->_get_html_page($time, $title, \$content);
}

sub _get_formated_results {
    my ($self, $lead_columns, $columns, $links, $class, $results) = @_;

    my $content;
    $content .= $self->{_cgi}->end_Tr();
    for my $i (0 .. $#$results) {
        $content .= $self->{_cgi}->start_Tr(
            { class => $class }
        );
        if ($i == 0) {
            # first line contains spanned cells
            $content .= $self->{_cgi}->td(
                { rowspan => scalar @$results },
                [
                    map { $results->[$i]->{$_} }
                    @$lead_columns
                ]
            );
        }
        $content .= $self->{_cgi}->td(
            [ 
                map { 
                    $links->{$_} && $results->[$i]->{$links->{$_}} ?
                        $self->{_cgi}->a(
                            { href => $results->[$i]->{$links->{$_}} },
                            $self->{_cgi}->escapeHTML($results->[$i]->{$_})
                        ) :
                        $self->{_cgi}->escapeHTML($results->[$i]->{$_})
                } @$columns
            ]
        );
        $content .= $self->{_cgi}->end_Tr();
    }

    return $content;
}


sub _get_html_page {
    my ($self, $time, $title, $body) = @_;

    my $content;
    $content .= $self->{_cgi}->start_html(
        -title => $title,
        -style => { code => $self->{_style} }
    );
    $content .= $self->{_cgi}->h1($title);
    $content .= $$body;
    $content .= $self->{_cgi}->hr();
    $content .= $self->{_cgi}->p(
        { class => 'footer' },
        "Page generated $time"
    );
    $content .= $self->{_cgi}->end_html();

    return \$content;
}

sub _obfuscate {
    my ($email) = @_;

    return unless $email;

    $email =~ s/\@/ at /;
    $email =~ s/\./ dot /;

    return $email;
}

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2002-2006, YOURI project

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

=cut

1;