]> granicus.if.org Git - docbook-dsssl/commitdiff
Support startinglinenumber and continuation on verbatim environments
authorNorman Walsh <ndw@nwalsh.com>
Wed, 27 Aug 2003 14:29:55 +0000 (14:29 +0000)
committerNorman Walsh <ndw@nwalsh.com>
Wed, 27 Aug 2003 14:29:55 +0000 (14:29 +0000)
xsl/extensions/saxon643/.classes/Makefile.common
xsl/extensions/saxon643/com/nwalsh/saxon/NumberLinesEmitter.java
xsl/extensions/saxon643/com/nwalsh/saxon/Verbatim.java
xsl/fo/verbatim.xsl
xsl/html/verbatim.xsl

index d55d94defa2162a74060164e7451f2dd1f04a853..50ee5306d2a63ee6e0b2298ed29a05c710b25d86 100644 (file)
@@ -1,4 +1,4 @@
-CLASSPATH=$(CLASSBASE):/usr/local/java/saxon-6.4.4/saxon.jar
+CLASSPATH=$(CLASSBASE):/usr/local/java/saxon-6.5.2/saxon.jar
 
 all: $(CLASSFILES)
        -@for f in $(SUBDIRS); do \
index 9d7466c54e9ea65a833b1af0c9b4b04cf0b3a193..ab471e045622e9fa9df859286e39c8747eb86cb9 100644 (file)
@@ -63,6 +63,9 @@ public class NumberLinesEmitter extends CopyEmitter {
   /** The XHTML namespace name. */
   protected static String xhURI = "http://www.w3.org/1999/xhtml";
 
+  /** The first line number will be <code>startinglinenumber</code>. */
+  protected int startinglinenumber = 1;
+
   /** Every <code>modulus</code> line will be numbered. */
   protected int modulus = 5;
 
@@ -85,6 +88,7 @@ public class NumberLinesEmitter extends CopyEmitter {
    */
   public NumberLinesEmitter(Controller controller,
                            NamePool namePool,
+                           int startingLineNumber,
                            int modulus,
                            int width,
                            String separator,
@@ -94,6 +98,7 @@ public class NumberLinesEmitter extends CopyEmitter {
     firstElement = true;
 
     this.modulus = modulus;
+    this.startinglinenumber = startingLineNumber;
     this.width = width;
     this.separator = separator;
     this.foStylesheet = foStylesheet;
@@ -108,7 +113,8 @@ public class NumberLinesEmitter extends CopyEmitter {
 
     if (lineNumber == 0) {
       // The first line is always numbered
-      formatLineNumber(++lineNumber);
+      lineNumber = startinglinenumber;
+      formatLineNumber(lineNumber);
     }
 
     // Walk through the text node looking for newlines
index 1ba7532d6317e1fdf1b1ac520c2ab8bf23bdf2b2..9e1febfbc58e254c33f60ffa012aecd0074c5e9d 100644 (file)
@@ -63,6 +63,8 @@ public class Verbatim {
   private static int modulus = 0;
   /** The width (in characters) of line numbers (for padding). */
   private static int width = 0;
+  /** The starting line number. */
+  private static int startinglinenumber = 1;
   /** The separator between the line number and the verbatim text. */
   private static String separator = "";
 
@@ -140,6 +142,9 @@ public class Verbatim {
    * <dt><code>linenumbering.separator</code></dt>
    * <dd>Specifies the string that separates line numbers from lines
    * in the program listing. (builtin default: " ").</dd>
+   * <dt><code>linenumbering.startinglinenumber</code></dt>
+   * <dd>Specifies the initial line number
+   * in the program listing. (builtin default: "1").</dd>
    * <dt><code>stylesheet.result.type</code></dt>
    * <dd>Specifies the stylesheet result type. The value is either 'fo'
    * (for XSL Formatting Objects) or it isn't. (builtin default: html).</dd>
@@ -152,6 +157,7 @@ public class Verbatim {
     // Hardcoded defaults
     modulus = 5;
     width = 3;
+    startinglinenumber = 1;
     separator = " ";
     foStylesheet = false;
 
@@ -173,6 +179,14 @@ public class Verbatim {
       System.out.println("$linenumbering.width is not a number: " + varString);
     }
 
+    // Get the startinglinenumber
+    varString = getVariable(context, "linenumbering.startinglinenumber");
+    try {
+      startinglinenumber = Integer.parseInt(varString);
+    } catch (NumberFormatException nfe) {
+      System.out.println("$linenumbering.startinglinenumber is not a number: " + varString);
+    }
+
     // Get the separator
     varString = getVariable(context, "linenumbering.separator");
     separator = varString;
@@ -245,6 +259,7 @@ public class Verbatim {
       NamePool namePool = controller.getNamePool();
       NumberLinesEmitter nlEmitter = new NumberLinesEmitter(controller,
                                                            namePool,
+                                                           startinglinenumber,
                                                            listingModulus,
                                                            listingWidth,
                                                            separator,
index 068255fc53c9b79e80f676df022adedfa5bedf1d..7ced1e45a58ed41f701eab03cbb472f2e48dac47 100644 (file)
     </xsl:choose>
   </xsl:variable>
 
+  <xsl:variable name="linenumbering.startinglinenumber">
+    <xsl:choose>
+      <xsl:when test="@startinglinenumber">
+        <xsl:value-of select="@startinglinenumber"/>
+      </xsl:when>
+      <xsl:when test="@continuation='continues'">
+        <xsl:variable name="lastLine">
+          <xsl:choose>
+            <xsl:when test="self::programlisting">
+              <xsl:call-template name="lastLineNumber">
+                <xsl:with-param name="listings"
+                     select="preceding::programlisting[@linenumbering='numbered']"/>
+              </xsl:call-template>
+            </xsl:when>
+            <xsl:when test="self::screen">
+              <xsl:call-template name="lastLineNumber">
+                <xsl:with-param name="listings"
+                     select="preceding::screen[@linenumbering='numbered']"/>
+              </xsl:call-template>
+            </xsl:when>
+            <xsl:when test="self::literallayout">
+              <xsl:call-template name="lastLineNumber">
+                <xsl:with-param name="listings"
+                     select="preceding::literallayout[@linenumbering='numbered']"/>
+              </xsl:call-template>
+            </xsl:when>
+            <xsl:when test="self::address">
+              <xsl:call-template name="lastLineNumber">
+                <xsl:with-param name="listings"
+                     select="preceding::address[@linenumbering='numbered']"/>
+              </xsl:call-template>
+            </xsl:when>
+            <xsl:when test="self::synopsis">
+              <xsl:call-template name="lastLineNumber">
+                <xsl:with-param name="listings"
+                     select="preceding::synopsis[@linenumbering='numbered']"/>
+              </xsl:call-template>
+            </xsl:when>
+            <xsl:otherwise>
+              <xsl:message>
+                <xsl:text>Unexpected verbatim environment: </xsl:text>
+                <xsl:value-of select="local-name(.)"/>
+              </xsl:message>
+              <xsl:value-of select="0"/>
+            </xsl:otherwise>
+          </xsl:choose>
+        </xsl:variable>
+
+        <xsl:value-of select="$lastLine + 1"/>
+      </xsl:when>
+      <xsl:otherwise>1</xsl:otherwise>
+    </xsl:choose>
+  </xsl:variable>
+
   <xsl:choose>
     <xsl:when test="function-available('sverb:numberLines')">
       <xsl:copy-of select="sverb:numberLines($rtf)"/>
   </xsl:choose>
 </xsl:template>
 
+<!-- ======================================================================== -->
+
+<xsl:template name="lastLineNumber">
+  <xsl:param name="listings"/>
+  <xsl:param name="number" select="0"/>
+
+  <xsl:variable name="lines">
+    <xsl:call-template name="countLines">
+      <xsl:with-param name="listing" select="string($listings[1])"/>
+    </xsl:call-template>
+  </xsl:variable>
+
+  <xsl:choose>
+    <xsl:when test="not($listings)">
+      <xsl:value-of select="$number"/>
+    </xsl:when>
+    <xsl:when test="$listings[1]/@startinglinenumber">
+      <xsl:value-of select="$number + $listings[1]/@startinglinenumber + $lines - 1"/>
+    </xsl:when>
+    <xsl:when test="$listings[1]/@continuation='continues'">
+      <xsl:call-template name="lastLineNumber">
+        <xsl:with-param name="listings" select="listings[position() &gt; 1]"/>
+        <xsl:with-param name="number" select="$number + $lines"/>
+      </xsl:call-template>
+    </xsl:when>
+    <xsl:otherwise>
+      <xsl:value-of select="$lines"/>
+    </xsl:otherwise>
+  </xsl:choose>
+</xsl:template>
+
+<xsl:template name="countLines">
+  <xsl:param name="listing"/>
+  <xsl:param name="count" select="1"/>
+
+  <xsl:choose>
+    <xsl:when test="contains($listing, '&#10;')">
+      <xsl:call-template name="countLines">
+        <xsl:with-param name="listing" select="substring-after($listing, '&#10;')"/>
+        <xsl:with-param name="count" select="$count + 1"/>
+      </xsl:call-template>
+    </xsl:when>
+    <xsl:otherwise>
+      <xsl:value-of select="$count"/>
+    </xsl:otherwise>
+  </xsl:choose>
+</xsl:template>
+
 </xsl:stylesheet>
index 02282d152ae4403f1695fecd7226e9e720252031..8ac794acb46562923afc12f05d9fa63189fc1975 100644 (file)
     </xsl:choose>
   </xsl:variable>
 
+  <xsl:variable name="linenumbering.startinglinenumber">
+    <xsl:choose>
+      <xsl:when test="@startinglinenumber">
+        <xsl:value-of select="@startinglinenumber"/>
+      </xsl:when>
+      <xsl:when test="@continuation='continues'">
+        <xsl:variable name="lastLine">
+          <xsl:choose>
+            <xsl:when test="self::programlisting">
+              <xsl:call-template name="lastLineNumber">
+                <xsl:with-param name="listings"
+                     select="preceding::programlisting[@linenumbering='numbered']"/>
+              </xsl:call-template>
+            </xsl:when>
+            <xsl:when test="self::screen">
+              <xsl:call-template name="lastLineNumber">
+                <xsl:with-param name="listings"
+                     select="preceding::screen[@linenumbering='numbered']"/>
+              </xsl:call-template>
+            </xsl:when>
+            <xsl:when test="self::literallayout">
+              <xsl:call-template name="lastLineNumber">
+                <xsl:with-param name="listings"
+                     select="preceding::literallayout[@linenumbering='numbered']"/>
+              </xsl:call-template>
+            </xsl:when>
+            <xsl:when test="self::address">
+              <xsl:call-template name="lastLineNumber">
+                <xsl:with-param name="listings"
+                     select="preceding::address[@linenumbering='numbered']"/>
+              </xsl:call-template>
+            </xsl:when>
+            <xsl:when test="self::synopsis">
+              <xsl:call-template name="lastLineNumber">
+                <xsl:with-param name="listings"
+                     select="preceding::synopsis[@linenumbering='numbered']"/>
+              </xsl:call-template>
+            </xsl:when>
+            <xsl:otherwise>
+              <xsl:message>
+                <xsl:text>Unexpected verbatim environment: </xsl:text>
+                <xsl:value-of select="local-name(.)"/>
+              </xsl:message>
+              <xsl:value-of select="0"/>
+            </xsl:otherwise>
+          </xsl:choose>
+        </xsl:variable>
+
+        <xsl:value-of select="$lastLine + 1"/>
+      </xsl:when>
+      <xsl:otherwise>1</xsl:otherwise>
+    </xsl:choose>
+  </xsl:variable>
+
   <xsl:choose>
     <xsl:when test="function-available('sverb:numberLines')">
       <xsl:copy-of select="sverb:numberLines($rtf)"/>
   </xsl:choose>
 </xsl:template>
 
-</xsl:stylesheet>
+<!-- ======================================================================== -->
 
+<xsl:template name="lastLineNumber">
+  <xsl:param name="listings"/>
+  <xsl:param name="number" select="0"/>
+
+  <xsl:variable name="lines">
+    <xsl:call-template name="countLines">
+      <xsl:with-param name="listing" select="string($listings[1])"/>
+    </xsl:call-template>
+  </xsl:variable>
 
+  <xsl:choose>
+    <xsl:when test="not($listings)">
+      <xsl:value-of select="$number"/>
+    </xsl:when>
+    <xsl:when test="$listings[1]/@startinglinenumber">
+      <xsl:value-of select="$number + $listings[1]/@startinglinenumber + $lines - 1"/>
+    </xsl:when>
+    <xsl:when test="$listings[1]/@continuation='continues'">
+      <xsl:call-template name="lastLineNumber">
+        <xsl:with-param name="listings" select="listings[position() &gt; 1]"/>
+        <xsl:with-param name="number" select="$number + $lines"/>
+      </xsl:call-template>
+    </xsl:when>
+    <xsl:otherwise>
+      <xsl:value-of select="$lines"/>
+    </xsl:otherwise>
+  </xsl:choose>
+</xsl:template>
 
+<xsl:template name="countLines">
+  <xsl:param name="listing"/>
+  <xsl:param name="count" select="1"/>
 
+  <xsl:choose>
+    <xsl:when test="contains($listing, '&#10;')">
+      <xsl:call-template name="countLines">
+        <xsl:with-param name="listing" select="substring-after($listing, '&#10;')"/>
+        <xsl:with-param name="count" select="$count + 1"/>
+      </xsl:call-template>
+    </xsl:when>
+    <xsl:otherwise>
+      <xsl:value-of select="$count"/>
+    </xsl:otherwise>
+  </xsl:choose>
+</xsl:template>
 
+</xsl:stylesheet>