12 use JavaScript::Minifier::XS;
17 my $canonical_url = 'http://www.exim.org/';
20 my %opt = parse_arguments();
23 do_doc( 'spec', $_ ) foreach @{ $opt{spec} || [] };
24 do_doc( 'filter', $_ ) foreach @{ $opt{filter} || [] };
25 do_web() if exists $opt{web};
27 ## Add the exim-html-current symlink
28 print "Symlinking exim-html-current to exim-html-$opt{latest}\n";
29 symlink( "$opt{docroot}/exim-html-$opt{latest}", "$opt{docroot}/exim-html-current" );
31 ## Generate the website files
34 ## Make sure the template web directory exists
35 die "No such directory: $opt{tmpl}/web\n" unless -d "$opt{tmpl}/web";
37 ## Scan the web templates
40 my ($path) = substr( $File::Find::name, length("$opt{tmpl}/web"), length($File::Find::name) ) =~ m#^/*(.*)$#;
42 if ( -d "$opt{tmpl}/web/$path" ) {
44 ## Create the directory in the doc root if it doesn't exist
45 if ( !-d "$opt{docroot}/$path" ) {
46 mkdir("$opt{docroot}/$path") or die "Unable to make $opt{docroot}/$path: $!\n";
52 ## Build HTML from XSL files and simply copy static files which have changed
53 if ( $path =~ /(.+)\.xsl$/ ) {
54 print "Generating : docroot:/$1.html\n";
55 transform( undef, "$opt{tmpl}/web/$path", "$opt{docroot}/$1.html" );
57 elsif ( -f "$opt{tmpl}/web/$path" ) {
59 ## Skip if the file hasn't changed (mtime based)
60 return if -f "$opt{docroot}/$path" && ( stat("$opt{tmpl}/web/$path") )[9] == ( stat("$opt{docroot}/$path") )[9];
62 if ( $path =~ /(.+)\.css$/ ) {
63 print "CSS to : docroot:/$path\n";
64 my $content = read_file("$opt{tmpl}/web/$path");
65 write_file( "$opt{docroot}/$path", $opt{minify} ? CSS::Minifier::XS::minify($content) : $content );
67 elsif ( $path =~ /(.+)\.js$/ ) {
68 print "JS to : docroot:/$path\n";
69 my $content = read_file("$opt{tmpl}/web/$path");
70 write_file( "$opt{docroot}/$path", $opt{minify} ? JavaScript::Minifier::XS::minify($content) : $content );
74 print "Copying to : docroot:/$path\n";
75 copy( "$opt{tmpl}/web/$path", "$opt{docroot}/$path" ) or die "$path: $!";
78 utime( time, ( stat("$opt{tmpl}/web/$path") )[9], "$opt{docroot}/$path" );
87 ## Generate index/chapter files for a doc
89 my ( $type, $xml_path ) = @_;
91 ## Read and validate the XML file
92 my $xml = XML::LibXML->new()->parse_file($xml_path) or die $!;
94 ## Get the version number
95 my $version = $xml->findvalue('/book/bookinfo/revhistory/revision/revnumber');
96 die "Unable to get version number\n" unless defined $version && $version =~ /^\d+(\.\d+)*$/;
98 ## Prepend chapter filenames?
99 my $prepend_chapter = $type eq 'filter' ? 'filter_' : '';
101 ## Add the canonical url for this document
102 $xml->documentElement()
103 ->appendTextChild( 'canonical_url',
104 "${canonical_url}exim-html-current/doc/html/spec_html/" . ( $type eq 'spec' ? 'index' : 'filter' ) . ".html" );
107 xref_fixup( $xml, $prepend_chapter );
109 ## Generate the front page
111 my $path = "exim-html-$version/doc/html/spec_html/" . ( $type eq 'filter' ? $type : 'index' ) . ".html";
112 print "Generating : docroot:/$path\n";
113 transform( $xml, "$opt{tmpl}/doc/index.xsl", "$opt{docroot}/$path", );
116 ## Generate a Table of Contents XML file
118 my $path = "exim-html-$version/doc/html/spec_html/" . ( $type eq 'filter' ? 'filter_toc' : 'index_toc' ) . ".xml";
119 print "Generating : docroot:/$path\n";
120 transform( $xml, "$opt{tmpl}/doc/toc.xsl", "$opt{docroot}/$path", );
123 ## Generate the chapters
125 foreach my $chapter ( map { $_->cloneNode(1) } $xml->findnodes('/book/chapter') ) {
127 ## Add a <chapter_id>N</chapter_id> node for the stylesheet to use
128 $chapter->appendTextChild( 'chapter_id', ++$counter );
130 ## Add previous/next/canonical urls for nav
132 $chapter->appendTextChild( 'prev_url',
137 : sprintf( '%sch%02d.html', $prepend_chapter, $counter - 1 ) );
138 $chapter->appendTextChild( 'next_url', sprintf( '%sch%02d.html', $prepend_chapter, $counter + 1 ) );
139 $chapter->appendTextChild( 'canonical_url',
140 sprintf( 'http://www.exim.org/exim-html-current/doc/html/spec_html/%sch%02d.html', $prepend_chapter, $counter ) );
143 ## Create an XML document from the chapter
144 my $doc = XML::LibXML::Document->createDocument( '1.0', 'UTF-8' );
145 $doc->setDocumentElement($chapter);
147 ## Transform the chapter into html
149 my $path = sprintf( 'exim-html-%s/doc/html/spec_html/%sch%02d.html', $version, $prepend_chapter, $counter );
150 print "Generating : docroot:/$path\n";
151 transform( $doc, "$opt{tmpl}/doc/chapter.xsl", "$opt{docroot}/$path", );
158 my ( $xml, $prepend_chapter ) = @_;
162 ## Add the "prepend_chapter" info
163 ( $xml->findnodes('/book') )[0]->appendTextChild( 'prepend_chapter', $prepend_chapter );
165 ## Iterate over each chapter
166 my $chapter_counter = 0;
167 foreach my $chapter ( $xml->findnodes('/book/chapter') ) {
170 my $chapter_id = $chapter->getAttribute('id');
171 unless ($chapter_id) { # synthesise missing id
172 $chapter_id = sprintf( 'chapter_noid_%04d', $chapter_counter );
173 $chapter->setAttribute( 'id', $chapter_id );
175 my $chapter_title = $chapter->findvalue('title');
177 $index{$chapter_id} = { chapter_id => $chapter_counter, chapter_title => $chapter_title };
179 ## Iterate over each section
180 my $section_counter = 0;
181 foreach my $section ( $chapter->findnodes('section') ) {
184 my $section_id = $section->getAttribute('id');
185 unless ($section_id) { # synthesise missing id
186 $section_id = sprintf( 'section_noid_%04d_%04d', $chapter_counter, $section_counter );
187 $section->setAttribute( 'id', $section_id );
189 my $section_title = $section->findvalue('title');
191 $index{$section_id} = {
192 chapter_id => $chapter_counter,
193 chapter_title => $chapter_title,
194 section_id => $section_counter,
195 section_title => $section_title
199 ## Build indexes as new chapters
200 build_indexes( $xml, $prepend_chapter, \%index );
202 ## Replace all of the xrefs in the XML
203 foreach my $xref ( $xml->findnodes('//xref') ) {
204 my $linkend = $xref->getAttribute('linkend');
205 if ( exists $index{$linkend} ) {
206 $xref->setAttribute( 'chapter_id', $index{$linkend}{'chapter_id'} );
207 $xref->setAttribute( 'chapter_title', $index{$linkend}{'chapter_title'} );
208 $xref->setAttribute( 'section_id', $index{$linkend}{'section_id'} ) if ( $index{$linkend}{'section_id'} );
209 $xref->setAttribute( 'section_title', $index{$linkend}{'section_title'} ) if ( $index{$linkend}{'section_title'} );
210 $xref->setAttribute( 'url',
211 sprintf( '%sch%02d.html', $prepend_chapter, $index{$linkend}{'chapter_id'} )
212 . ( $index{$linkend}{'section_id'} ? '#' . $linkend : '' ) );
219 my ( $xml, $prepend_chapter, $xref ) = @_;
223 foreach my $node ( $xml->findnodes('//section | //chapter | //indexterm') ) {
224 if ( $node->nodeName eq 'indexterm' ) {
225 my $role = $node->getAttribute('role') || 'concept';
226 my $primary = $node->findvalue('child::primary');
227 my $first = ( $primary =~ /^[A-Za-z]/ ) ? uc( substr( $primary, 0, 1 ) ) : ''; # first letter or marker
228 my $secondary = $node->findvalue('child::secondary') || '';
229 next unless ( $primary || $secondary ); # skip blank entries for now...
230 $index_hash->{$role}{$first}{$primary}{$secondary} ||= [];
231 push @{ $index_hash->{$role}{$first}{$primary}{$secondary} }, $current_id;
234 $current_id = $node->getAttribute('id');
238 # now we build a set of new chapters with the index data in
239 my $book = ( $xml->findnodes('/book') )[0];
240 foreach my $role ( sort { $a cmp $b } keys %{$index_hash} ) {
241 my $chapter = XML::LibXML::Element->new('chapter');
242 $book->appendChild($chapter);
243 $chapter->setAttribute( 'id', join( '_', 'index', $role ) );
244 $chapter->setAttribute( 'class', 'index' );
245 $chapter->appendTextChild( 'title', ( ucfirst($role) . ' Index' ) );
246 foreach my $first ( sort { $a cmp $b } keys %{ $index_hash->{$role} } ) {
247 my $section = XML::LibXML::Element->new('section');
248 my $list = XML::LibXML::Element->new('variablelist');
249 $chapter->appendChild($section);
250 $section->setAttribute( 'id', join( '_', 'index', $role, $first ) );
251 $section->setAttribute( 'class', 'index' );
252 $section->appendTextChild( 'title', $first ? $first : 'Symbols' );
253 $section->appendChild($list);
254 foreach my $primary ( sort { $a cmp $b } keys %{ $index_hash->{$role}{$first} } ) {
255 my $entry = XML::LibXML::Element->new('varlistentry');
256 my $item = XML::LibXML::Element->new('listitem');
257 $list->appendChild($entry)->appendTextChild( 'term', $primary );
258 $entry->appendChild($item);
260 foreach my $secondary ( sort { $a cmp $b } keys %{ $index_hash->{$role}{$first}{$primary} } ) {
261 my $para = XML::LibXML::Element->new('para');
262 if ( $secondary eq '' ) {
263 $item->appendChild($para); # skip having extra layer of heirarchy
267 $slist = XML::LibXML::Element->new('variablelist');
268 $item->appendChild($slist);
270 my $sentry = XML::LibXML::Element->new('varlistentry');
271 my $sitem = XML::LibXML::Element->new('listitem');
272 $slist->appendChild($sentry)->appendTextChild( 'term', $secondary );
273 $sentry->appendChild($sitem)->appendChild($para);
276 foreach my $ref ( @{ $index_hash->{$role}{$first}{$primary}{$secondary} } ) {
277 $para->appendText(', ')
279 my $xrefel = XML::LibXML::Element->new('xref');
280 $xrefel->setAttribute( linkend => $ref );
281 $xrefel->setAttribute( longref => 1 );
282 $para->appendChild($xrefel);
290 ## Handle the transformation
292 my ( $xml, $xsl_path, $out_path ) = @_;
294 ## Build an empty XML structure if an undefined $xml was passed
295 unless ( defined $xml ) {
296 $xml = XML::LibXML::Document->createDocument( '1.0', 'UTF-8' );
297 $xml->setDocumentElement( $xml->createElement('content') );
300 ## Add the current version of Exim to the XML
301 $xml->documentElement()->appendTextChild( 'current_version', $opt{latest} );
303 ## Parse the ".xsl" file as XML
304 my $xsl = XML::LibXML->new()->parse_file($xsl_path) or die $!;
306 ## Generate a stylesheet from the ".xsl" XML.
307 my $stylesheet = XML::LibXSLT->new()->parse_stylesheet($xsl);
309 ## Generate a doc from the XML transformed with the XSL
310 my $doc = $stylesheet->transform($xml);
312 ## Make the containing directory if it doesn't exist
313 mkdirp( ( $out_path =~ /^(.+)\/.+$/ )[0] );
315 ## Write out the document
316 open my $out, '>', $out_path or die $!;
317 print $out $stylesheet->output_as_bytes($doc);
326 foreach ( split( /\//, $path ) ) {
328 my $make = join( '/', @parts );
329 next unless length($make);
331 mkdir($make) or die "Unable to mkdir $make: $!\n";
336 sub parse_arguments {
338 my %opt = ( spec => [], filter => [], help => 0, man => 0, web => 0, minify => 1 );
339 GetOptions( \%opt, 'help|h!', 'man!', 'web!', 'spec=s{1,}', 'filter=s{1,}', 'latest=s', 'tmpl=s', 'docroot=s', 'minify!' )
340 || pod2usage( -exitval => 1, -verbose => 0 );
343 pod2usage(0) if ( $opt{help} );
344 pod2usage( -verbose => 2 ) if ( $opt{man} );
346 ## --spec and --filter lists
347 foreach my $set (qw[spec filter]) {
348 $opt{$set} = [ map { my $f = File::Spec->rel2abs($_); help( 1, 'No such file: ' . $_ ) unless -f $f; $f } @{ $opt{$set} } ];
351 help( 1, 'Missing value for latest' ) unless ( exists( $opt{latest} ) && defined( $opt{latest} ) );
352 help( 1, 'Invalid value for latest' ) unless $opt{latest} =~ /^\d+(?:\.\d+)*$/;
354 ## --tmpl and --docroot
355 foreach my $set (qw[tmpl docroot]) {
356 help( 1, 'Missing value for ' . $set ) unless ( exists( $opt{$set} ) && defined( $opt{$set} ) );
357 my $f = File::Spec->rel2abs( $opt{$set} );
358 help( 1, 'No such directory: ' . $opt{$set} ) unless -d $f;
361 help( 1, 'Excess arguments' ) if ( scalar(@ARGV) );
363 help( 1, 'Must include at least one of --web, --spec or --filter' )
364 unless ( defined $opt{web} || scalar( @{ $opt{spec} } ) || scalar( @{ $opt{web} } ) );
375 gen.pl - Generate exim html documentation and website
382 --help display this help and exits
383 --man displays man page
384 --spec file... spec docbook/XML source files
385 --filter file... filter docbook/XML source files
386 --web Generate the general website pages
387 --latest VERSION Required. Specify the latest stable version of Exim.
388 --tmpl PATH Required. Path to the templates directory
389 --docroot PATH Required. Path to the website document root
390 --[no-]minify [Don't] minify CSS and Javascript
398 Display help and exits
404 =item B<--spec> I<file...>
406 List of files that make up the specification documentation
407 docbook/XML source files.
409 =item B<--filter> I<file...>
411 List of files that make up the filter documentation docbook/XML
416 Generate the website from the template files.
418 =item B<--latest> I<version>
420 Specify the current exim version. This is used to create links to
421 the current documentation.
423 This option is I<required>
425 =item B<--tmpl> I<directory>
427 Specify the directory that the templates are kept in.
429 This option is I<required>
431 =item B<--docroot> I<directory>
433 Specify the directory that the output should be generated into.
434 This is the website C<docroot> directory.
436 This option is I<required>
440 If this option is set then both the CSS and Javascript files
441 processed are minified using L<CSS::Minifier::XS> and
442 L<JavaScript::Minifier::XS> respectively.
444 This option is set by default - to disable it specify C<--no-minify>
450 Generates the exim website and HTML documentation.
456 --spec docbook/*/spec.xml \
457 --filter docbook/*/filter.xml \
460 --docroot /tmp/website
466 Nigel Metheringham <nigel@exim.org> - mostly broke the framework
471 Copyright 2010 Exim Maintainers. All rights reserved.