variable_lookup="lenient")

# context locals.  these two objects are use by the application to
# bind objects to the current context.  A context is defined as the
# current thread and the current greenlet if there is greenlet support.
local = Local()
local_manager = LocalManager([local])
request = local("request")
application = local("application")

# create a new creole parser
creole_parser = creoleparser.Parser(
    dialect=creoleparser.create_dialect(
        creoleparser.creole10_base,
        wiki_links_base_url="",
        wiki_links_path_func=lambda page_name: href(page_name),
        wiki_links_space_char="_",
        no_wiki_monospace=True,
    ),
    method="html",
)


def generate_template(template_name, **context):
    """Load and generate a template."""
    context.update(href=href, format_datetime=format_datetime)
    return template_loader.load(template_name).generate(**context)


def parse_creole(markup):
    """Parse some creole markup and create a genshi stream."""
    return creole_parser.generate(markup)
Example #2
0
            try:
                lexer = get_lexer_by_name(lines.pop(0)[2:].strip())
            except ClassNotFound:
                pass
            else:
                return Markup(
                    highlight(u'\n'.join(lines), lexer, pygments_formatter))
        return builder.tag.pre(u'\n'.join(lines))


custom_dialect = creoleparser.create_dialect(creoleparser.creole10_base)
# hacky way to get rid of image support
custom_dialect.img = custom_dialect.no_wiki
custom_dialect.pre = CodeBlock()

_parser = creoleparser.Parser(dialect=custom_dialect, method='html')


def format_creole(text):
    return Markup(_parser.render(text, encoding=None))


def split_lines_wrapping(text, width=74, threshold=82):
    lines = text.splitlines()
    if all(len(line) <= threshold for line in lines):
        return lines
    result = []
    for line in lines:
        if len(line) <= threshold:
            result.append(line)
            continue
Example #3
0
from __future__ import with_statement
import re
import creoleparser
from difflib import SequenceMatcher
from operator import itemgetter
from itertools import chain
from genshi.core import Stream, QName, Attrs, START, END, TEXT
from contextlib import contextmanager

from jinja2 import Markup

_leading_space_re = re.compile(r'^(\s+)(?u)')
_diff_split_re = re.compile(r'(\s+)(?u)')

_parser = creoleparser.Parser(dialect=creoleparser.create_dialect(
    creoleparser.creole10_base),
                              method='html')


def format_creole(text, inline=False):
    """Format creole markup."""
    kwargs = {}
    if inline:
        kwargs['context'] = 'inline'
    return Markup(_parser.render(text, encoding=None, **kwargs))


def format_creole_diff(old, new):
    """Renders a creole diff for two texts."""
    differ = StreamDiffer(_parser.generate(old), _parser.generate(new))
    return Markup(differ.get_diff_stream().render('html', encoding=None))