text.py

text.py is a macro processing system.

It's common when writing code that requires HTML output to write functions that template the HTML so that one can be less verbose. For example:-

def xhtml(title, body): 
   return """\
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >\n
<html xmlns="http://www.w3.org/1999/xhtml" 
   xml:lang="en" lang="en" >
<head>\n<title>%s</title>\n</head>
<body>\n%s\n</body>
</html>\
""" % (title, body)

It got to the stage where I was writing fairly complex templating systems to give XHTML documents from code such as the following:-

homepage = h.xhtml(
 "Welcome to the Homepage", 
 ("So, welcome, and here's some test text.", 
  h.form('POST', 'text:someinput', 'submit'), 
  "Or browse some " + h.a('/blargh', 'stuff') + ".")
)

I realised that if I wanted to come up with something even more flexible and yet less verbose, I'd have to ditch Python tuple/list syntax. However, I didn't want to have to abandon the use of Python functions to handle the code.

The answer was to come up with a new syntax, and write a module that handles the syntax with Python functions. The concept is quite simple: some text interspersed with functions() (the string ")", if used in the text, must be escaped as "\)").

To separate arguments in the functions either "\t", "[\r\n]+", or a space " " can be used. The doc string for the function should contain the information as to which seperator is being used. This allows us to write something such as:-

xhtml( 
   Welcome to the Homepage
	So, welcome, and here's some test text.
	form(POST text:someinput submit)
	Or browse some a(/blargh stuff).
)

And, after processing, have it be equivalent to the Python code above.

Sean B. Palmer