Skip to content


Subversion checkout URL

You can clone with
Download ZIP
tree: 7542f59d83
Fetching contributors…

Cannot retrieve contributors at this time

195 lines (147 sloc) 6.029 kB
import re
from creole import Parser
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import get_lexer_by_name, TextLexer
from pygments.util import ClassNotFound
from biblion.models import Image
class Rules:
# For the link targets:
proto = r'http|https|ftp|nntp|news|mailto|telnet|file|irc'
extern = r'(?P<extern_addr>(?P<extern_proto>%s):.*)' % proto
interwiki = r'''
(?P<inter_wiki> [A-Z][a-zA-Z]+ ) :
(?P<inter_page> .* )
class HtmlEmitter(object):
Generate HTML output for the document
tree consisting of DocNodes.
addr_re = re.compile('|'.join([
]), re.X | re.U) # for addresses
def __init__(self, root):
self.root = root
def get_text(self, node):
"""Try to emit whatever text is in the node."""
return node.children[0].content or ''
return node.content or ''
def html_escape(self, text):
return text.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
def attr_escape(self, text):
return self.html_escape(text).replace('"', '&quot')
# *_emit methods for emitting nodes of the document
def document_emit(self, node):
return self.emit_children(node)
def text_emit(self, node):
return self.html_escape(node.content)
def separator_emit(self, node):
return u'<hr>';
def paragraph_emit(self, node):
return u'<p>%s</p>\n' % self.emit_children(node)
def bullet_list_emit(self, node):
return u'<ul>\n%s</ul>\n' % self.emit_children(node)
def number_list_emit(self, node):
return u'<ol>\n%s</ol>\n' % self.emit_children(node)
def list_item_emit(self, node):
return u'<li>%s</li>\n' % self.emit_children(node)
def table_emit(self, node):
return u'<table>\n%s</table>\n' % self.emit_children(node)
def table_row_emit(self, node):
return u'<tr>%s</tr>\n' % self.emit_children(node)
def table_cell_emit(self, node):
return u'<td>%s</td>' % self.emit_children(node)
def table_head_emit(self, node):
return u'<th>%s</th>' % self.emit_children(node)
def emphasis_emit(self, node):
return u'<i>%s</i>' % self.emit_children(node)
def strong_emit(self, node):
return u'<b>%s</b>' % self.emit_children(node)
def header_emit(self, node):
return u'<h%d>%s</h%d>\n' % (
node.level, self.html_escape(node.content), node.level)
def code_emit(self, node):
return u'<tt>%s</tt>' % self.html_escape(node.content)
def link_emit(self, node):
target = node.content
if node.children:
inside = self.emit_children(node)
inside = self.html_escape(target)
m = self.addr_re.match(target)
if m:
return u'<a href="%s">%s</a>' % (
self.attr_escape(target), inside)
raise NotImplementedError
return u'<a href="%s">%s</a>' % (
self.attr_escape(target), inside)
def image_emit(self, node):
target = node.content
text = self.get_text(node)
m = self.addr_re.match(target)
if m:
return u'<img src="%s" alt="%s">' % (
self.attr_escape(target), self.attr_escape(text))
raise NotImplementedError
return u'<img src="%s" alt="%s">' % (
self.attr_escape(target), self.attr_escape(text))
def macro_emit(self, node):
raise NotImplementedError
def break_emit(self, node):
return u"<br>"
def preformatted_emit(self, node):
return u"<pre>%s</pre>" % self.html_escape(node.content)
def default_emit(self, node):
"""Fallback function for emitting unknown nodes."""
raise TypeError
def emit_children(self, node):
"""Emit all the children of a node."""
return u''.join([self.emit_node(child) for child in node.children])
def emit_node(self, node):
"""Emit a single node."""
emit = getattr(self, '%s_emit' % node.kind, self.default_emit)
return emit(node)
def emit(self):
"""Emit the document represented by self.root DOM tree."""
return self.emit_node(self.root)
class PygmentsHtmlEmitter(HtmlEmitter):
def preformatted_emit(self, node):
content = node.content
lines = content.split("\n")
if lines[0].startswith("#!code"):
lexer_name = lines[0].split()[1]
del lines[0]
lexer_name = None
content = "\n".join(lines)
lexer = get_lexer_by_name(lexer_name)
except ClassNotFound:
lexer = TextLexer()
return highlight(content, lexer, HtmlFormatter(cssclass="syntax")).strip()
class ImageLookupHtmlEmitter(HtmlEmitter):
def image_emit(self, node):
target = node.content
if not re.match(r"^\d+$", target):
return super(ImageLookupHtmlEmitter, self).image_emit(node)
image = Image.objects.get(pk=int(target))
except Image.DoesNotExist:
# @@@ do something better here
return u""
return u"<img src=\"%s\" />" % (image.image_path.url,)
class BiblionHtmlEmitter(PygmentsHtmlEmitter, ImageLookupHtmlEmitter):
def parse(text, emitter=BiblionHtmlEmitter):
return emitter(Parser(text).parse()).emit()
def parse_with_highlighting(text, emitter=PygmentsHtmlEmitter):
return parse(text, emitter=emitter)
Jump to Line
Something went wrong with that request. Please try again.