import py import docutils.core from docutils.writers import html4css1 # override some stuff in docutils to make it easier to use the results with # templess class HTMLTranslatorNoNS(html4css1.HTMLTranslator): doctype = '' def astext(self): ret = ''.join(['\n\n'] + self.head + self.body_prefix + self.body_pre_docinfo + self.docinfo + self.body + self.body_suffix) return ret class HTMLWriterNoNS(html4css1.Writer): def __init__(self, *args, **kwargs): html4css1.Writer.__init__(self, *args, **kwargs) self.translator_class = HTMLTranslatorNoNS def rest2html(rst): w = HTMLWriterNoNS() html = docutils.core.publish_string(rst, writer=w) return html reg_html = py.std.re.compile(r'<[^>]+>') def rest2text(rst): """strip ReST markup""" # nasty trick: convert to XML and remove markup (slow!!) html = docutils.core.publish_string(rst, writer=html4css1.Writer()) return reg_html.sub(' ', html)