from StringIO import StringIO from elementtree.ElementTree import XML from libbe.restconvert import rest_xml def to_unix(text): skip_newline = False for ch in text: if ch not in ('\r', '\n'): yield ch else: if ch == '\n': if skip_newline: continue else: skip_newline = True yield '\n' def soft_text(text): first_space = False translations = {'\n': '
\n', '&': '&', '\x3c': '<', '\x3e': '>'} for ch in to_unix(text): if ch == ' ' and first_space is True: yield ' ' first_space = ch in (' ') try: yield translations[ch] except KeyError: yield ch def soft_pre(text): return XML('
'+ ''.join(soft_text(text)).encode('utf-8')+'
') def get_rest_body(rest): xml, warnings = rest_xml(StringIO(rest)) return xml.find('{http://www.w3.org/1999/xhtml}body'), warnings def comment_body_xhtml(comment): if comment.content_type == "text/restructured": return get_rest_body(comment.body)[0] else: return soft_pre(comment.body) def select_among(name, options, default, display_names=None): output = ['") return XML("".join(output))