10 use JavaScript::Minifier::XS;
14 my $canonical_url = 'http://www.exim.org/';
17 my %opt = parse_arguments();
20 do_doc( 'spec', $_ ) foreach @{ $opt{spec} || [] };
21 do_doc( 'filter', $_ ) foreach @{ $opt{filter} || [] };
22 do_web() if exists $opt{web};
24 ## Add the exim-html-current symlink
25 print "Symlinking exim-html-current to exim-html-$opt{latest}\n";
26 symlink( "$opt{docroot}/exim-html-$opt{latest}", "$opt{docroot}/exim-html-current" );
28 ## Generate the website files
31 ## Make sure the template web directory exists
32 die "No such directory: $opt{tmpl}/web\n" unless -d "$opt{tmpl}/web";
34 ## Scan the web templates
37 my ($path) = substr( $File::Find::name, length("$opt{tmpl}/web"), length($File::Find::name) ) =~ m#^/*(.*)$#;
39 if ( -d "$opt{tmpl}/web/$path" ) {
41 ## Create the directory in the doc root if it doesn't exist
42 if ( !-d "$opt{docroot}/$path" ) {
43 mkdir("$opt{docroot}/$path") or die "Unable to make $opt{docroot}/$path: $!\n";
49 ## Build HTML from XSL files and simply copy static files which have changed
50 if ( $path =~ /(.+)\.xsl$/ ) {
51 print "Generating : docroot:/$1.html\n";
52 transform( undef, "$opt{tmpl}/web/$path", "$opt{docroot}/$1.html" );
54 elsif ( -f "$opt{tmpl}/web/$path" ) {
56 ## Skip if the file hasn't changed (mtime based)
57 return if -f "$opt{docroot}/$path" && ( stat("$opt{tmpl}/web/$path") )[9] == ( stat("$opt{docroot}/$path") )[9];
59 if ( $path =~ /(.+)\.css$/ ) {
60 print "CSS to : docroot:/$path\n";
61 my $content = read_file("$opt{tmpl}/web/$path");
62 write_file( "$opt{docroot}/$path", $opt{minify} ? CSS::Minifier::XS::minify($content) : $content );
64 elsif ( $path =~ /(.+)\.js$/ ) {
65 print "JS to : docroot:/$path\n";
66 my $content = read_file("$opt{tmpl}/web/$path");
67 write_file( "$opt{docroot}/$path", $opt{minify} ? JavaScript::Minifier::XS::minify($content) : $content );
71 print "Copying to : docroot:/$path\n";
72 copy( "$opt{tmpl}/web/$path", "$opt{docroot}/$path" ) or die "$path: $!";
75 utime( time, ( stat("$opt{tmpl}/web/$path") )[9], "$opt{docroot}/$path" );
84 ## Generate index/chapter files for a doc
86 my ( $type, $xml_path ) = @_;
88 ## Read and validate the XML file
89 my $xml = XML::LibXML->new()->parse_file($xml_path) or die $!;
91 ## Get the version number
92 my $version = $xml->findvalue('/book/bookinfo/revhistory/revision/revnumber');
93 die "Unable to get version number\n" unless defined $version && $version =~ /^\d+(\.\d+)*$/;
95 ## Prepend chapter filenames?
96 my $prepend_chapter = $type eq 'filter' ? 'filter_' : '';
98 ## Add the canonical url for this document
99 $xml->documentElement()
100 ->appendTextChild( 'canonical_url',
101 "${canonical_url}exim-html-current/doc/html/spec_html/" . ( $type eq 'spec' ? 'index' : 'filter' ) . ".html" );
104 xref_fixup( $xml, $prepend_chapter );
106 ## Generate the front page
108 my $path = "exim-html-$version/doc/html/spec_html/" . ( $type eq 'filter' ? $type : 'index' ) . ".html";
109 print "Generating : docroot:/$path\n";
110 transform( $xml, "$opt{tmpl}/doc/index.xsl", "$opt{docroot}/$path", );
113 ## Generate a Table of Contents XML file
115 my $path = "exim-html-$version/doc/html/spec_html/" . ( $type eq 'filter' ? 'filter_toc' : 'index_toc' ) . ".xml";
116 print "Generating : docroot:/$path\n";
117 transform( $xml, "$opt{tmpl}/doc/toc.xsl", "$opt{docroot}/$path", );
120 ## Generate the chapters
122 foreach my $chapter ( map { $_->cloneNode(1) } $xml->findnodes('/book/chapter') ) {
124 ## Add a <chapter_id>N</chapter_id> node for the stylesheet to use
125 $chapter->appendTextChild( 'chapter_id', ++$counter );
127 ## Add previous/next/canonical urls for nav
129 $chapter->appendTextChild( 'prev_url',
134 : sprintf( '%sch%02d.html', $prepend_chapter, $counter - 1 ) );
135 $chapter->appendTextChild( 'next_url', sprintf( '%sch%02d.html', $prepend_chapter, $counter + 1 ) );
136 $chapter->appendTextChild( 'canonical_url',
137 sprintf( 'http://www.exim.org/exim-html-current/doc/html/spec_html/%sch%02d.html', $prepend_chapter, $counter ) );
140 ## Create an XML document from the chapter
141 my $doc = XML::LibXML::Document->createDocument( '1.0', 'UTF-8' );
142 $doc->setDocumentElement($chapter);
144 ## Transform the chapter into html
146 my $path = sprintf( 'exim-html-%s/doc/html/spec_html/%sch%02d.html', $version, $prepend_chapter, $counter );
147 print "Generating : docroot:/$path\n";
148 transform( $doc, "$opt{tmpl}/doc/chapter.xsl", "$opt{docroot}/$path", );
155 my ( $xml, $prepend_chapter ) = @_;
159 ## Add the "prepend_chapter" info
160 ( $xml->findnodes('/book') )[0]->appendTextChild( 'prepend_chapter', $prepend_chapter );
162 ## Iterate over each chapter
163 my $chapter_counter = 0;
164 foreach my $chapter ( $xml->findnodes('/book/chapter') ) {
167 my $chapter_id = $chapter->getAttribute('id');
168 unless ($chapter_id) { # synthesise missing id
169 $chapter_id = sprintf( 'chapter_noid_%04d', $chapter_counter );
170 $chapter->setAttribute( 'id', $chapter_id );
172 my $chapter_title = $chapter->findvalue('title');
174 $index{$chapter_id} = { chapter_id => $chapter_counter, chapter_title => $chapter_title };
176 ## Iterate over each section
177 my $section_counter = 0;
178 foreach my $section ( $chapter->findnodes('section') ) {
181 my $section_id = $section->getAttribute('id');
182 unless ($section_id) { # synthesise missing id
183 $section_id = sprintf( 'section_noid_%04d_%04d', $chapter_counter, $section_counter );
184 $section->setAttribute( 'id', $section_id );
186 my $section_title = $section->findvalue('title');
188 $index{$section_id} = {
189 chapter_id => $chapter_counter,
190 chapter_title => $chapter_title,
191 section_id => $section_counter,
192 section_title => $section_title
196 ## Build indexes as new chapters
197 build_indexes( $xml, $prepend_chapter, \%index );
199 ## Replace all of the xrefs in the XML
200 foreach my $xref ( $xml->findnodes('//xref') ) {
201 my $linkend = $xref->getAttribute('linkend');
202 if ( exists $index{$linkend} ) {
203 $xref->setAttribute( 'chapter_id', $index{$linkend}{'chapter_id'} );
204 $xref->setAttribute( 'chapter_title', $index{$linkend}{'chapter_title'} );
205 $xref->setAttribute( 'section_id', $index{$linkend}{'section_id'} ) if ( $index{$linkend}{'section_id'} );
206 $xref->setAttribute( 'section_title', $index{$linkend}{'section_title'} ) if ( $index{$linkend}{'section_title'} );
207 $xref->setAttribute( 'url',
208 sprintf( '%sch%02d.html', $prepend_chapter, $index{$linkend}{'chapter_id'} )
209 . ( $index{$linkend}{'section_id'} ? '#' . $linkend : '' ) );
216 my ( $xml, $prepend_chapter, $xref ) = @_;
220 foreach my $node ( $xml->findnodes('//section | //chapter | //indexterm') ) {
221 if ( $node->nodeName eq 'indexterm' ) {
222 my $role = $node->getAttribute('role') || 'concept';
223 my $primary = $node->findvalue('child::primary');
224 my $first = ( $primary =~ /^[A-Za-z]/ ) ? uc( substr( $primary, 0, 1 ) ) : ''; # first letter or marker
225 my $secondary = $node->findvalue('child::secondary') || '';
226 next unless ( $primary || $secondary ); # skip blank entries for now...
227 $index_hash->{$role}{$first}{$primary}{$secondary} ||= [];
228 push @{ $index_hash->{$role}{$first}{$primary}{$secondary} }, $current_id;
231 $current_id = $node->getAttribute('id');
235 # now we build a set of new chapters with the index data in
236 my $book = ( $xml->findnodes('/book') )[0];
237 foreach my $role ( sort { $a cmp $b } keys %{$index_hash} ) {
238 my $chapter = XML::LibXML::Element->new('chapter');
239 $book->appendChild($chapter);
240 $chapter->setAttribute( 'id', join( '_', 'index', $role ) );
241 $chapter->setAttribute( 'class', 'index' );
242 $chapter->appendTextChild( 'title', ( ucfirst($role) . ' Index' ) );
243 foreach my $first ( sort { $a cmp $b } keys %{ $index_hash->{$role} } ) {
244 my $section = XML::LibXML::Element->new('section');
245 my $list = XML::LibXML::Element->new('variablelist');
246 $chapter->appendChild($section);
247 $section->setAttribute( 'id', join( '_', 'index', $role, $first ) );
248 $section->setAttribute( 'class', 'index' );
249 $section->appendTextChild( 'title', $first ? $first : 'Symbols' );
250 $section->appendChild($list);
251 foreach my $primary ( sort { $a cmp $b } keys %{ $index_hash->{$role}{$first} } ) {
252 my $entry = XML::LibXML::Element->new('varlistentry');
253 my $item = XML::LibXML::Element->new('listitem');
254 $list->appendChild($entry)->appendTextChild( 'term', $primary );
255 $entry->appendChild($item);
257 foreach my $secondary ( sort { $a cmp $b } keys %{ $index_hash->{$role}{$first}{$primary} } ) {
258 my $para = XML::LibXML::Element->new('para');
259 if ( $secondary eq '' ) {
260 $item->appendChild($para); # skip having extra layer of heirarchy
264 $slist = XML::LibXML::Element->new('variablelist');
265 $item->appendChild($slist);
267 my $sentry = XML::LibXML::Element->new('varlistentry');
268 my $sitem = XML::LibXML::Element->new('listitem');
269 $slist->appendChild($sentry)->appendTextChild( 'term', $secondary );
270 $sentry->appendChild($sitem)->appendChild($para);
273 foreach my $ref ( @{ $index_hash->{$role}{$first}{$primary}{$secondary} } ) {
274 $para->appendText(', ')
276 my $xrefel = XML::LibXML::Element->new('xref');
277 $xrefel->setAttribute( linkend => $ref );
278 $xrefel->setAttribute( longref => 1 );
279 $para->appendChild($xrefel);
287 ## Handle the transformation
289 my ( $xml, $xsl_path, $out_path ) = @_;
291 ## Build an empty XML structure if an undefined $xml was passed
292 unless ( defined $xml ) {
293 $xml = XML::LibXML::Document->createDocument( '1.0', 'UTF-8' );
294 $xml->setDocumentElement( $xml->createElement('content') );
297 ## Add the current version of Exim to the XML
298 $xml->documentElement()->appendTextChild( 'current_version', $opt{latest} );
300 ## Parse the ".xsl" file as XML
301 my $xsl = XML::LibXML->new()->parse_file($xsl_path) or die $!;
303 ## Generate a stylesheet from the ".xsl" XML.
304 my $stylesheet = XML::LibXSLT->new()->parse_stylesheet($xsl);
306 ## Generate a doc from the XML transformed with the XSL
307 my $doc = $stylesheet->transform($xml);
309 ## Make the containing directory if it doesn't exist
310 mkdirp( ( $out_path =~ /^(.+)\/.+$/ )[0] );
312 ## Write out the document
313 open my $out, '>', $out_path or die $!;
314 print $out $stylesheet->output_as_bytes($doc);
323 foreach ( split( /\//, $path ) ) {
325 my $make = join( '/', @parts );
326 next unless length($make);
328 mkdir($make) or die "Unable to mkdir $make: $!\n";
333 sub parse_arguments {
335 my %opt = ( spec => [], filter => [], help => 0, web => 0, minify => 1 );
336 GetOptions( \%opt, 'help|h!', 'web!', 'spec=s{1,}', 'filter=s{1,}', 'latest=s', 'tmpl=s', 'docroot=s', 'minify!' )
337 || help( 1, 'Bad options' );
340 help(0) if ( $opt{help} );
342 ## --spec and --filter lists
343 foreach my $set (qw[spec filter]) {
344 $opt{$set} = [ map { my $f = File::Spec->rel2abs($_); help( 1, 'No such file: ' . $_ ) unless -f $f; $f } @{ $opt{$set} } ];
347 help( 1, 'Missing value for latest' ) unless ( exists( $opt{latest} ) && defined( $opt{latest} ) );
348 help( 1, 'Invalid value for latest' ) unless $opt{latest} =~ /^\d+(?:\.\d+)*$/;
350 ## --tmpl and --docroot
351 foreach my $set (qw[tmpl docroot]) {
352 help( 1, 'Missing value for ' . $set ) unless ( exists( $opt{$set} ) && defined( $opt{$set} ) );
353 my $f = File::Spec->rel2abs( $opt{$set} );
354 help( 1, 'No such directory: ' . $opt{$set} ) unless -d $f;
357 help( 1, 'Excess arguments' ) if ( scalar(@ARGV) );
359 help( 1, 'Must include at least one of --web, --spec or --filter' )
360 unless ( defined $opt{web} || scalar( @{ $opt{spec} } ) || scalar( @{ $opt{web} } ) );
367 my ( $exit_code, $msg ) = @_;
369 print "$msg\n\n" if defined $msg;
373 --help or -h : Print this help information and then exit
375 --web : Generate the general website pages
376 --spec PATH : Generate the spec pages. PATH is the path to the spec.xml
377 --filter PATH : Generate the filter pages. PATH is the path to the filter.xml
379 One or more of the above three options are required. --spec and --filter can
380 take multiple values to generate different sets of documentation for
381 different versions at the same time.
383 --latest VERSION : Required. Specify the latest stable version of Exim.
384 --tmpl PATH : Required. Path to the templates directory
385 --docroot PATH : Required. Path to the website document root
387 If CSS::Minifier::XS is installed, then CSS will be minified.
388 If JavaScript::Minifier::XS is installed, then JavaScript will be minified.
392 ./gen.pl --latest 4.72
394 --spec spec.xml 4.71/spec.xml
395 --filter filter.xml 4.71/filter.xml
396 --tmpl ~/www/templates
397 --docroot ~/www/docroot