fo: initial version of ledger.xsl master
authorFredrik Unger <fred@tree.se>
Tue, 30 Apr 2013 19:42:19 +0000 (21:42 +0200)
committerFredrik Unger <fred@tree.se>
Tue, 30 Apr 2013 19:42:19 +0000 (21:42 +0200)
This is a cleaned up version of the first attempt of an
xsl-fo stylesheet for ledger-cli.
The current version support several page sizes thanks
to parametrization, idea from Docbook xsl-fo stylesheets.
Basic formatting done, borders and colors can be adjusted.
Current setup helps recognize where the values apear.
Date converted to ISO 8601 extended format.

fo/ledger.xsl [new file with mode: 0644]

diff --git a/fo/ledger.xsl b/fo/ledger.xsl
new file mode 100644 (file)
index 0000000..a1ac2e7
--- /dev/null
@@ -0,0 +1,230 @@
+<xsl:stylesheet version="1.0"
+               xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+               xmlns:fo="http://www.w3.org/1999/XSL/Format">
+
+<xsl:output indent="yes"/>
+<xsl:strip-space elements="*"/>
+
+<xsl:param name="paper.type">A4</xsl:param>
+<xsl:param name="page.orientation">portrait</xsl:param>
+
+<xsl:param name="page.margin.bottom">0.5in</xsl:param>
+<xsl:param name="page.margin.top">0.5in</xsl:param>
+<xsl:param name="page.margin.inner">1in</xsl:param>
+<xsl:param name="page.margin.outer">1in</xsl:param>
+
+<xsl:param name="body.margin.bottom">10mm</xsl:param>
+<xsl:param name="body.margin.top">10mm</xsl:param>
+<xsl:param name="body.margin.inner">10mm</xsl:param>
+<xsl:param name="body.margin.outer">10mm</xsl:param>
+
+<xsl:param name="page.height.portrait">
+  <xsl:choose>
+    <xsl:when test="$paper.type = 'A4landscape'">210mm</xsl:when>
+    <xsl:when test="$paper.type = 'USletter'">11in</xsl:when>
+    <xsl:when test="$paper.type = 'USlandscape'">8.5in</xsl:when>
+    <xsl:when test="$paper.type = 'A3'">420mm</xsl:when>
+    <xsl:when test="$paper.type = 'A4'">297mm</xsl:when>
+    <xsl:when test="$paper.type = 'A5'">210mm</xsl:when>
+    <xsl:otherwise>11in</xsl:otherwise>
+  </xsl:choose>
+</xsl:param>
+<xsl:param name="page.width.portrait">
+  <xsl:choose>
+    <xsl:when test="$paper.type = 'A4landscape'">297mm</xsl:when>
+    <xsl:when test="$paper.type = 'USletter'">8.5in</xsl:when>
+    <xsl:when test="$paper.type = 'USlandscape'">11in</xsl:when>
+    <xsl:when test="$paper.type = 'A3'">297mm</xsl:when>
+    <xsl:when test="$paper.type = 'A4'">210mm</xsl:when>
+    <xsl:when test="$paper.type = 'A5'">148mm</xsl:when>
+    <xsl:otherwise>8.5in</xsl:otherwise>
+  </xsl:choose>
+</xsl:param>
+
+<xsl:param name="page.height">
+  <xsl:choose>
+    <xsl:when test="$page.orientation = 'portrait'">
+      <xsl:value-of select="$page.height.portrait"/>
+    </xsl:when>
+    <xsl:otherwise>
+      <xsl:value-of select="$page.width.portrait"/>
+    </xsl:otherwise>
+  </xsl:choose>
+</xsl:param>
+<xsl:param name="page.width">
+  <xsl:choose>
+    <xsl:when test="$page.orientation = 'portrait'">
+      <xsl:value-of select="$page.width.portrait"/>
+    </xsl:when>
+    <xsl:otherwise>
+      <xsl:value-of select="$page.height.portrait"/>
+    </xsl:otherwise>
+  </xsl:choose>
+</xsl:param>
+
+<xsl:template match="/">
+  <fo:root>
+    <fo:layout-master-set>
+      <fo:simple-page-master master-name="first-page"
+                            page-width="{$page.width}"
+                            page-height="{$page.height}"
+                            margin-top="{$page.margin.top}"
+                            margin-bottom="{$page.margin.bottom}"
+                            margin-left="{$page.margin.inner}"
+                            margin-right="{$page.margin.outer}">
+       <fo:region-body margin-bottom="{$body.margin.bottom}"
+                       margin-top="{$body.margin.top}"/>
+      </fo:simple-page-master>
+    </fo:layout-master-set>
+
+    <fo:page-sequence master-reference="first-page">
+      <fo:flow flow-name="xsl-region-body">
+       <fo:block
+           font-family="sans-serif"
+           font-size="24pt"
+           text-align="center">
+         Ledger Report
+       </fo:block>
+       <xsl:apply-templates/>
+      </fo:flow>
+    </fo:page-sequence>
+  </fo:root>
+</xsl:template>
+
+<xsl:template match="text()" />
+
+<xsl:template match="accounts">
+  <fo:block>
+    <fo:block
+       font-family="sans-serif"
+       font-size="18pt">
+      Balance Report
+    </fo:block>
+    <xsl:apply-templates/>
+  </fo:block>
+</xsl:template>
+
+<xsl:template match="accounts/account">
+  <fo:block border-width="0.5mm" border-style="solid" border-color="green">
+    <xsl:apply-templates/>
+  </fo:block>
+</xsl:template>
+
+<xsl:template match="accounts/*/account">
+  <xsl:choose>
+    <xsl:when test="./fullname = ./name">
+      <fo:block keep-together="always" border-width="0.5mm"
+               border-style="solid" border-color="red">
+       <xsl:apply-templates/>
+      </fo:block>
+    </xsl:when>
+    <xsl:otherwise>
+      <fo:block border-width="0.2mm"
+               border-style="solid" border-color="black">
+       <xsl:apply-templates/>
+      </fo:block>
+    </xsl:otherwise>
+  </xsl:choose>
+</xsl:template>
+
+<xsl:template match="account/account-total">
+  <fo:block>
+    <xsl:value-of select="../fullname"/>
+  </fo:block>
+  <fo:table table-layout="fixed" width="100%">
+    <fo:table-body>
+      <xsl:apply-templates/>
+    </fo:table-body>
+  </fo:table>
+</xsl:template>
+
+<xsl:template match="account/account-total/balance/amount|account/account-total/amount">
+  <fo:table-row>
+    <fo:table-cell>
+      <fo:block font-family="monospace" text-align="right">
+       <xsl:value-of select="./commodity/symbol"/>
+      </fo:block>
+    </fo:table-cell>
+    <fo:table-cell>
+      <fo:block font-family="monospace" text-align="right">
+       <xsl:value-of select="format-number(./quantity, '###0.00')"/>
+      </fo:block>
+    </fo:table-cell>
+  </fo:table-row>
+</xsl:template>
+
+<xsl:template match="transactions">
+  <fo:block>
+    <fo:block
+       page-break-before="always"
+       font-family="sans-serif"
+       font-size="18pt">
+      Register Report
+    </fo:block>
+    <xsl:apply-templates/>
+  </fo:block>
+</xsl:template>
+
+<xsl:template match="transaction">
+  <fo:block-container  keep-together="always" border-width="0.2mm"
+                      border-style="solid">
+    <xsl:apply-templates/>
+  </fo:block-container>
+</xsl:template>
+
+<xsl:template match="transaction/date">
+  <fo:block-container>
+    <fo:block>
+      <xsl:value-of select="concat(substring(., 1, 4),'-',substring(., 6, 2),'-',substring(., 9, 2))"/>
+    </fo:block>
+  </fo:block-container>
+</xsl:template>
+
+<xsl:template match="transaction/payee">
+  <fo:block-container background-color="silver">
+    <fo:block>
+      <xsl:value-of select="."/>
+    </fo:block>
+  </fo:block-container>
+</xsl:template>
+
+<xsl:template match="postings">
+  <fo:block-container space-after="3mm">
+    <fo:table table-layout="fixed" width="100%">
+      <fo:table-body>
+       <xsl:apply-templates/>
+      </fo:table-body>
+    </fo:table>
+  </fo:block-container>
+</xsl:template>
+
+<xsl:template match="postings/posting">
+  <fo:table-row>
+    <xsl:apply-templates/>
+  </fo:table-row>
+</xsl:template>
+
+<xsl:template match="postings/posting/account/name">
+  <fo:table-cell>
+    <fo:block font-family="monospace">
+      <xsl:value-of select="."/>
+    </fo:block>
+  </fo:table-cell>
+</xsl:template>
+
+<xsl:template match="postings/posting/post-amount/amount/quantity">
+  <fo:table-cell>
+    <fo:block font-family="monospace" text-align="right">
+      <xsl:value-of select="format-number(., '####.00')"/>
+    </fo:block>
+  </fo:table-cell>
+</xsl:template>
+
+<xsl:template match="postings/posting/post-amount/amount/commodity/symbol">
+  <fo:table-cell>
+    <fo:block font-family="monospace" text-align="right">
+      <xsl:value-of select="."/>
+    </fo:block>
+  </fo:table-cell>
+</xsl:template>
+</xsl:stylesheet>