def render_markup(self, language, source): if not language in MARKUP_LANGUAGES: raise ValueError('Unknown markup language %s' % language) if language == 'text': html = bleach.linkify( webhelpers.html.converters.format_paragraphs(source)) elif language == 'html': html = source elif language == 'md': html = webhelpers.html.converters.markdown(source) elif language == 'textile': html = webhelpers.html.converters.textilize(source) elif language == 'rst': overrides = { 'input_encoding': 'unicode', 'doctitle_xform': False, 'initial_header_level': 2, } html = docutils.core.publish_parts( source=source, writer_name='html', settings_overrides=overrides)['fragment'] elif language == 'creole': html = creole.html_emitter.HtmlEmitter( creole.Parser(source).parse()).emit() # Sanitize all HTML output with bleach (don't rely on safe-mode of # converters above as they're not necessarily as good and sometimes # disable useful features like embedding HTML in MarkDown) return webhelpers.html.builder.literal( bleach.clean(html, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRS))
def handle_entry(self, time, data): self.out.timestamp(time) if creole: emitter = creole.HtmlEmitter( creole.Parser(from_utf8(data)).parse(), self.out.new_id) data = emitter.emit() headers = emitter.headers else: data, headers = escape(data), [] self.out.newstate('entry', '', '', data) for level, text, targetid in headers: self.out.toc_entry(level, text, targetid)
def to_html(cls, kb_entry): def custom_link_emit(node): if node.children: text = html_emitter.emit_children(node) else: text = None return cls.convert_link(kb_entry, node.content, text) def custom_image_emit(node): target = cls.convert_attach(kb_entry, node.content) text = html_emitter.get_text(node) return u'<img src="%s" alt="%s" />' % (html_emitter.attr_escape( target), html_emitter.attr_escape(text)) parser = creole.Parser(unicode(kb_entry.body)) html_emitter = creole.HtmlEmitter(parser.parse(), macros=cls.get_macro_wrapper()) html_emitter.link_emit = custom_link_emit html_emitter.image_emit = custom_image_emit return html_emitter.emit()
def __call__(self, request, text): document = creole.Parser(text).parse() return creole.HtmlEmitter(document).emit().encode('utf-8', 'ignore')