tools/docco/rltemplate.py
author rptlab
Tue, 30 Apr 2013 14:28:14 +0100
branchpy33
changeset 3723 99aa837b6703
parent 3617 ae5744e97c42
child 4330 617ffa6bbdc8
permissions -rw-r--r--
second stage of port to Python 3.3; working hello world
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3617
ae5744e97c42 reportlab: copyright date changes
robin
parents: 3602
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2012
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     2
#see license.txt for license details
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/tools/docco/rltemplate.py
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     4
# doc template for RL manuals.  Currently YAML is hard-coded
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     5
#to use this, which is wrong.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     6
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     7
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     8
from reportlab.platypus import PageTemplate, \
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     9
     BaseDocTemplate, Frame, Paragraph
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    10
from reportlab.lib.units import inch, cm
3060
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3052
diff changeset
    11
from reportlab.lib.sequencer import Sequencer
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    12
from reportlab.rl_config import defaultPageSize
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    13
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    14
class FrontCoverTemplate(PageTemplate):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    15
    def __init__(self, id, pageSize=defaultPageSize):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    16
        self.pageWidth = pageSize[0]
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    17
        self.pageHeight = pageSize[1]
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    18
        frame1 = Frame(inch,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    19
                       3*inch,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    20
                       self.pageWidth - 2*inch,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    21
                       self.pageHeight - 518, id='cover')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    22
        PageTemplate.__init__(self, id, [frame1])  # note lack of onPage
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    23
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    24
    def afterDrawPage(self, canvas, doc):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    25
        canvas.saveState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    26
        canvas.drawImage('../images/replogo.gif',2*inch, 8*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    27
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    28
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    29
        canvas.setFont('Times-Roman', 10)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    30
        canvas.line(inch, 120, self.pageWidth - inch, 120)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    31
3602
339709dc8c64 Updated userguide
guillaume
parents: 3328
diff changeset
    32
        canvas.drawString(inch, 100, 'Thornton House')
339709dc8c64 Updated userguide
guillaume
parents: 3328
diff changeset
    33
        canvas.drawString(inch, 88, 'Thornton Road')
3052
53d349fb4d21 changed address
tim
parents: 3047
diff changeset
    34
        canvas.drawString(inch, 76, 'Wimbledon')
3602
339709dc8c64 Updated userguide
guillaume
parents: 3328
diff changeset
    35
        canvas.drawString(inch, 64, 'London SW19 4NG, UK')
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    36
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    37
        canvas.restoreState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    38
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    39
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    40
class OneColumnTemplate(PageTemplate):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    41
    def __init__(self, id, pageSize=defaultPageSize):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    42
        self.pageWidth = pageSize[0]
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    43
        self.pageHeight = pageSize[1]
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    44
        frame1 = Frame(inch,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    45
                       inch,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    46
                       self.pageWidth - 2*inch,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    47
                       self.pageHeight - 2*inch,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    48
                       id='normal')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    49
        PageTemplate.__init__(self, id, [frame1])  # note lack of onPage
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    50
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    51
    def afterDrawPage(self, canvas, doc):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    52
        y = self.pageHeight - 50
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    53
        canvas.saveState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    54
        canvas.setFont('Times-Roman', 10)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    55
        canvas.drawString(inch, y+8, doc.title)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    56
        canvas.drawRightString(self.pageWidth - inch, y+8, doc.chapter)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    57
        canvas.line(inch, y, self.pageWidth - inch, y)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    58
        canvas.drawCentredString(doc.pagesize[0] / 2, 0.75*inch, 'Page %d' % canvas.getPageNumber())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    59
        canvas.restoreState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    60
3047
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    61
class TOCTemplate(PageTemplate):
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    62
    def __init__(self, id, pageSize=defaultPageSize):
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    63
        self.pageWidth = pageSize[0]
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    64
        self.pageHeight = pageSize[1]
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    65
        frame1 = Frame(inch,
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    66
                       inch,
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    67
                       self.pageWidth - 2*inch,
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    68
                       self.pageHeight - 2*inch,
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    69
                       id='normal')
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    70
        PageTemplate.__init__(self, id, [frame1])  # note lack of onPage
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    71
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    72
    def afterDrawPage(self, canvas, doc):
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    73
        y = self.pageHeight - 50
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    74
        canvas.saveState()
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    75
        canvas.setFont('Times-Roman', 10)
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    76
        canvas.drawString(inch, y+8, doc.title)
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    77
        canvas.drawRightString(self.pageWidth - inch, y+8, 'Table of contents')
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    78
        canvas.line(inch, y, self.pageWidth - inch, y)
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    79
        canvas.drawCentredString(doc.pagesize[0] / 2, 0.75*inch, 'Page %d' % canvas.getPageNumber())
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    80
        canvas.restoreState()
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
    81
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    82
class TwoColumnTemplate(PageTemplate):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    83
    def __init__(self, id, pageSize=defaultPageSize):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    84
        self.pageWidth = pageSize[0]
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    85
        self.pageHeight = pageSize[1]
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    86
        colWidth = 0.5 * (self.pageWidth - 2.25*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    87
        frame1 = Frame(inch,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    88
                       inch,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    89
                       colWidth,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    90
                       self.pageHeight - 2*inch,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    91
                       id='leftCol')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    92
        frame2 = Frame(0.5 * self.pageWidth + 0.125,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    93
                       inch,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    94
                       colWidth,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    95
                       self.pageHeight - 2*inch,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    96
                       id='rightCol')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    97
        PageTemplate.__init__(self, id, [frame1, frame2])  # note lack of onPage
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    98
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    99
    def afterDrawPage(self, canvas, doc):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   100
        y = self.pageHeight - 50
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   101
        canvas.saveState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   102
        canvas.setFont('Times-Roman', 10)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   103
        canvas.drawString(inch, y+8, doc.title)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   104
        canvas.drawRightString(self.pageWidth - inch, y+8, doc.chapter)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   105
        canvas.line(inch, y, self.pageWidth - inch, y*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   106
        canvas.drawCentredString(doc.pagesize[0] / 2, 0.75*inch, 'Page %d' % canvas.getPageNumber())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   107
        canvas.restoreState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   108
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   109
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   110
class RLDocTemplate(BaseDocTemplate):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   111
    def afterInit(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   112
        self.addPageTemplates(FrontCoverTemplate('Cover', self.pagesize))
3047
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
   113
        self.addPageTemplates(TOCTemplate('TOC', self.pagesize))
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   114
        self.addPageTemplates(OneColumnTemplate('Normal', self.pagesize))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   115
        self.addPageTemplates(TwoColumnTemplate('TwoColumn', self.pagesize))
3060
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3052
diff changeset
   116
        self.seq = Sequencer()
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   117
3060
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3052
diff changeset
   118
    def beforeDocument(self):
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3052
diff changeset
   119
        self.canv.showOutline()
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   120
        self.title = "(Document Title Goes Here)"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   121
        self.chapter = "(No chapter yet)"
3328
4d7f836cd947 reportlab: fix 2to3 warnings
rgbecker
parents: 3060
diff changeset
   122
        self.seq.reset('section')
4d7f836cd947 reportlab: fix 2to3 warnings
rgbecker
parents: 3060
diff changeset
   123
        self.seq.reset('chapter')
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   124
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   125
    def afterFlowable(self, flowable):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   126
        """Detect Level 1 and 2 headings, build outline,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   127
        and track chapter title."""
3060
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3052
diff changeset
   128
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   129
        if isinstance(flowable, Paragraph):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   130
            style = flowable.style.name
3060
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3052
diff changeset
   131
            txt = flowable.getPlainText()
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   132
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   133
            if style == 'Title':
3047
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
   134
                self.title = txt
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   135
            elif style == 'Heading1':
3047
ddf424d4c290 Added table of content to userguide.
jonas
parents: 2963
diff changeset
   136
                self.chapter = txt 
3060
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3052
diff changeset
   137
                key = 'ch%s' % self.seq.nextf('chapter')
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   138
                self.canv.bookmarkPage(key)
3060
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3052
diff changeset
   139
                self.canv.addOutlineEntry(txt, key, 0, 0)
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3052
diff changeset
   140
                self.seq.reset("section")
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3052
diff changeset
   141
                self.notify('TOCEntry', (0, txt, self.page, key))
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   142
            elif style == 'Heading2':
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   143
                self.section = flowable.text
3060
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3052
diff changeset
   144
                key = 'ch%ss%s' % (self.seq.thisf("chapter"), self.seq.nextf("section"))
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   145
                self.canv.bookmarkPage(key)
3060
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3052
diff changeset
   146
                self.canv.addOutlineEntry(txt, key, 1, 0)
3328
4d7f836cd947 reportlab: fix 2to3 warnings
rgbecker
parents: 3060
diff changeset
   147
                self.notify('TOCEntry', (1, txt, self.page, key))