]> git.draconx.ca Git - homepage.git/blobdiff - layouts/functions.xsl
Let's start a blog!
[homepage.git] / layouts / functions.xsl
index cdfb4829fc69fc11d20f7ffcec947260c1004164..d9c5748053bbf4a3759b4a572d05f5d90061c12f 100644 (file)
@@ -1,7 +1,7 @@
 <!--
   Nick's web site: XSLT helper functions.
 
-  Copyright © 2019 Nick Bowler
+  Copyright © 2019-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
 <!-- Returns true iff the node is a nonempty text node or a span element -->
 <func:function name='f:node-is-span'>
   <xsl:param name='node' select='.' />
-  <func:result test='( $node/self::text() and normalize-text($node) )
-                     or f:element-is-span($node)' />
+  <func:result select='( $node/self::text() and normalize-text($node) )
+                       or f:element-is-span($node)' />
+</func:function>
+
+<!-- Remove leading whitespace from a string -->
+<func:function name='f:strip-leading'>
+  <xsl:param name='str' select='.' />
+  <xsl:param name='curlen' select='string-length($str)' />
+
+  <xsl:variable name='a' select='substring($str, 1, $curlen)' />
+  <xsl:variable name='b' select='substring($str, 1+$curlen)' />
+
+  <xsl:choose>
+    <xsl:when test='2 > $curlen'>
+      <func:result select='concat(normalize-space($a), $b)' />
+    </xsl:when>
+    <xsl:when test='normalize-space($a) = ""'>
+      <func:result select='f:strip-leading($b)' />
+    </xsl:when>
+    <xsl:otherwise>
+      <func:result select='f:strip-leading($str, ceiling($curlen div 2))' />
+    </xsl:otherwise>
+  </xsl:choose>
+</func:function>
+
+<!-- Remove trailing whitespace from a string -->
+<func:function name='f:strip-trailing'>
+  <xsl:param name='str' select='.' />
+  <xsl:param name='curlen' select='string-length($str)' />
+
+  <xsl:variable name='split' select='string-length($str) - $curlen' />
+  <xsl:variable name='a' select='substring($str, 1, $split)' />
+  <xsl:variable name='b' select='substring($str, 1+$split)' />
+
+  <xsl:choose>
+    <xsl:when test='2 > $curlen'>
+      <func:result select='concat($a, normalize-space($b))' />
+    </xsl:when>
+    <xsl:when test='normalize-space($b) = ""'>
+      <func:result select='f:strip-trailing($a)' />
+    </xsl:when>
+    <xsl:otherwise>
+      <func:result select='f:strip-trailing($str, ceiling($curlen div 2))' />
+    </xsl:otherwise>
+  </xsl:choose>
+</func:function>
+
+<!--
+  Convert the given node to a string containing an XHTML listing for that node.
+-->
+<func:function name='f:xhtml-listing'>
+  <xsl:param name='nodeset' select='.' />
+  <xsl:variable name='node' select='$nodeset[1]' />
+
+  <func:result>
+    <xsl:choose>
+      <xsl:when test='$node/self::text()'>
+        <!-- text node -->
+        <xsl:value-of select='$node' />
+      </xsl:when>
+      <xsl:when test='$node/self::comment()'>
+        <!-- comment node -->
+        <xsl:value-of select='concat("&lt;!--", $node, "--&gt;")' />
+      </xsl:when>
+      <xsl:when test='$node/self::*'>
+        <!-- element node -->
+        <xsl:value-of select='concat("&lt;", local-name($node))' />
+        <xsl:value-of select='f:xhtml-listing($node/@*)' />
+        <xsl:if test='not($node/node())'>/</xsl:if>
+        <xsl:value-of select='concat("&gt;", f:xhtml-listing($node/node()))' />
+        <xsl:if test='$node/node()'>
+          <xsl:value-of select='concat("&lt;/", local-name($node), "&gt;")' />
+        </xsl:if>
+      </xsl:when>
+      <xsl:when test='$node'>
+        <!-- attribute node -->
+        <xsl:value-of select='concat(" ", local-name($node), "=")' />
+        <xsl:value-of select='concat("&apos;", $node, "&apos;")' />
+      </xsl:when>
+    </xsl:choose>
+    <xsl:for-each select='$nodeset[position()>1]'>
+      <xsl:value-of select='f:xhtml-listing()' />
+    </xsl:for-each>
+  </func:result>
 </func:function>
 
 </xsl:stylesheet>