| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WRT::Renderer; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 3 |  |  | 3 |  | 16 | use strict; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 68 |  | 
| 4 | 3 |  |  | 3 |  | 13 | use warnings; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 59 |  | 
| 5 | 3 |  |  | 3 |  | 25 | use 5.10.0; | 
|  | 3 |  |  |  |  | 9 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 3 |  |  | 3 |  | 14 | use base qw(Exporter); | 
|  | 3 |  |  |  |  | 4 |  | 
|  | 3 |  |  |  |  | 269 |  | 
| 8 |  |  |  |  |  |  | our @EXPORT_OK = qw(render); | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 3 |  |  | 3 |  | 997 | use Data::Dumper; | 
|  | 3 |  |  |  |  | 12784 |  | 
|  | 3 |  |  |  |  | 162 |  | 
| 11 | 3 |  |  | 3 |  | 19 | use File::Basename; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 140 |  | 
| 12 | 3 |  |  | 3 |  | 846 | use File::Copy; | 
|  | 3 |  |  |  |  | 4982 |  | 
|  | 3 |  |  |  |  | 139 |  | 
| 13 | 3 |  |  | 3 |  | 18 | use File::Find; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 158 |  | 
| 14 | 3 |  |  | 3 |  | 16 | use File::Path qw(make_path); | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 1459 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | # TODO:  In general, this should not print anything.  It should | 
| 17 |  |  |  |  |  |  | #        either create a log or call a callback with log data, I think. | 
| 18 |  |  |  |  |  |  | #        It should, further, probably allow the user to render selectively. | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub render { | 
| 21 |  |  |  |  |  |  | # this is invoked off of $w, so it's passing in $self there: | 
| 22 | 1 |  |  | 1 | 0 | 471 | my ($wrt) = shift; | 
| 23 | 1 |  |  |  |  | 4 | my $entry_dir = $wrt->entry_dir; | 
| 24 | 1 |  |  |  |  | 5 | my $publish_dir = $wrt->publish_dir; | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | # Insure that publication path exists and is a directory: | 
| 27 | 1 | 50 |  |  |  | 12 | if (-e $publish_dir) { | 
| 28 | 1 | 50 |  |  |  | 6 | unless (-d $publish_dir) { | 
| 29 | 0 |  |  |  |  | 0 | die("$publish_dir exists but is not a directory"); | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  | } else { | 
| 32 | 0 |  |  |  |  | 0 | my $path_err; | 
| 33 | 0 |  |  |  |  | 0 | make_path($publish_dir, { error => \$path_err }); | 
| 34 | 0 | 0 |  |  |  | 0 | if (@{ $path_err }) { | 
|  | 0 |  |  |  |  | 0 |  | 
| 35 | 0 |  |  |  |  | 0 | print Dumper($path_err); | 
| 36 | 0 |  |  |  |  | 0 | die("Could not create $publish_dir: " . Dumper($path_err)); | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | # Handle the front page and Atom feed: | 
| 41 | 1 |  |  |  |  | 6 | file_put_contents("${publish_dir}/index.html", $wrt->display('new')); | 
| 42 | 1 |  |  |  |  | 6 | file_put_contents("${publish_dir}/feed", $wrt->display('feed')); | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | # Handle any other paths that aren't derived direct from files: | 
| 45 | 1 |  |  |  |  | 5 | my @meta_paths = qw(all); | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 1 |  |  |  |  | 2 | my $rendered_count = 0; | 
| 48 | 1 |  |  |  |  | 1 | my $copied_count   = 0; | 
| 49 | 1 |  |  |  |  | 5 | for my $target (get_source_files($entry_dir), @meta_paths) | 
| 50 |  |  |  |  |  |  | { | 
| 51 | 17 |  |  |  |  | 25 | my $path_err; | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | # Lowercase and alpanumeric + underscores + dashes, no dots - an entry: | 
| 54 | 17 | 100 |  |  |  | 54 | if ($target =~ $wrt->entrypath_expr) { | 
| 55 | 12 |  |  |  |  | 446 | make_path("${publish_dir}/$target", { error => \$path_err }); | 
| 56 | 12 | 50 |  |  |  | 33 | print Dumper($path_err) if (@{ $path_err }); | 
|  | 12 |  |  |  |  | 23 |  | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 12 |  |  |  |  | 29 | my $rendered = $wrt->display($target); | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 12 |  |  |  |  | 33 | my $target_file = "${publish_dir}/$target/index.html"; | 
| 61 | 12 |  |  |  |  | 317 | say "[write] $target_file " . length($rendered); | 
| 62 | 12 |  |  |  |  | 46 | file_put_contents($target_file, $rendered); | 
| 63 | 12 |  |  |  |  | 28 | $rendered_count++; | 
| 64 | 12 |  |  |  |  | 42 | next; | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | # A directory - no-op: | 
| 68 | 5 | 50 |  |  |  | 65 | if (-d "$entry_dir/$target") { | 
| 69 | 0 |  |  |  |  | 0 | say "[directory] $entry_dir/$target"; | 
| 70 | 0 |  |  |  |  | 0 | next; | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | # Some other file - a static asset of some kind: | 
| 74 | 5 |  |  |  |  | 278 | my $dirname = dirname($target); | 
| 75 | 5 |  |  |  |  | 139 | say "[copy] archives/$target -> ${publish_dir}/$target"; | 
| 76 | 5 |  |  |  |  | 224 | make_path("public/$dirname", { error => \$path_err }); | 
| 77 | 5 | 50 |  |  |  | 19 | print Dumper($path_err) if (@{ $path_err }); | 
|  | 5 |  |  |  |  | 14 |  | 
| 78 | 5 |  |  |  |  | 35 | copy("$entry_dir/$target", "${publish_dir}/$target"); | 
| 79 | 5 |  |  |  |  | 1427 | $copied_count++; | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 1 |  |  |  |  | 38 | say "rendered $rendered_count entries"; | 
| 83 | 1 |  |  |  |  | 25 | say "copied $copied_count static files"; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | sub file_put_contents { | 
| 87 | 14 |  |  | 14 | 0 | 113 | my ($file, $contents) = @_; | 
| 88 | 14 | 50 |  |  |  | 808 | open(my $fh, '>', $file) | 
| 89 |  |  |  |  |  |  | or die "Unable to open $file for writing: $!"; | 
| 90 | 14 |  |  |  |  | 87 | print $fh $contents; | 
| 91 | 14 |  |  |  |  | 651 | close $fh; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | # Collect a list of things to render: | 
| 95 |  |  |  |  |  |  | sub get_source_files { | 
| 96 | 1 |  |  | 1 | 0 | 2 | my ($entry_dir) = @_; | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 1 |  |  |  |  | 2 | my @source_files; | 
| 99 |  |  |  |  |  |  | find( | 
| 100 |  |  |  |  |  |  | sub { | 
| 101 |  |  |  |  |  |  | # We skip index files, because they'll be rendered from the dir path: | 
| 102 | 22 | 100 |  | 22 |  | 112 | return if /index$/; | 
| 103 | 17 | 100 |  |  |  | 163 | if ($File::Find::name =~ m{^ \Q$entry_dir\E / (.*) $}x) { | 
| 104 | 16 |  |  |  |  | 31 | my $target = $1; | 
| 105 | 16 |  |  |  |  | 334 | push @source_files, $target; | 
| 106 |  |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  | }, | 
| 108 | 1 |  |  |  |  | 100 | $entry_dir | 
| 109 |  |  |  |  |  |  | ); | 
| 110 | 1 |  |  |  |  | 10 | return @source_files; | 
| 111 |  |  |  |  |  |  | } |