]> git.draconx.ca Git - homepage.git/blobdiff - layouts/default.xsl
Let's start a blog!
[homepage.git] / layouts / default.xsl
index 2af8dc264ef345e2b3c17bac443a8c30c79eec56..f41467bd87e01d6de9a7aa0d594ab074f16ac494 100644 (file)
@@ -1,8 +1,8 @@
 <?xml version='1.0' encoding='UTF-8' ?>
 <!--
 <?xml version='1.0' encoding='UTF-8' ?>
 <!--
-  Nick's web site: Final XHTML output stage
+  Nick's web site: XHTML output stage
 
 
-  Copyright © 2018 Nick Bowler
+  Copyright © 2018-2020 Nick Bowler
 
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
 
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   extension-element-prefixes='func f'
   exclude-result-prefixes='xhtml'>
 
   extension-element-prefixes='func f'
   exclude-result-prefixes='xhtml'>
 
+<xsl:import href='layouts/functions.xsl' />
+
 <xsl:output method='xml' encoding='UTF-8' indent='yes'
   doctype-public='-//W3C//DTD XHTML 1.1//EN'
 <xsl:output method='xml' encoding='UTF-8' indent='yes'
   doctype-public='-//W3C//DTD XHTML 1.1//EN'
-  doctype-system='http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd' />
-<xsl:strip-space elements='*' />
-<xsl:preserve-space elements='xhtml:p' />
+  doctype-system='http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'
+  cdata-section-elements='style' />
 
 <xsl:param name='source-uri'
   select='"//git.draconx.ca/gitweb/homepage.git/blob/"' />
 <xsl:param name='site-title' select='"The Citrine Citadel"' />
 
 <xsl:param name='source-uri'
   select='"//git.draconx.ca/gitweb/homepage.git/blob/"' />
 <xsl:param name='site-title' select='"The Citrine Citadel"' />
+<xsl:param name='section-links' select='//document/section-links' />
 
 <func:function name='f:ends-with'>
   <xsl:param name='a' />
 
 <func:function name='f:ends-with'>
   <xsl:param name='a' />
   <xsl:copy><xsl:apply-templates select='node()|@*' /></xsl:copy>
 </xsl:template>
 
   <xsl:copy><xsl:apply-templates select='node()|@*' /></xsl:copy>
 </xsl:template>
 
+<!--
+  Nokogiri's pretty-printer is a bit weird.  Regardless of the indentation
+  setting, if an element has no child text nodes then it will be pretty-
+  printed.  This works by adding arbitrary whitespace to that element, and
+  then all of its children are eligible to be pretty-printed.
+
+  If an element has any text nodes at all, then it is not pretty-printed and
+  neither are any of its descendents.
+
+  Adding arbitrary whitespace to <pre> is bad, so we inject zero-width non-
+  breaking spaces to prevent this.  This will render fine but the spaces
+  should be removed before final output to avoid problems with copy+paste.
+-->
+<xsl:template match='xhtml:pre'>
+  <xsl:copy>
+    <xsl:apply-templates select='node()|@*' />
+    <xsl:text>&#x2060;</xsl:text>
+  </xsl:copy>
+</xsl:template>
+
+<!--
+  Likewise, adding spaces between consecutive span-level elements where
+  none existed before won't go over well.
+-->
+<xsl:template name='glue-preceding-span'>
+  <xsl:if test='f:element-is-span(preceding-sibling::node()[1])'>
+    <xsl:text>&#x2060;</xsl:text>
+  </xsl:if>
+</xsl:template>
+
+<xsl:template match='*[f:element-is-span()]'>
+  <xsl:call-template name='glue-preceding-span' />
+  <xsl:copy>
+    <xsl:apply-templates select='node()|@*' />
+    <xsl:if test='*'>
+      <!-- avoid breaking within a span element -->
+      <xsl:text>&#x2060;</xsl:text>
+    </xsl:if>
+  </xsl:copy>
+</xsl:template>
+
+<!--
+  Manually strip whitespace-only text nodes so the pretty printer can do its
+  thing on remaining elements.
+-->
+<xsl:template match='text()[normalize-space(.) = ""]'>
+  <xsl:choose>
+    <!-- preserve anything according to xml:space -->
+    <xsl:when test='ancestor::*[@xml:space][1][@xml:space="preserve"]'>
+      <xsl:copy />
+    </xsl:when>
+    <!-- preserve anything under <pre> -->
+    <xsl:when test='ancestor::xhtml:pre'><xsl:copy /></xsl:when>
+    <!-- preserve whitespace which is the only child node of an element -->
+    <xsl:when test='count(../node()) = 1'><xsl:copy /></xsl:when>
+    <!-- preserve whitespace between consecutive span-level elements
+         which have at least one non-whitespace sibling text element -->
+    <xsl:when test='f:element-is-span(preceding-sibling::node()[1])
+                    and f:element-is-span(following-sibling::node()[1])
+                    and ../text()[normalize-space(.) != ""]'>
+      <xsl:copy />
+    </xsl:when>
+  </xsl:choose>
+</xsl:template>
+
+<!-- Clean up whitespace where harmless to do so -->
+<xsl:template match='xhtml:p/node()[1][self::text()]'>
+  <xsl:value-of select='f:strip-leading()' />
+</xsl:template>
+<xsl:template match='xhtml:p/node()[position()=last()][self::text()]'>
+  <xsl:value-of select='f:strip-trailing()' />
+</xsl:template>
+
 <!-- Add rel attributes to external links -->
 <xsl:template match='xhtml:a[starts-with(@href,"http://")
                           or starts-with(@href,"https://")
 <!-- Add rel attributes to external links -->
 <xsl:template match='xhtml:a[starts-with(@href,"http://")
                           or starts-with(@href,"https://")
   <xsl:variable name='fragment' select='concat("#", @id)' />
   <xsl:copy>
     <xsl:apply-templates select='node()|@*' />
   <xsl:variable name='fragment' select='concat("#", @id)' />
   <xsl:copy>
     <xsl:apply-templates select='node()|@*' />
-    <xsl:text> </xsl:text>
-    <small class='permalink'>
-      (<a href='{$fragment}'><xsl:value-of select='$fragment' /></a>)
-    </small>
+    <xsl:if test='$section-links = "yes"'>
+      <xsl:text> </xsl:text>
+      <small class='permalink'>
+        (<a href='{$fragment}'><xsl:value-of select='$fragment' /></a>)
+      </small>
+    </xsl:if>
+  </xsl:copy>
+</xsl:template>
+
+<!--
+  Convert caption attribute on tables into proper caption elements, to allow
+  a simple way to add captions to kramdown tables.
+-->
+<xsl:template match='@caption[parent::xhtml:table]' />
+<xsl:template match='xhtml:table[@caption]'>
+  <xsl:copy>
+    <xsl:apply-templates select='@*' />
+    <caption><xsl:value-of select='normalize-space(@caption)' /></caption>
+    <xsl:apply-templates select='node()' />
   </xsl:copy>
 </xsl:template>
 
   </xsl:copy>
 </xsl:template>
 
+<!--
+  Delete style elements, as they will get hoisted occur under <head> below.
+  If the generate-listing attribute was specified, produce a code listing
+  where the style attribute was found.
+-->
+<xsl:template match='xhtml:style|@generate-listing[parent::xhtml:style]' />
+<xsl:template match='xhtml:style[@generate-listing]'>
+  <pre>&#x2060;<code><xsl:value-of select='f:strip-leading(.)' /></code></pre>
+</xsl:template>
+
+<!--
+  Add a simple way to reference a document node by ID and include the XHTML
+  code listing directly in the document.
+-->
+<xsl:template match='xhtml:generate-xhtml-listing'>
+  <xsl:variable name='target' select='@target' />
+  <pre>&#x2060;<code>
+    <xsl:value-of select='f:xhtml-listing(//xhtml:*[@id=$target])' />
+  </code></pre>
+</xsl:template>
+
 <xsl:template match='copyright'>
   <p>
     <xsl:text>Copyright © </xsl:text>
 <xsl:template match='copyright'>
   <p>
     <xsl:text>Copyright © </xsl:text>
   </p>
 </xsl:template>
 
   </p>
 </xsl:template>
 
+<xsl:template match='xhtml:h1[not(preceding::xhtml:h1)]'>
+  <xsl:copy><xsl:apply-templates select='node()|@*' /></xsl:copy>
+  <xsl:if test='/document/article/published'>
+    <div id='article-info'>
+      <p>
+        <xsl:text>Posted </xsl:text>
+        <xsl:value-of select='/document/article/published' />
+      </p>
+    </div>
+  </xsl:if>
+</xsl:template>
+
 <xsl:template match='/'>
   <html>
     <head>
       <meta name='viewport' content='width=device-width, initial-scale=1' />
       <link rel='stylesheet' type='text/css' href='/style.css' />
 <xsl:template match='/'>
   <html>
     <head>
       <meta name='viewport' content='width=device-width, initial-scale=1' />
       <link rel='stylesheet' type='text/css' href='/style.css' />
+      <link rel="icon" href="data:," />
       <title>
         <xsl:variable name='page-title' select='string(/document/title)' />
         <xsl:if test='$page-title and $site-title != $page-title'>
       <title>
         <xsl:variable name='page-title' select='string(/document/title)' />
         <xsl:if test='$page-title and $site-title != $page-title'>
         </xsl:if>
         <xsl:value-of select='$site-title' />
       </title>
         </xsl:if>
         <xsl:value-of select='$site-title' />
       </title>
+      <!-- Hoist all style elements to <head> as required by the doctype. -->
+      <xsl:for-each select='//xhtml:style'>
+        <xsl:copy><xsl:apply-templates select='node()|@*' /></xsl:copy>
+      </xsl:for-each>
     </head>
     <body>
       <xsl:apply-templates select='/document/xhtml:html/@*' />
     </head>
     <body>
       <xsl:apply-templates select='/document/xhtml:html/@*' />
+
+      <xsl:if test='/document/hierarchy/parent'>
+        <p id='sitetitle'>
+          <small><xsl:value-of select='$site-title' /></small>
+        </p>
+        <div id='breadcrumbs'>
+          <strong>Return to: </strong>
+          <ul>
+            <xsl:for-each select='/document/hierarchy/parent'>
+              <li><a href='{uri}'><xsl:value-of select='name'/></a></li>
+            </xsl:for-each>
+          </ul>
+        </div>
+        <hr />
+      </xsl:if>
+
       <xsl:apply-templates select='/document/xhtml:html/node()' />
 
       <hr />
       <xsl:apply-templates select='/document/xhtml:html/node()' />
 
       <hr />