File Coverage

lib/Kwiki/PerlTidyModule.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Kwiki::PerlTidyModule;
2 1     1   44044 use Kwiki::Plugin -Base;
  0            
  0            
3             use Kwiki::Installer -Base;
4             our $VERSION = '0.12';
5              
6             const class_id => 'perl_tidy_module';
7             const css_file => 'perl_tidy_module.css';
8              
9             sub register {
10             my $registry = shift;
11             $registry->add(prerequisite => 'cache');
12             $registry->add(wafl => perl_tidy_module => 'Kwiki::PerlTidyModule::Wafl');
13             }
14              
15             package Kwiki::PerlTidyModule::Wafl;
16             use Spoon::Formatter;
17             use base 'Spoon::Formatter::WaflPhrase';
18              
19             sub to_html {
20             my $thing = $self->arguments;
21             my $file_name = $thing =~ /^[a-z0-9_]+$/
22             ? $self->file_from_class_id($thing)
23             : $thing =~ /^[\w\:]+$/
24             ? $self->file_from_module($thing)
25             : '';
26             return $self->wafl_error
27             unless $file_name;
28             return join '',
29             qq{<table class="perl_tidy_module"><tr><td><pre>\n},
30             $self->tidy($file_name),
31             qq{</pre></td></tr></table>\n};
32             }
33              
34             sub file_from_class_id {
35             my $class_id = shift;
36             my $object = eval {$self->hub->$class_id}
37             or return;
38             $self->file_from_module(ref($object));
39             }
40              
41             sub file_from_module {
42             my $module = shift;
43             eval "use $module; 1"
44             or return;
45             $module =~ s/::/\//g;
46             $module .= '.pm';
47             return $INC{$module};
48             }
49              
50             sub tidy {
51             my $file_name = shift;
52             my $source = io($file_name)->slurp;
53             return $self->escape_html($source)
54             unless $file_name =~ /\.(pl|pm|cgi|t|dd)$/;
55             my $html = $self->hub->cache->process(
56             sub { $self->cache_this($source) }, 'perl_tidy_module', $source
57             );
58             $html =~ s/^<pre>\s*(.*)\s*<\/pre>\s*\z/$1/s;
59             return $html;
60             }
61              
62             sub cache_this {
63             my $source = shift;
64             require Perl::Tidy;
65             my $result;
66             eval {
67             Perl::Tidy::perltidy(
68             source => \$source,
69             destination => \$result,
70             argv => [qw( -q -html -pre -npro )],
71             );
72             };
73             $@ ? $source : $result;
74             }
75              
76             package Kwiki::PerlTidyModule;
77             __DATA__
78              
79             =head1 NAME
80              
81             Kwiki::PerlTidyModule - Kwiki Perl Tidy Module Plugin
82              
83             =head1 SYNOPSIS
84              
85             =head1 DESCRIPTION
86              
87             =head1 AUTHOR
88              
89             Brian Ingerson <INGY@cpan.org>
90              
91             =head1 COPYRIGHT
92              
93             Copyright (c) 2004. Brian Ingerson. All rights reserved.
94              
95             This program is free software; you can redistribute it and/or modify it
96             under the same terms as Perl itself.
97              
98             See http://www.perl.com/perl/misc/Artistic.html
99              
100             =cut
101             __css/perl_tidy_module.css__
102             table.perl_tidy_module {
103             border-collapse: collapse;
104             margin: .5em;
105             }
106              
107             table.perl_tidy_module td {
108             border: 1px;
109             border-style: solid;
110             padding: .5em;
111             }
112              
113             .pd { color: #404080;} /* pod-text */
114             .c { color: #404080;} /* comment */
115              
116             .cm { color: #800097;} /* comma */
117             .co { color: #800097;} /* colon */
118              
119             .h { color: #804848;} /* here-doc-target */
120             .hh { color: #800000;} /* here-doc-text */
121             .q { color: #800000;} /* quote */
122             .v { color: #800000;} /* v-string */
123              
124             .i { color: #008080;} /* identifier */
125              
126             .k { color: #0000FF;} /* keyword */
127             .n { color: #E02020;} /* numeric */
128              
129             .m { color: #C00080;} /* subroutine */
130             .j { color: #C00080;} /* label */
131             .w { color: #C00080;} /* bareword */
132              
133             .p { color: #800080;} /* paren */
134             .s { color: #800080;} /* structure */
135             .sc { color: #800080;} /* semicolon */
136              
137             .pu { color: #C44800;} /* punctuation */