# - - - 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:
Double-quote (") characters are converted
to the TEX convention. Even-numbered occurrences are
converted to "``", and odd-numbered
occurrences to "''". See Section 5.11, “TEX_LQ” and Section 5.12, “TEX_RQ”.
Even-numbered underbars are converted to the TEX "{\it ", and odd-numbered underbars to
"\/}". See Section 5.13, “TEX_ITAL_START” and Section 5.14, “TEX_ITAL_END”.
We use an instance of Section 10.10, “class Texer: State machine for TEX
markup” to
perform these substitutions.
#-- 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.
#-- 3 --
if texer.italics:
raise ValueError("Unbalanced underbars: '%s'" % rawEng)
if texer.quotes:
raise ValueError("Unbalanced double-quotes: '%s'" % rawEng)
else:
return result