[Checkins] SVN: z3c.rml/trunk/ Implement pageNumber directive.
Christian Theune
ct at gocept.com
Tue Mar 11 15:51:31 EDT 2008
Log message for revision 84586:
Implement pageNumber directive.
Changed:
U z3c.rml/trunk/CHANGES.txt
U z3c.rml/trunk/src/z3c/rml/__init__.py
A z3c.rml/trunk/src/z3c/rml/paraparser.py
U z3c.rml/trunk/src/z3c/rml/tests/expected/tag-para.pdf
U z3c.rml/trunk/src/z3c/rml/tests/input/tag-para.rml
U z3c.rml/trunk/src/z3c/rml/tests/test_rml.py
-=-
Modified: z3c.rml/trunk/CHANGES.txt
===================================================================
--- z3c.rml/trunk/CHANGES.txt 2008-03-11 19:48:38 UTC (rev 84585)
+++ z3c.rml/trunk/CHANGES.txt 2008-03-11 19:51:31 UTC (rev 84586)
@@ -7,7 +7,9 @@
- Bug: Use python executable as a part of the subprocess command.
+- Add support for RML's `pageNumber` element.
+
0.7.3 (2007-11-10)
------------------
Modified: z3c.rml/trunk/src/z3c/rml/__init__.py
===================================================================
--- z3c.rml/trunk/src/z3c/rml/__init__.py 2008-03-11 19:48:38 UTC (rev 84585)
+++ z3c.rml/trunk/src/z3c/rml/__init__.py 2008-03-11 19:51:31 UTC (rev 84586)
@@ -0,0 +1,3 @@
+
+# Hook up our custom paragraph parser.
+import z3c.rml.paraparser
Added: z3c.rml/trunk/src/z3c/rml/paraparser.py
===================================================================
--- z3c.rml/trunk/src/z3c/rml/paraparser.py (rev 0)
+++ z3c.rml/trunk/src/z3c/rml/paraparser.py 2008-03-11 19:51:31 UTC (rev 84586)
@@ -0,0 +1,62 @@
+##############################################################################
+#
+# Copyright (c) 2007-2008 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Paragraph-internal XML parser extensions.
+
+$Id: flowable.py 76814 2007-06-19 20:12:41Z srichter $
+"""
+__docformat__ = "reStructuredText"
+
+import copy
+import inspect
+
+import reportlab.lib.fonts
+import reportlab.platypus.paraparser
+
+
+class DynamicFragment(reportlab.platypus.paraparser.ParaFrag):
+ """A fragment whose `text` is computed at access time."""
+
+ @property
+ def text(self):
+ # Guess 1: We're in a paragraph in a story.
+ frame = inspect.currentframe(4)
+ canvas = frame.f_locals.get('canvas', None)
+
+ if canvas is None:
+ # Guess 2: We're in a template
+ canvas = frame.f_locals.get('canv', None)
+
+ if canvas is None:
+ raise Exception("Can't use <pageNumber/> in this location.")
+
+ return str(canvas.getPageNumber())
+
+
+class Z3CParagraphParser(reportlab.platypus.paraparser.ParaParser):
+ """Extensions to paragraph-internal XML parsing."""
+
+ def start_pageNumber(self, attributes):
+ frag = DynamicFragment()
+ frag.__dict__.update(self._stack[-1].__dict__)
+ frag.fontName = reportlab.lib.fonts.tt2ps(frag.fontName, frag.bold, frag.italic)
+ self.fragList.append(frag)
+ self._stack.append(frag)
+
+ def end_pageNumber(self):
+ self._pop()
+
+
+# Monkey-patch reportlabs global parser instance. Wah.
+import reportlab.platypus.paragraph
+reportlab.platypus.paragraph._parser = Z3CParagraphParser()
Property changes on: z3c.rml/trunk/src/z3c/rml/paraparser.py
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: z3c.rml/trunk/src/z3c/rml/tests/expected/tag-para.pdf
===================================================================
--- z3c.rml/trunk/src/z3c/rml/tests/expected/tag-para.pdf 2008-03-11 19:48:38 UTC (rev 84585)
+++ z3c.rml/trunk/src/z3c/rml/tests/expected/tag-para.pdf 2008-03-11 19:51:31 UTC (rev 84586)
@@ -20,8 +20,8 @@
endobj
% 'F2': class PDFType1Font
3 0 obj
-% Font Times-Bold
-<< /BaseFont /Times-Bold
+% Font Times-Roman
+<< /BaseFont /Times-Roman
/Encoding /WinAnsiEncoding
/Name /F2
/Subtype /Type1
@@ -29,8 +29,8 @@
endobj
% 'F3': class PDFType1Font
4 0 obj
-% Font Times-BoldItalic
-<< /BaseFont /Times-BoldItalic
+% Font Times-Bold
+<< /BaseFont /Times-Bold
/Encoding /WinAnsiEncoding
/Name /F3
/Subtype /Type1
@@ -38,8 +38,8 @@
endobj
% 'F4': class PDFType1Font
5 0 obj
-% Font Times-Roman
-<< /BaseFont /Times-Roman
+% Font Times-BoldItalic
+<< /BaseFont /Times-BoldItalic
/Encoding /WinAnsiEncoding
/Name /F4
/Subtype /Type1
@@ -83,11 +83,12 @@
endobj
% 'R9': class PDFInfo
9 0 obj
-<< /Author (anonymous)
- /CreationDate (20070416154145)
+<< /Author (\(anonymous\))
+ /CreationDate (20080311200521)
+ /Keywords ()
/Producer (ReportLab http://www.reportlab.com)
- /Subject (unspecified)
- /Title (untitled) >>
+ /Subject (\(unspecified\))
+ /Title (\(anonymous\)) >>
endobj
% 'R10': class PDFPages
10 0 obj
@@ -101,14 +102,9 @@
% page stream
<< /Filter [ /ASCII85Decode
/FlateDecode ]
- /Length 364 >>
+ /Length 410 >>
stream
-Gat>Ph+GR='F4.)6JpbW%'!^hPE2l!Ym\D,KbkEn!h%<I>hof#p$GK&G';]'
-<Z6:HSG[ja#t!W8o`(6ZmNoem"Jd/AJ=LS?:[fLEi&qLQ/u9Fo4QS#N3?q4:
-ju3'f`#"e`(q:DF"hT`1V,G+19Vo,?U]Ljn+EQj(LIc=M?Jg:gJ\6]Ko>QlB
-p>]m$./r$joVbcm%:CF\9r6N.MYgW%g(`pGM+JeH<YRVY=IWb_.?4KY%[_L@
-]"j"TC2MI?e'k%n at uV6nY7@F+f'9mV)JZai_fRoFqTDXojU;W;qesnsk\<%9
-5!/S:=\6AKdpE@[gtE3^0V4361b+`8rA`B79gI4qb7+`0+7W:FrV~>endstream
+Gat=gbA+pK&4Q>Er53=XDP=L/%mN\-U/9<-(+JGPjB?QO`^qX"gAZM3`D.H6J/ooBd*19d#E9:BZd><4"[!1^&d0I&<e/D*+;XGjVNW[FZ-G6>$n7l8I@"F``2;*4R!LkF)Nhm//W;(YoIEer/tIX$Pdpk.#OqiS_*["Ih*R7FV(?!TKT*L,1YSK?Tam;e:bSMm;2$51:H5p27V^p,"%R1MX_PY4Qbe.n]dAiuCr1Ht1/kcfjZZWTNC5MuAl0L9#,6?)"@Z'6Frl]&M at N$/*._jZjtoJ<@^O at e4KJdn8?8l)V,jY1/nA+bX:cGpDO&*jSX8W>S9a?=p:K^qgG.4D#/cAE7t$X\r-MAF_#h<'f;VLgp#kjAh_kO2f at Mlq5/5oWoo"%7,$"K=DmWq6q#fS(mP"~>endstream
endobj
% 'R12': class PDFOutlines
@@ -122,23 +118,23 @@
0000000113 00000 n
0000000257 00000 n
0000000422 00000 n
-0000000589 00000 n
-0000000768 00000 n
+0000000591 00000 n
+0000000758 00000 n
0000000937 00000 n
0000001106 00000 n
0000001385 00000 n
0000001521 00000 n
-0000001718 00000 n
-0000001825 00000 n
-0000002335 00000 n
+0000001746 00000 n
+0000001853 00000 n
+0000002409 00000 n
trailer
<< /ID
% ReportLab generated PDF document -- digest (http://www.reportlab.com)
- [(3\233n\324\257'\207K\311v\262\2733\357\330g) (3\233n\324\257'\207K\311v\262\2733\357\330g)]
+ [(\0126\207\310\231\324\344v\266\220i\002'f\004M) (\0126\207\310\231\324\344v\266\220i\002'f\004M)]
/Info 9 0 R
/Root 8 0 R
/Size 13 >>
startxref
-2387
+2461
%%EOF
Modified: z3c.rml/trunk/src/z3c/rml/tests/input/tag-para.rml
===================================================================
--- z3c.rml/trunk/src/z3c/rml/tests/input/tag-para.rml 2008-03-11 19:48:38 UTC (rev 84585)
+++ z3c.rml/trunk/src/z3c/rml/tests/input/tag-para.rml 2008-03-11 19:51:31 UTC (rev 84586)
@@ -7,6 +7,13 @@
<template>
<pageTemplate id="main">
<frame id="first" x1="1cm" y1="1cm" width="19cm" height="26cm"/>
+
+ <pageGraphics>
+ <place x="0.5cm" y="0.5cm" width="10cm" height="1cm">
+ <para>Page <pageNumber /></para>
+ </place>
+ </pageGraphics>
+
</pageTemplate>
</template>
<story>
@@ -17,5 +24,6 @@
<para doc:example="">Paragraph</para>
<pre doc:example="">Preformatted <b>text</b> only.</pre>
<xpre doc:example="">Preformatted with <i>markup</i>.</xpre>
+ <para>Page number: <pageNumber/> </para>
</story>
</document>
Modified: z3c.rml/trunk/src/z3c/rml/tests/test_rml.py
===================================================================
--- z3c.rml/trunk/src/z3c/rml/tests/test_rml.py 2008-03-11 19:48:38 UTC (rev 84585)
+++ z3c.rml/trunk/src/z3c/rml/tests/test_rml.py 2008-03-11 19:51:31 UTC (rev 84586)
@@ -23,10 +23,13 @@
import z3c.rml.tests
from z3c.rml import rml2pdf, attr
-GS_COMMAND = ('gs -q -sNOPAUSE -sDEVICE=png256 -sOutputFile=%s[Page-%%d].png '
- '%s -c quit')
+def gs_command(path):
+ return ('gs', '-q', '-sNOPAUSE', '-sDEVICE=png256',
+ '-sOutputFile=%s[Page-%%d].png' % path[:-4],
+ path, '-c', 'quit')
+
class RMLRenderingTestCase(unittest.TestCase):
def __init__(self, inPath, outPath):
@@ -78,13 +81,11 @@
def runTest(self):
# Convert the base PDF to image(s)
- status = subprocess.Popen(
- GS_COMMAND %(self._basePath[:-4], self._basePath)).wait()
+ status = subprocess.Popen(gs_command(self._basePath)).wait()
if status:
return
# Convert the test PDF to image(s)
- status = subprocess.Popen(
- GS_COMMAND %(self._testPath[:-4], self._testPath)).wait()
+ status = subprocess.Popen(gs_command(self._testPath)).wait()
if status:
return
# Go through all pages and ensure their equality
More information about the Checkins
mailing list