/usr/share/perl5/Padre/Browser.pm is in padre 1.00+dfsg-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | package Padre::Browser;
use 5.008;
use strict;
use warnings;
use Carp ();
use Scalar::Util ();
use Padre::Browser::POD ();
our $VERSION = '1.00';
use Class::XSAccessor {
getters => {
get_providers => 'providers',
get_viewers => 'viewers',
get_schemes => 'schemes',
},
setters => {
set_providers => 'providers',
set_viewers => 'viewers',
set_schemes => 'schemes',
},
};
=pod
=head1 NAME
Padre::Browser -- documentation browser for Padre
=head1 DESCRIPTION
Provide an interface for retrieving / generating documentation, resolving terms
to documentation (search?) and formatting documentation.
Allow new packages to be loaded and interrogated for the MIME types they can
generate documentation for. Provide similar mechanism for registering new
documentation viewers and URI schemes accepted for resolving.
B<NOTE:> I think all the method names are wrong. Blast it.
=head1 SYNOPSIS
# Does perlish things by default via 'Padre::Browser::POD'
my $browser = Padre::Browser->new;
my $source = Padre::Document->new( filename=>'source/Package.pm' );
my $docs = $browser->docs( $source );
# $docs provided by Browser::POD->generate
# should be Padre::Browser::Document , application/x-pod
my $output = $browser->browse( $docs );
# $output provided by Browser::POD->render
# should be Padre::Document , text/x-html
$browser->load_viewer( 'Padre::Browser::PodAdvanced' );
# PodAdvanced->render might add an html TOC in addition to
# just pod2html
my $new_output = $browser->browse( $docs );
# $new_output now with a table of contents
=head1 METHODS
=head2 new
Boring constructor, pass nothing. Yet.
=head2 load_provider
Accepts a single class name, will attempt to auto-L<use> the class and
interrogate its C<provider_for> method. Any MIME types returned will be
associated with the class for dispatch to C<generate>.
Additionally, interrogate class for C<accept_schemes> and associate the class
with URI schemes for dispatch to C<resolve>.
=head2 load_viewer
Accepts a single class name, will attempt to auto-L<use> the class and
interrogate its C<viewer_for> method. Any MIME types returned will be
associated with the class for dispatch to C<render>.
=head2 resolve
Accepts a URI or scalar
=head2 browse
=head2 accept
=head1 EXTENDING
package My::Browser::Doxygen;
# URI of doxygen:$string or doxygen://path?query
sub accept_schemes {
'doxygen',
}
sub provider_for {
'text/x-c++src'
}
sub viewer_for {
'text/x-doxygen',
}
sub generate {
my ($self,$doc) = @_;
# $doc will be Padre::Document of any type specified
# by ->provider_for
# push $doc through doxygen
# ...
# that was easy :)
# You know your own output type, be explicit
my $response = Padre::Document->new;
$response->{original_content} = $doxygen->output;
$response->set_mimetype( 'text/x-doxygen' );
return $response;
}
sub render {
my ($self,$docs) = @_;
# $docs will be of any type specified
# by ->viewer_for;
## turn $docs into doxygen(y) html document
# ...
#
my $response = Padre::Document->new;
$response->{original_content} = $doxy2html->output;
$response->set_mimetype( 'text/x-html' );
return $response;
}
=cut
sub new {
my ( $class, %args ) = @_;
my $self = bless \%args, ref($class) || $class;
$self->set_providers( {} ) unless $args{providers};
$self->set_viewers( {} ) unless $args{viewers};
$self->set_schemes( {} ) unless $args{schemes};
# Provides pod from perl, pod: perldoc: schemes
$self->load_provider('Padre::Browser::POD');
# Produces html view of POD
$self->load_viewer('Padre::Browser::POD');
return $self;
}
# Load a class, safely and efficiently
sub _load_class {
my $class = shift;
( my $source = "$class.pm" ) =~ s{::}{/}g;
eval { require $source };
}
sub load_provider {
my ( $self, $class ) = @_;
unless ( $class->VERSION ) {
_load_class($class)
or die "Failed to load $class: $@";
}
if ( $class->can('provider_for') ) {
$self->register_providers( $_ => $class ) for $class->provider_for;
} else {
Carp::confess("$class is not a provider for anything.");
}
if ( $class->can('accept_schemes') ) {
$self->register_schemes( $_ => $class ) for $class->accept_schemes;
} else {
Carp::confess("$class accepts no uri schemes");
}
return $self;
}
sub load_viewer {
my ( $self, $class ) = @_;
unless ( $class->VERSION ) {
_load_class($class)
or die("Failed to load $class: $@");
}
if ( $class->can('viewer_for') ) {
$self->register_viewers( $_ => $class ) for $class->viewer_for;
}
$self;
}
sub register_providers {
my ( $self, %provides ) = @_;
while ( my ( $type, $class ) = each %provides ) {
# TO DO - handle collisions, ie multi providers
# (Ticket #673)
$self->get_providers->{$type} = $class;
}
$self;
}
sub register_viewers {
my ( $self, %viewers ) = @_;
while ( my ( $type, $class ) = each %viewers ) {
$self->get_viewers->{$type} = $class;
unless ( $class->VERSION ) {
_load_class($class)
or die("Failed to load $class: $@");
}
}
$self;
}
sub register_schemes {
my ( $self, %schemes ) = @_;
while ( my ( $scheme, $class ) = each %schemes ) {
$self->get_schemes->{$scheme} = $class;
}
$self;
}
sub provider_for {
my ( $self, $type ) = @_;
my $p;
eval {
if ( exists $self->get_providers->{$type} ) {
$p = $self->get_providers->{$type}->new;
}
};
Carp::confess($@) if $@;
return $p;
}
sub accept {
my ( $self, $scheme ) = @_;
if ( defined $self->get_schemes->{$scheme} ) {
return $self->get_schemes->{$scheme};
}
return;
}
sub viewer_for {
my ( $self, $type ) = @_;
my $v;
eval {
if ( exists $self->get_viewers->{$type} ) {
$v = $self->get_viewers->{$type}->new;
}
};
Carp::confess($@) if $@;
return $v;
}
sub docs {
my ( $self, $doc ) = @_;
if ( my $provider = $self->provider_for( $doc->guess_mimetype ) ) {
my $docs = $provider->generate($doc);
return $docs;
}
return;
}
sub resolve {
my ( $self, $ref, $hints ) = @_;
my @refs;
if ( Scalar::Util::blessed($ref) and $ref->isa('URI') ) {
return $self->resolve_uri( $ref, $hints );
}
# TO DO this doubles up if a provider subscribes to multi
# mimetypes .
# (Ticket #674)
foreach my $class ( values %{ $self->get_providers } ) {
my $resp = $class->resolve( $ref, $hints );
push @refs, $resp if $resp;
last if $resp;
}
return $refs[0];
}
sub resolve_uri {
my ( $self, $uri, $hints ) = @_;
my $resolver = $self->accept( $uri->scheme );
return unless $resolver;
my $doc = $resolver->resolve( $uri, $hints );
return $doc;
}
sub browse {
my ( $self, $docs ) = @_;
if ( my $viewer = $self->viewer_for( $docs->mimetype ) ) {
return $viewer->render($docs);
}
return;
}
1;
# Copyright 2008-2013 The Padre development team as listed in Padre.pm.
# LICENSE
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl 5 itself.
|