blib/lib/App/Grok.pm

filter: Statementstmt 87.5% 127 / 145 Branchbran 59.3% 38 / 64 Conditioncond 61.1% 11 / 18 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 96.5% 28 / 29 Podpod 100% 7 / 7 totaltotal 77.6% 212 / 273 CC 40 SCAR 19.8
CC
40
Cov
78%
_print
25.3
run
10.9
_get_options
8.2
CRAP 7.2
« blib/lib/App/Grok/Common.pm ↑ Summary blib/lib/App/Grok/Parser/Pod5.pm »
1 package App::Grok;
2 40 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 40 $App::Grok::AUTHORITY = 'cpan:HINRIK';
4 }
5 {
6 40 $App::Grok::VERSION = '0.26';
7 }
8  
9 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use App::Grok::Resource::File qw<:ALL>;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use App::Grok::Resource::Functions qw<:ALL>;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use App::Grok::Resource::Spec qw<:ALL>;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 40 ▶use App::Grok::Resource::Tablet qw<:ALL>;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 40 ▶use App::Grok::Resource::u4x qw<:ALL>;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 40 ▶use Config qw<%Config>;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 40 ▶use File::Temp qw<tempfile>;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 40 ▶use File::Spec::Functions qw<catdir>;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 40 ▶use IO::Interactive qw<is_interactive>;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 40 ▶use Getopt::Long qw<:config bundling>;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 40 ▶use List::Util qw<first>;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 40 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 40 my %opt;
25  
26 40 our $GOT_ANSI;
27 40 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=10.1
28 40 ▶ if ($^O eq 'Win32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'Win32') { } 0 40 40
29 0 eval {
30 0 require Win32::Console::ANSI;
31 0 $GOT_ANSI = 1;
32 }
33 }
34 else {
35 40 $GOT_ANSI = 1;
36 }
37 }
38  
39 39 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
40 39 my ($package, %self) = @_;
41 39 return bless \%self, $package;
42 }
43  
44 39 ▶sub run {
Subroutine run: called CC=10 SCAR=23.9
Pod documented
45 39 my ($self) = @_;
46  
47 39 $self->_get_options();
48  
49 35 ▶ if ($opt{update}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opt{'update'}) { } 0 35 35
elsif ($opt{'index'}) { } 2 33 35
50 0 spec_update();
51 0 tablet_update();
52 0 return;
53 }
54 elsif ($opt{index}) {
55 2 my @index = $self->target_index();
56 2 print "$_\n" for @index;
57 2 return;
58 }
59  
60 33 ▶ my $target = defined $opt{file} ? $opt{file} : $ARGV[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'file'} ? : 19 14 33
61  
62 33 ▶ if ($opt{locate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'locate'}) { } 2 31 33
63 2 ▶ if (defined $opt{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{'file'}) { } 0 2 2
64 0 print file_locate($opt{file}), "\n";
65 }
66 else {
67 2 my $file = $self->locate_target($target);
68 2 ▶ defined $file
Branch: 1 branch
BranchTrueFalseTotal
defined $file ? : 2 0 2
69 ? print $file, "\n"
70 : die "Target file not found\n";
71 ;
72 }
73 }
74 else {
75 31 my $rendered;
76 31 ▶ if ($opt{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'file'}) { } 19 12 31
77 19 $rendered = $self->render_file($opt{file}, $opt{output});
78 }
79 else {
80 12 $rendered = $self->render_target($target, $opt{output});
81 }
82  
83 30 ▶ die "Target '$target' not recognized\n" if !defined $rendered;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rendered 0 30 30
84 30 $self->_print($rendered, $opt{output});
85 }
86  
87 32 return;
88 }
89  
90 39 ▶sub _get_options {
Subroutine _get_options: called CC=7 SCAR=21.0
91 39 my ($self) = @_;
92  
93 GetOptions(
94 'F|file=s' => \$opt{file},
95 2 ▶ 'h|help' => sub { pod2usage(1) },
Subroutine __ANON__: called CC=1 SCAR=0.0
96 'i|index' => \$opt{index},
97 'l|locate' => \$opt{locate},
98 'o|output=s' => \($opt{output} = $GOT_ANSI ? 'ansi' : 'text'),
99 'T|no-pager' => \$opt{no_pager},
100 0 ▶ 'u|unformatted' => sub { $opt{output} = 'pod' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 'U|update' => \$opt{update},
102 2 ▶ 'V|version' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
103 40 ▶ no strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
104 2 ▶ my $version = defined $VERSION ? $VERSION : 'dev-git';
Branch: 1 branch
BranchTrueFalseTotal
defined $VERSION ? : 2 0 2
105 2 print "grok $version\n";
106 2 exit;
107 },
108 39 ▶ ) or pod2usage();
Branch: 2 branches
BranchTrueFalseTotal
&GetOptions("F|file=s", \$opt{"file"}, "h|help", sub { pod2usage(1); } , "i|index", \$opt{"index"}, "l|locate", \$opt{"locate"}, "o|output=s", \($opt{"output"} = $App::Grok::GOT_ANSI ? "ansi" : "text"), "T|no-pager", \$opt{"no_pager"}, "u|unformatted", sub { $opt{"output"} = "pod"; } , "U|update", \$opt{"update"}, "V|version", sub { no strict 'vars'; my $version = defined $VERSION ? $VERSION : "dev-git"; print "grok $version\n"; exit; } ) or pod2usage() 35 0 35
$App::Grok::GOT_ANSI ? : 39 0 39
109  
110 35 ▶ if (!$opt{update} && !$opt{index} && !defined $opt{file} && !@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"update"} or $opt{"index"} or defined $opt{"file"} or @ARGV) 0 35 35
Condition77%7/9
$opt{"update"} or $opt{"index"} or defined $opt{"file"} or @ARGV
l!l&&r!l&&!r
21140
$opt{"update"} or $opt{"index"} or defined $opt{"file"}
l!l&&r!l&&!r
21914
$opt{"update"} or $opt{"index"}
l!l&&r!l&&!r
0233
MC/DC0%0/4
$opt{"update"} or $opt{"index"} or defined $opt{"file"} or @ARGV
$opt{"update"} $opt{"index"} defined $opt{"file"} @ARGV
Truth table60%3/5
A or B or C or D
A= $opt{"update"}B= $opt{"index"}C= defined $opt{"file"}D= @ARGV
ABCDresult
1XXX1
01XX1
001X1
00011
00000
111 0 warn "Too few arguments\n";
112 0 pod2usage();
113 }
114  
115 35 return;
116 }
117  
118 2 ▶sub target_index {
Subroutine target_index: called CC=1 SCAR=0.0
Pod documented
119 2 my ($self) = @_;
120 2 my %index;
121 2 @index{tablet_index()} = 1;
122 2 @index{spec_index()} = 1;
123 2 @index{func_index()} = 1;
124 2 @index{u4x_index()} = 1;
125 2 return keys %index;
126 }
127  
128 2 ▶sub locate_target {
Subroutine locate_target: called CC=6 SCAR=19.9
Pod documented
129 2 my ($self, $target) = @_;
130  
131 2 my $found = u4x_locate($target);
132 2 ▶ $found = func_locate($target) if !defined $found;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $found 2 0 2
133 2 ▶ $found = spec_locate($target) if !defined $found;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $found 2 0 2
134 2 ▶ $found = tablet_locate($target) if !defined $found;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $found 0 2 2
135 2 ▶ $found = file_locate($target) if !defined $found;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $found 0 2 2
136  
137 2 ▶ return $found if defined $found;
Branch: 1 branch
BranchTrueFalseTotal
if defined $found 2 0 2
138 0 return;
139 }
140  
141 30 ▶sub detect_source {
Subroutine detect_source: called CC=4 SCAR=14.0
Pod documented
142 30 my ($self, $target) = @_;
143  
144 30 $target =~ s/.*^=encoding\b.*$//m; # skip over =encoding
145 30 my ($first_pod) = $target =~ /^(=\S+)/m;
146 30 ▶ return if !defined $first_pod; # no Pod found
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first_pod 0 30 30
147  
148 30 ▶ if ($first_pod =~ /^=(?:pod|head\d+|over)$/
Branch: 1 branch
BranchTrueFalseTotal
if ($first_pod =~ /^=(?:pod|head\d+|over)$/ or $target =~ /^=cut\b/m) { } 15 15 30
Condition66%2/3
$first_pod =~ /^=(?:pod|head\d+|over)$/ or $target =~ /^=cut\b/m
l!l&&r!l&&!r
15015
MC/DC50%1/2
$first_pod =~ /^=(?:pod|head\d+|over)$/ or $target =~ /^=cut\b/m
$first_pod =~ /^=(?:pod|head\d+|over)$/ $target =~ /^=cut\b/m
Truth table66%2/3
A or B
A= $first_pod =~ /^=(?:pod|head\d+|over)$/B= $target =~ /^=cut\b/m
ABresult
1X1
011
000
149 || $target =~ /^=cut\b/m) {
150 15 return 'App::Grok::Parser::Pod5';
151 }
152 else {
153 15 return 'App::Grok::Parser::Pod6';
154 }
155 }
156  
157 12 ▶sub render_target {
Subroutine render_target: called CC=7 SCAR=19.5
Pod documented
158 12 my ($self, $target, $output) = @_;
159  
160 12 my $found = u4x_fetch($target);
161 12 ▶ $found = func_fetch($target) if !defined $found;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $found 12 0 12
162 12 ▶ $found = spec_fetch($target) if !defined $found;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $found 8 4 12
163 12 ▶ $found = tablet_fetch($target) if !defined $found;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $found 2 10 12
164 12 ▶ $found = file_fetch($target) if !defined $found;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $found 2 10 12
165 12 ▶ die "Target '$target' not recognized\n" if !defined $found;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $found 1 11 12
166  
167 11 my $parser = $self->detect_source($found);
168 11 eval "require $parser";
169 11 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 11 11
170 11 return $parser->new->render_string($found, $output);
171 }
172  
173 19 ▶sub render_file {
Subroutine render_file: called CC=3 SCAR=11.1
Pod documented
174 19 my ($self, $file, $output) = @_;
175
176 19 ▶ open my $handle, '<', $file or die "Can't open $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, "<", $file or die "Can't open ${file}: $!\n" 19 0 19
177 19 my $pod = do { local $/ = undef; scalar <$handle> };
178  
179 19 my $parser = $self->detect_source($pod);
180 19 close $handle;
181 19 eval "require $parser";
182 19 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 19 19
183 19 return $parser->new->render_string($pod, $output);
184 }
185  
186 30 ▶sub _print {
Subroutine _print: called CC=7 SCAR=32.3
187 30 my ($self, $rendered, $output) = @_;
188  
189 30 ▶ if ($opt{no_pager} || !is_interactive()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'no_pager'} or not is_interactive()) { } 30 0 30
Condition66%2/3
$opt{"no_pager"} or not is_interactive()
l!l&&r!l&&!r
2280
MC/DC0%0/2
$opt{"no_pager"} or not is_interactive()
$opt{"no_pager"} not is_interactive()
Truth table66%2/3
A or not(B)
A= $opt{"no_pager"}B= not is_interactive()
ABresult
1X1
011
000
190 30 print $rendered;
191 }
192 else {
193 0 ▶ my $pager = defined $ENV{PAGER} ? $ENV{PAGER} : $Config{pager};
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'PAGER'} ? : 0 0 0
194  
195 0 my @args;
196 # tell less(1) to display colors without a fuss
197 0 ▶ push @args, '-f', '-R' if $pager =~ /less/ && $output eq 'ansi';
Branch: 1 branch
BranchTrueFalseTotal
if $pager =~ /less/ and $output eq "ansi" 0 0 0
Condition0%0/3
$pager =~ /less/ and $output eq "ansi"
!ll&&!rl&&r
000
MC/DC0%0/2
$pager =~ /less/ and $output eq "ansi"
$pager =~ /less/ $output eq "ansi"
Truth table0%0/3
A and B
A= $pager =~ /less/B= $output eq "ansi"
ABresult
0X0
100
111
198  
199 0 my ($temp_fh, $temp) = tempfile(UNLINK => 1);
200 0 print $temp_fh $rendered;
201 0 close $temp_fh;
202  
203 # $pager might contain options (e.g. "more /e") so we pass a string
204 0 ▶ $^O eq 'MSWin32'
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 0 0
205 ? system $pager . qq{ @args "$temp"}
206 : system $pager . qq{ @args '$temp'}
207 ;
208 }
209  
210 30 return;
211 }
212  
213 40 1;
214  
215 =encoding utf8
216  
217 =head1 NAME
218  
219 App::Grok - Does most of grok's heavy lifting
220  
221 =head1 DESCRIPTION
222  
223 This class provides the main functionality needed by grok. It has some
224 methods you can use if you need to hook into grok.
225  
226 =head1 METHODS
227  
228 =head2 C<new>
229  
230 This is the constructor. It takes no arguments.
231  
232 =head2 C<run>
233  
234 If you call this method, it will look at the command line arguments in
235 C<@ARGV> and act accordingly. This is basically what the L<C<grok>|grok>
236 program does. Takes no arguments.
237  
238 =head2 C<target_index>
239  
240 Takes no arguments. Returns a list of all the targets known to C<grok>.
241  
242 =head2 C<detect_source>
243  
244 Takes a filename as an argument. Returns the name of the appropriate
245 C<App::Grok::*> class to parse it. Returns nothing if the file doesn't contain
246 any Pod.
247  
248 =head2 C<locate_target>
249  
250 Takes a target name as an argument. Returns the path to the target, or nothing
251 if the target is not recognized.
252  
253 =head2 C<render_target>
254  
255 Takes two arguments, a target and the name of an output format. Returns a
256 string containing the rendered documentation, or nothing if the target is
257 unrecognized.
258  
259 =head2 C<render_file>
260  
261 Takes two arguments, a filename and the name of an output format. Returns
262 a string containing the rendered document. B<Note:> this method is called
263 by L<C<render_target>|/render_target>.
264  
265 =head1 AUTHOR
266  
267 Hinrik Örn Sigurðsson, L<hinrik.sig@gmail.com>
268  
269 =head1 LICENSE AND COPYRIGHT
270  
271 Copyright 2009 Hinrik Örn Sigurðsson
272  
273 C<grok> is distributed under the terms of the Artistic License 2.0.
274 For more details, see the full text of the license in the file F<LICENSE>
275 that came with this distribution.
276  
277 =cut
« blib/lib/App/Grok/Common.pm ↑ Summary blib/lib/App/Grok/Parser/Pod5.pm »