| 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.004'; # 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:1080px;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__ |