6 use CSS::Minifier::XS 0.07;
9 use File::Path qw(make_path);
13 use JavaScript::Minifier::XS;
18 my $canonical_url = 'http://www.exim.org/';
21 my %opt = parse_arguments();
24 my %cache; # General cache object
25 do_doc( 'spec', $_ ) foreach @{ $opt{spec} || [] };
26 do_doc( 'filter', $_ ) foreach @{ $opt{filter} || [] };
27 do_web() if ( $opt{web} );
29 ## Add the exim-html-current symlink
30 print "Symlinking exim-html-current to exim-html-$opt{latest}\n" if ( $opt{verbose} );
31 unlink("$opt{docroot}/exim-html-current") if ( -l "$opt{docroot}/exim-html-current" );
32 symlink( "exim-html-$opt{latest}", "$opt{docroot}/exim-html-current" )
33 || die "symlink to $opt{docroot}/exim-html-current failed";
35 ## Generate the website files
38 ## Make sure the template web directory exists
39 die "No such directory: $opt{tmpl}/web\n" unless -d "$opt{tmpl}/web";
41 ## Scan the web templates
45 substr( $File::Find::name, length("$opt{tmpl}/web"), length($File::Find::name) ) =~ m#^/*(.*)$#;
47 if ( -d "$opt{tmpl}/web/$path" ) {
49 ## Create the directory in the doc root if it doesn't exist
50 if ( !-d "$opt{docroot}/$path" ) {
51 mkdir("$opt{docroot}/$path") or die "Unable to make $opt{docroot}/$path: $!\n";
57 ## Build HTML from XSL files and simply copy static files which have changed
58 if ( $path =~ /(.+)\.xsl$/ ) {
59 print "Generating : docroot:/$1.html\n" if ( $opt{verbose} );
60 transform( undef, "$opt{tmpl}/web/$path", "$opt{docroot}/$1.html" );
62 elsif ( -f "$opt{tmpl}/web/$path" ) {
64 ## Skip if the file hasn't changed (mtime based)
66 if -f "$opt{docroot}/$path"
67 && ( stat("$opt{tmpl}/web/$path") )[9] == ( stat("$opt{docroot}/$path") )[9];
69 if ( $path =~ /(.+)\.css$/ ) {
70 print "CSS to : docroot:/$path\n" if ( $opt{verbose} );
71 my $content = read_file("$opt{tmpl}/web/$path");
72 write_file( "$opt{docroot}/$path",
73 $opt{minify} ? CSS::Minifier::XS::minify($content) : $content );
75 elsif ( $path =~ /(.+)\.js$/ ) {
76 print "JS to : docroot:/$path\n" if ( $opt{verbose} );
77 my $content = read_file("$opt{tmpl}/web/$path");
78 write_file( "$opt{docroot}/$path",
79 $opt{minify} ? JavaScript::Minifier::XS::minify($content) : $content );
83 print "Copying to : docroot:/$path\n" if ( $opt{verbose} );
84 copy( "$opt{tmpl}/web/$path", "$opt{docroot}/$path" ) or die "$path: $!";
87 utime( time, ( stat("$opt{tmpl}/web/$path") )[9], "$opt{docroot}/$path" );
96 ## Generate index/chapter files for a doc
98 my ( $type, $xml_path ) = @_;
100 ## Read and validate the XML file
101 my $xml = XML::LibXML->new()->parse_file($xml_path) or die $!;
103 ## Get the version number
104 my $version = $xml->findvalue('/book/bookinfo/revhistory/revision/revnumber');
105 die "Unable to get version number\n" unless defined $version && $version =~ /^\d+(\.\d+)*$/;
107 ## Prepend chapter filenames?
108 my $prepend_chapter = $type eq 'filter' ? 'filter_' : '';
110 ## Add the canonical url for this document
111 $xml->documentElement()
112 ->appendTextChild( 'canonical_url',
113 "${canonical_url}exim-html-current/doc/html/spec_html/" . ( $type eq 'spec' ? 'index' : 'filter' ) . ".html" );
115 ## Add a url for the latest version of this document
116 if ( $version ne $opt{latest} ) {
117 $xml->documentElement()
118 ->appendTextChild( 'current_url',
119 "../../../../exim-html-current/doc/html/spec_html/" . ( $type eq 'spec' ? 'index' : 'filter' ) . ".html" );
123 xref_fixup( $xml, $prepend_chapter );
125 ## Generate the front page
127 my $path = "exim-html-$version/doc/html/spec_html/" . ( $type eq 'filter' ? $type : 'index' ) . ".html";
128 print "Generating : docroot:/$path\n" if ( $opt{verbose} );
129 transform( $xml, "$opt{tmpl}/doc/index.xsl", "$opt{docroot}/$path", );
132 ## Generate a Table of Contents XML file
135 "exim-html-$version/doc/html/spec_html/" . ( $type eq 'filter' ? 'filter_toc' : 'index_toc' ) . ".xml";
136 print "Generating : docroot:/$path\n" if ( $opt{verbose} );
137 transform( $xml, "$opt{tmpl}/doc/toc.xsl", "$opt{docroot}/$path", );
140 ## Generate the chapters
142 my @chapters = map { $_->cloneNode(1) } $xml->findnodes('/book/chapter');
143 foreach my $chapter (@chapters) {
145 ## Add a <chapter_id>N</chapter_id> node for the stylesheet to use
146 $chapter->appendTextChild( 'chapter_id', ++$counter );
148 ## Add previous/next/canonical urls for nav
150 $chapter->appendTextChild( 'prev_url',
155 : sprintf( '%sch%02d.html', $prepend_chapter, $counter - 1 ) );
156 $chapter->appendTextChild( 'next_url', sprintf( '%sch%02d.html', $prepend_chapter, $counter + 1 ) )
157 unless int(@chapters) == $counter;
158 $chapter->appendTextChild( 'toc_url', ( $type eq 'filter' ? 'filter' : 'index' ) . '.html' );
159 $chapter->appendTextChild(
162 'http://www.exim.org/exim-html-current/doc/html/spec_html/%sch%02d.html',
163 $prepend_chapter, $counter
166 if ( $version ne $opt{latest} ) {
167 $chapter->appendTextChild(
170 '../../../../exim-html-current/doc/html/spec_html/%sch%02d.html',
171 $prepend_chapter, $counter
177 ## Create an XML document from the chapter
178 my $doc = XML::LibXML::Document->createDocument( '1.0', 'UTF-8' );
179 $doc->setDocumentElement($chapter);
181 ## Transform the chapter into html
183 my $path = sprintf( 'exim-html-%s/doc/html/spec_html/%sch%02d.html', $version, $prepend_chapter, $counter );
184 print "Generating : docroot:/$path\n" if ( $opt{verbose} );
185 transform( $doc, "$opt{tmpl}/doc/chapter.xsl", "$opt{docroot}/$path", );
192 my ( $xml, $prepend_chapter ) = @_;
196 ## Add the "prepend_chapter" info
197 ( $xml->findnodes('/book') )[0]->appendTextChild( 'prepend_chapter', $prepend_chapter );
199 ## Iterate over each chapter
200 my $chapter_counter = 0;
201 foreach my $chapter ( $xml->findnodes('/book/chapter') ) {
204 my $chapter_id = $chapter->getAttribute('id');
205 unless ($chapter_id) { # synthesise missing id
206 $chapter_id = sprintf( 'chapter_noid_%04d', $chapter_counter );
207 $chapter->setAttribute( 'id', $chapter_id );
209 my $chapter_title = $chapter->findvalue('title');
211 $index{$chapter_id} = { chapter_id => $chapter_counter, chapter_title => $chapter_title };
213 ## Iterate over each section
214 my $section_counter = 0;
215 foreach my $section ( $chapter->findnodes('section') ) {
218 my $section_id = $section->getAttribute('id');
219 unless ($section_id) { # synthesise missing id
220 $section_id = sprintf( 'section_noid_%04d_%04d', $chapter_counter, $section_counter );
221 $section->setAttribute( 'id', $section_id );
223 my $section_title = $section->findvalue('title');
225 $index{$section_id} = {
226 chapter_id => $chapter_counter,
227 chapter_title => $chapter_title,
228 section_id => $section_counter,
229 section_title => $section_title
233 ## Build indexes as new chapters
234 build_indexes( $xml, $prepend_chapter, \%index );
236 ## Replace all of the xrefs in the XML
237 foreach my $xref ( $xml->findnodes('//xref') ) {
238 my $linkend = $xref->getAttribute('linkend');
239 if ( exists $index{$linkend} ) {
240 $xref->setAttribute( 'chapter_id', $index{$linkend}{'chapter_id'} );
241 $xref->setAttribute( 'chapter_title', $index{$linkend}{'chapter_title'} );
242 $xref->setAttribute( 'section_id', $index{$linkend}{'section_id'} ) if ( $index{$linkend}{'section_id'} );
243 $xref->setAttribute( 'section_title', $index{$linkend}{'section_title'} )
244 if ( $index{$linkend}{'section_title'} );
245 $xref->setAttribute( 'url',
246 sprintf( '%sch%02d.html', $prepend_chapter, $index{$linkend}{'chapter_id'} )
247 . ( $index{$linkend}{'section_id'} ? '#' . $linkend : '' ) );
254 my ( $xml, $prepend_chapter, $xref ) = @_;
258 foreach my $node ( $xml->findnodes('//section | //chapter | //indexterm') ) {
259 if ( $node->nodeName eq 'indexterm' ) {
260 my $role = $node->getAttribute('role') || 'concept';
261 my $primary = $node->findvalue('child::primary');
262 my $first = ( $primary =~ /^[A-Za-z]/ ) ? uc( substr( $primary, 0, 1 ) ) : ''; # first letter or marker
263 my $secondary = $node->findvalue('child::secondary') || '';
264 next unless ( $primary || $secondary ); # skip blank entries for now...
265 $index_hash->{$role}{$first}{$primary}{$secondary} ||= [];
266 push @{ $index_hash->{$role}{$first}{$primary}{$secondary} }, $current_id;
269 $current_id = $node->getAttribute('id');
273 # now we build a set of new chapters with the index data in
274 my $book = ( $xml->findnodes('/book') )[0];
275 foreach my $role ( sort { $a cmp $b } keys %{$index_hash} ) {
276 my $chapter = XML::LibXML::Element->new('chapter');
277 $book->appendChild($chapter);
278 $chapter->setAttribute( 'id', join( '_', 'index', $role ) );
279 $chapter->setAttribute( 'class', 'index' );
280 $chapter->appendTextChild( 'title', ( ucfirst($role) . ' Index' ) );
281 foreach my $first ( sort { $a cmp $b } keys %{ $index_hash->{$role} } ) {
282 my $section = XML::LibXML::Element->new('section');
283 my $list = XML::LibXML::Element->new('variablelist');
284 $chapter->appendChild($section);
285 $section->setAttribute( 'id', join( '_', 'index', $role, $first ) );
286 $section->setAttribute( 'class', 'index' );
287 $section->appendTextChild( 'title', $first ? $first : 'Symbols' );
288 $section->appendChild($list);
289 foreach my $primary ( sort { $a cmp $b } keys %{ $index_hash->{$role}{$first} } ) {
290 my $entry = XML::LibXML::Element->new('varlistentry');
291 my $item = XML::LibXML::Element->new('listitem');
292 $list->appendChild($entry)->appendTextChild( 'term', $primary );
293 $entry->appendChild($item);
295 foreach my $secondary ( sort { $a cmp $b } keys %{ $index_hash->{$role}{$first}{$primary} } ) {
296 my $para = XML::LibXML::Element->new('para');
297 if ( $secondary eq '' ) {
298 $item->appendChild($para); # skip having extra layer of heirarchy
302 $slist = XML::LibXML::Element->new('variablelist');
303 $item->appendChild($slist);
305 my $sentry = XML::LibXML::Element->new('varlistentry');
306 my $sitem = XML::LibXML::Element->new('listitem');
307 $slist->appendChild($sentry)->appendTextChild( 'term', $secondary );
308 $sentry->appendChild($sitem)->appendChild($para);
311 foreach my $ref ( @{ $index_hash->{$role}{$first}{$primary}{$secondary} } ) {
312 $para->appendText(', ')
314 my $xrefel = XML::LibXML::Element->new('xref');
315 $xrefel->setAttribute( linkend => $ref );
316 $xrefel->setAttribute( longref => 1 );
317 $para->appendChild($xrefel);
325 ## Handle the transformation
327 my ( $xml, $xsl_path, $out_path ) = @_;
329 ## Build an empty XML structure if an undefined $xml was passed
330 unless ( defined $xml ) {
331 $xml = XML::LibXML::Document->createDocument( '1.0', 'UTF-8' );
332 $xml->setDocumentElement( $xml->createElement('content') );
335 ## Add the current version of Exim to the XML
336 $xml->documentElement()->appendTextChild( 'current_version', $opt{latest} );
338 ## Add the old versions of Exim to the XML
339 $xml->documentElement()->appendTextChild( 'old_versions', $_ ) foreach old_docs_versions();
341 ## Parse the ".xsl" file as XML
342 my $xsl = XML::LibXML->new()->parse_file($xsl_path) or die $!;
344 ## Generate a stylesheet from the ".xsl" XML.
345 my $stylesheet = XML::LibXSLT->new()->parse_stylesheet($xsl);
347 ## Generate a doc from the XML transformed with the XSL
348 my $doc = $stylesheet->transform($xml);
350 ## Make the containing directory if it doesn't exist
351 make_path( ( $out_path =~ /^(.+)\/.+$/ )[0], { verbose => $opt{verbose} } );
353 ## Write out the document
354 open my $out, '>', $out_path or die "Unable to write $out_path - $!";
355 print $out $stylesheet->output_as_bytes($doc);
359 ## Look in the docroot for old versions of the documentation
360 sub old_docs_versions {
361 if ( !exists $cache{old_docs_versions} ) {
363 foreach ( glob("$opt{docroot}/exim-html-*") ) {
364 push @versions, $1 if /-(\d+(?:\.\d+)?)$/ && $1 < $opt{latest} && -d $_;
366 $cache{old_docs_versions} = [ reverse sort { $a <=> $b } @versions ];
368 return @{ $cache{old_docs_versions} };
376 pod2usage( -exitval => 1, -verbose => 0 );
380 sub parse_arguments {
382 my %opt = ( spec => [], filter => [], help => 0, man => 0, web => 0, minify => 1, verbose => 0 );
384 \%opt, 'help|h!', 'man!', 'web!', 'spec=s{1,}', 'filter=s{1,}',
385 'latest=s', 'tmpl=s', 'docroot=s', 'minify!', 'verbose!'
386 ) || pod2usage( -exitval => 1, -verbose => 0 );
389 pod2usage(0) if ( $opt{help} );
390 pod2usage( -verbose => 2 ) if ( $opt{man} );
392 ## --spec and --filter lists
393 foreach my $set (qw[spec filter]) {
395 [ map { my $f = File::Spec->rel2abs($_); help( 1, 'No such file: ' . $_ ) unless -f $f; $f }
399 error_help('Missing value for latest') unless ( exists( $opt{latest} ) && defined( $opt{latest} ) );
400 error_help('Invalid value for latest') unless $opt{latest} =~ /^\d+(?:\.\d+)*$/;
402 ## --tmpl and --docroot
403 foreach my $set (qw[tmpl docroot]) {
404 error_help( 'Missing value for ' . $set ) unless ( exists( $opt{$set} ) && defined( $opt{$set} ) );
405 my $f = File::Spec->rel2abs( $opt{$set} );
406 error_help( 'No such directory: ' . $opt{$set} ) unless -d $f;
409 error_help('Excess arguments') if ( scalar(@ARGV) );
411 error_help('Must include at least one of --web, --spec or --filter')
412 unless ( $opt{web} || scalar( @{ $opt{spec} || [] } ) || scalar( @{ $opt{filter} || [] } ) );
423 gen.pl - Generate exim html documentation and website
430 --help display this help and exits
431 --man displays man page
432 --spec file... spec docbook/XML source files
433 --filter file... filter docbook/XML source files
434 --web Generate the general website pages
435 --latest VERSION Required. Specify the latest stable version of Exim.
436 --tmpl PATH Required. Path to the templates directory
437 --docroot PATH Required. Path to the website document root
438 --[no-]minify [Don't] minify CSS and Javascript
446 Display help and exits
452 =item B<--spec> I<file...>
454 List of files that make up the specification documentation
455 docbook/XML source files.
457 =item B<--filter> I<file...>
459 List of files that make up the filter documentation docbook/XML
464 Generate the website from the template files.
466 =item B<--latest> I<version>
468 Specify the current exim version. This is used to create links to
469 the current documentation.
471 This option is I<required>
473 =item B<--tmpl> I<directory>
475 Specify the directory that the templates are kept in.
477 This option is I<required>
479 =item B<--docroot> I<directory>
481 Specify the directory that the output should be generated into.
482 This is the website C<docroot> directory.
484 This option is I<required>
488 If this option is set then both the CSS and Javascript files
489 processed are minified using L<CSS::Minifier::XS> and
490 L<JavaScript::Minifier::XS> respectively.
492 This option is set by default - to disable it specify C<--no-minify>
498 Generates the exim website and HTML documentation.
504 --spec docbook/*/spec.xml \
505 --filter docbook/*/filter.xml \
508 --docroot /tmp/website
514 Nigel Metheringham <nigel@exim.org> - mostly broke the framework
519 Copyright 2010-2011 Exim Maintainers. All rights reserved.