blib/lib/HTML/Notebook.pm untested

filter: Statementstmt 0.0% 0 / 33 Branchbran n/a Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 43 CC 1 SCAR 6.9
CC
1
Cov
0%
render
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package HTML::Notebook;
2  
3 use utf8;
4  
5 use Moose;
6 use Text::Template;
7 use HTML::Notebook::Style;
8 use namespace::autoclean;
9  
10 our $VERSION = '0.003'; # VERSION
11  
12 =encoding utf-8
13  
14 =head1 NAME
15  
16 HTML::Notebook - Compose HTML documents using notebook style
17  
18 =head1 SYNOPSIS
19  
20 use HTML::Notebook;
21 use HTML::Notebook::Cell;
22 use HTML::Show;
23
24 my $notebook = HTML::Notebook->new();
25 my $text_cell = HTML::Notebook::Cell->new( content => 'Simple linear regression models the relationship between a scalar variable y and another scalar variable x. For example:' );
26 $notebook->add_cell($text_cell);
27 my $data_cell = HTML::Notebook::Cell->new( content => '
28 <table class="table table-striped table-hover">
29 <thead>
30 <tr>
31 <th>X</th>
32 <th>Y</th>
33 </tr>
34 </thead>
35 <tbody>
36 <tr>
37 <td>1</td>
38 <td>10.5</td>
39 </tr>
40 <tr>
41 <td>2</td>
42 <td>19</td>
43 </tr>
44 <tr>
45 <td>3</td>
46 <td>27</td>
47 </tr>
48 <tr>
49 <td>4</td>
50 <td>41</td>
51 </tr>
52 </tbody>
53 </table>
54 ' );
55 $notebook->add_cell($data_cell);
56
57 HTML::Show::show($notebook->render());
58  
59 =head1 DESCRIPTION
60  
61 Compose HTML documents using notebook style. Every notebook is composed of HTML::Notebook::Cell objects that you can add, update and remove.
62  
63 =head1 METHODS
64  
65 =cut
66  
67 has 'cells' => ( traits => ['Array'],
68 is => 'rw',
69 isa => 'ArrayRef[HTML::Notebook::Cell]',
70 default => sub { [] },
71 handles => { add_cell => 'push',
72 get_cell => 'get',
73 delete_cell => 'delete',
74 }
75 );
76  
77 =head2 render
78  
79 Render object to HTML
80  
81 =cut
82  
83 sub render {
84 my $self = shift();
85 my %params = @_;
86 my $style = $params{'style'} // HTML::Notebook::Style->new();
87 my $html = <<'HTML';
88 <!DOCTYPE html>
89 <html>
90 <meta charset="utf-8" />
91 <head>
92 {$head}
93 </head>
94 <body>
95 <div id="notebook-header">
96 {$header}
97 </div>
98 </div>
99 <div id="notebook-body">
100 {$body}
101 </div>
102 </body>
103 </html>
104 HTML
105  
106 my $cell_template = <<'CELL';
107 <div class="cell" id="{$cell_id}">
108 {$content}
109 </div>
110 CELL
111  
112 my $body = "";
113 my $renderer = Text::Template->new( TYPE => 'STRING', SOURCE => $html );
114 my $cell_renderer = Text::Template->new( TYPE => 'STRING', SOURCE => $cell_template );
115 for my $cell ( @{ $self->cells } ) {
116 $body .= $cell_renderer->fill_in(
117 HASH => { cell_id => $cell->id,
118 content => $cell->content
119 }
120 );
121 }
122  
123 return $renderer->fill_in( HASH => { body => $body, head => $style->head, header => $style->header } );
124 }
125  
126 __PACKAGE__->meta->make_immutable();
127  
128 1;
129  
130 __END__
131  
132 =head1 AUTHOR
133  
134 Pablo Rodríguez González
135  
136 =head1 BUGS
137  
138 Please report any bugs or feature requests via github: L<https://github.com/pablrod/p5-HTML-Notebook/issues>
139  
140 =head1 LICENSE AND COPYRIGHT
141  
142 Copyright 2016 Pablo Rodríguez González.
143  
144 The MIT License (MIT)
145  
146 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
147  
148 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
149  
150 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
151  
152 =cut
153