File Coverage

blib/lib/Statocles/App/Basic.pm
Criterion Covered Total %
statement 37 37 100.0
branch 13 16 81.2
condition 3 7 42.8
subroutine 3 3 100.0
pod 1 1 100.0
total 57 64 89.0


line stmt bran cond sub pod time code
1             package Statocles::App::Basic;
2             our $VERSION = '0.085';
3             # ABSTRACT: Build Markdown and collateral files
4              
5 19     19   24924 use Statocles::Base 'Class';
  19         44  
  19         159  
6 19     19   130525 use Statocles::Util qw( run_editor );
  19         52  
  19         10698  
7             with 'Statocles::App::Role::Store';
8              
9             #pod =attr store
10             #pod
11             #pod The L<store path|Statocles::Store> containing this app's documents and files.
12             #pod Required.
13             #pod
14             #pod =cut
15              
16             #pod =method command
17             #pod
18             #pod my $exitval = $app->command( $app_name, @args );
19             #pod
20             #pod Run a command on this app. Commands allow creating, editing, listing, and
21             #pod viewing pages.
22             #pod
23             #pod =cut
24              
25             my $USAGE_INFO = <<'ENDHELP';
26             Usage:
27             $name help -- This help file
28             $name edit <path> -- Edit a page, creating it if necessary
29             ENDHELP
30              
31             sub command {
32 6     6 1 35639 my ( $self, $name, @argv ) = @_;
33              
34 6 100       21 if ( !$argv[0] ) {
35 1         24 say STDERR "ERROR: Missing command";
36 1         73 say STDERR eval "qq{$USAGE_INFO}";
37 1         7 return 1;
38             }
39              
40 5 100       14 if ( $argv[0] eq 'help' ) {
41 2         146 say eval "qq{$USAGE_INFO}";
42 2         16 return 0;
43             }
44              
45 3 100       9 if ( $argv[0] eq 'edit' ) {
46 2         12 $argv[1] =~ s{^/}{};
47 2 100       22 my $path = Path::Tiny->new(
48             $argv[1] =~ /[.](?:markdown|md)$/ ? $argv[1] : "$argv[1]/index.markdown",
49             );
50              
51 2         71 my %doc;
52             # Read post content on STDIN
53 2 50       11 if ( !-t *STDIN ) {
54 2         4 my $content = do { local $/; <STDIN> };
  2         8  
  2         26  
55 2         24 %doc = (
56             %doc,
57             $self->store->parse_frontmatter( "<STDIN>", $content ),
58             );
59              
60             # Re-open STDIN as the TTY so that the editor (vim) can use it
61             # XXX Is this also a problem on Windows?
62 2 50       17 if ( -e '/dev/tty' ) {
63 2         7 close STDIN;
64 2         24 open STDIN, '/dev/tty';
65             }
66             }
67              
68 2 50 33     15 if ( !$self->store->has_file( $path ) || keys %doc ) {
69 2   50     127 $doc{title} ||= '';
70 2   50     11 $doc{content} ||= "Markdown content goes here.\n";
71 2         12 $self->store->write_document( $path => \%doc );
72             }
73 2         15 my $full_path = $self->store->path->child( $path );
74              
75 2 100       92 if ( !run_editor( $full_path ) ) {
76 1         5 say "New page at: $full_path";
77             }
78              
79             }
80             else {
81 1         34 say STDERR qq{ERROR: Unknown command "$argv[0]"};
82 1         77 say STDERR eval "qq{$USAGE_INFO}";
83 1         8 return 1;
84             }
85              
86 2         44 return 0;
87             }
88              
89             1;
90              
91             __END__
92              
93             =pod
94              
95             =encoding UTF-8
96              
97             =head1 NAME
98              
99             Statocles::App::Basic - Build Markdown and collateral files
100              
101             =head1 VERSION
102              
103             version 0.085
104              
105             =head1 SYNOPSIS
106              
107             my $app = Statocles::App::Basic->new(
108             url_root => '/',
109             store => 'share/root',
110             );
111             my @pages = $app->pages;
112              
113             =head1 DESCRIPTION
114              
115             This application builds basic pages based on L<Markdown documents|Statocles::Document> and
116             other files. Use this to have basic informational pages like "About Us" and "Contact Us".
117              
118             =head1 ATTRIBUTES
119              
120             =head2 store
121              
122             The L<store path|Statocles::Store> containing this app's documents and files.
123             Required.
124              
125             =head1 METHODS
126              
127             =head2 command
128              
129             my $exitval = $app->command( $app_name, @args );
130              
131             Run a command on this app. Commands allow creating, editing, listing, and
132             viewing pages.
133              
134             =head1 AUTHOR
135              
136             Doug Bell <preaction@cpan.org>
137              
138             =head1 COPYRIGHT AND LICENSE
139              
140             This software is copyright (c) 2016 by Doug Bell.
141              
142             This is free software; you can redistribute it and/or modify it under
143             the same terms as the Perl 5 programming language system itself.
144              
145             =cut