from io import StringIO from pygments import highlight from pygments.lexers import PythonLexer from pygments.lexers import get_lexer_by_name from pygments.formatters import HtmlFormatter def src(code): lexer = get_lexer_by_name('lisp') return highlight(code, lexer, HtmlFormatter()) builddoc ={ "HEADER1": "h2", "HEADER2": "h3", "HEADER3": "h4", "BREAK": "br", "TEXT": "p", "SRC_BEGIN": src, "EXAMPLE": 'pre', } def html(doc): response = StringIO() for item in doc: tag = builddoc.get(item.token) if not tag: continue if callable(tag): response.write(tag(item.value)) continue else: response.write('<%s>%s<%s/>\n' % (tag, item.value, tag)) response.seek(0) return response