Fix protocol relative link generation to handle file:.
authorCameron McCormack <cam@mcc.id.au>
Fri, 18 May 2012 13:31:01 +1000
changeset 24 a4506dff8291
parent 23 d7f042a0cb65
child 25 a501318e854c
Fix protocol relative link generation to handle file:.
publish.xsl
single-page.xsl
--- a/publish.xsl	Fri May 18 13:09:48 2012 +1000
+++ b/publish.xsl	Fri May 18 13:31:01 2012 +1000
@@ -171,9 +171,25 @@
         <xsl:apply-templates select='head/*[not(self::link) and not(self::meta) and not(self::title)]'/>
         <xsl:apply-templates select='head/link[not(@rel="stylesheet" and contains(@href, "StyleSheets/TR") or @media="print")]'/>
         <link rel='stylesheet' href='{if ($local-style-sheets) then "style/" else "//www.w3.org/StyleSheets/TR/"}W3C-{$maturity-short}{if ($local-style-sheets) then ".css" else ""}' type='text/css' media='screen'/>
+        <script>
+          var n, local = location.protocol == "file:";
+          if (local) {
+            for (n = document.head.firstChild; n; n = n.nextSibling) {
+              if (n.nodeName.toLowerCase() == "link") {
+                if (n.getAttribute("href").indexOf("//") == 0) {
+                  n.href = "https:" + n.getAttribute("href");
+                }
+              }
+            }
+          }
+        </script>
         <xsl:if test='body//m:*'>
           <!-- If there were an "MML_SVG" config we should use that, to avoid the overhead of looking for TeX-style math. -->
-          <script>(function() { var e = document.createElement("script"); e.src = (location.protocol == "file:" ? "https:" : location.protocol) + "//d3eoax9i5htok0.cloudfront.net/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_SVG"; document.head.appendChild(e); })()</script>
+          <script data-script-mathjax="">
+            n = document.createElement("script");
+            n.src = (local ? "https:" : location.protocol) + "//d3eoax9i5htok0.cloudfront.net/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_SVG";
+            document.head.appendChild(n);
+          </script>
         </xsl:if>
       </head>
       <body>
--- a/single-page.xsl	Fri May 18 13:09:48 2012 +1000
+++ b/single-page.xsl	Fri May 18 13:31:01 2012 +1000
@@ -26,10 +26,22 @@
   <xsl:template match="h:head">
     <xsl:copy>
       <xsl:apply-templates select="@*"/>
-      <xsl:apply-templates/>
+      <xsl:apply-templates select="*[not(self::script)]"/>
       <xsl:call-template name='do-chapters-style'>
         <xsl:with-param name='cs' select='concat($chapters, " ")'/>
       </xsl:call-template>
+      <script>
+        var n, local = location.protocol == "file:";
+        if (local) {
+          for (n = document.head.firstChild; n; n = n.nextSibling) {
+            if (n.nodeName.toLowerCase() == "link") {
+              if (n.getAttribute("href").indexOf("//") == 0) {
+                n.href = "https:" + n.getAttribute("href");
+              }
+            }
+          }
+        }
+      </script>
       <xsl:call-template name='do-chapters-script'>
         <xsl:with-param name='cs' select='concat($chapters, " ")'/>
       </xsl:call-template>
@@ -74,7 +86,7 @@
   </xsl:template>
 
   <xsl:template match='h:script[@src="style/expanders.js"]'/>
-  <xsl:template match='h:script[contains(@src, "mathjax")]'/>
+  <xsl:template match='h:script[@data-script-mathjax]'/>
 
   <xsl:template match='node()|@*'>
     <xsl:param name='c'/>
@@ -120,7 +132,7 @@
     <xsl:param name='cs'/>
     <xsl:variable name='c' select='concat(substring-before($cs, " "), "-")'/>
     <xsl:if test='$c != "" and $c != "-"'>
-      <xsl:variable name="mathjax" select='document(concat($publish, "/", substring($c, 1, string-length($c) - 1), ".html"))/h:html/h:head/h:script[contains(@src, "mathjax")]'/>
+      <xsl:variable name="mathjax" select='document(concat($publish, "/", substring($c, 1, string-length($c) - 1), ".html"))/h:html/h:head/h:script[@data-script-mathjax]'/>
       <xsl:choose>
         <xsl:when test="count($mathjax) != 0">
           <xsl:copy-of select="$mathjax[1]"/>