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