summaryrefslogtreecommitdiffstats
path: root/docs/makedocs.pl
blob: 93bdd5190a2a60b951323689086d58d4bcffa1f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/perl
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

# This script compiles all the documentation.
#
# Required software:
#
# 1) Sphinx documentation builder (python-sphinx package on Debian/Ubuntu)
#
# 2a) rst2pdf
# or
# 2b) pdflatex, which means the following Debian/Ubuntu packages:
#     * texlive-latex-base
#     * texlive-latex-recommended
#     * texlive-latex-extra
#     * texlive-fonts-recommended
#
# All these TeX packages together are close to a gig :-| But after you've
# installed them, you can remove texlive-latex-extra-doc to save 400MB.

use 5.14.0;
use strict;
use warnings;

use File::Basename;
BEGIN { chdir dirname($0); }

use lib qw(.. ../lib lib ../local/lib/perl5);
use open ':encoding(utf8)';

use Cwd;
use File::Copy::Recursive qw(rcopy);
use File::Path qw(rmtree make_path);
use File::Which qw(which);
use Pod::Simple;
use Pod::ParseLink;

use Bugzilla::Constants qw(BUGZILLA_VERSION bz_locations);
use Pod::Simple::Search;
use Pod::POM::View::Restructured;
use Tie::File;
use File::Spec::Functions qw(:ALL);

###############################################################################
# Subs
###############################################################################

my $error_found = 0;

sub MakeDocs {
    my ($name, $cmdline) = @_;

    say "Creating $name documentation ..." if defined $name;
    system('make', $cmdline) == 0
        or $error_found = 1;
    print "\n";
}

# Compile all the POD and make it part of the API docs
# Also duplicate some Extension docs into the User section, Admin section, and
# WebService docs
sub pod2rst {
    my $path = shift;

    say "Converting POD to RST...";
    my $name2path = Pod::Simple::Search->new->inc(0)->verbose(0)
        ->survey(@{ ['../'] });

    my $ind_path = catdir($path, 'rst', 'integrating', 'internals');
    rmtree($ind_path);
    make_path($ind_path);

    my $FILE;
    open($FILE, '>', catfile($ind_path, 'index.rst'))
        || die("Can't open rst file");
    print($FILE <<'EOI');
.. highlight:: perl

.. _developer

===================================
Developer Documentation
===================================

This section exposes the POD for all modules and scripts, including extensions,
in Bugzilla.

.. toctree::
   :maxdepth: 2

EOI

    foreach my $mod (sort keys %$name2path) {
        # Ignoring library files;
        if ($mod =~ /^b?(lib::|local)/) {
            delete $name2path->{$mod};
            next;
        }

        my $title = $mod;

        my $ext = $title =~ s/^extensions:://;
        $title =~ s/lib:://;
        $title =~ s/::Extension$// if ($ext);
        my $header = "=" x length($title);

        my $abs_path = $name2path->{$mod};
        my $fpath = abs2rel($abs_path, '..');
        my ($volume, $directories, $file) = splitpath($fpath);
        $directories =~ s/lib\///;
        $directories = '.' if ($directories eq '');

        my $dir_path = catdir($ind_path, $directories);
        make_path($dir_path);
        $file =~ s/\.[^.]*$//;
        my $out_file = $file . '.rst';
        my $full_out_file = catfile($dir_path, $out_file);

        my $callbacks = {
            link => sub {
                my $text = shift;

                my @results = parselink($text);

                # A link to another page.
                if (defined($results[1]) && defined($results[2])) {
                    if ($results[2] =~ /^http/) {
                        return ($results[2], $results[1]);
                    }
                    elsif ($results[2] =~ m/Bugzilla/) {
                        my $depth = scalar(split('/', $directories));
                        my @split = split('::', $results[2]);
                        my $base  = '../' x $depth;
                        my $url   = $base . join('/', @split);
                        $url .= '.html';
                        $url .= '#' . $results[3] if defined $results[3];
                        return ($url, $results[1]);
                    }
                    else {
                        # Not a Bugzilla package, link to CPAN.
                        my $url
                            = 'http://search.cpan.org/search?query='
                            . $results[2]
                            . '&mode=all';
                        return ($url, $results[1]);
                    }
                }

                # A Link within a page
                elsif (defined($results[1]) && defined($results[4])) {
                    my $anchor = $results[1];
                    $anchor =~ s/"//g;
                    return ("#$anchor", $anchor);
                }
                else {
                    die "Don't know how to parse $text";
                }
            }
        };
        my $conv = Pod::POM::View::Restructured->new(
                             { namespace => $title, callbacks => $callbacks });
        my $rv = $conv->convert_file($abs_path, $title, $full_out_file,
                                                                   $callbacks);
        print($FILE "   ", catfile($directories, $out_file), "\n");

        if ($ext) {
            my $api_path = catdir($path, 'rst', $directories);
            my $adminfile = catfile($api_path, "index-admin.rst");
            my $userfile  = catfile($api_path, "index-user.rst");
            my $apifile   = catfile($api_path, 'api', 'v1', 'index.rst');
            make_path($api_path) unless -d $api_path;

            # Add Core doc to User & Admin guides
            if ($file eq 'Extension') {
                my $FH;
                open($FH, '<', $full_out_file) || die "$full_out_file: $!";
                my @lines = <$FH>;
                close $FH;

                my @array;

                # ensure out of order docs are at the top of the page without
                # playing with file handles
                tie @array, 'Tie::File', $adminfile or die "$adminfile: $!";
                unshift @array, @lines, "", "", ".. toctree::", "", "";
                untie @array;

                tie @array, 'Tie::File', $userfile or die "$userfile: $!";
                unshift @array, @lines, "", "", ".. toctree::", "", "";
                untie @array;

            }

            # Add Config doc to Admin guide
            elsif ($file eq 'Config') {
                rcopy($full_out_file, $api_path);
                `perl -E 'say "   $file.rst"' >> "$adminfile"`;
            }

            # Add WebServices doc to API docs
            elsif ($file eq 'WebService') {
                my $apidir = catdir($api_path, 'api', 'v1');
                make_path($apidir)
                    unless -d $apidir;
                `perl -E 'say ".. toctree::\n\n"' >> $apifile`
                    unless -f $apifile;

                my $FH;
                open($FH, '<', $full_out_file) || die "$full_out_file: $!";
                my @lines = <$FH>;
                close $FH;

                my @array;

                tie @array, 'Tie::File', $apifile or die "$apifile: $!";
                unshift @array, @lines;
                untie @array;
            }
            elsif ($api_path =~ /WebService\/$/) {
                my $apidir = catdir($api_path, '..', 'api', 'v1');
                $apifile = catfile($apidir, 'index.rst');
                make_path($apidir) unless -d $apidir;
                rcopy($full_out_file, $apidir);
                `perl -E 'say ".. toctree::\n\n"' >> $apifile`
                    unless -f $apifile;
                `perl -E 'say "   $file.rst"' >> $apifile`;
            }
        }
    }

    close($FILE);
}

###############################################################################
# Make the docs ...
###############################################################################

my @langs;

# search for sub directories which have a 'rst' sub-directory
opendir(LANGS, './');
foreach my $dir (readdir(LANGS)) {
    next if (($dir eq '.') || ($dir eq '..') || (!-d $dir));
    if (-d "$dir/rst") {
        push(@langs, $dir);
    }
}
closedir(LANGS);

my $docparent = getcwd();
foreach my $lang (@langs) {

    rmtree("$lang/html", 0, 1);
    rmtree("$lang/txt",  0, 1);

    my @sub_dirs = grep {-d} glob("$lang/rst/extensions/*");
    rmtree(@sub_dirs, { verbose => 0, safe => 1 });

    pod2rst("$docparent/$lang");

    next if grep { $_ eq '--pod-only' } @ARGV;

    chdir "$docparent/$lang";

    MakeDocs('HTML', 'html');
    MakeDocs('TXT',  'text');

    if (grep { $_ eq '--with-pdf' } @ARGV) {
        if (which('pdflatex')) {
            MakeDocs('PDF', 'latexpdf');
        }
        elsif (which('rst2pdf')) {
            rmtree('pdf', 0, 1);
            MakeDocs('PDF', 'pdf');
        }
        else {
            say 'pdflatex or rst2pdf not found. Skipping PDF file creation';
        }
    }

    rmtree('doctrees', 0, 1);
}

die "Error occurred building the documentation\n" if $error_found;