DocSet/RunTime.pm untested

filter: Statementstmt 0.0% 0 / 110 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 188 CC 18 SCAR 23.7
CC
18
Cov
0%
find_src_doc
30.0
set_opt
20.0
set_render_obj
20.0
CRAP 10.7
1 package DocSet::RunTime;
2  
3 # META: this class acts as Singleton, consider to actually use
4 # Class::Singleton
5  
6 use strict;
7 use warnings;
8  
9 use File::Spec::Functions qw(catdir catfile splitdir);
10 use File::Find;
11  
12 use DocSet::Util;
13 use Carp;
14  
15 use vars qw(@ISA @EXPORT %opts);
16 @ISA = qw(Exporter);
17 @EXPORT = qw(get_opts find_src_doc set_render_obj get_render_obj unset_render_obj);
18  
19 my %registers = ();
20  
21 my @search_paths = ();
22 my %src_docs = ();
23 my %exts = ();
24 my $render_obj;
25  
26 sub registers_reset {
27 %registers = ();
28 }
29  
30 sub register {
31 my ($register, $key, $val) = @_;
32 push @{$registers{$register}{$key}}, $val;
33 return @{ $registers{$register}{$key} || []};
34 }
35  
36  
37 sub set_opt {
38 my (%args) = ();
39 if (@_ == 1) {
40 my $arg = shift;
41 my $ref = ref $arg;
42 if ($ref) {
43 %args = $ref eq 'HASH' ? %$arg : @$arg;
44 } else {
45 die "must be a ref to or an array/hash";
46 }
47 } else {
48 %args = @_;
49 }
50 @opts{keys %args} = values %args;
51 }
52  
53 sub get_opts {
54 my $opt = shift;
55 exists $opts{$opt} ? $opts{$opt} : '';
56 }
57  
58 # check whether we have a Storable avalable
59 use constant HAS_STORABLE => eval { require Storable; };
60 sub has_storable_module {
61 return HAS_STORABLE;
62 }
63  
64 # check for existence of html2ps and ps2pdf
65  
66 my $html2ps_exec = which('html2ps');
67 sub can_create_ps {
68 # ps2html is bundled, so we can always create PS
69 return $html2ps_exec if $html2ps_exec;
70  
71 print 'It seems that you do not have html2ps installed! You have',
72 'to install it if you want to generate the PDF file';
73 return 0;
74  
75 # if you unbundle it make sure you write here a code similar to
76 # can_create_pdf()
77 }
78  
79 my $ps2pdf_exec = which('ps2pdf');
80 sub can_create_pdf {
81 # check whether ps2pdf exists
82 return $ps2pdf_exec if $ps2pdf_exec;
83  
84 print 'It seems that you do not have ps2pdf installed! You have',
85 'to install it if you want to generate the PDF file';
86 return 0;
87 }
88  
89 sub scan_src_docs {
90 my ($base, $ra_search_paths, $ra_search_exts) = @_;
91  
92 @search_paths = @{$ra_search_paths || []};
93  
94 # .cfg is for matching config.cfg to become index.html
95 %exts = map {$_ => 1} @{$ra_search_exts || []}, 'cfg';
96  
97 my @ext_accept_pattern = map {quotemeta($_)."\$"} keys %exts;
98 my $rsub_keep_ext =
99 build_matchmany_sub(\@ext_accept_pattern);
100  
101 my %seen;
102 for my $rel_path (@search_paths) {
103 my $full_base_path = catdir $base, $rel_path;
104 die "'search_paths' attr: $full_base_path is not a dir"
105 unless -d $full_base_path;
106  
107 my @seen_pattern = map {"^".quotemeta($_)} keys %seen;
108 my $rsub_skip_seen = build_matchmany_sub(\@seen_pattern);
109  
110 my $rel_uri = path2uri($rel_path);
111 $src_docs{$rel_uri} = {
112 # autogenerated index.html
113 map { s/config\.cfg$/index.html/; ($_ => 1) }
114 # full path => relative uri
115 map path2uri( DocSet::Util::abs2rel($_, $full_base_path) ),
116 grep $rsub_keep_ext->($_), # get files with wanted exts
117 grep !$rsub_skip_seen->($_), # skip seen base dirs
118 @{ expand_dir($full_base_path) }
119 };
120  
121 note "Scanning for src files: $full_base_path";
122 $seen{$full_base_path}++;
123 }
124  
125 # dumper \%src_docs;
126 }
127  
128 # this function returns a URI, so its separators are always /
129 sub find_src_doc {
130 my ($resource_rel_path) = @_;
131  
132 # push the html extension, because of autogenerated index.html,
133 # which should be found automatically
134 $exts{html} = 1 unless exists $exts{html};
135  
136 for my $path (keys %src_docs) {
137 if (my $found_path = match_in_doc_src_subset($path,
138 $resource_rel_path)) {
139 return path2uri($found_path);
140 }
141 }
142  
143 # if we didn't find anything so far, it's possible that the path was
144 # specified with a longer prefix, that was needed (the above
145 # searches only the end leaves), so try locate the segments of the
146 # search path and search within maching sub-sets
147 for my $path (@search_paths) {
148 if ($resource_rel_path =~ m|^$path/(.*)|) {
149 if (my $found_path = match_in_doc_src_subset($path, $1)) {
150 return path2uri($found_path);
151 }
152 }
153 }
154  
155 #dumper $src_docs{"docs/1.0"};
156 return;
157 }
158  
159 # accepts the base_path (from the @search_paths) and the rel_path as
160 # args, then it tries to find the match by applying known extensions.
161 #
162 # if matched, returns the whole path relative to the root, otherwise
163 # returns undef
164 sub match_in_doc_src_subset {
165 my ($base_path, $rel_path) = @_;
166 for my $ext (keys %exts) {
167 #print qq{Try: $base_path :: $rel_path.$ext\n};
168 if (exists $src_docs{$base_path}{"$rel_path.$ext"}) {
169 #print qq{Found $base_path/$rel_path.$ext\n};
170 return catdir $base_path, "$rel_path.$ext";
171 }
172 }
173 return;
174 }
175  
176 # set render object: sort of Singleton, it'll complain aloud if the
177 # object is set over the existing object, without first unsetting it
178 sub set_render_obj {
179 Carp::croak("usage: set_render_obj(\$obj) ") unless @_;
180 Carp::croak("unset render_obj before setting a new one") if $render_obj;
181 Carp::croak("undefined render_obj passed") unless defined $_[0];
182 $render_obj = shift;
183 }
184  
185 sub get_render_obj {
186 Carp::croak("render_obj is not available") unless $render_obj;
187  
188 return $render_obj;
189 }
190  
191 sub unset_render_obj {
192 Carp::croak("render_obj is not set") unless $render_obj;
193  
194 undef $render_obj;
195 }
196  
197  
198 1;
199 __END__