Plugin/HelpMan.pm

filter: Statementstmt 44.1% 64 / 145 Branchbran 24.1% 14 / 58 Conditioncond 9.0% 2 / 22 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 44.0% 11 / 25 Podpod 80.0% 8 / 10 totaltotal 36.4% 99 / 272 CC 38 SCAR 23.5
CC
38
Cov
36%
_doc_html
30.0
hm_doc_body
20.0
hm_doc_title
20.0
CRAP 10.5
↑ Summary HelpMan.pm »
1 package CGI::Application::Plugin::HelpMan;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 #use base 'CGI::Application';
5 1 ▶use LEOCHARRE::DEBUG;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 @ISA = qw/ Exporter /;
10 1 @EXPORT_OK = (qw(
11 __abs_path_doc_to_html
12 __find_abs
13 __string_looks_like_command
14 __term_to_command
15 __term_to_namespace
16 _doc_html
17 _term_abs_path
18 hm_abs_tmp
19 hm_doc_body
20 hm_doc_title
21 hm_found_term_abs
22 hm_found_term_doc
23 hm_found_term_query
24 hm_set_term
25 hm_term_get
26 hm_help_title
27 hm_help_body
28 _hm_reset_data
29 _set_term_as_caller
30  
31 ));
32 1 %EXPORT_TAGS = (
33 ALL => \@EXPORT_OK,
34 basic => \@EXPORT_OK,
35 all => \@EXPORT_OK,
36 );
37 1 $VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)/g;
38  
39 # 1) is there something to look for?
40 0 ▶sub hm_found_term_query {
Subroutine hm_found_term_query: not called CC=2 SCAR=17.9
Pod documented
41 0 my $self = shift;
42 0 ▶ $self->hm_term_get or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->hm_term_get or return 0 0 0 0
43 0 return 1;
44 }
45  
46 # 2) can we resolve it to disk?
47 0 ▶sub hm_found_term_abs {
Subroutine hm_found_term_abs: not called CC=2 SCAR=17.9
Pod documented
48 0 my $self = shift;
49 0 ▶ $self->_term_abs_path or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_term_abs_path or return 0 0 0 0
50 0 return 1;
51 }
52  
53 0 ▶sub _term_abs_path {
Subroutine _term_abs_path: not called CC=3 SCAR=24.8
54 0 my $self = shift;
55 0 ▶ $self->{_hm_data_}->{_term_abs_path} ||= __find_abs($self->hm_term_get) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_hm_data_"}{"_term_abs_path"} ||= __find_abs($self->hm_term_get) or return 0 0 0
Condition0%0/3
$self->{"_hm_data_"}{"_term_abs_path"} ||= __find_abs($self->hm_term_get)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_hm_data_"}{"_term_abs_path"} ||= __find_abs($self->hm_term_get)
$self->{"_hm_data_"}{"_term_abs_path"} __find_abs($self->hm_term_get)
Truth table0%0/3
A ||= B
A= $self->{"_hm_data_"}{"_term_abs_path"}B= __find_abs($self->hm_term_get)
ABresult
1X1
011
000
56 return $self->{_hm_data_}->{_term_abs_path}
57 0 }
58  
59 # 3) does it have doc?
60 0 ▶sub hm_found_term_doc {
Subroutine hm_found_term_doc: not called CC=2 SCAR=17.9
Pod documented
61 0 my $self = shift;
62 0 ▶ $self->_doc_html or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_doc_html or return 0 0 0 0
63 0 return 1;
64 }
65  
66 # body text for template
67 0 ▶sub hm_doc_body {
Subroutine hm_doc_body: not called CC=4 SCAR=30.0
Pod documented
68 0 my $self = shift;
69 0 ▶ my $html = $self->_doc_html or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $html = $self->_doc_html or return 0 0 0 0
70 0 ▶ if( $html=~m/<body[^<>]*>(.+)<\/body>/si ){
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ m[<body[^<>]*>(.+)</body>]is) 0 0 0
71 0 my $body = $1;
72
73 # sometimes Pod::Html will output even when there's no doc.
74 0 my $length = length($html);
75 0 debug("length $length\n");
76 # if less then 500, report nothing.
77 0 ▶ $length > 500 or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$length > 500 or return 0 0 0 0
78 0 return $body;
79 }
80 0 return 0;
81 }
82  
83 # title text for template
84 0 ▶sub hm_doc_title {
Subroutine hm_doc_title: not called CC=4 SCAR=30.0
Pod documented
85 0 my $self = shift;
86 0 my $title;
87
88 0 ▶ my $html = $self->_doc_html or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $html = $self->_doc_html or return 0 0 0 0
89  
90
91 0 ▶ if( $html=~m/<title[^<>]*>(.+)<\/title>/si ){
Branch: 2 branches
BranchTrueFalseTotal
if ($html =~ m[<title[^<>]*>(.+)</title>]is) { } 0 0 0
elsif ($self->hm_term_get) { } 0 0 0
92 0 $title = $1;
93 0 debug("[$title]via html\n");
94 0 return $title;
95
96 }
97 elsif( $self->hm_term_get ){
98 0 my $namespace = __term_to_namespace($self->hm_term_get);
99 0 debug("[$namespace] via term to namespace\n");
100 0 return $namespace;
101 }
102
103 0 return 0;
104 }
105  
106 0 ▶sub hm_abs_tmp {
Subroutine hm_abs_tmp: not called CC=2 SCAR=17.9
Pod documented
107 0 my $self = shift;
108 0 my $d = $self->param('abs_tmp');
109 0 ▶ $d ||= '/tmp';
Condition0%0/2
$d ||= "/tmp"
l!l
00
MC/DC0%0/1
$d ||= "/tmp"
$d
Truth table0%0/2
A
A= $d
Aresult
11
00
110 0 return $d;
111 }
112  
113 # force set the term
114 0 ▶sub hm_set_term {
Subroutine hm_set_term: not called CC=2 SCAR=17.9
Pod documented
115 0 my $self = shift;
116 0 my $term = shift;
117 0 ▶ defined $term or confess('missing arg');
Branch: 1 branch
BranchTrueFalseTotal
defined $term or confess("missing arg") 0 0 0
118 0 $self->{_hm_data_}->{_man_searchterm} = $term;
119 0 return 1;
120 }
121  
122 # term from query string, then from namespace of caller, your cgi app
123 0 ▶sub hm_term_get {
Subroutine hm_term_get: not called CC=3 SCAR=24.8
Pod documented
124 0 my $self = shift;
125
126 0 ▶ unless( $self->{_hm_data_}->{_man_searchterm} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_hm_data_"}{"_man_searchterm"}) 0 0 0
127
128 # first try from query
129 0 my $term = $self->query->param('query');
130
131 # then from caller
132 0 ▶ $term ||= caller; # was using caller(1), wrong.
Condition0%0/3
$term ||= caller
l!l&&r!l&&!r
000
MC/DC0%0/2
$term ||= caller
$term caller
Truth table0%0/3
A ||= B
A= $termB= caller
ABresult
1X1
011
000
133 0 $self->{_hm_data_}->{_man_searchterm} = $term;
134 0 debug(" term is [$term]\n");
135 }
136 0 return $self->{_hm_data_}->{_man_searchterm};
137 }
138  
139  
140  
141  
142  
143  
144 # # private methods....
145  
146 0 ▶sub _doc_html {
Subroutine _doc_html: not called CC=5 SCAR=34.0
147 0 my $self = shift;
148 0 ▶ unless(defined $self->{_hm_data_}->{_abs_path_htmlcode}){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_hm_data_"}{"_abs_path_htmlcode"}) 0 0 0
149  
150 0 ▶ unless( $self->_term_abs_path ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_term_abs_path) 0 0 0
151 0 warn("no abs path for term");
152 0 $self->{_hm_data_}->{_abs_path_htmlcode} = 0;
153 0 return 0;
154 }
155 0 my $help_runmode_name = $self->get_current_runmode;
156 0 ▶ $help_runmode_name ||=undef;
Condition0%0/2
$help_runmode_name ||= undef
l!l
00
MC/DC0%0/1
$help_runmode_name ||= undef
$help_runmode_name
Truth table0%0/2
A
A= $help_runmode_name
Aresult
11
00
157 $self->{_hm_data_}->{_abs_path_htmlcode} =
158 0 __abs_path_doc_to_html(
159 $self->_term_abs_path, $self->hm_abs_tmp, $help_runmode_name );
160
161 0 ▶ $self->{_hm_data_}->{_abs_path_htmlcode} ||=0;
Condition0%0/2
$self->{"_hm_data_"}{"_abs_path_htmlcode"} ||= 0
l!l
00
MC/DC0%0/1
$self->{"_hm_data_"}{"_abs_path_htmlcode"} ||= 0
$self->{"_hm_data_"}{"_abs_path_htmlcode"}
Truth table0%0/2
A
A= $self->{"_hm_data_"}{"_abs_path_htmlcode"}
Aresult
11
00
162 }
163  
164 0 return $self->{_hm_data_}->{_abs_path_htmlcode};
165 }
166  
167  
168 # GET TITLE AND BODY FOR THE CALLER, NOT A QUERY
169  
170 0 ▶sub hm_help_body {
Subroutine hm_help_body: not called CC=1 SCAR=6.9
Pod undocumented
171 0 my $self = shift;
172 0 $self->_set_term_as_caller;
173 0 return $self->hm_doc_body;
174 }
175  
176 0 ▶sub hm_help_title {
Subroutine hm_help_title: not called CC=1 SCAR=6.9
Pod undocumented
177 0 my $self = shift;
178 0 $self->_set_term_as_caller;
179
180 0 return $self->hm_doc_title;
181 }
182  
183 0 ▶sub _set_term_as_caller {
Subroutine _set_term_as_caller: not called CC=3 SCAR=24.8
184 0 my $self = shift;
185
186 0 my $caller = caller(1);
187 0 ▶ $caller or confess('caller should return');
Branch: 1 branch
BranchTrueFalseTotal
$caller or confess("caller should return") 0 0 0
188
189 0 ▶ unless( $self->hm_term_get eq $caller ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->hm_term_get eq $caller) 0 0 0
190 0 $self->_hm_reset_data;
191 0 $self->hm_set_term($caller);
192 }
193
194 0 return 1;
195 }
196  
197  
198 0 ▶sub _hm_reset_data {
Subroutine _hm_reset_data: not called CC=1 SCAR=6.9
199 0 my $self = shift;
200 0 $self->{_hm_data_} =undef;
201 0 return 1;
202 }
203  
204  
205 #######################################################################
206  
207 # THE FOLLOWING SUBS ARE NOT OO
208  
209 ##############################
210 # get html
211  
212 9 ▶sub __abs_path_doc_to_html {
Subroutine __abs_path_doc_to_html: called CC=6 SCAR=18.8
213 9 ▶ my ($abs,$tmp,$runmode) = @_; defined $abs and defined $tmp or confess('missing args');
Branch: 1 branch
BranchTrueFalseTotal
defined $abs and defined $tmp or confess("missing args") 9 0 9
Condition33%1/3
defined $abs and defined $tmp
!ll&&!rl&&r
009
MC/DC0%0/2
defined $abs and defined $tmp
defined $abs defined $tmp
Truth table33%1/3
A and B
A= defined $absB= defined $tmp
ABresult
0X0
100
111
214 9 debug("$abs\n");
215  
216 9 ▶ $runmode ||= 'help_view';
Condition50%1/2
$runmode ||= "help_view"
l!l
09
MC/DC0%0/1
$runmode ||= "help_view"
$runmode
Truth table50%1/2
A
A= $runmode
Aresult
11
00
217 9 debug("runomde = $runmode");
218 # can we write to this place, the tmp place? # TODO $self->hm_abs_tmp ?
219 9 ▶ chdir $tmp or confess("$!, cant chdir to $tmp"); # if you dont... breaks. because perl2html ne4eds to write a tmp file
Branch: 1 branch
BranchTrueFalseTotal
chdir $tmp or confess("$!, cant chdir to $tmp") 9 0 9
220
221 9 require Pod::Html;
222 9 require File::Slurp;
223  
224 9 my $out = $tmp.'/helpman_temp_'. (int rand(600000));
225 9 debug("$out\n");
226
227 9 Pod::Html::pod2html($abs,
228 "--outfile=$out",
229 # "--verbose",
230 # '--css=http://search.cpan.org/s/style.css'
231 "--htmlroot=?rm=$runmode".'&query=', # WORKS for LINKING
232 );
233 #TODO needs work up there.
234
235 9 ▶ my $html = File::Slurp::slurp($out) or warn("could not slurp $out");
Branch: 1 branch
BranchTrueFalseTotal
my $html = File::Slurp::slurp($out) or warn "could not slurp $out" 9 0 9
236  
237 # debug("\n\n$html\n\n"); NO
238
239 9 return $html;
240 }
241  
242  
243 #####################################
244 # find on disk
245  
246 11 ▶sub __find_abs {
Subroutine __find_abs: called CC=7 SCAR=26.6
247 11 ▶ my $term = shift; $term or confess('missing arg');
Branch: 1 branch
BranchTrueFalseTotal
$term or confess("missing arg") 11 0 11
248
249 11 my $as_command = __term_to_command($term);
250 11 my $as_namespace = __term_to_namespace($term);
251  
252 11 my $abs;
253  
254 11 require Pod::Simple::Search;
255 11 my $pss = Pod::Simple::Search->new;
256 11 ▶ if( $abs = $pss->find($as_namespace) ){
Branch: 2 branches
BranchTrueFalseTotal
if ($abs = $pss->find($as_namespace)) { } 9 2 11
elsif (defined $as_command) { } 0 2 2
257 9 debug("via namespace: [$as_namespace] -> $abs\n");
258 9 return $abs;
259 }
260 elsif ( defined $as_command ){
261 0 require File::Which;
262 0 ▶ $abs = File::Which::which($as_command) or return;
Branch: 1 branch
BranchTrueFalseTotal
$abs = File::Which::which($as_command) or return 0 0 0
263 0 debug("via command: [$as_command] -> $abs\n");
264
265 0 require Cwd;
266 0 ▶ Cwd::abs_path( $abs ) or warn("cant resolve $abs") and return;
Branch: 1 branch
BranchTrueFalseTotal
Cwd::abs_path($abs) or warn "cant resolve $abs" and return 0 0 0
Condition0%0/5
Cwd::abs_path($abs) || warn("cant resolve $abs") && (return)
l!l&&r!l&&!r
000
warn "cant resolve $abs" and return
!ll
00
MC/DC0%0/2
Cwd::abs_path($abs) || warn("cant resolve $abs") && (return)
Cwd::abs_path($abs) warn "cant resolve $abs"
Truth table0%0/3
A || B
A= Cwd::abs_path($abs)B= warn "cant resolve $abs"
ABresult
1X1
011
000
267 0 return $abs;
268 }
269 2 return;
270 }
271  
272 11 ▶sub __term_to_command {
Subroutine __term_to_command: called CC=3 SCAR=11.0
273 11 ▶ my $term = shift; defined $term or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $term or return 11 0 11
274 11 $term=~s/^\s+|\s$//g;
275 11 ▶ __string_looks_like_command($term) or return;
Branch: 1 branch
BranchTrueFalseTotal
__string_looks_like_command($term) or return 1 10 11
276 1 return $term;
277 }
278  
279 11 ▶sub __string_looks_like_command {
Subroutine __string_looks_like_command: called CC=3 SCAR=11.0
280 11 ▶ my $string = shift; $string or return;
Branch: 1 branch
BranchTrueFalseTotal
$string or return 11 0 11
281 11 ▶ $string=~/^[a-z]+[\w\-]+[a-zA-Z]+$/ or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$string =~ /^[a-z]+[\w\-]+[a-zA-Z]+$/ or return 0 1 10 11
282 1 return 1;
283 }
284  
285 #turn some silly string into a namespace
286 11 ▶sub __term_to_namespace {
Subroutine __term_to_namespace: called CC=2 SCAR=7.0
287 11 ▶ my $term = shift ; defined $term or confess('no term arg');
Branch: 1 branch
BranchTrueFalseTotal
defined $term or confess("no term arg") 11 0 11
288 11 debug($term);
289 11 $term=~s/^\W|\W$//g;
290
291 11 $term=~s/\/+/::/g;
292  
293 11 $term=~s/\.html?$|\.pm$|\.pl$//g;
294 11 debug(": $term\n");
295 11 return $term;
296 }
297  
298  
299  
300 1 1;
301  
302 __END__
↑ Summary HelpMan.pm »