Application/Model/Pod.pm untested

filter: Statementstmt 0.0% 0 / 104 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 20 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 186 CC 20 SCAR 34.8
CC
20
Cov
0%
get_folder_info
72.0
show_pod
56.0
resolve_pod_page_link
20.0
CRAP 32.4
1 package QBit::Application::Model::Pod;
2 $QBit::Application::Model::Pod::VERSION = '0.003';
3 use qbit;
4  
5 use base qw(QBit::Application::Model);
6  
7 use Pod::Checker;
8  
9 sub clear_path {
10 my ($path) = @_;
11  
12 $path =~ s/[\/\\]{2}/\//g;
13  
14 return $path;
15 }
16  
17 sub folder_has_pod {
18 my ($folder) = @_;
19  
20 my @files = <$folder/*>;
21 chomp(@files);
22  
23 foreach my $file (@files) {
24 return TRUE if -d $file && folder_has_pod($file) || $file =~ /\.(?:pm|pod)$/ && -f $file;
25 }
26  
27 return FALSE;
28 }
29  
30 sub check_pod {
31 my ($file) = @_;
32  
33 open(F, ">", '/dev/null');
34 return podchecker($file, \*F, -warnings => 0);
35 }
36  
37 sub get_folder_info {
38 my ($base_paths, $path) = @_;
39  
40 my %res = ();
41 foreach my $base_path (@$base_paths) {
42 my $folder = clear_path("${base_path}${path}");
43  
44 my @files = <$folder/*>;
45 chomp(@files);
46  
47 foreach my $file (@files) {
48 my $filename = $file;
49 $filename = $1 if $filename =~ /([^\\\/]+)$/;
50  
51 next if exists($res{$filename});
52  
53 if (-d $file) {
54 $res{$filename} = {type => 1, name => $filename} if folder_has_pod($file);
55 next;
56 }
57  
58 if ($file =~ /\.(?:pm|pod)/ && -f $file) {
59 my $pod_chk = check_pod($file);
60  
61 unless ($pod_chk) {
62 $res{$filename} = {type => 2, name => $filename};
63 } else {
64 if ($pod_chk == -1) {
65 $res{$filename} = {type => 3, name => $filename};
66 } else {
67 $res{$filename} = {type => 4, name => $filename};
68 }
69 }
70 }
71 }
72 }
73  
74 return [values(%res)];
75 }
76  
77 sub pod_errors {
78 my ($file) = @_;
79  
80 my $data = '';
81 open(F, ">", \$data);
82 my $err_cnt = podchecker($file, \*F, -warnings => 0);
83 close(F);
84  
85 return $err_cnt ? $data : undef;
86 }
87  
88 sub get_pod {
89 my ($file) = @_;
90  
91 my $pod_errors = pod_errors($file);
92 if ($pod_errors) {
93 $pod_errors =~ s/\n/<br>/g;
94 return $pod_errors;
95 }
96  
97 my $text = '<a name="___top"><h1>Index</h1></a>';
98 my $pod = QBit::Application::Model::Pod::HTML->new();
99 $pod->bare_output(TRUE);
100 $pod->index(TRUE);
101 $pod->output_string(\$text);
102 $pod->parse_file($file);
103  
104 return $text;
105 }
106  
107 sub show_pod {
108 my ($self, $base_paths, $path, %opts) = @_;
109  
110 $base_paths ||= ['/'];
111 $base_paths = [$base_paths] if ref($base_paths) ne 'ARRAY';
112  
113 $path = "/$path" unless $path =~ /^[\\\/]/;
114 $path =~ s/\.\.//g;
115  
116 foreach my $base_path (@$base_paths) {
117 my $file = clear_path("${base_path}${path}");
118  
119 next unless -e $file;
120  
121 return {type => 1, data => get_folder_info($base_paths, $path), path => clear_path($path)} if -d $file;
122  
123 if (-f $file) {
124 my $name = clear_path($path);
125 my $fname = $1 if $name =~ /([^\\\/]+?)$/;
126 $name =~ s/^[\\\/]+//;
127 $name =~ s/[\\\/]/::/g;
128 $name =~ s/\.pm$//;
129 $path =~ s/[^\\\/]+$//;
130 return {type => 2, data => get_pod($file), path => clear_path($path), name => $name, fname => $fname};
131 }
132 }
133 }
134  
135 package QBit::Application::Model::Pod::HTML;
136 $QBit::Application::Model::Pod::HTML::VERSION = '0.003';
137 use qbit;
138  
139 use base qw(Pod::Simple::HTML);
140  
141 sub resolve_pod_page_link {
142 my ($self, $to, $section) = @_;
143  
144 my $url = '';
145  
146 if (defined($to) && length($to)) {
147 my ($path, $file) = ('', '');
148 ($path, $file) = ($1, $2) if $to =~ /^(.*?)(?:\:\:)?([\w\d_]+)$/;
149 $path =~ s/::/\//g;
150 $path = uri_escape($path);
151 $file = uri_escape($file);
152  
153 $url .= "?path=/$path&file=$file.pm";
154 }
155  
156 if (defined($section) && length($section)) {
157 $url .= "#$section";
158 }
159  
160 return $url;
161 }
162  
163 package Pod::Checker;
164 $Pod::Checker::VERSION = '0.003';
165 no warnings 'redefine';
166  
167 sub hyperlink { } # Ugly hack to disable checking internal links
168  
169 1;