1 |
package Frey::Pod; |
2 |
use Moose; |
3 |
|
4 |
=head1 NAME |
5 |
|
6 |
Frey::Pod - display documentation |
7 |
|
8 |
=cut |
9 |
|
10 |
extends 'Frey::Class::Loader'; |
11 |
with 'Frey::Web'; |
12 |
with 'Frey::File'; |
13 |
|
14 |
has 'class' => ( |
15 |
is => 'rw', |
16 |
isa => 'Str', |
17 |
required => 1, |
18 |
default => 'Frey::Manual', |
19 |
); |
20 |
|
21 |
use Pod::Find qw/pod_where/; |
22 |
use Data::Dump qw/dump/; |
23 |
|
24 |
=head2 as_markup |
25 |
|
26 |
my $html = $o->as_markup; |
27 |
|
28 |
my ( $toc_html, $html ) = $o->as_markup; |
29 |
|
30 |
=cut |
31 |
|
32 |
sub as_markup { |
33 |
my $self = shift; |
34 |
my $class = $self->class; |
35 |
use Pod::Simple::HTML; |
36 |
my $path = pod_where( { -inc => 1 }, $class ); |
37 |
return $self->error( "Can't find pod for $class\n" ) unless $path; |
38 |
my $pod = $self->read_file( $path ); |
39 |
my $converter = Pod::Simple::HTML->new(); |
40 |
my $body; |
41 |
my $my_classes = join('|', $self->classes); |
42 |
$converter->output_string( \$body ); |
43 |
$converter->parse_string_document($pod); |
44 |
$body =~ s{.*?<body [^>]+>}{}s; |
45 |
$body =~ s{</body>\s*</html>\s*$}{}; |
46 |
$body =~ s!%3A%3A!::!g; |
47 |
# $body =~ s{<a href="http://search\.cpan\.org/perldoc\?($my_classes)"([^>]*)>}{<a href="/$1"$2>}g; |
48 |
$body =~ s{<a href="http://(search\.cpan\.org/perldoc\?)([^"]+)"([^>]*)>([^<]+)<([^>]+)>}{<a href="/$2"$3>$4<$5><sup><a target="$1" title="CPAN" style="text-decoration: none" href="http://$1$2"$3>◊<$5></sup>}g; |
49 |
$body =~ s!</li>\n\t<ul>!<ul>!; |
50 |
$body =~ s!</ul>!</ul></li>!; |
51 |
$body =~ s!<p></p>!!; |
52 |
$body =~ s!__index__!index!g; |
53 |
|
54 |
our @toc = (); |
55 |
|
56 |
sub heading { |
57 |
my ($level,$html) = @_; |
58 |
push @toc, { $level => $html }; |
59 |
warn "## heading $level $html" if $self->debug; |
60 |
qq|<$level>$html</$level>|; |
61 |
} |
62 |
$body =~ s{<(h\d+)>(.+?)</\1>}{heading($1,$2)}egs; |
63 |
|
64 |
$self->title( $class ); |
65 |
|
66 |
# $body .= $self->html_dump( $toc ); |
67 |
warn "# toc ", dump( @toc ); |
68 |
|
69 |
my $toc_html = ''; |
70 |
my $current_level = 0; |
71 |
foreach my $entry ( @toc ) { |
72 |
my ( $level, $html ) = %$entry; |
73 |
|
74 |
if ( $level =~ m{h(\d+)} ) { |
75 |
my $num = $1; |
76 |
if ( $num > $current_level ) { |
77 |
if ( ! $toc_html ) { # first ul |
78 |
$toc_html .= qq|<ul class="first">|; |
79 |
} else { |
80 |
$toc_html .= qq|<ul>|; |
81 |
} |
82 |
} elsif ( $num < $current_level ) { |
83 |
$toc_html .= qq|</ul>|; |
84 |
} |
85 |
$current_level = $num; |
86 |
} |
87 |
|
88 |
my $target = $html; |
89 |
$target =~ s{<[^>]+/?>}{}gs; # remove html |
90 |
$target = qq|<a href="#$2">$target</a>| if $html =~ m{<a[^<]+name=(['"]?)([^'"<]+?)\1[^<]+>}; |
91 |
|
92 |
$toc_html .= qq|<li title="$level">$target</li>\n|; |
93 |
} |
94 |
|
95 |
$toc_html .= qq|</ul>| while ( $current_level-- ); |
96 |
|
97 |
if ( $toc_html && ! wantarray ) { |
98 |
$self->add_css(qq| |
99 |
.pod-toc { |
100 |
float: right; |
101 |
background: #eee; |
102 |
font-size: 80%; |
103 |
} |
104 |
.pod-toc .first { |
105 |
padding-left: 1em; |
106 |
padding-right: 1em; |
107 |
} |
108 |
.pod-toc ul > li { |
109 |
list-style: none; |
110 |
} |
111 |
.pod-toc a { |
112 |
text-decoration: none; |
113 |
} |
114 |
|
115 |
|); |
116 |
$toc_html = qq|<div class="pod-toc">$toc_html</div>|; |
117 |
} |
118 |
|
119 |
$self->add_css(qq| |
120 |
pre { |
121 |
color: #444; |
122 |
border: 1px solid #eee; |
123 |
padding-top: 0.5em; |
124 |
padding-bottom: 0.5em; |
125 |
} |
126 |
|); |
127 |
|
128 |
return ( $toc_html , $body ) if wantarray; |
129 |
return $toc_html . $body; |
130 |
|
131 |
} |
132 |
|
133 |
1; |
134 |
|