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