Navigation updates and fixes
authorMike Cardwell <github@grepular.com>
Thu, 6 Dec 2012 16:13:40 +0000 (16:13 +0000)
committerMike Cardwell <github@grepular.com>
Thu, 6 Dec 2012 16:13:40 +0000 (16:13 +0000)
script/gen.pl
templates/doc/chapter.xsl
templates/doc/index.xsl
templates/doc/toc.xsl
templates/static/doc/chapter.css
templates/static/doc/chapter.js

index 298fb1436410680045d5bcdc24ecad6894c001f4..fa0cc7e9213d69da03a09cdee3926fc6f421c215 100755 (executable)
@@ -179,11 +179,17 @@ sub do_doc {
     ## Generate the chapters
     my $counter = 0;
     my @chapters = map { $_->cloneNode(1) } $xml->findnodes('/book/chapter');
+    my( $chapter_title, $chapter_title_prev, $chapter_title_next );
     foreach my $chapter (@chapters) {
 
         ## Add a <chapter_id>N</chapter_id> node for the stylesheet to use
         $chapter->appendTextChild( 'chapter_id', ++$counter );
 
+        ## Get the current and surrounding chapter titles
+        $chapter_title_prev = $chapter_title;
+        $chapter_title      = $chapter_title_next || $chapter->findvalue('title_uri');
+        $chapter_title_next = $chapters[$counter]->findvalue('title_uri') if $counter < int(@chapters);
+
         ## Add previous/next/canonical urls for nav
         {
             $chapter->appendTextChild( 'prev_url',
@@ -191,23 +197,24 @@ sub do_doc {
                 ? $type eq 'filter'
                         ? 'filter.html'
                         : 'index.html'
-                : sprintf( '%sch%02d.html', $prepend_chapter, $counter - 1 ) );
-            $chapter->appendTextChild( 'next_url', sprintf( '%sch%02d.html', $prepend_chapter, $counter + 1 ) )
+                : sprintf( '%sch-%s.html', $prepend_chapter, $chapter_title_prev ) );
+            $chapter->appendTextChild( 'this_url', sprintf( '%sch-%s.html', $prepend_chapter, $chapter_title ) );
+            $chapter->appendTextChild( 'next_url', sprintf( '%sch-%s.html', $prepend_chapter, $chapter_title_next ) )
                 unless int(@chapters) == $counter;
             $chapter->appendTextChild( 'toc_url', ( $type eq 'filter' ? 'filter' : 'index' ) . '.html' );
             $chapter->appendTextChild(
                 'canonical_url',
                 sprintf(
-                    'http://www.exim.org/exim-html-current/doc/html/spec_html/%sch%02d.html',
-                    $prepend_chapter, $counter
+                    'http://www.exim.org/exim-html-current/doc/html/spec_html/%sch-%s.html',
+                    $prepend_chapter, $chapter_title
                 )
             );
             if ( $version ne $opt{latest} ) {
                 $chapter->appendTextChild(
                     'current_url',
                     sprintf(
-                        '../../../../exim-html-current/doc/html/spec_html/%sch%02d.html',
-                        $prepend_chapter, $counter
+                        '../../../../exim-html-current/doc/html/spec_html/%sch-%s.html',
+                        $prepend_chapter, $chapter_title
                     )
                 );
             }
@@ -219,9 +226,15 @@ sub do_doc {
 
         ## Transform the chapter into html
         {
-            my $path = sprintf( 'exim-html-%s/doc/html/spec_html/%sch%02d.html', $version, $prepend_chapter, $counter );
-            print "Generating  : docroot:/$path\n" if ( $opt{verbose} );
-            transform( $doc, "$opt{tmpl}/doc/chapter.xsl", "$opt{docroot}/$path", $staticroot );
+            my $real_path = sprintf( 'exim-html-%s/doc/html/spec_html/%sch-%s.html', $version, $prepend_chapter, $chapter_title );
+            my $link_path = sprintf( 'exim-html-%s/doc/html/spec_html/%sch%02d.html',  $version, $prepend_chapter, $counter );
+            print "Generating  : docroot:/$real_path\n" if ( $opt{verbose} );
+            transform( $doc, "$opt{tmpl}/doc/chapter.xsl", "$opt{docroot}/$real_path", $staticroot );
+            print "Symlinking  : docroot:/$link_path to docroot:$real_path\n" if ( $opt{verbose} );
+            if ( -f "$opt{docroot}/$link_path" ) {
+               unlink("$opt{docroot}/$link_path") or die "failed removing $opt{docroot}/$link_path: $!";
+            }
+            symlink( "$opt{docroot}/$real_path", "$opt{docroot}/$link_path" ) || die "symlink to $opt{docroot}/$link_path failed: $!";
         }
     }
 }
@@ -248,6 +261,9 @@ sub xref_fixup {
         }
         my $chapter_title = $chapter->findvalue('title');
 
+        ## Set title_uri so we can use eg ch-introduction.html instead of ch01.html
+        $chapter->appendTextChild( 'title_uri', title_to_uri($chapter_title) );
+
         $index{$chapter_id} = { chapter_id => $chapter_counter, chapter_title => $chapter_title };
 
         ## Iterate over each section
@@ -283,7 +299,7 @@ sub xref_fixup {
             $xref->setAttribute( 'section_title', $index{$linkend}{'section_title'} )
                 if ( $index{$linkend}{'section_title'} );
             $xref->setAttribute( 'url',
-                sprintf( '%sch%02d.html', $prepend_chapter, $index{$linkend}{'chapter_id'} )
+                sprintf( '%sch-%s.html', $prepend_chapter, title_to_uri($index{$linkend}{'chapter_title'}) )
                     . ( $index{$linkend}{'section_id'} ? '#' . $linkend : '' ) );
         }
     }
@@ -319,6 +335,8 @@ sub build_indexes {
         $chapter->setAttribute( 'id', join( '_', 'index', $role ) );
         $chapter->setAttribute( 'class', 'index' );
         $chapter->appendTextChild( 'title', ( ucfirst($role) . ' Index' ) );
+        $chapter->appendTextChild( 'title_uri', title_to_uri(ucfirst($role) . ' Index') );
+
         foreach my $first ( sort { $a cmp $b } keys %{ $index_hash->{$role} } ) {
             my $section = XML::LibXML::Element->new('section');
             my $list    = XML::LibXML::Element->new('variablelist');
@@ -405,6 +423,15 @@ sub transform {
     close $out;
 }
 
+# ------------------------------------------------------------------
+## Takes a chapter title and fixes it up so it is suitable for use in a URI
+sub title_to_uri {
+    my $title = lc(shift);
+    $title =~ s/[^a-z0-9\s]+//gi; # Only allow spaces, numbers and letters
+    $title =~ s/\s+/_/g;          # Replace spaces with underscores so URLs are easier to copy about
+    return $title;
+}
+
 # ------------------------------------------------------------------
 ## Look in the docroot for old versions of the documentation
 sub old_docs_versions {
index 0b0bc0631de18405aefa6e5fd747837d8f0d7c6b..6430eee88a89e1600f4335ceb3d9c485c18b5396 100644 (file)
@@ -60,7 +60,7 @@
 
             <!-- Chapter Title -->
             <h2 id="{@id}" class="{@class}">
-               <a href="{toc_url}">
+               <a href="{this_url}">
                   <xsl:value-of select="concat('Chapter ',chapter_id,' - ',title)"/>
                </a>
             </h2>
@@ -72,7 +72,7 @@
 
          <!-- Navigation -->
             <a class="previous_page" href="{prev_url}"><![CDATA[<-previous]]></a>
-            <a class="toc_page"      href="{prev_url}"><![CDATA[Table of Contents]]></a>
+            <a class="toc_page"      href="{toc_url}"><![CDATA[Table of Contents]]></a>
             <xsl:if test="next_url">
                <a class="next_page"  href="{next_url}"><![CDATA[next->]]></a>
             </xsl:if>
index 6237269bf231b69f97e6b449749478be4b12640d..751f83fcfc06d6abfd300a032f2ea3f9c503d82b 100644 (file)
 
          <!-- Calculate the URL to the chapter. Store in $chapter_url -->
             <xsl:variable name="chapter_url">
-               <xsl:choose>
-                  <xsl:when test="position()&lt;10">
-                     <xsl:value-of select="concat(/book/prepend_chapter,'ch',0,position(),'.html')"/>
-                  </xsl:when>
-                  <xsl:otherwise>
-                     <xsl:value-of select="concat(/book/prepend_chapter,'ch',position(),'.html')"/>
-                  </xsl:otherwise>
-               </xsl:choose>
+               <xsl:value-of select="concat(/book/prepend_chapter,'ch-',title_uri,'.html')"/>
             </xsl:variable>
 
          <!-- Chapter info -->
index 31d7bc1e8ac11297f2dc925f9b724ae9175bb1fc..f118bffb85b193150fa1d6bbd91e51f44f2fe8e6 100644 (file)
 
          <!-- Calculate the URL to the chapter. Store in $chapter_url -->
             <xsl:variable name="chapter_url">
-               <xsl:choose>
-                  <xsl:when test="position()&lt;10">
-                     <xsl:value-of select="concat(/book/prepend_chapter,'ch',0,position(),'.html')"/>
-                  </xsl:when>
-                  <xsl:otherwise>
-                     <xsl:value-of select="concat(/book/prepend_chapter,'ch',position(),'.html')"/>
-                  </xsl:otherwise>
-               </xsl:choose>
+               <xsl:value-of select="concat(/book/prepend_chapter,'ch-',title_uri,'.html')"/>
             </xsl:variable>
 
          <!-- Chapter info -->
index 9f9b22e7ac79d15935b8f5aea63d9eab43c1d521..74c00867ca4b7ef98787d63ec12e55c28eec9e15 100644 (file)
@@ -49,9 +49,8 @@ body.no-js #old_version_warning span.closebar {
 
 .toc_page {
   position:     absolute;
-  right:        13em;
-  left:         13em;
-  text-align:   center;
+  width:       10em;
+  left:         45%;
 }
 
 #chapter {
index add4bd3184415e1a0b2102a25889491050cd97b5..45bb4afc7f40680d47757413b7be90f5011cf342 100644 (file)
@@ -1,3 +1,19 @@
+// Rewrite chapter urls using the canonical name. e.g ch01.html becomes introduction.html
+(function ($) {
+    if (document.location.pathname.match(/\/ch\d+\.html$/)) {
+        var url = document.location.href;
+        var canonical = $('link[rel="canonical"]').attr('href');
+        if (canonical) {
+            canonical = canonical.replace(/^[^#]+(\/[^\/#]+).*/,'$1');
+            url = url.replace(/\/ch\d+\.html/, canonical);
+            if ("history" in window && "replaceState" in window.history)
+                window.history.replaceState("", document.title, url);
+            else
+                document.location.href = url;
+        }
+    }
+})(jQuery);
+
 // Warnings about reading old version of documentation
 (function ($) {
     if( $.grep( document.cookie.split(/\s*;\s*/), function(a){return a === 'old_version_warning_removed=true' ? true : false }).length === 0 ){