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.99';
7 12     12   81 use Moose;
  12         32  
  12         84  
8             with 'HiD::Role::IsConverted';
9             with 'HiD::Role::IsPublished';
10 12     12   81600 use namespace::autoclean;
  12         33  
  12         124  
11              
12 12     12   1336 use 5.014; # strict, unicode_strings
  12         43  
13 12     12   69 use utf8;
  12         29  
  12         85  
14 12     12   330 use autodie;
  12         25  
  12         98  
15 12     12   60761 use warnings qw/ FATAL utf8 /;
  12         28  
  12         628  
16 12     12   79 use open qw/ :std :utf8 /;
  12         23  
  12         105  
17 12     12   1898 use charnames qw/ :full /;
  12         28  
  12         111  
18              
19 12     12   2754 use Path::Tiny;
  12         23  
  12         5934  
20              
21              
22 30     30 1 154 sub get_default_layout { 'default' }
23              
24              
25             sub publish {
26 40     40 1 2303 my $self = shift;
27              
28 40         1151 my $out = path( $self->output_filename );
29 40         1015 my $dir = $out->parent;
30              
31 40 100       2166 $dir->mkpath unless $dir->is_dir;
32              
33 40         2761 $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   111 my $self = shift;
39              
40 40   100     1377 my $format = $self->get_metadata( 'permalink' ) // 'none';
41              
42 40         1058 my $source = $self->source;
43              
44 40         968 my $path_frag = $self->input_path;
45 40         311 $path_frag =~ s/^$source//;
46 40   66     181 $path_frag ||= Path::Tiny->rootdir();
47              
48 40         1278 my $naive = path( $path_frag , $self->basename() );
49              
50 40         1502 my %_valid_exts = map { $_=>1 } qw(rss xml html htm xhtml xhtm shtml shtm);
  320         789  
51 40 100       1143 my $ext = exists $_valid_exts{$self->ext} ? $self->ext : 'html';
52              
53 40         96 my $url;
54              
55 40 100       213 if( $format eq 'none' ) { $url = $naive . ".$ext" }
  30 100       152  
56 1         4 elsif( $format eq 'pretty' ) { $url = $naive . '/' }
57 9         46 else { $url = "/$format" }
58              
59 40         261 $url =~ s|//+|/|g;
60              
61 40         1033 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.99
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