Using 'pygmentize' to style the markup. Improved one list.
authorTony Graham
Sun, 27 Jul 2014 21:35:55 +0100
changeset 44 902a8c95ed31
parent 43 7db8588cbd7d
child 45 46c7c7f8f793
Using 'pygmentize' to style the markup. Improved one list.
FOPRunXSLTExt/examples/balisage2014.sh
FOPRunXSLTExt/examples/balisage2014.xml
FOPRunXSLTExt/examples/balisage2014.xsl
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FOPRunXSLTExt/examples/balisage2014.sh	Sun Jul 27 21:35:55 2014 +0100
@@ -0,0 +1,39 @@
+#!/bin/bash
+pygmentize -f xslfo -l xml balisage2014.xml > balisage2014.xml.fo
+pygmentize -f xslfo -l xslt -O colorful balisage2014.xsl > balisage2014.xsl.fo
+
+
+AHF61_64_HOME="/usr/AHFormatterV61_64"
+export AHF61_64_HOME
+
+AHF61_64_LIB_FOLDER=${AHF61_64_HOME}/lib
+AHF61_64_BIN_FOLDER=${AHF61_64_HOME}/bin
+AHF61_64_ETC_FOLDER=${AHF61_64_HOME}/etc
+AHF61_64_SDATA_FOLDER=${AHF61_64_HOME}/sdata
+
+LD_LIBRARY_PATH=${AHF61_64_LIB_FOLDER}:${LD_LIBRARY_PATH}
+export LD_LIBRARY_PATH
+
+AHF61_64_LIC_PATH=${AHF61_64_ETC_FOLDER}
+export AHF61_64_LIC_PATH
+
+AHF61_64_HYPDIC_PATH=${AHF61_64_ETC_FOLDER}/hyphenation
+export AHF61_64_HYPDIC_PATH
+
+AHF61_64_DMC_TBLPATH=${AHF61_64_SDATA_FOLDER}/base2
+export AHF61_64_DMC_TBLPATH
+
+AHF61_64_DEFAULT_HTML_CSS=${AHF61_64_ETC_FOLDER}/html.css
+export AHF61_64_DEFAULT_HTML_CSS
+
+AHF61_64_FONT_CONFIGFILE=${AHF61_64_ETC_FOLDER}/font-config.xml
+export AHF61_64_FONT_CONFIGFILE
+
+SAXON=/usr/local/share/saxon/saxon9he.jar
+AHF=/usr/AHFormatterV61_64/lib/XfoJavaCtl.jar
+CP=$SAXON:/usr/AHFormatterV61_64/lib/XfoJavaCtl.jar
+CP=$CP:/usr/local/src/pplcg/hg/FOPRunXSLTExt/dist/FOPRunXSLTExt.jar
+
+java -cp $CP net.sf.saxon.Transform -init:org.w3c.ppl.xslt.ext.ahf.saxon.RunAHFExtInitializer -s:balisage2014.xml -xsl:balisage2014.xsl -o:balisage2014.fo -it:main  ppl-formatter=ahf pygmentize=yes
+
+/usr/AHFormatterV61_64/run.sh -d balisage2014.fo -o out/balisage2014.pdf
\ No newline at end of file
--- a/FOPRunXSLTExt/examples/balisage2014.xml	Fri Jul 25 23:22:25 2014 +0100
+++ b/FOPRunXSLTExt/examples/balisage2014.xml	Sun Jul 27 21:35:55 2014 +0100
@@ -1,8 +1,8 @@
-<example name="Example 4">
-<header>Print and Page Layout Community Group XSLT Extensions <svg href="ppl-logo.svg" id="logo"/></header>
+<example name="Balisage 2014">
+<header>Print and Page Layout Community Group <svg href="ppl-logo.svg" id="logo"/> XSLT Extensions</header>
 
-<box id="box001" width="2267pt" height="600pt">
-<paragraph id="para001">The XSLT extension functions from the Print and Page Layout Community Group @ W3C allow you to run the XSL-FO formatter to get an area tree to do things like: adjust the start-indent of a fo:list-block based on the length of the longest fo:list-item-label; or size this text to be <font-size/> so it exactly fits this box.</paragraph>
+<box id="box001" width="2220pt" height="600pt">
+<paragraph id="para001">The XSLT extension functions from the Print and Page Layout Community Group @ W3C allow you to run the XSL-FO formatter to get an area tree to do things like: adjust the start-indent of a fo:list-block based on the length of the longest fo:list-item-label; or size this text to be <font-size/> so it fits this box.</paragraph>
 </box>
 <paragraph id="para002">The extension is available for Java and DotNet and uses either the Apache FOP XSL formatter or Antenna House AHF formatter to produce the area trees.</paragraph>
 <paragraph>The single Java jar file covers four combinations of XSLT processor and XSL-FO formatter:</paragraph>
@@ -20,11 +20,26 @@
 </list>
 
 <paragraph>API:</paragraph>
-<list id="list003">
-  <item label="ppl:area-tree($fo-tree as node()) as document-node()">Runs the XSL-FO formatter on $fo-tree to get an area tree.</item>
-  <item label="ppl:block-by-id($area-tree as document-node(), $id as string) as element()?">Returns the block area with ID $id.</item>
-  <item label="ppl:block-bpd($block as element()) as xs:double">Returns the block-progression-dimension of $block in points.</item>
-  <item label="ppl:block-bpd($block as element()) as xs:double">Returns the inline-progression-dimension of $block in points.</item>
-  <item label="ppl:block-available-ipd($block as element()) as xs:double">Returns the difference, in points, between the inline-progression-dimension of $block and the inline-progression-dimension of its ancestor reference area.</item>
-</list>
+<api-list id="list003">
+  <item>
+    <function>ppl:area-tree($fo-tree as node()) as document-node()</function>
+    <def>Runs the XSL-FO formatter on $fo-tree to get an area tree.</def>
+  </item>
+  <item>
+    <function>ppl:block-by-id($area-tree as document-node(), $id as string) as element()?</function>
+<def>Returns the block area with ID $id.</def>
+  </item>
+  <item>
+    <function>ppl:block-bpd($block as element()) as xs:double</function>
+    <def>Returns the block-progression-dimension of $block in points.</def>
+  </item>
+  <item>
+    <function>ppl:block-bpd($block as element()) as xs:double</function>
+    <def>Returns the inline-progression-dimension of $block in points.</def>
+  </item>
+  <item>
+    <function>ppl:block-available-ipd($block as element()) as xs:double</function>
+    <def>Returns the difference, in points, between the inline-progression-dimension of $block and the inline-progression-dimension of its ancestor reference area.</def>
+  </item>
+</api-list>
 </example>
--- a/FOPRunXSLTExt/examples/balisage2014.xsl	Fri Jul 25 23:22:25 2014 +0100
+++ b/FOPRunXSLTExt/examples/balisage2014.xsl	Sun Jul 27 21:35:55 2014 +0100
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!-- ============================================================= -->
-<!-- FOPRunXSLTExt Saxon example 5                                 -->
+<!-- PPL CG Balisage 2014 Poster                                   -->
 <!--                                                               -->
 <!-- See http://www.w3.org/community/ppl/wiki/XSLTExtensions       -->
 <!--                                                               -->
@@ -15,7 +15,8 @@
     xmlns:ppl="http://www.w3.org/community/ppl/ns/"
     xmlns:fo="http://www.w3.org/1999/XSL/Format"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
-    exclude-result-prefixes="ppl xs">
+    xmlns:axf="http://www.antennahouse.com/names/XSL/Extensions"
+    exclude-result-prefixes="axf ppl xs">
 
 <!-- ============================================================= -->
 <!-- IMPORTS                                                       -->
@@ -24,15 +25,15 @@
 <!-- Print and Page Layout Community Group extensions. -->
 <xsl:import href="ppl-extensions.xsl" />
 
-<!-- Common templates for formatting FOPRunXSLTExt examples -->
-<xsl:import href="formatting.xsl" />
-
 <xsl:strip-space elements="example box" />
 
 <!-- ============================================================= -->
 <!-- KEYS                                                          -->
 <!-- ============================================================= -->
 
+<!-- 'override' elements used to override default property values. -->
+<xsl:key name="overrides" match="override" use="@id" />
+
 <!-- All 'box' elements in source. -->
 <xsl:key name="boxes" match="box" use="true()" />
 
@@ -42,7 +43,7 @@
 <!-- ============================================================= -->
 
 <!-- Initial font size. -->
-<xsl:param name="font-size" select="12" as="xs:double" />
+<xsl:param name="font-size" select="40" as="xs:double" />
 
 <!-- Allowed difference in height between outer box and formatted
      paragraph text to be able to say paragraph fits within box and
@@ -51,18 +52,53 @@
 
 <!-- Difference between font-size.minimum and font-size.maximum below
      which it's not worth continuing. -->
-<xsl:param name="font-size-tolerance" select="0.01" as="xs:double" />
+<xsl:param name="font-size-tolerance" select="0.1" as="xs:double" />
+
+<!-- Maximum number of iterations. -->
+<xsl:param name="iteration-max" select="40" as="xs:integer"/>
+
+<!-- Whether to use 'pygmentize' to highlight inserted markup. -->
+<xsl:param name="pygmentize" select="'no'" as="xs:string" />
 
 
 <!-- ============================================================= -->
 <!-- STYLESHEET VARIABLES                                          -->
 <!-- ============================================================= -->
 
+<!-- Overrides of default property values. -->
+<xsl:variable name="overrides">
+  <no-overrides/>
+</xsl:variable>
+
 <!-- A0, portrait, page size. -->
 <xsl:variable name="page-height" select="'1189mm'" as="xs:string" />
 <xsl:variable name="page-width" select="'841mm'" as="xs:string" />
 
 <!-- ============================================================= -->
+<!-- ATTRIBUTE SETS                                                -->
+<!-- ============================================================= -->
+
+<xsl:attribute-set name="code-title">
+  <xsl:attribute name="font-weight" select="'bold'" />
+  <xsl:attribute name="font-size" select="'2em'" />
+  <xsl:attribute name="space-before" select="'1em'" />
+  <xsl:attribute name="space-after" select="'0.5em'" />
+</xsl:attribute-set>
+
+<xsl:attribute-set name="code">
+  <xsl:attribute name="font-family" select="'monospace'" />
+  <xsl:attribute name="padding" select="'6pt'" />
+  <xsl:attribute name="space-before.conditionality" select="'retain'" />
+  <xsl:attribute name="background-color" select="'#f8f8f8'" />
+  <xsl:attribute name="linefeed-treatment" select="'preserve'" />
+  <xsl:attribute name="white-space-collapse" select="'false'" />
+  <xsl:attribute name="white-space-treatment" select="'preserve'" />
+  <xsl:attribute name="axf:line-number" select="'show'" />
+  <xsl:attribute name="axf:line-number-color" select="'silver'" />
+  <xsl:attribute name="axf:line-number-font-size" select="'0.75em'" />
+</xsl:attribute-set>
+
+<!-- ============================================================= -->
 <!-- INITIAL TEMPLATE                                              -->
 <!-- ============================================================= -->
 
@@ -77,25 +113,36 @@
        reached. -->
   <xsl:call-template name="do-box">
     <xsl:with-param name="font-size" select="$font-size" as="xs:double" tunnel="yes" />
-    <xsl:with-param
-        name="font-size.minimum" select="$font-size" as="xs:double" tunnel="yes" />
-    <xsl:with-param
-        name="font-size.maximum" select="$font-size * 10" as="xs:double" tunnel="yes" />
+    <xsl:with-param name="font-size.minimum"
+		    select="$font-size" as="xs:double" tunnel="yes" />
+    <xsl:with-param name="font-size.maximum"
+		    select="$font-size * 3" as="xs:double" tunnel="yes" />
     <xsl:with-param name="iteration" select="1" as="xs:integer" />
-    <xsl:with-param name="iteration-max" select="30" as="xs:integer" tunnel="yes" />
-    <xsl:with-param name="tolerance" select="$tolerance" as="xs:double" tunnel="yes" />
-    <xsl:with-param name="font-size-tolerance" select="$font-size-tolerance" as="xs:double" tunnel="yes" />
+    <xsl:with-param name="iteration-max"
+		    select="$iteration-max" as="xs:integer" tunnel="yes" />
+    <xsl:with-param name="tolerance"
+		    select="$tolerance" as="xs:double" tunnel="yes" />
+    <xsl:with-param name="font-size-tolerance"
+		    select="$font-size-tolerance"
+		    as="xs:double" tunnel="yes" />
   </xsl:call-template>
 </xsl:template>
 
 <xsl:template name="do-box">
   <xsl:param name="font-size" as="xs:double" tunnel="yes" />
-  <xsl:param name="font-size.minimum" as="xs:double" tunnel="yes" />
-  <xsl:param name="font-size.maximum" as="xs:double" tunnel="yes" />
-  <xsl:param name="iteration" select="1" as="xs:integer" />
-  <xsl:param name="iteration-max" select="5" as="xs:integer" tunnel="yes" />
-  <xsl:param name="tolerance" select="$tolerance" as="xs:double" tunnel="yes" />
-  <xsl:param name="font-size-tolerance" select="$font-size-tolerance" as="xs:double" tunnel="yes" />
+  <xsl:param name="font-size.minimum"
+	     as="xs:double" tunnel="yes" />
+  <xsl:param name="font-size.maximum"
+	     as="xs:double" tunnel="yes" />
+  <xsl:param name="iteration"
+	     select="1" as="xs:integer" />
+  <xsl:param name="iteration-max"
+	     select="5" as="xs:integer" tunnel="yes" />
+  <xsl:param name="tolerance"
+	     select="$tolerance" as="xs:double" tunnel="yes" />
+  <xsl:param name="font-size-tolerance"
+	     select="$font-size-tolerance"
+	     as="xs:double" tunnel="yes" />
 
   <xsl:message>iteration = <xsl:value-of select="$iteration" /></xsl:message>
   <xsl:message>font-size = <xsl:value-of select="$font-size" /></xsl:message>
@@ -115,7 +162,9 @@
   <!-- Save the FO tree in a variable. -->
   <xsl:variable name="fo_tree">
     <xsl:apply-templates select="/">
-      <xsl:with-param name="overrides" select="$overrides" as="document-node()" tunnel="yes" />
+      <xsl:with-param name="overrides"
+		      select="$overrides"
+		      as="document-node()" tunnel="yes" />
     </xsl:apply-templates>
   </xsl:variable>
 
@@ -153,7 +202,8 @@
             tunnel="yes" />
       </xsl:apply-templates>
     </xsl:when>
-    <xsl:when test="$font-size.maximum - $font-size.minimum &lt; $font-size-tolerance">
+    <xsl:when test="$font-size.maximum - $font-size.minimum &lt; $font-size-tolerance and
+		    $target-height - $bpd > 0">
       <xsl:message>Font size difference less than $font-size-tolerance.  Using <xsl:value-of select="$font-size" />.</xsl:message>
       <xsl:apply-templates select="/">
         <xsl:with-param
@@ -174,10 +224,12 @@
       </xsl:apply-templates>
     </xsl:when>
     <xsl:when test="$bpd > $target-height">
+      <xsl:message>$bpd gt $target-height</xsl:message>
       <xsl:call-template name="do-box">
         <xsl:with-param
             name="font-size"
-            select="($font-size + $font-size.minimum) div 2"
+            select="min(($font-size * ($bpd div $target-height),
+		         $font-size.minimum))"
             as="xs:double"
             tunnel="yes" />
         <xsl:with-param
@@ -185,10 +237,12 @@
             select="$font-size"
             as="xs:double"
             tunnel="yes" />
-        <xsl:with-param name="iteration" select="$iteration + 1" as="xs:integer" />
+        <xsl:with-param name="iteration"
+			select="$iteration + 1" as="xs:integer" />
       </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
+      <xsl:message>$bpd lt $target-height</xsl:message>
       <xsl:call-template name="do-box">
         <xsl:with-param
             name="font-size"
@@ -200,7 +254,8 @@
             select="$font-size"
             as="xs:double"
             tunnel="yes" />
-        <xsl:with-param name="iteration" select="$iteration + 1" as="xs:integer" />
+        <xsl:with-param name="iteration"
+			select="$iteration + 1" as="xs:integer" />
       </xsl:call-template>
     </xsl:otherwise>
   </xsl:choose>
@@ -208,7 +263,7 @@
 
 
 <xsl:template match="/">
-  <fo:root font-family="verdana, sans-serif" font-size="12pt">
+  <fo:root font-family="'Calluna Sans', verdana, sans-serif" font-size="12pt">
     <fo:layout-master-set>
       <fo:simple-page-master master-name="test-page">
         <xsl:if test="exists($page-height)">
@@ -221,26 +276,30 @@
       </fo:simple-page-master>
     </fo:layout-master-set>
 
-    <fo:page-sequence master-reference="test-page">
+    <fo:page-sequence master-reference="test-page"
+		      axf:line-number-start="5"
+		      axf:line-number-interval="5"
+		      axf:line-number-offset="0.375em + 6pt">
       <fo:flow flow-name="xsl-region-body">
         <xsl:apply-templates/>
         <fo:block-container
             font-size="9pt"
             axf:column-count="4"
-            xmlns:axf="http://www.antennahouse.com/names/XSL/Extensions">
-	  <fo:block font-weight="bold"
-		    font-size="2em">XML</fo:block>
-          <fo:block linefeed-treatment="preserve"
-                    white-space-collapse="false"
-                    white-space-treatment="preserve">
-            <xsl:value-of select="unparsed-text(base-uri())" />
+	    column-gap="48pt">
+	  <fo:block  xsl:use-attribute-sets="code-title">XML</fo:block>
+          <fo:block xsl:use-attribute-sets="code">
+            <xsl:copy-of
+		select="if ($pygmentize ne 'no')
+			then doc(concat(tokenize(base-uri(), '/')[last()], '.fo'))
+			else unparsed-text(base-uri())" />
           </fo:block>
-	  <fo:block font-weight="bold"
-		    font-size="2em">XSLT</fo:block>
-          <fo:block linefeed-treatment="preserve"
-                    white-space-collapse="false"
-                    white-space-treatment="preserve">
-            <xsl:value-of select="unparsed-text('')" />
+	  <fo:block xsl:use-attribute-sets="code-title">XSLT</fo:block>
+          <fo:block height="100%"
+		    xsl:use-attribute-sets="code">
+            <xsl:copy-of
+		select="if ($pygmentize ne 'no')
+			then doc(concat(tokenize(base-uri(doc('')), '/')[last()], '.fo'))
+			else unparsed-text('')" />
           </fo:block>
         </fo:block-container>
       </fo:flow>
@@ -255,6 +314,94 @@
   </fo:block>
 </xsl:template>
 
+<xsl:template match="box">
+  <xsl:param name="overrides" select="$overrides"
+	     as="document-node()" tunnel="yes"/>
+
+  <fo:block-container role="{local-name()}" border="medium solid black"
+	    width="{@width}" height="{@height}" padding="12pt"
+	    id="{@id}">
+    <xsl:if test="key('overrides', @id, $overrides)/@rotate = 'yes'">
+      <xsl:attribute name="reference-orientation" select="'270'" />
+    </xsl:if>
+    <xsl:if test="key('overrides', @id, $overrides)/@font-size">
+      <xsl:attribute name="font-size"
+                     select="concat(key('overrides', @id, $overrides)/@font-size, 'pt')" />
+    </xsl:if>
+    <xsl:apply-templates/>
+  </fo:block-container>
+</xsl:template>
+
+<xsl:template match="paragraph">
+  <fo:block id="{@id}" text-align="justify" space-before="3pt">
+    <xsl:apply-templates/>
+  </fo:block>
+</xsl:template>
+
+<xsl:template match="list">
+  <xsl:param name="overrides" select="$overrides" as="document-node()" tunnel="yes"/>
+  <fo:list-block
+      provisional-distance-between-starts="{if (@label-width = 'narrow')
+                                              then '10mm'
+                                            else '35mm'}"
+      provisional-label-separation="4pt"
+      space-after="3pt"
+      id="{@id}">
+    <!-- If we know the exact label width, set the provisional
+         distance between starts to the width plus the label
+         separation. -->
+    <xsl:if test="exists(key('overrides', @id, $overrides))">
+      <xsl:attribute
+          name="provisional-distance-between-starts"
+          select="concat(key('overrides', @id, $overrides)/@label-width, ' + 4pt')" />
+    </xsl:if>
+    <xsl:apply-templates/>
+  </fo:list-block>
+</xsl:template>
+
+<xsl:template match="item">
+  <fo:list-item space-before="3pt">
+    <fo:list-item-label end-indent="label-end()">
+      <fo:block color="red">
+        <xsl:apply-templates select="@label"/>
+      </fo:block>
+    </fo:list-item-label>
+    <fo:list-item-body start-indent="body-start()">
+      <fo:block>
+        <xsl:apply-templates/>
+      </fo:block>
+    </fo:list-item-body>
+  </fo:list-item>
+</xsl:template>
+
+<xsl:template match="api-list">
+  <fo:list-block
+      provisional-distance-between-starts="35mm"
+      provisional-label-separation="4pt"
+      space-after="3pt"
+      id="{@id}">
+    <xsl:apply-templates/>
+  </fo:list-block>
+</xsl:template>
+
+<xsl:template match="api-list/item">
+  <fo:list-item space-before="3pt">
+    <fo:list-item-label end-indent="0">
+      <fo:block xsl:use-attribute-sets="code"
+		background-color="transparent">
+        <xsl:apply-templates select="function"/>
+      </fo:block>
+    </fo:list-item-label>
+    <fo:list-item-body start-indent="body-start()">
+      <fo:block />
+      <fo:block space-before="2em"
+		space-before.conditionality="retain">
+        <xsl:apply-templates select="def" />
+      </fo:block>
+    </fo:list-item-body>
+  </fo:list-item>
+</xsl:template>
+
 <xsl:template match="svg">
   <fo:instream-foreign-object id="{@id}" content-height="1em">
     <xsl:copy-of select="document(@href)"/>