Next / Previous / Contents / Shipman's homepage

6.9. renderTitle(): Remove tags from a title element. Test case: ABCD

The purpose of this function is to convert the content of a DocBook title to an equivalent FO inline. The argument is a DocBook node that contains an info node that in turn contains the title node.

In most cases the content will be only text, but we will convert certain markup to FO inlines:

The title of this section contains a test case: letter A is inside an application tag, letter B is inside a phrase tag (to be ignored), letter C is outside the phrase but inside the application; and letter D is outside the application. The rendered title should end with “ABC” marked up as application, but “D” not marked up.

The logic to do this conversion is a bit tricky. For elements that are rendered, the .tail text on that element must appear in the .tail of the inline; but for elements that are not rendered, the .tail text goes inside the same element containing the surrounding text.

At this level, the logic is fairly simple. We create a new inline as a container for all the rendered text, and call Section 6.10, “foRenderContent(): Render text and descendants into FO” to render the content of the title element into that inline. The inline is returned as the result of this function.

We'll need a dictionary that maps each DocBook element to the corresponding FO font. In the intended functions that follow, the phrase “selected markup” refers to elements whose tags are keys in the markupMap dictionary.

# - - -   r e n d e r T i t l e

def renderTitle(node):
    '''Similar to the text() function in XSLT.

      [ node is a DocBook node as an et.Element ->
          if node has an info/title descendant ->
            return that title's content as an FO inline, with
            selected markup rendered as FO and the remaining content
            not marked up ]
    #-- 1
    # [ result  :=  a new, empty FO inline ]
    result = E.inline()

Refer to Section 6.3.6, “NS_MAP: Namespace prefix map” for the dictionary that defines the NSURI of the namespace prefix “db:”.

    #-- 2
    # [ if node has an info/title descendant ->
    #     titleNode  :=  that descendant as an et.Element
    #   else ->
    #     sys.stderr  +:=  error message
    #     stop execution ]
    nodeList = node.xpath('db:info/db:title', namespaces=NS_MAP)
    if len(nodeList) != 1:
        fatal("Couldn't find info/title: element ({0}), "
              "{1}".format(node.tag, node.attrib))
        titleNode, = nodeList

See Section 6.10, “foRenderContent(): Render text and descendants into FO” for the logic that renders the title into the resulting inline.

    #-- 3
    # [ result  :=  result + (node.text if any) +
    #       (descendants of node with selected markup) ]
    foRenderContent(result, titleNode)

    #-- 3
    return result