Next / Previous / Contents / Shipman's homepage

29. fohelptest: Test driver for fohelpers

This driver file generates a document with even-odd layout, a running head, and the page number in the footer.

fohelptest
#!/usr/bin/env python
#================================================================
# fohelptest: Test driver for fohelpers.py
#   For documentation, see:
#      http://www.nmt.edu/~shipman/soft/fohelpers/
#================================================================
# Imports
#----------------------------------------------------------------
import sys
from fohelpers import *

#================================================================
# Manifest constants
#----------------------------------------------------------------

#--
# Names of the simple masters and the repeatable master
#--
ODD_MASTER = "odd"
EVEN_MASTER = "even"
REPEAT_MASTER = "all"

#--
# Names of the regions
#--
ODD_BEFORE = "odd-before"
EVEN_BEFORE = "even-before"
ODD_AFTER = "odd-after"
EVEN_AFTER = "even-after"

We start by setting up some PageDim instances for the two different page layouts (even and odd), and three fonts, headFont for the header, footFont for the footer, and bodyFont for the body text.

fohelptest
# - - - - -   m a i n

oddDims = pageDimFactory("letter")
oddDims.pageMargins.left = "2.0in"
oddDims.pageMargins.right = "1.0in"

evenDims = pageDimFactory("letter")
evenDims.pageMargins.left="1.0in"
evenDims.pageMargins.right="2.0in"

mainFamily = "Palatino, Paladino, serif"
monoFamily = "Lucida Typewriter, monospace"

headFont = font(fontFamily=mainFamily, fontSize="12pt")
footFont = font(fontFamily=mainFamily, fontSize="9pt",
                fontStyle="italic")
bodyFont = font(fontFamily=mainFamily, fontSize="10pt")
monoFont = font(fontFamily=monoFamily, fontSize="10pt")

The tree is built from the top down: first the FlowTree instance that holds the whole document, then the page masters, then the main page sequence.

fohelptest
#--
# Create the root element, and attach the layout-master-set
# and its children: two simple-page-master elements and a
# page-sequence-master with repeatable-page-master-alternatives.
#--

tree = FlowTree()

oddMaster = subElement(tree.masters,
    simpleMaster(ODD_MASTER, oddDims,
        regionBody(oddDims),
        regionBefore(oddDims, ODD_BEFORE),
        regionAfter(oddDims, ODD_AFTER)))

evenMaster = subElement(tree.masters,
    simpleMaster(EVEN_MASTER, evenDims,
        regionBody(evenDims),
        regionBefore(evenDims, EVEN_BEFORE),
        regionAfter(evenDims, EVEN_AFTER)))

tree.masters.append(repeatMaster(REPEAT_MASTER,
    conditionalMaster(ODD_MASTER, oddEven="odd"),
    conditionalMaster(EVEN_MASTER, oddEven="even")))

The content is in a page-sequence element that is a child of the root. There are five child elements of the page-sequence: four static-content elements defining the odd-page header, odd-page footer, even-page header, and even-page footer; and a flow element containing the body content.

fohelptest
#--
# Append a page-sequence to the root, then pour static content
# into the header and footer of the even and odd page areas.
#--
pages = subElement(tree.root, pageSequence(REPEAT_MASTER))

oddHead = subElement(pages,
    staticContent(ODD_BEFORE,
        block(headFont, dash(textAlign='right'),
              "Running head")))

oddFoot = subElement ( pages,
    staticContent(ODD_AFTER,
        block(footFont, dash(textAlign='right'),
              E("page-number"))))

evenHead = subElement ( pages,
    staticContent(EVEN_BEFORE,
        block(headFont, dash(textAlign='left'),
              "Running head")))

evenFoot = subElement ( pages,
    staticContent(EVEN_AFTER,
        block(footFont, dash(textAlign='left'),
              E("page-number"))))

Into the flow element we pour ten paragraphs, each of which is twenty copies of a sample sentence.

fohelptest
sampleText = ( "Now is the time for all good organisms to "
               "come to the aid of their planet. " )
flow = subElement(pages, E.flow(dash(flowName="xsl-region-body")))
for i in range(10):
    parBlock = subElement(flow, block(bodyFont))
    parBlock.attrib["space-before"] = "10pt"
    parBlock.text = sampleText*20

tree.write(sys.stdout)