Next / Previous / Contents / Shipman's homepage

10.9. texify(): Mark up text for TEX

xnomo3.py
# - - -   t e x i f y

def texify(rawEng):
    '''Mark up an English name for TeX.

      [ if rawEng contains both an even number of double-quotes and
        an even number of underbars ->
          return rawEng marked up for TeX
        else -> raise ValueError ]
    '''

This function performs two substitutions:

We use an instance of Section 10.10, “class Texer: State machine for TEX markup” to perform these substitutions.

xnomo3.py
    #-- 1 --
    # [ texer  :=  a new Texer instance in non-quotes, non-italic
    #              state ]
    texer = Texer()

    #-- 2 --
    # [ result  :=  rawEng, with its double-quotes and underbars
    #       converted according to the state of texer
    #   texer  :=  texer with its state modified according to the
    #       number of double-quotes and underbars in rawEng ]
    result = ''.join ( [ texer.convert(c)
                         for c in rawEng ] )

If quotes and underbars were balanced, texer should be in its initial state. Otherwise it's an error.

xnomo3.py
    #-- 3 --
    if texer.italics:
        raise ValueError("Unbalanced underbars: '%s'" % rawEng)
    if texer.quotes:
        raise ValueError("Unbalanced double-quotes: '%s'" % rawEng)
    else:
        return result