| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::WRT::Renderer; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 438 | use strict; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 62 |  | 
| 4 | 2 |  |  | 2 |  | 10 | use warnings; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 46 |  | 
| 5 | 2 |  |  | 2 |  | 22 | use 5.10.0; | 
|  | 2 |  |  |  |  | 7 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 2 |  |  | 2 |  | 13 | use Carp; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 133 |  | 
| 8 | 2 |  |  | 2 |  | 14 | use File::Basename; | 
|  | 2 |  |  |  |  | 8 |  | 
|  | 2 |  |  |  |  | 1597 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | =pod | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | =head1 NAME | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | App::WRT::Renderer - render a wrt repo to publishable HTML | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | use App::WRT; | 
| 19 |  |  |  |  |  |  | use App::WRT::Renderer; | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | my $wrt = App::WRT::new_from_file($config_file); | 
| 22 |  |  |  |  |  |  | my $renderer = App::WRT::Renderer->new( | 
| 23 |  |  |  |  |  |  | $wrt, | 
| 24 |  |  |  |  |  |  | sub { say $_[0]; } | 
| 25 |  |  |  |  |  |  | ); | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | $renderer->render(); | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | =head1 METHODS | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | =over | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | =item new($class, $entry_dir, $logger, $io) | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | Get a new Renderer.  Takes an instance of App::WRT, a logging callback, and a | 
| 36 |  |  |  |  |  |  | App::WRT::FileIO or similar object to be used for the actual intake and | 
| 37 |  |  |  |  |  |  | mangling of things on the filesystem. | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | =cut | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub new { | 
| 42 | 2 |  |  | 2 | 1 | 7 | my $class = shift; | 
| 43 | 2 |  |  |  |  | 6 | my ($wrt, $logger, $io) = @_; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 2 | 50 |  |  |  | 10 | ref($logger) eq 'CODE' or | 
| 46 |  |  |  |  |  |  | croak("Error: Renderer expects an anonymous function for logging"); | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 2 |  |  |  |  | 10 | my %params = ( | 
| 49 |  |  |  |  |  |  | wrt    => $wrt, | 
| 50 |  |  |  |  |  |  | logger => $logger, | 
| 51 |  |  |  |  |  |  | io     => $io, | 
| 52 |  |  |  |  |  |  | ); | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 2 |  |  |  |  | 6 | my $self = \%params; | 
| 55 | 2 |  |  |  |  | 8 | bless $self, $class; | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | =item write($path, $contents) | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | Write $contents to $path, using the FileIO object passed into the constructor | 
| 62 |  |  |  |  |  |  | above. | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | =cut | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | sub write { | 
| 67 | 60 |  |  | 60 | 1 | 132 | my ($self, $file, $contents) = @_; | 
| 68 | 60 |  |  |  |  | 176 | $self->{io}->file_put_contents($file, $contents) | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | =item render($class, $entry_dir) | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | Render entries to F. | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | =cut | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | sub render { | 
| 79 | 2 |  |  | 2 | 1 | 9 | my $self = shift; | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 2 |  |  |  |  | 13 | my $entry_dir = $self->{wrt}->{entry_dir}; | 
| 82 | 2 |  |  |  |  | 7 | my $publish_dir = $self->{wrt}->{publish_dir}; | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | # Ensure that publication path exists and is a directory: | 
| 85 | 2 | 50 |  |  |  | 47 | if (-e $publish_dir) { | 
| 86 | 2 | 50 |  |  |  | 42 | unless (-d $publish_dir) { | 
| 87 | 0 |  |  |  |  | 0 | croak("$publish_dir exists but is not a directory"); | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  | } else { | 
| 90 | 0 |  |  |  |  | 0 | $self->log("Attempting to create $publish_dir"); | 
| 91 | 0 | 0 |  |  |  | 0 | unless ($self->dir_make_logged($publish_dir)) { | 
| 92 | 0 |  |  |  |  | 0 | croak("Could not create $publish_dir"); | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | # Handle the front page.  With no entries given, display() will use the | 
| 97 |  |  |  |  |  |  | # configured default, which is probably "new" unless the user has changed it. | 
| 98 |  |  |  |  |  |  | $self->write( | 
| 99 |  |  |  |  |  |  | "${publish_dir}/index.html", | 
| 100 |  |  |  |  |  |  | $self->{wrt}->display() | 
| 101 | 2 |  |  |  |  | 19 | ); | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | # Handle feed formats: | 
| 104 | 2 |  |  |  |  | 6 | my $feed_alias = $self->{wrt}->{feed_alias}; | 
| 105 | 2 |  |  |  |  | 10 | my $xml_feed_content = $self->{wrt}->feed_print_recent(); | 
| 106 | 2 |  |  |  |  | 398 | my $json_feed_content = $self->{wrt}->feed_print_json_recent(); | 
| 107 | 2 |  |  |  |  | 165 | $self->write("${publish_dir}/${feed_alias}", $xml_feed_content); | 
| 108 | 2 |  |  |  |  | 23 | $self->write("${publish_dir}/${feed_alias}.xml", $xml_feed_content); | 
| 109 | 2 |  |  |  |  | 15 | $self->write("${publish_dir}/${feed_alias}.json", $json_feed_content); | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | # Handle any other paths that aren't derived directly from files: | 
| 112 | 2 |  |  |  |  | 11 | my @meta_paths = qw(all); | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 2 |  |  |  |  | 7 | my $rendered_count = 0; | 
| 115 | 2 |  |  |  |  | 4 | my $copied_count   = 0; | 
| 116 | 2 |  |  |  |  | 11 | for my $target ($self->{wrt}->{entries}->all(), @meta_paths) | 
| 117 |  |  |  |  |  |  | { | 
| 118 |  |  |  |  |  |  | # Skip index files - these are the text content of an entry, not | 
| 119 |  |  |  |  |  |  | # a sub-entry: | 
| 120 | 88 | 100 |  |  |  | 229 | next if $target =~ m{/index$}; | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | # Lowercase and alphanumeric + underscores + dashes, no dots - an entry: | 
| 123 | 72 | 100 |  |  |  | 253 | if ($self->{wrt}->{entries}->is_renderable($target)) { | 
| 124 | 52 |  |  |  |  | 188 | $self->dir_make_logged("$publish_dir/$target"); | 
| 125 |  |  |  |  |  |  |  | 
| 126 | 52 |  |  |  |  | 140 | my $rendered = $self->{wrt}->display($target); | 
| 127 |  |  |  |  |  |  |  | 
| 128 | 52 |  |  |  |  | 132 | my $target_file = "$publish_dir/$target/index.html"; | 
| 129 | 52 |  |  |  |  | 590 | $self->log("[write] $target_file " . length($rendered)); | 
| 130 | 52 |  |  |  |  | 235 | $self->write($target_file, $rendered); | 
| 131 | 52 |  |  |  |  | 71 | $rendered_count++; | 
| 132 | 52 |  |  |  |  | 134 | next; | 
| 133 |  |  |  |  |  |  | } | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | # A directory - no-op: | 
| 136 | 20 | 50 |  |  |  | 495 | if (-d "$entry_dir/$target") { | 
| 137 | 0 |  |  |  |  | 0 | $self->log("[directory] $entry_dir/$target"); | 
| 138 | 0 |  |  |  |  | 0 | next; | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | # Some other file - a static asset of some kind: | 
| 142 | 20 |  |  |  |  | 662 | my $dirname = dirname($target); | 
| 143 | 20 |  |  |  |  | 94 | $self->log("[copy] archives/$target -> $publish_dir/$target"); | 
| 144 | 20 |  |  |  |  | 222 | $self->dir_make_logged("$publish_dir/$dirname"); | 
| 145 | 20 |  |  |  |  | 78 | $self->{io}->file_copy("$entry_dir/$target", "$publish_dir/$target"); | 
| 146 | 20 |  |  |  |  | 35 | $copied_count++; | 
| 147 |  |  |  |  |  |  | } | 
| 148 |  |  |  |  |  |  |  | 
| 149 | 2 |  |  |  |  | 24 | $self->log("rendered $rendered_count entries"); | 
| 150 | 2 |  |  |  |  | 14 | $self->log("copied $copied_count static files"); | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | # Presumed success: | 
| 153 | 2 |  |  |  |  | 17 | return 1; | 
| 154 |  |  |  |  |  |  | } | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | =item dir_make_logged($path) | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | Make a directory path or log an error. | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =cut | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | sub dir_make_logged { | 
| 164 | 72 |  |  | 72 | 1 | 111 | my ($self, $path) = @_; | 
| 165 | 72 |  |  |  |  | 89 | my $path_err; | 
| 166 | 72 |  |  |  |  | 167 | $self->log("[create] $path"); | 
| 167 | 72 |  |  |  |  | 301 | $self->{io}->dir_make($path); | 
| 168 |  |  |  |  |  |  | # XXX: surface these somehow | 
| 169 |  |  |  |  |  |  | # $self->log(Dumper($path_err)) if @{ $path_err }; | 
| 170 |  |  |  |  |  |  | } | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | =item log(@log_items) | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | Call logging callback with passed parameters. | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | =cut | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | sub log { | 
| 180 | 148 |  |  | 148 | 1 | 201 | my ($self) = shift; | 
| 181 | 148 |  |  |  |  | 349 | $self->{logger}->(@_); | 
| 182 |  |  |  |  |  |  | } | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | =back | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | =cut | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | 1; |