File Coverage

blib/lib/HiD/Page.pm
Criterion Covered Total %
statement 49 49 100.0
branch 8 8 100.0
condition 4 5 80.0
subroutine 12 12 100.0
pod 2 2 100.0
total 75 76 98.6


line stmt bran cond sub pod time code
1             # ABSTRACT: Pages that are converted during the output process
2              
3              
4             package HiD::Page;
5             our $AUTHORITY = 'cpan:GENEHACK';
6             $HiD::Page::VERSION = '1.991';
7 12     12   77 use Moose;
  12         24  
  12         76  
8             with 'HiD::Role::IsConverted';
9             with 'HiD::Role::IsPublished';
10 12     12   70866 use namespace::autoclean;
  12         23  
  12         100  
11              
12 12     12   1034 use 5.014; # strict, unicode_strings
  12         32  
13 12     12   58 use utf8;
  12         24  
  12         84  
14 12     12   263 use autodie;
  12         25  
  12         78  
15 12     12   53391 use warnings qw/ FATAL utf8 /;
  12         27  
  12         547  
16 12     12   63 use open qw/ :std :utf8 /;
  12         18  
  12         87  
17 12     12   1671 use charnames qw/ :full /;
  12         22  
  12         107  
18              
19 12     12   2534 use Path::Tiny;
  12         31  
  12         5428  
20              
21              
22 30     30 1 114 sub get_default_layout { 'default' }
23              
24              
25             sub publish {
26 40     40 1 1863 my $self = shift;
27              
28 40         996 my $out = path( $self->output_filename );
29 40         929 my $dir = $out->parent;
30              
31 40 100       1908 $dir->mkpath unless $dir->is_dir;
32              
33 40         2292 $out->spew_utf8( $self->rendered_content );
34             }
35              
36             # used to populate the 'url' attr in Role::IsPublished
37             sub _build_url {
38 40     40   69 my $self = shift;
39              
40 40   100     1141 my $format = $self->get_metadata( 'permalink' ) // 'none';
41              
42 40         924 my $source = $self->source;
43              
44 40         812 my $path_frag = $self->input_path;
45 40         215 $path_frag =~ s/^$source//;
46 40   66     124 $path_frag ||= Path::Tiny->rootdir();
47              
48 40         1004 my $naive = path( $path_frag , $self->basename() );
49              
50 40         1234 my %_valid_exts = map { $_=>1 } qw(rss xml html htm xhtml xhtm shtml shtm);
  320         638  
51 40 100       946 my $ext = exists $_valid_exts{$self->ext} ? $self->ext : 'html';
52              
53 40         92 my $url;
54              
55 40 100       122 if( $format eq 'none' ) { $url = $naive . ".$ext" }
  30 100       129  
56 1         3 elsif( $format eq 'pretty' ) { $url = $naive . '/' }
57 9         26 else { $url = "/$format" }
58              
59 40         207 $url =~ s|//+|/|g;
60              
61 40         962 return $url;
62             }
63              
64             __PACKAGE__->meta->make_immutable;
65             1;
66              
67             __END__
68              
69             =pod
70              
71             =encoding UTF-8
72              
73             =head1 NAME
74              
75             HiD::Page - Pages that are converted during the output process
76              
77             =head1 SYNOPSIS
78              
79             my $page = HiD::Page->new({
80             dest_dir => 'path/to/output/dir' ,
81             hid => $hid_object ,
82             input_filename => 'path/to/page/file' ,
83             layouts => $hash_of_hid_layout_objects,
84             });
85              
86             =head1 DESCRIPTION
87              
88             Class representing a "page" object -- i.e., a file that is not a blog post,
89             but that is still processed (e.g., converted from Markdown or Textile to HTML
90             and run through a layout rendering step) during publication.
91              
92             =head1 METHODS
93              
94             =head2 get_default_layout
95              
96             Get the name of the default page layout. (The default is 'default'.)
97              
98             =head2 publish
99              
100             Publish -- convert, render through any associated layouts, and write out to
101             disk -- this data from this object.
102              
103             =head1 NOTE
104              
105             Also consumes L<HiD::Role::IsConverted> and L<HiD::Role::IsPublished>; see
106             documentation for that role as well if you're trying to figure out how an
107             object from this class works.
108              
109             =head1 VERSION
110              
111             version 1.991
112              
113             =head1 AUTHOR
114              
115             John SJ Anderson <genehack@genehack.org>
116              
117             =head1 COPYRIGHT AND LICENSE
118              
119             This software is copyright (c) 2015 by John SJ Anderson.
120              
121             This is free software; you can redistribute it and/or modify it under
122             the same terms as the Perl 5 programming language system itself.
123              
124             =cut