Merge branch 'patch-3' of https://github.com/gjones2/Freeside (fix closing tag)
[freeside.git] / rt / lib / RT / Pod / HTMLBatch.pm
1 use strict;
2 use warnings;
3
4 package RT::Pod::HTMLBatch;
5 use base 'Pod::Simple::HTMLBatch';
6
7 use List::MoreUtils qw/all/;
8
9 use RT::Pod::Search;
10 use RT::Pod::HTML;
11
12 sub new {
13     my $self = shift->SUPER::new(@_);
14     $self->verbose(0);
15
16     # Per-page output options
17     $self->css_flurry(0);          # No CSS
18     $self->javascript_flurry(0);   # No JS
19     $self->no_contents_links(1);   # No header/footer "Back to contents" links
20
21     # TOC options
22     $self->index(1);                    # Write a per-page TOC
23     $self->contents_file("index.html"); # Write a global TOC
24
25     $self->html_render_class('RT::Pod::HTML');
26     $self->search_class('RT::Pod::Search');
27
28     return $self;
29 }
30
31 sub classify {
32     my $self = shift;
33     my %info = (@_);
34
35     my $is_install_doc = sub {
36         my %page = @_;
37         local $_ = $page{name};
38         return 1 if /^(README|UPGRADING)/;
39         return 1 if $_ eq "RT_Config";
40         return 1 if $_ eq "web_deployment";
41         return 1 if $page{infile} =~ m{^configure(\.ac)?$};
42         return 0;
43     };
44
45     my $section = $info{infile} =~ m{/plugins/([^/]+)}      ? "05 Extension: $1"           :
46                   $info{infile} =~ m{/local/}               ? '04 Local Documenation'      :
47                   $is_install_doc->(%info)                  ? '00 Install and Upgrade '.
48                                                                  'Documentation'           :
49                   $info{infile} =~ m{/(docs|etc)/}          ? '01 User Documentation'      :
50                   $info{infile} =~ m{/bin/}                 ? '02 Utilities (bin)'         :
51                   $info{infile} =~ m{/sbin/}                ? '03 Utilities (sbin)'        :
52                   $info{name}   =~ /^RT::Action/            ? '08 Actions'                 :
53                   $info{name}   =~ /^RT::Condition/         ? '09 Conditions'              :
54                   $info{name}   =~ /^RT(::|$)/              ? '07 Developer Documentation' :
55                   $info{infile} =~ m{/devel/tools/}         ? '20 Utilities (devel/tools)' :
56                                                               '06 Miscellaneous'           ;
57
58     if ($info{infile} =~ m{/(docs|etc)/}) {
59         $info{name} =~ s/_/ /g;
60         $info{name} = join "/", map { ucfirst } split /::/, $info{name};
61     }
62
63     return ($info{name}, $section);
64 }
65
66 sub write_contents_file {
67     my ($self, $to) = @_;
68     return unless $self->contents_file;
69
70     my $file = join "/", $to, $self->contents_file;
71     open my $index, ">", $file
72         or warn "Unable to open index file '$file': $!\n", return;
73
74     my $pages = $self->_contents;
75     return unless @$pages;
76
77     # Classify
78     my %toc;
79     for my $page (@$pages) {
80         my ($name, $infile, $outfile, $pieces) = @$page;
81
82         my ($title, $section) = $self->classify(
83             name    => $name,
84             infile  => $infile,
85         );
86
87         (my $path = $outfile) =~ s{^\Q$to\E/?}{};
88
89         push @{ $toc{$section} }, {
90             name => $title,
91             path => $path,
92         };
93     }
94
95     # Write out index
96     print $index "<dl class='superindex'>\n";
97
98     for my $key (sort keys %toc) {
99         next unless @{ $toc{$key} };
100
101         (my $section = $key) =~ s/^\d+ //;
102         print $index "<dt>", esc($section), "</dt>\n";
103         print $index "<dd>\n";
104
105         my @sorted = sort {
106             my @names = map { $_->{name} } $a, $b;
107
108             # Sort just the upgrading docs descending within everything else
109             @names = reverse @names
110                 if all { /^UPGRADING-/ } @names;
111
112             $names[0] cmp $names[1]
113         } @{ $toc{$key} };
114
115         for my $page (@sorted) {
116             print $index "  <a href='", esc($page->{path}), "'>",
117                                 esc($page->{name}),
118                            "</a><br>\n";
119         }
120         print $index "</dd>\n";
121     }
122     print $index '</dl>';
123
124     close $index;
125 }
126
127 sub esc {
128     Pod::Simple::HTMLBatch::esc(@_);
129 }
130
131 1;