Skip to content

Commit

Permalink
Properly implement pto tag so that tag not found errors are not r…
Browse files Browse the repository at this point in the history
…aised.
  • Loading branch information
strichter committed Jun 14, 2019
1 parent e14cdf0 commit 3daec7a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 35 deletions.
89 changes: 55 additions & 34 deletions src/z3c/rml/flowable.py
Expand Up @@ -1067,38 +1067,6 @@ def process(self):
self.klass(img, flow.flow, **args))


class IPTO(interfaces.IRMLDirectiveSignature):
'''A container for flowables decorated with trailer & header lists.
If the split operation would be called then the trailer and header
lists are injected before and after the split. This allows specialist
"please turn over" and "continued from previous" like behaviours.'''

class PTO(Flowable):
signature = IPTO
klass = reportlab.platypus.flowables.PTOContainer

def process(self):
# Get Content
flow = Flow(self.element, self.parent)
flow.process()
# Get the header
ptoHeader = self.element.find('pto_header')
header = None
if ptoHeader is not None:
header = Flow(ptoHeader, self.parent)
header.process()
header = header.flow
# Get the trailer
ptoTrailer = self.element.find('pto_trailer')
trailer = None
if ptoTrailer is not None:
trailer = Flow(ptoTrailer, self.parent)
trailer.process()
trailer = trailer.flow
# Create and add the PTO Container
self.parent.flow.append(self.klass(flow.flow, trailer, header))


class IIndent(interfaces.IRMLDirectiveSignature):
"""Indent the contained flowables."""

Expand All @@ -1112,6 +1080,7 @@ class IIndent(interfaces.IRMLDirectiveSignature):
description=u'The indentation to the right.',
required=False)


class Indent(Flowable):
signature = IIndent

Expand Down Expand Up @@ -1568,7 +1537,6 @@ class IFlow(interfaces.IRMLDirectiveSignature):
occurence.ZeroOrMore('keepTogether', IKeepTogether),
occurence.ZeroOrMore('img', IImage),
occurence.ZeroOrMore('imageAndFlowables', IImageAndFlowables),
occurence.ZeroOrMore('pto', IPTO),
occurence.ZeroOrMore('indent', IIndent),
occurence.ZeroOrMore('fixedSize', IFixedSize),
occurence.ZeroOrMore('bookmarkPage', IBookmarkPage),
Expand Down Expand Up @@ -1637,7 +1605,6 @@ class Flow(directive.RMLDirective):
'keepTogether': KeepTogether,
'img': Image,
'imageAndFlowables': ImageAndFlowables,
'pto': PTO,
'indent': Indent,
'fixedSize': FixedSize,
'bookmarkPage': BookmarkPage,
Expand Down Expand Up @@ -1665,3 +1632,57 @@ def __init__(self, *args, **kw):
def process(self):
self.processSubDirectives()
return self.flow


class IPTOHeader(IFlow):
"""A container for flowables used at the beginning of a frame.
"""


class PTOHeader(Flow):

def process(self):
self.parent.header = super(PTOHeader, self).process()


class IPTOTrailer(IFlow):
"""A container for flowables used at the end of a frame.
"""


class PTOTrailer(Flow):

def process(self):
self.parent.trailer = super(PTOTrailer, self).process()


class IPTO(IFlow):
"""A container for flowables decorated with trailer & header lists.
If the split operation would be called then the trailer and header
lists are injected before and after the split. This allows specialist
"please turn over" and "continued from previous" like behaviours.
"""
occurence.containing(
occurence.ZeroOrOne('pto_header', IPTOHeader),
occurence.ZeroOrOne('pto_trailer', IPTOTrailer),
)


class PTO(Flow):
signature = IPTO
klass = reportlab.platypus.flowables.PTOContainer

factories = dict(
pto_header=PTOHeader,
pto_trailer=PTOTrailer,
**Flow.factories
)

header = None
trailer = None

def process(self):
flow = super(PTO, self).process()
self.parent.flow.append(
self.klass(flow, self.trailer, self.header))
10 changes: 9 additions & 1 deletion src/z3c/rml/template.py
Expand Up @@ -23,6 +23,7 @@
class IStory(flowable.IFlow):
"""The story of the PDF file."""
occurence.containing(
occurence.ZeroOrMore('pto', flowable.IPTO),
*flowable.IFlow.getTaggedValue('directives'))

firstPageTemplate = attr.Text(
Expand All @@ -31,9 +32,15 @@ class IStory(flowable.IFlow):
default=None,
required=False)


class Story(flowable.Flow):
signature = IStory

factories = dict(
pto=flowable.PTO,
**flowable.Flow.factories
)

def process(self):
self.parent.flowables = super(Story, self).process()
self.parent.doc._firstPageTemplateIndex = self.getFirstPTIndex()
Expand Down Expand Up @@ -123,7 +130,8 @@ def process(self):
args = dict(self.getAttributeValues())
# Deal with percentages
for name, dir in (('x1', 0), ('y1', 1), ('width', 0), ('height', 1)):
if isinstance(args[name], six.string_types) and args[name].endswith('%'):
if (isinstance(args[name], six.string_types) and
args[name].endswith('%')):
args[name] = float(args[name][:-1])/100*size[dir]
frame = platypus.Frame(**args)
self.parent.frames.append(frame)
Expand Down

0 comments on commit 3daec7a

Please sign in to comment.