Strip leading and trailing whitespace from all <p> tags.
authorNick Bowler <nbowler@draconx.ca>
Wed, 9 Jan 2019 04:27:39 +0000 (23:27 -0500)
committerNick Bowler <nbowler@draconx.ca>
Wed, 9 Jan 2019 04:27:39 +0000 (23:27 -0500)
This should be harmless and improves the pretty-printed output a lot.

layouts/default.xsl
layouts/functions.xsl

index d7381d5dc5a3ace18fdaa400b2a45ebae917e844..020d1fdc61bd89728a910eb97587f052345eaf89 100644 (file)
   </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://")
index cdfb4829fc69fc11d20f7ffcec947260c1004164..96952b20d1c49c4e912686d63094e85ba616dad9 100644 (file)
 <!-- 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>
 
 </xsl:stylesheet>