6.14.9.6. Prepare Text and Code for Lout

Converting programming language code to something that Lout can typeset gracefully is a bit tricky. First, the @Verbatim command of Lout does not appear to work right on Windows/NT. More than likely, the version of sed is not behaving properly. The next most simple approach wraps code strings in double quotes after quoting any double quotes already present and duplicating backslashes. This is a problem because Lout then sees the string as a single, unbreakable object. Line wrapping then works incorrectly. This also enables us to color the text that we display. We can then display keywords in bold, comments grayed out and strings in italics, for instance.

The only reasonable solution left is to map characters special to Lout to their @Char names. It takes more space, but is always guaranteed to be correct.

Start python section to interscript/weavers/lout.py[8 /34 ] Next Prev First Last
   122: #line 277 "lout_weaver.ipk"
   123:     loutCharMap = { "@"  : "{@Char at}",
   124:                     "\"" : "{@Char quotedbl}",
   125:                     "\\" : "{@Char backslash}",
   126:                     "{"  : "{@Char braceleft}",
   127:                     "}"  : "{@Char braceright}",
   128:                     "/"  : "{@Char slash}",
   129:                     "#"  : "{@Char numbersign}",
   130:                     "~"  : "{@Char tilde}",
   131:                     "^"  : "{@Char asciicircum}",
   132:                     "&"  : "{@Char ampersand}",
   133:                     "$"  : "{@Char dollar}",
   134:                     "|"  : "{@Char bar}"
   135:                     }
   136: 
   137:     def cvt_code(self, text):
   138:         n = []
   139:         for c in text:
   140:             if lout_weaver.loutCharMap.has_key(c):
   141:                 n.append(lout_weaver.loutCharMap[c])
   142:             else:
   143:                 n.append(c)
   144: 
   145:         return string.join(n,'')
   146: 
   147:     def cvt_text(self, text):
   148:         if self.translating:
   149:             return self.cvt_code(text)
   150:         else:
   151:             return text
End python section to interscript/weavers/lout.py[8]

The next thing to consider is dealing with formatting issues that the current weaver API does not support. To allow us to do anything that Lout can do we simply disable the translation that we have established above. set_translating() does this. This approach was selected because a begin_translating() and end_translating() pair would be strange because translation is enabled by default.

Start python section to interscript/weavers/lout.py[9 /34 ] Next Prev First Last
   152: #line 325 "lout_weaver.ipk"
   153:     def set_translating(self, translation=1):
   154:         self.translating = translation
End python section to interscript/weavers/lout.py[9]

To quickly test this: Now we should be able to do things like @Box{box} a word.