Next / Previous / Contents / TCC Help System / NM Tech homepage

23. buildSubpage(): Build the subpage for one letter

# - - -   b u i l d S u b p a g e

def buildSubpage(dirPath, letterList, k, kwicIndex):
    '''Build the subpage for letterList[k].

      [ (dirPath is a path relative to and
        (letterList is a list of the unique initial letters in
        kwicList) and
        (0 <= k < len(letterList)) and
        (kwicIndex is a kwic.KwicIndex instance) ->
          directory (dirPath)  +:=  (a subpage for letterList[k],
              using letterList to find the next and previous
              pages, displaying entries in kwicIndex for

For the logic that creates a TCCPage, see Section 24, “emptySubpage(): Set up an empty subpage”.

    #-- 1 --
    # [ letter  :=  letterList[k].upper()
    #   pagePath  :=  absolute path to subpage for letterList[k]
    #   page  :=  a new tp.TCCPage instance for letterList[k]
    #       with links to previous and next letters in letterList
    #       if any
    #   table  :=  the table et.Element on that page ]
    letter = letterList[k].upper()
    pagePath, page, table = emptySubpage(dirPath, letterList, k)

The .genWords() method on kwicIndex generates all the keywords that start with our letter, but we have to test to see when that sequence terminates. See Section 25, “addRow(): Add one row to the subpage table”.

    #-- 2 --
    # [ table  +:=  tr elements containing links to entries in
    #       kwicIndex starting with (letter) ]
    for kwicWord in kwicIndex.genWords(letter):
        if kwicWord.word[0].upper() == letter:
            for kwicRef in kwicWord.genRefs():
                addRow(table, kwicRef)

    #-- 3 --
        outFile = file(pagePath, 'w')
    except IOError, details:
        fatal("Can't open subpage '%s' for letter '%r': %s" %
              (pagePath, letter, str(details)))