-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Implemented the following doc-programming directives:
* docAssert * docAssign * docElse * docIf * docExec * docPara * docWhile
- Loading branch information
Showing
6 changed files
with
336 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
############################################################################## | ||
# | ||
# Copyright (c) 2012 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. | ||
# | ||
############################################################################## | ||
"""``doc*`` directives. | ||
""" | ||
import reportlab.platypus | ||
from z3c.rml import attr, directive, flowable, interfaces, occurence | ||
|
||
class IDocAssign(interfaces.IRMLDirectiveSignature): | ||
"""Assign a value to the namesapce.""" | ||
|
||
var = attr.String( | ||
title=u'Variable Name', | ||
description=u'The name under which the value is stored.', | ||
required=True) | ||
|
||
expr = attr.String( | ||
title=u'Expression', | ||
description=u'The expression that creates the value when evaluated.', | ||
required=True) | ||
|
||
class DocAssign(flowable.Flowable): | ||
signature = IDocAssign | ||
klass = reportlab.platypus.flowables.DocAssign | ||
|
||
|
||
class IDocExec(interfaces.IRMLDirectiveSignature): | ||
"""Execute a statement.""" | ||
|
||
stmt = attr.String( | ||
title=u'Statement', | ||
description=u'The statement to be executed.', | ||
required=True) | ||
|
||
class DocExec(flowable.Flowable): | ||
signature = IDocExec | ||
klass = reportlab.platypus.flowables.DocExec | ||
|
||
|
||
class IDocPara(interfaces.IRMLDirectiveSignature): | ||
"""Create a paragraph with the value returned from the expression.""" | ||
|
||
expr = attr.String( | ||
title=u'Expression', | ||
description=u'The expression to be executed.', | ||
required=True) | ||
|
||
format = attr.String( | ||
title=u'Format', | ||
description=u'The format used to render the expression value.', | ||
required=False) | ||
|
||
style = attr.Style( | ||
title=u'Style', | ||
description=u'The style of the paragraph.', | ||
required=False) | ||
|
||
escape = attr.Boolean( | ||
title=u'Escape Text', | ||
description=u'When set (default) the expression value is escaped.', | ||
required=False) | ||
|
||
class DocPara(flowable.Flowable): | ||
signature = IDocPara | ||
klass = reportlab.platypus.flowables.DocPara | ||
|
||
|
||
class IDocAssert(interfaces.IRMLDirectiveSignature): | ||
"""Assert a certain condition.""" | ||
|
||
cond = attr.String( | ||
title=u'Condition', | ||
description=u'The condition to be asserted.', | ||
required=True) | ||
|
||
format = attr.String( | ||
title=u'Format', | ||
description=u'The text displayed if assertion fails.', | ||
required=False) | ||
|
||
class DocAssert(flowable.Flowable): | ||
signature = IDocAssert | ||
klass = reportlab.platypus.flowables.DocAssert | ||
|
||
|
||
class IDocElse(interfaces.IRMLDirectiveSignature): | ||
"""Starts 'else' block.""" | ||
|
||
class DocElse(flowable.Flowable): | ||
signature = IDocElse | ||
|
||
def process(self): | ||
if not isinstance(self.parent, DocIf): | ||
raise ValueError("<docElse> can only be placed inside a <docIf>") | ||
self.parent.flow = self.parent.elseFlow | ||
|
||
|
||
class IDocIf(flowable.IFlow): | ||
"""Display story flow based on the value of the condition.""" | ||
|
||
cond = attr.String( | ||
title=u'Condition', | ||
description=u'The condition to be tested.', | ||
required=True) | ||
|
||
class DocIf(flowable.Flow): | ||
signature = IDocAssert | ||
klass = reportlab.platypus.flowables.DocIf | ||
|
||
def __init__(self, *args, **kw): | ||
super(flowable.Flow, self).__init__(*args, **kw) | ||
self.thenFlow = self.flow = [] | ||
self.elseFlow = [] | ||
|
||
def process(self): | ||
args = dict(self.getAttributeValues()) | ||
self.processSubDirectives() | ||
dif = self.klass( | ||
thenBlock = self.thenFlow, elseBlock = self.elseFlow, **args) | ||
self.parent.flow.append(dif) | ||
|
||
class IDocWhile(flowable.IFlow): | ||
"""Repeat the included directives as long as the condition is true.""" | ||
|
||
cond = attr.String( | ||
title=u'Condition', | ||
description=u'The condition to be tested.', | ||
required=True) | ||
|
||
class DocWhile(flowable.Flow): | ||
signature = IDocAssert | ||
klass = reportlab.platypus.flowables.DocWhile | ||
|
||
def process(self): | ||
args = dict(self.getAttributeValues()) | ||
self.processSubDirectives() | ||
dwhile = self.klass(whileBlock = self.flow, **args) | ||
self.parent.flow.append(dwhile) | ||
|
||
|
||
flowable.Flow.factories['docAssign'] = DocAssign | ||
flowable.Flow.factories['docExec'] = DocExec | ||
flowable.Flow.factories['docPara'] = DocPara | ||
flowable.Flow.factories['docAssert'] = DocAssert | ||
flowable.Flow.factories['docIf'] = DocIf | ||
flowable.Flow.factories['docElse'] = DocElse | ||
flowable.Flow.factories['docWhile'] = DocWhile | ||
|
||
flowable.IFlow.setTaggedValue( | ||
'directives', | ||
flowable.IFlow.getTaggedValue('directives') + | ||
(occurence.ZeroOrMore('docAssign', IDocAssign), | ||
occurence.ZeroOrMore('docExec', IDocExec), | ||
occurence.ZeroOrMore('docPara', IDocPara), | ||
occurence.ZeroOrMore('docIf', IDocIf), | ||
occurence.ZeroOrMore('docElse', IDocElse), | ||
occurence.ZeroOrMore('docWhile', IDocWhile), | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
src/z3c/rml/tests/input/rml-examples-039-doc-programming.rml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
<!DOCTYPE document SYSTEM "rml_1_0.dtd"> | ||
<document filename="test_039_doc_programming.pdf" invariant="1"> | ||
|
||
<template pageSize="letter" leftMargin="72" showBoundary="1"> | ||
<pageTemplate id="main" pageSize="letter portrait"> | ||
<pageGraphics> | ||
<setFont name="Helvetica-Bold" size="18"/> | ||
<drawString x="35" y="760">RML Example 40: Doc_programming</drawString> | ||
<image file="logo_no_bar.png" preserveAspectRatio="1" x="488" y="730" width="72" height="72"/> | ||
<image file="strapline.png" preserveAspectRatio="1" x="35" y="0" width="525" /> | ||
</pageGraphics> | ||
<frame id="second" x1="35" y1="45" width="525" height="590"/> | ||
</pageTemplate> | ||
|
||
<pageTemplate id="main2" pageSize="letter portrait"> | ||
<pageGraphics> | ||
<setFont name="Helvetica-Bold" size="18"/> | ||
<drawString x="35" y="760">RML Example 40: Doc_programming</drawString> | ||
<image file="logo_no_bar.png" preserveAspectRatio="1" x="488" y="730" width="72" height="72"/> | ||
<image file="strapline.png" preserveAspectRatio="1" x="35" y="0" width="525" /> | ||
<drawRightString x="523" y="800">RML2PDF Test Suite</drawRightString> | ||
</pageGraphics> | ||
<frame id="second" x1="35" y1="45" width="525" height="685"/> | ||
</pageTemplate> | ||
<pageTemplate id="autoNextTemplate" pageSize="letter portrait" autoNextTemplate="autoFollow"> | ||
<pageGraphics> | ||
<setFont name="Helvetica-Bold" size="18"/> | ||
<drawString x="35" y="760">RML Example 40: Doc_programming</drawString> | ||
<image file="logo_no_bar.png" preserveAspectRatio="1" x="488" y="730" width="72" height="72"/> | ||
<image file="strapline.png" preserveAspectRatio="1" x="35" y="0" width="525" /> | ||
<drawString x="72" y="72">template is autoNextTemplate</drawString> | ||
</pageGraphics> | ||
<frame id="second" x1="35" y1="45" width="525" height="685"/> | ||
</pageTemplate> | ||
<pageTemplate id="autoFollow" pageSize="letter portrait"> | ||
<pageGraphics> | ||
<setFont name="Helvetica-Bold" size="18"/> | ||
<drawString x="35" y="760">RML Example 40: Doc_programming</drawString> | ||
<image file="logo_no_bar.png" preserveAspectRatio="1" x="488" y="730" width="72" height="72"/> | ||
<image file="strapline.png" preserveAspectRatio="1" x="35" y="0" width="525" /> | ||
<drawString x="72" y="72">template is autoFollow</drawString> | ||
</pageGraphics> | ||
<frame id="second" x1="35" y1="45" width="525" height="685"/> | ||
</pageTemplate> | ||
</template> | ||
|
||
<stylesheet> | ||
<initialize> | ||
<alias id="style.normal" value="style.Normal"/> | ||
</initialize> | ||
<paraStyle name="h1" fontName="Helvetica-BoldOblique" fontSize="32" leading="36"/> | ||
<paraStyle name="normal" fontName="Helvetica" fontSize="10" leading="12"/> | ||
<paraStyle name="spaced" fontName="Helvetica" fontSize="10" leading="12" | ||
spaceBefore="12" spaceAfter="12"/> | ||
<paraStyle name="intro" fontName="Helvetica" fontSize="12" leading="12" spaceAfter="12"/> | ||
</stylesheet> | ||
|
||
<story> | ||
<storyPlace x="35" y="660" width="525" height="73" origin="page"> | ||
<para style="intro">RML (Report Markup Language) is ReportLab's own language for specifying the appearance of a printed page, which is converted into PDF by the utility rml2pdf.</para> | ||
<hr color="white" thickness="8pt"/> | ||
<para style="intro">These RML samples showcase techniques and features for generating various types of ouput and are distributed within our commercial package as test cases. Each should be self explanatory and stand alone.</para> | ||
<illustration height="3" width="525" align="center"> | ||
<fill color= "(0,0.99,0.97,0.0)" /> | ||
<rect x="0" y = "-12" width="525" height="3" round="1" fill="1" stroke = "Yes" /> | ||
</illustration> | ||
</storyPlace> | ||
<setNextTemplate name="main2"/> | ||
<para style="normal">Hello World. This is a normal paragraph. Blah <font color="red">IPO</font> blah blah blah blah growth forecast blah | ||
blah blah forecast blah.Blah blah blah blah blah blah blah blah blah blah blah profit blah blah blah blah blah | ||
blah blah blah blah blah IPO.Blah blah blah blah blah blah blah reengineering blah growth blah blah blah | ||
proactive direction strategic blah blah blah forward-thinking blah.Blah blah doubletalk blah blah blah blah | ||
blah profit blah blah growth blah blah blah blah blah profit.Blah blah blah blah venture capital blah blah blah | ||
blah blah forward-thinking blah. Here are some common characters &#x92; = ’ | ||
</para> | ||
<para style="normal">This is another normal paragraph. Blah IPO blah blah blah blah growth forecast blah | ||
blah blah forecast blah.Blah blah blah blah blah blah blah blah blah blah blah profit blah blah blah blah blah | ||
blah blah blah blah blah IPO.Blah blah blah blah blah blah blah reengineering blah growth blah blah blah | ||
proactive direction strategic blah blah blah forward-thinking blah.Blah blah doubletalk blah blah blah blah | ||
blah profit blah blah growth blah blah blah blah blah profit.Blah blah blah blah venture capital blah blah blah | ||
blah blah forward-thinking blah. | ||
</para> | ||
<para style="normal"> | ||
I should NOT have a tiny leading space in front of me! | ||
</para> | ||
<para style="spaced">This is spaced. There should be 12 points before and after.</para> | ||
<para style="normal">Hello World. This is a normal paragraph. Blah IPO blah blah blah blah growth forecast blah | ||
blah blah forecast blah.Blah blah blah blah blah blah blah blah blah blah blah profit blah blah blah blah blah | ||
blah blah blah blah blah IPO.Blah blah blah blah blah blah blah reengineering blah growth blah blah blah | ||
proactive direction strategic blah blah blah forward-thinking blah.Blah blah doubletalk blah blah blah blah | ||
blah profit blah blah growth blah blah blah blah blah profit.Blah blah blah blah venture capital blah blah blah | ||
blah blah forward-thinking blah. | ||
</para> | ||
<docAssign var='currentFrame' expr='doc.frame.id'/> | ||
<docAssign var='currentPageTemplate' expr='doc.pageTemplate.id'/> | ||
<docAssign var='aW' expr='availableWidth'/> | ||
<docAssign var='aH' expr='availableHeight'/> | ||
<docAssign var='aWH' expr='availableWidth,availableHeight'/> | ||
<docAssign var='i' expr="3"/> | ||
<docIf cond='i>3'> | ||
<para style="normal">The value of i is larger than 3</para> | ||
<docElse/> | ||
<para style="normal">The value of i is not larger than to 3</para> | ||
</docIf> | ||
<docIf cond='i==3'> | ||
<para style="normal">The value of i is equal to 3</para> | ||
<docElse/> | ||
<para style="normal">The value of i is not equal to 3</para> | ||
</docIf> | ||
<docIf cond='i<3'> | ||
<para style="normal">The value of i is less than 3</para> | ||
<docElse/> | ||
<para style="normal">The value of i is not less than to 3</para> | ||
</docIf> | ||
<docWhile cond='i'> | ||
<docPara expr='i' format='The value of i is %(__expr__)d'/> | ||
<docExec stmt='i-=1'/> | ||
</docWhile> | ||
<docPara expr='repr(doc._nameSpace)' escape="1"/> | ||
<docWhile cond="doc.page<3"> | ||
<nextPage/> | ||
<docIf cond="doc.page<3"> | ||
<para style="normal">This is the thenPart</para> | ||
<para style="normal">thenPart.....</para> | ||
<docElse/> | ||
<para style="normal">This is the elsePart</para> | ||
<para style="normal">elsePart.....</para> | ||
</docIf> | ||
</docWhile> | ||
<para style="normal">Now we should be on page 3</para> | ||
<setNextTemplate name="autoNextTemplate"/> | ||
<nextPage/> | ||
<para style="normal">This should be on a template called autoNextTemplate</para> | ||
<docAssert cond="doc.pageTemplate.id=='autoNextTemplate'" format="expected doc.pageTemplate.id=='autoNextTemplate'"/> | ||
<nextPage/> | ||
<para style="normal">This should be on a template called autoFollow</para> | ||
<docAssert cond="doc.pageTemplate.id=='autoFollow'" format="expected doc.pageTemplate.id=='autoFollow'"/> | ||
<setNextTemplate name="autoNextTemplate"/> | ||
<nextPage/> | ||
<para style="normal">(2)This should be on a template called autoNextTemplate</para> | ||
<docAssert cond="doc.pageTemplate.id=='autoNextTemplate'" format="expected doc.pageTemplate.id=='autoNextTemplate'"/> | ||
<setNextTemplate name="main"/> | ||
<nextPage/> | ||
<para style="normal">This should be on a template called main</para> | ||
<docAssert cond="doc.pageTemplate.id=='main'" format="expected doc.pageTemplate.id=='main'"/> | ||
|
||
</story> | ||
|
||
</document> |