rt 4.0.23
[freeside.git] / rt / devel / tools / rt-static-docs
1 #!/usr/bin/env perl
2 # BEGIN BPS TAGGED BLOCK {{{
3 #
4 # COPYRIGHT:
5 #
6 # This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
7 #                                          <sales@bestpractical.com>
8 #
9 # (Except where explicitly superseded by other copyright notices)
10 #
11 #
12 # LICENSE:
13 #
14 # This work is made available to you under the terms of Version 2 of
15 # the GNU General Public License. A copy of that license should have
16 # been provided with this software, but in any event can be snarfed
17 # from www.gnu.org.
18 #
19 # This work is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 # General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 # 02110-1301 or visit their web page on the internet at
28 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
29 #
30 #
31 # CONTRIBUTION SUBMISSION POLICY:
32 #
33 # (The following paragraph is not intended to limit the rights granted
34 # to you to modify and distribute this software under the terms of
35 # the GNU General Public License and is only of importance to you if
36 # you choose to contribute your changes and enhancements to the
37 # community by submitting them to Best Practical Solutions, LLC.)
38 #
39 # By intentionally submitting any modifications, corrections or
40 # derivatives to this work, or any other work intended for use with
41 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
42 # you are the copyright holder for those contributions and you grant
43 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
44 # royalty-free, perpetual, license to use, copy, create derivative
45 # works based on those contributions, and sublicense and distribute
46 # those contributions and any derivatives thereof.
47 #
48 # END BPS TAGGED BLOCK }}}
49 use strict;
50 use warnings;
51
52 use Getopt::Long;
53 use File::Temp;
54 use File::Spec;
55 use File::Path qw(make_path rmtree);
56 use File::Copy qw(copy);
57 use HTML::Entities qw(encode_entities);
58 use RT::Pod::HTMLBatch;
59
60 my %opts;
61 GetOptions(
62     \%opts,
63     "help|h",
64     "rt=s",
65     "to=s",
66 );
67
68 if ( $opts{'help'} ) {
69     require Pod::Usage;
70     print Pod::Usage::pod2usage( -verbose => 2 );
71     exit;
72 }
73
74 die "--to=DIRECTORY is required\n"  unless $opts{to};
75
76 $opts{to} = File::Spec->rel2abs($opts{to});
77
78 make_path( $opts{to} )              unless -e $opts{to};
79 die "--to MUST be a directory\n"    unless -d $opts{to};
80
81 # Unpack the tarball, if that's what we're given.
82 my $tmpdir;
83 if (($opts{rt} || '') =~ /\.tar\.gz$/ and -f $opts{rt}) {
84     $tmpdir = File::Temp->newdir();
85
86     system("tar", "xzpf", $opts{rt}, "-C", $tmpdir);
87     $opts{rt} = <$tmpdir/rt-*>;
88 }
89 chdir $opts{rt} if $opts{rt};
90
91 my @dirs = (
92     qw(
93         docs
94         etc
95         lib
96         bin
97         sbin
98         devel/tools
99         local/lib
100         local/sbin
101         local/bin
102     ),
103     glob("local/plugins/*/{lib,sbin,bin}"),
104     glob("docs/UPGRADING*"),
105 );
106
107 my $converter = RT::Pod::HTMLBatch->new;
108
109 sub generate_configure_help {
110     my $configure = shift;
111     my $help = `./$configure --help`;
112     my $dest = "$opts{to}/configure.html";
113
114     if ($help and open my $html, ">", $dest) {
115         print $html join "\n",
116             "<pre>", encode_entities($help), "</pre>", "\n";
117         close $html;
118         $converter->note_for_contents_file(["configure options"], $configure, $dest);
119     } else {
120         warn "Can't open $dest: $!";
121     }
122 }
123
124 # Generate a page for ./configure --help if we can
125 if (-x "configure.ac" and -d ".git") {
126     rmtree("autom4te.cache") if -d "autom4te.cache";
127     generate_configure_help("configure.ac");
128 }
129 elsif (-x "configure") {
130     generate_configure_help("configure");
131 }
132 else {
133     warn "Unable to generate a page for ./configure --help!\n";
134 }
135
136 # Manually "convert" README* and 3.8-era UPGRADING* to HTML and push them into
137 # the known contents.
138 for my $file (<README* UPGRADING*>) {
139     (my $name = $file) =~ s{^.+/}{};
140     my $dest = "$opts{to}/$name.html";
141
142     open my $source, "<", $file
143         or warn "Can't open $file: $!", next;
144
145     open my $html, ">", $dest
146         or warn "Can't open $dest: $!", next;
147
148     print $html "<pre>";
149     print $html encode_entities($_) while <$source>;
150     print $html "</pre>";
151
152     close $source; close $html;
153
154     $converter->note_for_contents_file([$name], $file, $dest);
155 }
156
157 # Copy images into place
158 make_path("$opts{to}/images/");
159 copy($_, "$opts{to}/images/")
160     for <docs/images/*.{png,jpeg,jpg,gif}>;
161
162 # Temporarily set executable bits on upgrading doc to work around
163 # Pod::Simple::Search limitation/bug:
164 #    https://rt.cpan.org/Ticket/Display.html?id=80082
165 sub system_chmod {
166     system("chmod", @_) == 0
167         or die "Unable to chmod: $! (exit $?)";
168 }
169 system_chmod("+x", $_) for <docs/UPGRADING*>;
170
171 # Convert each POD file to HTML
172 $converter->batch_convert( \@dirs, $opts{to} );
173
174 # Remove execution bit from workaround above
175 system_chmod("-x", $_) for <docs/UPGRADING*>;
176
177 # Need to chdir back out, if we are in the tmpdir, to let it clean up
178 chdir "/" if $tmpdir;
179
180 exit 0;
181
182 __END__
183
184 =head1 NAME
185
186 rt-static-docs - generate doc shipped with RT
187
188 =head1 SYNOPSIS
189
190     rt-static-docs --to /path/to/output [--rt /path/to/rt]
191
192 =head1 DESCRIPTION
193
194 RT ships with documentation (written in POD) embedded in library files, at the
195 end of utility scripts, and in standalone files.  This script finds all of that
196 documentation, collects and converts it into a nice set of HTML files, and tops
197 it off with a helpful index.
198
199 Best Practical uses this to publish documentation under
200 L<http://bestpractical.com/rt/docs/>.
201
202 =head1 OPTIONS
203
204 =over
205
206 =item --to
207
208 Set the destination directory for the output files.
209
210 =item --rt
211
212 Set the RT base directory to search under.  Defaults to the current working
213 directory, which is fine if you're running this script as
214 C<devel/tools/rt-static-docs>.
215
216 May also point to a tarball (a file ending in C<.tar.gz>) which will be
217 unpacked into a temporary directory and used as the RT base directory.
218
219 =item --help
220
221 Print this help.
222
223 =back
224
225 =cut