blib/lib/HTML/Notebook/Multiple.pm untested

filter: Statementstmt 0.0% 0 / 38 Branchbran n/a Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 49 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::Multiple;
2  
3 use utf8;
4  
5 use Moose;
6 use Text::Template;
7 use HTML::Notebook::Style;
8 use Path::Tiny;
9 use namespace::autoclean;
10  
11 our $VERSION = '0.003'; # VERSION
12  
13 =encoding utf-8
14  
15 =head1 NAME
16  
17 HTML::Notebook::Multiple - Notebook composed of multiple Notebooks in different HTML files
18  
19 =head1 SYNOPSIS
20  
21 use HTML::Notebook;
22 use HTML::Notebook::Multiple;
23 use HTML::Notebook::Cell;
24 use HTML::Show;
25
26 my $notebook_multiple = HTML::Notebook::Multiple->new();
27
28 $notebook_multiple->set_notebook( 'Primero' => GenerateNotebook(1) );
29 $notebook_multiple->set_notebook( 'Segundo' => GenerateNotebook(2) );
30
31 HTML::Show::show( $notebook_multiple->render() );
32
33 sub GenerateNotebook {
34 my $index = shift();
35 my $notebook = HTML::Notebook->new();
36 my $text_cell = HTML::Notebook::Cell->new( content => 'Simple Notebook' );
37 $notebook->add_cell($text_cell);
38 my $data_cell = HTML::Notebook::Cell->new( content => 'Notebook ' . $index );
39 $notebook->add_cell($data_cell);
40 return $notebook;
41 }
42  
43 =head1 DESCRIPTION
44  
45 Notebook composed of multiple Notebooks in different HTML files
46  
47 =head1 METHODS
48  
49 =cut
50  
51 has 'notebooks' => ( traits => ['Hash'],
52 is => 'rw',
53 isa => 'HashRef[HTML::Notebook]',
54 default => sub { {} },
55 handles => { set_notebook => 'set',
56 get_notebook => 'get',
57 delete_notebook => 'delete',
58 }
59 );
60  
61 =head2 render
62  
63 Render object to HTML
64  
65 =cut
66  
67 sub render {
68 my $self = shift();
69 my %params = @_;
70 my $style = $params{'style'} // HTML::Notebook::Style->new();
71 my $html = <<'HTML';
72 <!DOCTYPE html>
73 <html>
74 <meta charset="utf-8" />
75 <head>
76 {$head}
77 </head>
78 <body>
79 <div id="multiple-notebook-header">
80 {$header}
81 </div>
82 </div>
83 <div id="multiple-notebook-body">
84 <div id="multiple-notebook-leftnav" style="width:10%;height:100%;float:left;">
85 <div id="notebook-header"></div>
86 <div id="notebook-body">
87 {$leftnav}
88 </div>
89 </div>
90 <div style="width:90%;height:100%;float:right;">
91 <iframe id="content-frame" name="content" style="width:100%;height:800px;border:none;"></iframe>
92 </div>
93 </div>
94 <script>
95 window.onload=function()\{
96 var links = document.getElementById("notebook-body").getElementsByTagName("a");
97 document.getElementById("content-frame").setAttribute("src", links[0].href);
98 \}
99 </script>
100 </body>
101 </html>
102 HTML
103  
104 my $cell_template = <<'CELL';
105 <div class="cell" id="{$cell_id}">
106 {$content}
107 </div>
108 CELL
109  
110 my $leftnav = "";
111 my $renderer = Text::Template->new( TYPE => 'STRING', SOURCE => $html );
112 for my $name ( sort keys %{ $self->notebooks } ) {
113 my $notebook = $self->notebooks->{$name};
114 my $html_file = Path::Tiny::tempfile( UNLINK => 0, SUFFIX => '.html' );
115 $html_file->spew_utf8( $notebook->render( style => $style ) );
116 $leftnav .= '<a href="' . $html_file->canonpath() . "\" target=content>$name</a><br>";
117 }
118  
119 return $renderer->fill_in( HASH => { leftnav => $leftnav, head => $style->head, header => "" } );
120 }
121  
122 __PACKAGE__->meta->make_immutable();
123  
124 1;
125  
126 __END__
127  
128 =head1 AUTHOR
129  
130 Pablo Rodríguez González
131  
132 =head1 BUGS
133  
134 Please report any bugs or feature requests via github: L<https://github.com/pablrod/p5-HTML-Notebook/issues>
135  
136 =head1 LICENSE AND COPYRIGHT
137  
138 Copyright 2016 Pablo Rodríguez González.
139  
140 The MIT License (MIT)
141  
142 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:
143  
144 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
145  
146 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.
147  
148 =cut
149