Next / Previous / Contents / Shipman's homepage

4.9. processElt(): Process one literate element

This function takes three arguments:

  1. The fileMap is the dictionary whose keys are the names of output files we've already seen, and each corresponding value is a writeable file handle for that file.

  2. The outName is the name of the output file.

  3. The elt is the actual literate element, as an et.Element instance.

litdocbook5
# - - -   p r o c e s s E l t

def processElt(fileMap, outName, elt):
    '''Process one element that may be literate.

      [ P: fileMap is a dictionary whose keys are file names and
           each corresponding value is a writeable file handle
        P: outName is a file name as a string
        P: elt is an et.Element
        if fileMap has a key (outName) ->
          fileMap[outName]  +:=  text of elt
        else if outName can be opened new for writing ->
          fileMap[outName]  :=  that file, so opened, containing
              all text content in elt and its ancestors
        else ->
          sys.stderr  +:=  error message(s) ]
    '''

First we check to see if this is a new output file. If so, we try to open it for writing. This can fail, in which case we'll need to send an error message to the standard error stream, and return prematurely.

litdocbook5
    #-- 1
    # [ if outName is a key of fileMap ->
    #     I
    #   else if outName can be opened new for writing ->
    #     fileMap[outName]  :=  that file, so opened
    #   else ->
    #     sys.stderr  +:=  error message(s)
    #     return ]
    if outName not in fileMap:
        try:
            fileMap[outName] = open(outName, "w")
        except IOError as x:
            message("*** Can't open '{0}' for writing: {1}".format(
                    outName, str(x)))
            return

At this point we have a destination file handle, fileMap[outName]; the next step is to write the literate source code to that file. For the function that extracts all the text descendants of a node, see Section 4.10, “descendantText(): Find all contained text nodes”.

litdocbook5
    #-- 2
    # [ fileMap[outName]  +:=  concatenation of all text descendants
    #       of elt ]
    text = descendantText(elt)
    fileMap[outName].write(text)