def render_open(self, parser, node_index): tag_data = parser.tag_data globals = tag_data.setdefault("EvalTag.globals", {}) locals = tag_data.setdefault("EvalTag.locals", {}) contents = self.get_contents(parser).strip() self.skip_contents(parser) stdout = sys.stdout contents = "\n".join(contents.split('\r\n')) + "\n" sys.stdout = StringIO() try: try: try: exec contents in globals, locals except Exception, e: result = str(e) except: result = "" finally: result = sys.stdout.getvalue() sys.stdout = stdout if not self.params.lower() == "safe": result = postmarkup._escape(result) return result.rstrip()
class EvalTag(postmarkup.TagBase): DEFAULT_NAME = "eval" def __init__(self, name, **kwargs): postmarkup.TagBase.__init__(self, name, inline=True, enclosed=True, strip_first_newline=True) def render_open(self, parser, node_index): tag_data = parser.tag_data globals = tag_data.setdefault("EvalTag.globals", {}) locals = tag_data.setdefault("EvalTag.locals", {}) contents = self.get_contents(parser).strip() self.skip_contents(parser) try: result = str(eval(contents, globals, locals)) except Exception, e: try: result = str(e) except: result = "(Error in eval tag)" if not self.params.lower() == "safe": result = postmarkup._escape(result) return result.rstrip()
def code_linenumbers(content, language): try: lexer = get_lexer_by_name(language, stripall=True) except ClassNotFound: content = _escape(content) return '<div class="code"><pre>%s</pre></div>' % content formatter = HtmlFormatter(linenos=True, cssclass="code") return mark_safe( highlight(content, lexer, formatter).strip() )
def render(markup, markup_type): markup = markup or "" if markup_type == "postmarkup": tag_data = {} html = post_render(markup, paragraphs=True, clean=True, tag_data=tag_data) text = postmarkup.textilize(html) output = tag_data.get("output", {}) sections = output.get("sections", {}) for key, value in sections.items(): sections[key] = [post_markup(s, paragraphs=True, clean=True) for s in value] data = output summary_markup = output.get("summary", "") summary_html = "" if summary_markup.strip(): summary = post_markup(output.get("summary", ""), paragraphs=True, clean=True) summary_html = summary elif markup_type == "emarkup": sections = extendedmarkup.parse(markup) html = extendedmarkup.chunks_to_html(sections["main"]) summary_html = html text = postmarkup.textilize(html) data = dict(sections=sections) elif markup_type == "comment_bbcode": html = postmarkup.render_bbcode(markup, paragraphs=True, clean=True) text = postmarkup.textilize(html) return html, html, text, {} elif markup_type == "comment_wordpress": html = markup summary_html = html text = postmarkup.textilize(html) data = {} elif markup_type == "text": html = "<p>%s</p>" % postmarkup._escape(markup) summary_html = html text = postmarkup.textilize(markup) data = {} else: html = markup summary_html = html text = postmarkup.textilize(html) data = {} more_i = html.find("<!--more-->") if more_i == -1: more_i = html.find("<!-- more -->") if more_i != -1: summary_html = html[:more_i] summary_html = unicode(BeautifulSoup(summary_html)) return html, summary_html, text, data
def render(markup, markup_type): markup = markup or "" if markup_type == "postmarkup": tag_data = {} html = post_render(markup, paragraphs=True, clean=True, tag_data=tag_data) text = postmarkup.textilize(html) output = tag_data.get('output', {}) sections = output.get("sections", {}) for key, value in sections.items(): sections[key] = [ post_markup(s, paragraphs=True, clean=True) for s in value ] data = output summary_markup = output.get("summary", "") summary_html = "" if summary_markup.strip(): summary = post_markup(output.get("summary", ""), paragraphs=True, clean=True) summary_html = summary elif markup_type == "emarkup": sections = extendedmarkup.parse(markup) html = extendedmarkup.chunks_to_html(sections['main']) summary_html = html text = postmarkup.textilize(html) data = dict(sections=sections) elif markup_type == "comment_bbcode": html = postmarkup.render_bbcode(markup, paragraphs=True, clean=True) text = postmarkup.textilize(html) return html, html, text, {} elif markup_type == "comment_wordpress": html = markup summary_html = html text = postmarkup.textilize(html) data = {} elif markup_type == "text": html = "<p>%s</p>" % postmarkup._escape(markup) summary_html = html text = postmarkup.textilize(markup) data = {} else: html = markup summary_html = html text = postmarkup.textilize(html) data = {} more_i = html.find('<!--more-->') if more_i == -1: more_i = html.find('<!-- more -->') if more_i != -1: summary_html = html[:more_i] summary_html = unicode(BeautifulSoup(summary_html)) return html, summary_html, text, data