Skip to content

Commit

Permalink
- Renamed pdfInclude to documented includePdfPages and added …
Browse files Browse the repository at this point in the history
…`pages`

  attribute, so that you can only include specific pages.
  • Loading branch information
strichter committed Dec 21, 2012
1 parent f2f5c7c commit ce77a5f
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 57 deletions.
5 changes: 4 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
CHANGES
=======

1.2.0 (unreleased)
2.0.0 (unreleased)
------------------

- Implemented ``saveState`` and ``restoreState`` directives. (LP #666194)
Expand Down Expand Up @@ -58,6 +58,9 @@ CHANGES

- Implemented ``color`` directive inside the ``initialize`` directive.

- Renamed ``pdfInclude`` to documented ``includePdfPages`` and added `pages`
attribute, so that you can only include specific pages.

- Don't show "doc" namespace in reference snippets.

- Create a list of RML2PDF and z3c.rml differences.
Expand Down
6 changes: 1 addition & 5 deletions RML-DIFFERENCES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ denotes a feature not in RML2PDF. The "->" arrow designates a difference in
naming.


- pre/xpre: -bulletText, -dedent, -text
- pre/xpre: -bulletText, -dedent

- blockTable: -repeatRows, -alignment

Expand All @@ -78,8 +78,6 @@ naming.

- doForm

- includePdfPages

- figure

- imageFigure
Expand Down Expand Up @@ -115,5 +113,3 @@ naming.

- frame: -*Padding, -showBoundary

- -pdfInclude

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def read(*rnames):

setup (
name='z3c.rml',
version='1.1.1dev',
version='2.0.0dev',
author = "Stephan Richter and the Zope Community",
author_email = "zope-dev@zope.org",
description = "An alternative implementation of RML",
Expand Down
21 changes: 21 additions & 0 deletions src/z3c/rml/attr.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,27 @@ def fromUnicode(self, ustr):
return result


class IntegerSequence(Sequence):
"""A sequence of integers."""

def fromUnicode(self, ustr):
ustr = ustr.strip()
pieces = self.splitre.split(ustr)
numbers = set([])
for piece in pieces:
# Ignore empty pieces.
if not piece:
continue
# The piece is a range.
if '-' in piece:
start, end = piece.split('-')
# Make range lower and upper bound inclusive.
numbers.update(range(int(start), int(end)+1))
continue
# The piece is just a number
numbers.add(int(piece))
return list(numbers)

class Choice(BaseChoice):
"""A choice of several values. The values are always case-insensitive."""

Expand Down
32 changes: 20 additions & 12 deletions src/z3c/rml/pdfinclude.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@
from z3c.rml import attr, flowable, interfaces, occurence, page


class PDFIncludeFlowable(flowables.Flowable):
class IncludePdfPagesFlowable(flowables.Flowable):

def __init__(self, pdf_file, mergeprocessor):
def __init__(self, pdf_file, pages, mergeprocessor):
flowables.Flowable.__init__(self)
self.pdf_file = pdf_file
self.proc = mergeprocessor

pdf = pyPdf.PdfFileReader(pdf_file)
self.num_pages = pdf.getNumPages()
self.pages = pages if pages else range(1, self.num_pages+1)

self.width = 10<<32
self.height = 10<<32

Expand All @@ -40,9 +42,9 @@ def draw():

def split(self, availWidth, availheight):
result = []
for i in range(self.num_pages):
for i in self.pages:
result.append(flowables.PageBreak())
result.append(PDFPageFlowable(self, i, availWidth, availheight))
result.append(PDFPageFlowable(self, i-1, availWidth, availheight))
return result


Expand All @@ -67,17 +69,22 @@ def draw(self):
def split(self, availWidth, availheight):
return [self]

class IPDFInclude(interfaces.IRMLDirectiveSignature):
"""Inserts a PDF"""
class IIncludePdfPages(interfaces.IRMLDirectiveSignature):
"""Inserts a set of pages from a given PDF."""

filename = attr.File(
title=u'Path to file',
description=u'The pdf file to include.',
required=True)

pages = attr.IntegerSequence(
title=u'Pages',
description=u'A list of pages to insert.',
required=False)


class PDFInclude(flowable.Flowable):
signature = IPDFInclude
class IncludePdfPages(flowable.Flowable):
signature = IIncludePdfPages

def getProcessor(self):
manager = attr.getManager(self, interfaces.IPostProcessorManager)
Expand All @@ -92,14 +99,15 @@ def process(self):
if pyPdf is None:
raise Exception(
'pyPdf is not installed, so this feature is not available.')
(pdf_file,) = self.getAttributeValues(valuesOnly=True)
args = dict(self.getAttributeValues())
proc = self.getProcessor()
self.parent.flow.append(PDFIncludeFlowable(pdf_file, proc))
self.parent.flow.append(
IncludePdfPagesFlowable(args['filename'], args.get('pages'), proc))


flowable.Flow.factories['pdfInclude'] = PDFInclude
flowable.Flow.factories['includePdfPages'] = IncludePdfPages
flowable.IFlow.setTaggedValue(
'directives',
flowable.IFlow.getTaggedValue('directives') +
(occurence.ZeroOrMore('pdfInclude', IPDFInclude),)
(occurence.ZeroOrMore('includePdfPages', IIncludePdfPages),)
)
2 changes: 1 addition & 1 deletion src/z3c/rml/reference.pt
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
</drawCenteredString>
<setFont name="Helvetica" size="24" />
<drawCenteredString x="10.5cm" y="18.5cm">
Version 1.1
Version 2.0
</drawCenteredString>
</pageGraphics>
<frame id="main" x1="3cm" y1="2cm" width="17cm" height="25.7cm" />
Expand Down
Binary file modified src/z3c/rml/rml-reference.pdf
Binary file not shown.
Loading

0 comments on commit ce77a5f

Please sign in to comment.