3cf3acd52e32929f48cea8d0ace4f80856fa349f
[exim-website.git] / script / gen.pl
1 #!/usr/bin/env perl
2 #
3 use strict;
4 use warnings;
5
6 use CSS::Minifier::XS 0.07;
7 use File::Copy;
8 use File::Find;
9 use File::Path qw(make_path);
10 use File::Slurp;
11 use File::Spec;
12 use Getopt::Long;
13 use JavaScript::Minifier::XS;
14 use Pod::Usage;
15 use XML::LibXML;
16 use XML::LibXSLT;
17
18 my $canonical_url = 'http://www.exim.org/';
19
20 ## Parse arguments
21 my %opt = parse_arguments();
22
23 ## Generate the pages
24 do_doc( 'spec',   $_ ) foreach @{ $opt{spec}   || [] };
25 do_doc( 'filter', $_ ) foreach @{ $opt{filter} || [] };
26 do_web() if exists $opt{web};
27
28 ## Add the exim-html-current symlink
29 print "Symlinking exim-html-current to exim-html-$opt{latest}\n";
30 unlink("$opt{docroot}/exim-html-current") if ( -l "$opt{docroot}/exim-html-current" );
31 symlink( "exim-html-$opt{latest}", "$opt{docroot}/exim-html-current" )
32     || die "symlink to $opt{docroot}/exim-html-current failed";
33
34 ## Generate the website files
35 sub do_web {
36
37     ## Make sure the template web directory exists
38     die "No such directory: $opt{tmpl}/web\n" unless -d "$opt{tmpl}/web";
39
40     ## Scan the web templates
41     find(
42         sub {
43             my ($path) =
44                 substr( $File::Find::name, length("$opt{tmpl}/web"), length($File::Find::name) ) =~ m#^/*(.*)$#;
45
46             if ( -d "$opt{tmpl}/web/$path" ) {
47
48                 ## Create the directory in the doc root if it doesn't exist
49                 if ( !-d "$opt{docroot}/$path" ) {
50                     mkdir("$opt{docroot}/$path") or die "Unable to make $opt{docroot}/$path: $!\n";
51                 }
52
53             }
54             else {
55
56                 ## Build HTML from XSL files and simply copy static files which have changed
57                 if ( $path =~ /(.+)\.xsl$/ ) {
58                     print "Generating  : docroot:/$1.html\n" if ( $opt{verbose} );
59                     transform( undef, "$opt{tmpl}/web/$path", "$opt{docroot}/$1.html" );
60                 }
61                 elsif ( -f "$opt{tmpl}/web/$path" ) {
62
63                     ## Skip if the file hasn't changed (mtime based)
64                     return
65                         if -f "$opt{docroot}/$path"
66                             && ( stat("$opt{tmpl}/web/$path") )[9] == ( stat("$opt{docroot}/$path") )[9];
67
68                     if ( $path =~ /(.+)\.css$/ ) {
69                         print "CSS to  : docroot:/$path\n" if ( $opt{verbose} );
70                         my $content = read_file("$opt{tmpl}/web/$path");
71                         write_file( "$opt{docroot}/$path",
72                             $opt{minify} ? CSS::Minifier::XS::minify($content) : $content );
73                     }
74                     elsif ( $path =~ /(.+)\.js$/ ) {
75                         print "JS to  : docroot:/$path\n" if ( $opt{verbose} );
76                         my $content = read_file("$opt{tmpl}/web/$path");
77                         write_file( "$opt{docroot}/$path",
78                             $opt{minify} ? JavaScript::Minifier::XS::minify($content) : $content );
79                     }
80                     else {
81                         ## Copy
82                         print "Copying to  : docroot:/$path\n" if ( $opt{verbose} );
83                         copy( "$opt{tmpl}/web/$path", "$opt{docroot}/$path" ) or die "$path: $!";
84                     }
85                     ## Set mtime
86                     utime( time, ( stat("$opt{tmpl}/web/$path") )[9], "$opt{docroot}/$path" );
87                 }
88             }
89
90         },
91         "$opt{tmpl}/web"
92     );
93 }
94
95 ## Generate index/chapter files for a doc
96 sub do_doc {
97     my ( $type, $xml_path ) = @_;
98
99     ## Read and validate the XML file
100     my $xml = XML::LibXML->new()->parse_file($xml_path) or die $!;
101
102     ## Get the version number
103     my $version = $xml->findvalue('/book/bookinfo/revhistory/revision/revnumber');
104     die "Unable to get version number\n" unless defined $version && $version =~ /^\d+(\.\d+)*$/;
105
106     ## Prepend chapter filenames?
107     my $prepend_chapter = $type eq 'filter' ? 'filter_' : '';
108
109     ## Add the canonical url for this document
110     $xml->documentElement()
111         ->appendTextChild( 'canonical_url',
112         "${canonical_url}exim-html-current/doc/html/spec_html/" . ( $type eq 'spec' ? 'index' : 'filter' ) . ".html" );
113
114     ## Add a url for the latest version of this document
115     if( $version ne $opt{latest} ){
116        $xml->documentElement()
117            ->appendTextChild( 'current_url',
118            "../../../../exim-html-current/doc/html/spec_html/" . ( $type eq 'spec' ? 'index' : 'filter' ) . ".html" );
119     }
120
121     ## Fixup the XML
122     xref_fixup( $xml, $prepend_chapter );
123
124     ## Generate the front page
125     {
126         my $path = "exim-html-$version/doc/html/spec_html/" . ( $type eq 'filter' ? $type : 'index' ) . ".html";
127         print "Generating  : docroot:/$path\n";
128         transform( $xml, "$opt{tmpl}/doc/index.xsl", "$opt{docroot}/$path", );
129     }
130
131     ## Generate a Table of Contents XML file
132     {
133         my $path =
134             "exim-html-$version/doc/html/spec_html/" . ( $type eq 'filter' ? 'filter_toc' : 'index_toc' ) . ".xml";
135         print "Generating  : docroot:/$path\n" if ( $opt{verbose} );
136         transform( $xml, "$opt{tmpl}/doc/toc.xsl", "$opt{docroot}/$path", );
137     }
138
139     ## Generate the chapters
140     my $counter = 0;
141     my @chapters = map { $_->cloneNode(1) } $xml->findnodes('/book/chapter');
142     foreach my $chapter ( @chapters ) {
143
144         ## Add a <chapter_id>N</chapter_id> node for the stylesheet to use
145         $chapter->appendTextChild( 'chapter_id', ++$counter );
146
147         ## Add previous/next/canonical urls for nav
148         {
149             $chapter->appendTextChild( 'prev_url',
150                   $counter == 1
151                 ? $type eq 'filter'
152                         ? 'filter.html'
153                         : 'index.html'
154                 : sprintf( '%sch%02d.html', $prepend_chapter, $counter - 1 ) );
155             $chapter->appendTextChild( 'next_url', sprintf( '%sch%02d.html', $prepend_chapter, $counter + 1 ) ) unless int(@chapters) == $counter;
156             $chapter->appendTextChild(
157                 'canonical_url',
158                 sprintf(
159                     'http://www.exim.org/exim-html-current/doc/html/spec_html/%sch%02d.html',
160                     $prepend_chapter, $counter
161                 )
162             );
163             if( $version ne $opt{latest} ){
164                $chapter->appendTextChild(
165                   'current_url',
166                   sprintf(
167                      '../../../../exim-html-current/doc/html/spec_html/%sch%02d.html',
168                      $prepend_chapter, $counter
169                   )
170                );
171            }
172         }
173
174         ## Create an XML document from the chapter
175         my $doc = XML::LibXML::Document->createDocument( '1.0', 'UTF-8' );
176         $doc->setDocumentElement($chapter);
177
178         ## Transform the chapter into html
179         {
180             my $path = sprintf( 'exim-html-%s/doc/html/spec_html/%sch%02d.html', $version, $prepend_chapter, $counter );
181             print "Generating  : docroot:/$path\n" if ( $opt{verbose} );
182             transform( $doc, "$opt{tmpl}/doc/chapter.xsl", "$opt{docroot}/$path", );
183         }
184     }
185 }
186
187 ## Fixup xref tags
188 sub xref_fixup {
189     my ( $xml, $prepend_chapter ) = @_;
190
191     my %index = ();
192
193     ## Add the "prepend_chapter" info
194     ( $xml->findnodes('/book') )[0]->appendTextChild( 'prepend_chapter', $prepend_chapter );
195
196     ## Iterate over each chapter
197     my $chapter_counter = 0;
198     foreach my $chapter ( $xml->findnodes('/book/chapter') ) {
199         ++$chapter_counter;
200
201         my $chapter_id = $chapter->getAttribute('id');
202         unless ($chapter_id) {    # synthesise missing id
203             $chapter_id = sprintf( 'chapter_noid_%04d', $chapter_counter );
204             $chapter->setAttribute( 'id', $chapter_id );
205         }
206         my $chapter_title = $chapter->findvalue('title');
207
208         $index{$chapter_id} = { chapter_id => $chapter_counter, chapter_title => $chapter_title };
209
210         ## Iterate over each section
211         my $section_counter = 0;
212         foreach my $section ( $chapter->findnodes('section') ) {
213             ++$section_counter;
214
215             my $section_id = $section->getAttribute('id');
216             unless ($section_id) {    # synthesise missing id
217                 $section_id = sprintf( 'section_noid_%04d_%04d', $chapter_counter, $section_counter );
218                 $section->setAttribute( 'id', $section_id );
219             }
220             my $section_title = $section->findvalue('title');
221
222             $index{$section_id} = {
223                 chapter_id    => $chapter_counter,
224                 chapter_title => $chapter_title,
225                 section_id    => $section_counter,
226                 section_title => $section_title
227             };
228         }
229     }
230     ## Build indexes as new chapters
231     build_indexes( $xml, $prepend_chapter, \%index );
232
233     ## Replace all of the xrefs in the XML
234     foreach my $xref ( $xml->findnodes('//xref') ) {
235         my $linkend = $xref->getAttribute('linkend');
236         if ( exists $index{$linkend} ) {
237             $xref->setAttribute( 'chapter_id',    $index{$linkend}{'chapter_id'} );
238             $xref->setAttribute( 'chapter_title', $index{$linkend}{'chapter_title'} );
239             $xref->setAttribute( 'section_id', $index{$linkend}{'section_id'} ) if ( $index{$linkend}{'section_id'} );
240             $xref->setAttribute( 'section_title', $index{$linkend}{'section_title'} )
241                 if ( $index{$linkend}{'section_title'} );
242             $xref->setAttribute( 'url',
243                 sprintf( '%sch%02d.html', $prepend_chapter, $index{$linkend}{'chapter_id'} )
244                     . ( $index{$linkend}{'section_id'} ? '#' . $linkend : '' ) );
245         }
246     }
247 }
248
249 ## Build indexes
250 sub build_indexes {
251     my ( $xml, $prepend_chapter, $xref ) = @_;
252
253     my $index_hash = {};
254     my $current_id;
255     foreach my $node ( $xml->findnodes('//section | //chapter | //indexterm') ) {
256         if ( $node->nodeName eq 'indexterm' ) {
257             my $role      = $node->getAttribute('role') || 'concept';
258             my $primary   = $node->findvalue('child::primary');
259             my $first     = ( $primary =~ /^[A-Za-z]/ ) ? uc( substr( $primary, 0, 1 ) ) : '';  # first letter or marker
260             my $secondary = $node->findvalue('child::secondary') || '';
261             next unless ( $primary || $secondary );    # skip blank entries for now...
262             $index_hash->{$role}{$first}{$primary}{$secondary} ||= [];
263             push @{ $index_hash->{$role}{$first}{$primary}{$secondary} }, $current_id;
264         }
265         else {
266             $current_id = $node->getAttribute('id');
267         }
268     }
269
270     # now we build a set of new chapters with the index data in
271     my $book = ( $xml->findnodes('/book') )[0];
272     foreach my $role ( sort { $a cmp $b } keys %{$index_hash} ) {
273         my $chapter = XML::LibXML::Element->new('chapter');
274         $book->appendChild($chapter);
275         $chapter->setAttribute( 'id', join( '_', 'index', $role ) );
276         $chapter->setAttribute( 'class', 'index' );
277         $chapter->appendTextChild( 'title', ( ucfirst($role) . ' Index' ) );
278         foreach my $first ( sort { $a cmp $b } keys %{ $index_hash->{$role} } ) {
279             my $section = XML::LibXML::Element->new('section');
280             my $list    = XML::LibXML::Element->new('variablelist');
281             $chapter->appendChild($section);
282             $section->setAttribute( 'id', join( '_', 'index', $role, $first ) );
283             $section->setAttribute( 'class', 'index' );
284             $section->appendTextChild( 'title', $first ? $first : 'Symbols' );
285             $section->appendChild($list);
286             foreach my $primary ( sort { $a cmp $b } keys %{ $index_hash->{$role}{$first} } ) {
287                 my $entry = XML::LibXML::Element->new('varlistentry');
288                 my $item  = XML::LibXML::Element->new('listitem');
289                 $list->appendChild($entry)->appendTextChild( 'term', $primary );
290                 $entry->appendChild($item);
291                 my $slist;
292                 foreach my $secondary ( sort { $a cmp $b } keys %{ $index_hash->{$role}{$first}{$primary} } ) {
293                     my $para = XML::LibXML::Element->new('para');
294                     if ( $secondary eq '' ) {
295                         $item->appendChild($para);    # skip having extra layer of heirarchy
296                     }
297                     else {
298                         unless ($slist) {
299                             $slist = XML::LibXML::Element->new('variablelist');
300                             $item->appendChild($slist);
301                         }
302                         my $sentry = XML::LibXML::Element->new('varlistentry');
303                         my $sitem  = XML::LibXML::Element->new('listitem');
304                         $slist->appendChild($sentry)->appendTextChild( 'term', $secondary );
305                         $sentry->appendChild($sitem)->appendChild($para);
306                     }
307                     my $count = 0;
308                     foreach my $ref ( @{ $index_hash->{$role}{$first}{$primary}{$secondary} } ) {
309                         $para->appendText(', ')
310                             if ( $count++ );
311                         my $xrefel = XML::LibXML::Element->new('xref');
312                         $xrefel->setAttribute( linkend => $ref );
313                         $xrefel->setAttribute( longref => 1 );
314                         $para->appendChild($xrefel);
315                     }
316                 }
317             }
318         }
319     }
320 }
321
322 ## Handle the transformation
323 sub transform {
324     my ( $xml, $xsl_path, $out_path ) = @_;
325
326     ## Build an empty XML structure if an undefined $xml was passed
327     unless ( defined $xml ) {
328         $xml = XML::LibXML::Document->createDocument( '1.0', 'UTF-8' );
329         $xml->setDocumentElement( $xml->createElement('content') );
330     }
331
332     ## Add the current version of Exim to the XML
333     $xml->documentElement()->appendTextChild( 'current_version', $opt{latest} );
334
335     ## Parse the ".xsl" file as XML
336     my $xsl = XML::LibXML->new()->parse_file($xsl_path) or die $!;
337
338     ## Generate a stylesheet from the ".xsl" XML.
339     my $stylesheet = XML::LibXSLT->new()->parse_stylesheet($xsl);
340
341     ## Generate a doc from the XML transformed with the XSL
342     my $doc = $stylesheet->transform($xml);
343
344     ## Make the containing directory if it doesn't exist
345     make_path( ( $out_path =~ /^(.+)\/.+$/ )[0], { verbose => 1 } );
346
347     ## Write out the document
348     open my $out, '>', $out_path or die "Unable to write $out_path - $!";
349     print $out $stylesheet->output_as_bytes($doc);
350     close $out;
351 }
352
353 ## error_help
354 sub error_help {
355     my $msg = shift;
356
357     warn $msg;
358     pod2usage( -exitval => 1, -verbose => 0 );
359 }
360
361 ## Parse arguments
362 sub parse_arguments {
363
364     my %opt = ( spec => [], filter => [], help => 0, man => 0, web => 0, minify => 1, verbose => 0 );
365     GetOptions(
366         \%opt,      'help|h!', 'man!',      'web!',    'spec=s{1,}', 'filter=s{1,}',
367         'latest=s', 'tmpl=s',  'docroot=s', 'minify!', 'verbose!'
368     ) || pod2usage( -exitval => 1, -verbose => 0 );
369
370     ## --help
371     pod2usage(0) if ( $opt{help} );
372     pod2usage( -verbose => 2 ) if ( $opt{man} );
373
374     ## --spec and --filter lists
375     foreach my $set (qw[spec filter]) {
376         $opt{$set} =
377             [ map { my $f = File::Spec->rel2abs($_); help( 1, 'No such file: ' . $_ ) unless -f $f; $f }
378                 @{ $opt{$set} } ];
379     }
380     ## --latest
381     error_help('Missing value for latest') unless ( exists( $opt{latest} ) && defined( $opt{latest} ) );
382     error_help('Invalid value for latest') unless $opt{latest} =~ /^\d+(?:\.\d+)*$/;
383
384     ## --tmpl and --docroot
385     foreach my $set (qw[tmpl docroot]) {
386         error_help( 'Missing value for ' . $set ) unless ( exists( $opt{$set} ) && defined( $opt{$set} ) );
387         my $f = File::Spec->rel2abs( $opt{$set} );
388         error_help( 'No such directory: ' . $opt{$set} ) unless -d $f;
389         $opt{$set} = $f;
390     }
391     error_help('Excess arguments') if ( scalar(@ARGV) );
392
393     error_help('Must include at least one of --web, --spec or --filter')
394         unless ( $opt{web} || scalar( @{ $opt{spec} || [] } ) || scalar( @{ $opt{filter} || [] } ) );
395
396     return %opt;
397 }
398
399 1;
400
401 __END__
402
403 =head1 NAME
404
405 gen.pl - Generate exim html documentation and website
406
407 =head1 SYNOPSIS
408
409 gen.pl [options]
410
411  Options:
412    --help              display this help and exits
413    --man               displays man page
414    --spec file...      spec docbook/XML source files
415    --filter file...    filter docbook/XML source files
416    --web               Generate the general website pages
417    --latest VERSION    Required. Specify the latest stable version of Exim.
418    --tmpl PATH         Required. Path to the templates directory
419    --docroot PATH      Required. Path to the website document root
420    --[no-]minify       [Don't] minify CSS and Javascript    
421
422 =head1 OPTIONS
423
424 =over 4
425
426 =item B<--help>
427
428 Display help and exits
429
430 =item B<--man>
431
432 Display man page
433
434 =item B<--spec> I<file...>
435
436 List of files that make up the specification documentation
437 docbook/XML source files.
438
439 =item B<--filter> I<file...>
440
441 List of files that make up the filter documentation docbook/XML
442 source files.
443
444 =item B<--web>
445
446 Generate the website from the template files.
447
448 =item B<--latest> I<version>
449
450 Specify the current exim version. This is used to create links to
451 the current documentation.
452
453 This option is I<required>
454
455 =item B<--tmpl> I<directory>
456
457 Specify the directory that the templates are kept in.
458
459 This option is I<required>
460
461 =item B<--docroot> I<directory>
462
463 Specify the directory that the output should be generated into.
464 This is the website C<docroot> directory.
465
466 This option is I<required>
467
468 =item B<--minify>
469
470 If this option is set then both the CSS and Javascript files
471 processed are minified using L<CSS::Minifier::XS> and
472 L<JavaScript::Minifier::XS> respectively.
473
474 This option is set by default - to disable it specify C<--no-minify>
475
476 =back
477
478 =head1 DESCRIPTION
479
480 Generates the exim website and HTML documentation.
481
482 =head1 EXAMPLE
483
484     script/gen.pl \
485       --web \
486       --spec docbook/*/spec.xml \
487       --filter  docbook/*/filter.xml \
488       --latest 4.72 \
489       --tmpl templates \
490       --docroot /tmp/website
491
492 =head1 AUTHOR
493
494 Mike Cardwell
495
496 Nigel Metheringham <nigel@exim.org> - mostly broke the framework
497 Mike produced.
498
499 =head1 COPYRIGHT
500
501 Copyright 2010-2011 Exim Maintainers. All rights reserved.
502
503 =cut