]> granicus.if.org Git - docbook-dsssl/commitdiff
Changed name of prepend-pad template to pad-string and twheeked so
authorMichael Smith <xmldoc@users.sourceforge.net>
Thu, 21 Jun 2007 02:45:08 +0000 (02:45 +0000)
committerMichael Smith <xmldoc@users.sourceforge.net>
Thu, 21 Jun 2007 02:45:08 +0000 (02:45 +0000)
it can do both right/left padding.

xsl/lib/lib.xweb

index 5a08d64c1a76a32f05b2c24e01ea36e88ab7de0a..8c988e5eefc94d73d2cf9442dae32b586ec1c903 100644 (file)
@@ -633,22 +633,24 @@ by <function>comment-escape-string</function>.</para>
 
 <!-- ================================================================== -->
 
-<refentry id="prepend-pad">
+<refentry id="pad-string">
 <refnamediv>
-<refname>prepend-pad</refname>
-<refpurpose>Right-pad a string out to a certain length</refpurpose>
+<refname>pad-string</refname>
+<refpurpose>Right-pad or left-pad a string out to a certain length</refpurpose>
 </refnamediv>
 
 <refsect1><title>Description</title>
 
 <para>This function takes string <parameter>padVar</parameter> and
-pads it out to the string-length <parameter>length</parameter>, using
-string <parameter>padChar</parameter> (a space character by default)
-as the padding string (note that <parameter>padChar</parameter> can be
-a string; it is not limited to just being a single character).</para>
+  pads it out in the direction <parameter>rightLeft</parameter> to
+  the string-length <parameter>length</parameter>, using string
+  <parameter>padChar</parameter> (a space character by default) as
+  the padding string (note that <parameter>padChar</parameter> can
+  be a string; it is not limited to just being a single
+  character).</para>
 
   <note>
-    <para>This function is a copy of Nate Austin's
+    <para>This function began as a copy of Nate Austin's
     <function>prepend-pad</function> function in the <ulink
     url="http://www.dpawson.co.uk/xsl/sect2/padding.html" >Padding
     Content</ulink> section of Dave Pawson's <ulink
@@ -656,24 +658,39 @@ a string; it is not limited to just being a single character).</para>
     FAQ</ulink>.</para>
   </note>
 
-<programlisting><src:fragment id='prepend-pad.frag'>
-  <xsl:template name="prepend-pad">    
-  <!-- recursive template to right justify and prepend-->
-  <!-- the value with whatever padChar is passed in   -->
+<programlisting><src:fragment id='pad-string.frag'>
+  <xsl:template name="pad-string">    
+    <!-- * recursive template to right/left pad the value with -->
+    <!-- * whatever padChar is passed in -->
     <xsl:param name="padChar" select="' '"/>
+    <xsl:param name="leftRight">left</xsl:param>
     <xsl:param name="padVar"/>
     <xsl:param name="length"/>
     <xsl:choose>
       <xsl:when test="string-length($padVar) &lt; $length">
-        <xsl:call-template name="prepend-pad">
+        <xsl:call-template name="pad-string">
           <xsl:with-param name="padChar" select="$padChar"/>
-          <xsl:with-param name="padVar" select="concat($padChar,$padVar)"/>
+          <xsl:with-param name="leftRight" select="$leftRight"/>
+          <xsl:with-param name="padVar">
+            <xsl:choose>
+              <!-- * determine whether string should be -->
+              <!-- * right- or left-padded -->
+              <xsl:when test="$leftRight = 'left'">
+              <!-- * pad it to left -->
+                <xsl:value-of select="concat($padChar,$padVar)"/>
+              </xsl:when>
+              <xsl:otherwise>
+                <!-- * otherwise, right-pad the string -->
+                <xsl:value-of select="concat($padVar,$padChar)"/>
+              </xsl:otherwise>
+            </xsl:choose>
+          </xsl:with-param>
           <xsl:with-param name="length" select="$length"/>
         </xsl:call-template>
       </xsl:when>
       <xsl:otherwise>
         <xsl:value-of 
-         select="substring($padVar,string-length($padVar) - $length + 1)"/>
+          select="substring($padVar,string-length($padVar) - $length + 1)"/>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:template>
@@ -1231,7 +1248,7 @@ around these functions.</para>
 <src:fragref linkend="xpath.location.frag"/>
 <src:fragref linkend="comment-escape-string.frag"/>
 <src:fragref linkend="comment-escape-string.recursive.frag"/>
-<src:fragref linkend="prepend-pad.frag"/>
+<src:fragref linkend="pad-string.frag"/>
 <src:fragref linkend="str.tokenize.keep.delimiters.frag"/>
 <src:fragref linkend="apply-string-subst-map.frag"/>
 <src:fragref linkend="apply-character-map.frag"/>