Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

111 lines (83 sloc) 2.797 kB
Support for different markup languages for the body of a post.
The following markup languages are supported:
- Plain text
- ReStructured Text
- Markdown
- Textile
For ReStructuredText and Markdown syntax highlighting of source code is
# TODO: Add summary rendering.
# TODO: Docstrings.
import logging
import re
from cStringIO import StringIO
# Bloggart is currently based on Django 0.96
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from google.appengine.dist import use_library
use_library('django', '0.96')
from django.utils import html
from django.utils import text
import config
import utils
# Fix sys.path
import fix_path
# Import markup module from lib/
import markdown
import markdown_processor
import rst_directive
import textile
from docutils.core import publish_parts
CUT_SEPARATOR_REGEX = r'<!--.*cut.*-->'
def render_rst(content):
warning_stream = StringIO()
parts = publish_parts(content, writer_name='html4css1',
'_disable_config': True,
'embed_stylesheet': False,
'warning_stream': warning_stream,
'report_level': 2,
rst_warnings = warning_stream.getvalue()
if rst_warnings:
return parts['html_body']
def render_markdown(content):
md = markdown.Markdown()
md.textPreprocessors.insert(0, markdown_processor.CodeBlockPreprocessor())
return md.convert(content)
def render_textile(content):
return textile.textile(content.encode('utf-8'))
# Mapping: string ID -> (human readable name, renderer)
'html': ('HTML', lambda c: c),
'txt': ('Plain Text', lambda c: html.linebreaks(html.escape(c))),
'markdown': ('Markdown', render_markdown),
'textile': ('Textile', render_textile),
'rst': ('ReStructuredText', render_rst),
def get_renderer(post):
"""Returns a render function for this posts body markup."""
return MARKUP_MAP.get(post.body_markup)[1]
def clean_content(content):
"""Clean up the raw body.
Actually this removes the cut separator.
return re.sub(CUT_SEPARATOR_REGEX, '', content)
def render_body(post):
"""Return the post's body rendered to HTML."""
renderer = get_renderer(post)
return renderer(clean_content(post.body))
def render_summary(post):
"""Return the post's summary rendered to HTML."""
renderer = get_renderer(post)
match =, post.body)
if match:
return renderer(post.body[:match.start(0)])
return text.truncate_html_words(renderer(clean_content(post.body)),
Jump to Line
Something went wrong with that request. Please try again.