Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / rt / lib / RT / Pod / HTML.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2013 Best Practical Solutions, LLC
6 #                                          <sales@bestpractical.com>
7 #
8 # (Except where explicitly superseded by other copyright notices)
9 #
10 #
11 # LICENSE:
12 #
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
16 # from www.gnu.org.
17 #
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 #
29 #
30 # CONTRIBUTION SUBMISSION POLICY:
31 #
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
37 #
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
46 #
47 # END BPS TAGGED BLOCK }}}
48
49 use strict;
50 use warnings;
51
52 package RT::Pod::HTML;
53 use base 'Pod::Simple::XHTML';
54
55 use HTML::Entities qw//;
56
57 sub new {
58     my $self = shift->SUPER::new(@_);
59     $self->index(1);
60     $self->anchor_items(1);
61     return $self;
62 }
63
64 sub decode_entities {
65     my $self = shift;
66     return HTML::Entities::decode_entities($_[0]);
67 }
68
69 sub perldoc_url_prefix { "http://metacpan.org/module/" }
70
71 sub html_header { '' }
72 sub html_footer {
73     my $self = shift;
74     my $toc  = "../" x ($self->batch_mode_current_level - 1);
75     return '<a href="./' . $toc . '">&larr; Back to index</a>';
76 }
77
78 sub start_F {
79     $_[0]{'scratch_F'} = $_[0]{'scratch'};
80     $_[0]{'scratch'}   = "";
81 }
82 sub end_F   {
83     my $self = shift;
84     my $text = $self->{scratch};
85     my $file = $self->decode_entities($text);
86
87     if (my $local = $self->resolve_local_link($file)) {
88         $text = qq[<a href="$local">$text</a>];
89     }
90
91     $self->{'scratch'} = delete $self->{scratch_F};
92     $self->{'scratch'} .= "<i>$text</i>";
93 }
94
95 sub _end_head {
96     my $self = shift;
97     $self->{scratch} = '<a href="#___top">' . $self->{scratch} . '</a>';
98     return $self->SUPER::_end_head(@_);
99 }
100
101 sub resolve_pod_page_link {
102     my $self = shift;
103     my ($name, $section) = @_;
104
105     # Only try to resolve local links if we're in batch mode and are linking
106     # outside the current document.
107     return $self->SUPER::resolve_pod_page_link(@_)
108         unless $self->batch_mode and $name;
109
110     my $local = $self->resolve_local_link($name, $section);
111
112     return $local
113         ? $local
114         : $self->SUPER::resolve_pod_page_link(@_);
115 }
116
117 sub resolve_local_link {
118     my $self = shift;
119     my ($name, $section) = @_;
120
121     $section = defined $section
122         ? '#' . $self->idify($section, 1)
123         : '';
124
125     my $local;
126     if ($name =~ /^RT::/) {
127         $local = join "/",
128                   map { $self->encode_entities($_) }
129                 split /::/, $name;
130     }
131     elsif ($name =~ /^rt[-_]/) {
132         $local = $self->encode_entities($name);
133     }
134     elsif ($name eq "RT_Config" or $name eq "RT_Config.pm") {
135         $local = "RT_Config";
136     }
137     # These matches handle links that look like filenames, such as those we
138     # parse out of F<> tags.
139     elsif (   $name =~ m{^(?:lib/)(RT/[\w/]+?)\.pm$}
140            or $name =~ m{^(?:docs/)(.+?)\.pod$})
141     {
142         $local = join "/",
143                   map { $self->encode_entities($_) }
144                 split /\//, $1;
145     }
146
147     if ($local) {
148         # Resolve links correctly by going up
149         my $depth = $self->batch_mode_current_level - 1;
150         return ($depth ? "../" x $depth : "") . "$local.html$section";
151     } else {
152         return;
153     }
154 }
155
156 1;