Exemple #1
0
def render_creole(source):
    dialect = create_dialect(creole10_base, wiki_links_base_url="/page/",
                             add_heading_ids="")
    parser = Parser(dialect=dialect, method='html', encoding=None)

    html = parser.render(source)

    # add favicons to external URLs
    soup = BeautifulSoup(html, "html5lib")
    for a_tag in soup.find_all('a'):
        url = a_tag['href']
        if is_external(url):
            favicon = soup.new_tag('img')
            favicon['src'] = "//www.google.com/s2/favicons?domain=" + quote(url)
            favicon['class'] = 'favicon'

            a_tag.insert_after(favicon)

    # circular import fix
    from .models import Page

    # build a set of all possible internal links
    page_urls = Page.objects.all_urls()

    # add a class to links to nonexistent pages
    for a_tag in soup.find_all('a'):
        url = a_tag['href']
        if not is_external(url):
            if not url.endswith('/'):
                url = url + '/'

            if url not in page_urls:
                a_tag['class'] = 'nonexistent'

    return soup.find('body').encode_contents().decode('utf8')
Exemple #2
0
def creole(text, **kw):
    """Returns the text rendered by the Creole markup.
    """
    if Creole is None and settings.DEBUG:
        raise template.TemplateSyntaxError("Error in creole filter: "
            "The Creole library isn't installed, try easy_install Creoleparser.")
    parser = CreoleParser(dialect=dialect)
    return parser.render(text)
Exemple #3
0
def creole(text, **kw):
    """Returns the text rendered by the Creole markup.
    """
    if Creole is None and settings.DEBUG:
        raise template.TemplateSyntaxError("Error in creole filter: "
            "The Creole library isn't installed, try easy_install Creoleparser.")
    parser = CreoleParser(dialect=dialect)
    return parser.render(text)
Exemple #4
0
def creole(value, default_prefix=None):
    try:
        from creoleparser.core import Parser
        from creoleparser.dialects import create_dialect, creole11_base
    except ImportError:
        if settings.TEMPLATE_DEBUG:
            raise template.TemplateSyntaxError, "Error in {% creole %} filter: The Python creoleparser library isn't installed."
        return value
    else:
        __prepare_global_data_structures()
        default_prefix = default_prefix or u'en'
        parser_kwargs = {
            'wiki_links_base_url': '/%s/' % default_prefix,
            'no_wiki_monospace': True,
            'wiki_links_path_func':
            __interwiki_links_path_funcs[default_prefix],
            'wiki_links_class_func': __wiki_links_class_func(default_prefix),
            'interwiki_links_base_urls': __interwiki_links_base_urls,
            # on the next line, we copy the dict first because creoleparser's
            # create_dialect function modifies it inexplicably! (see
            # creoleparser issue #50)
            'interwiki_links_path_funcs': dict(__interwiki_links_path_funcs),
            'interwiki_links_class_funcs': __interwiki_links_class_funcs,
            'external_links_class': 'external',
            'disable_external_content': True,
            'bodied_macros': __bodied_macros,
            'non_bodied_macros': __non_bodied_macros,
        }
        creole2html = Parser(create_dialect(creole11_base, **parser_kwargs),
                             encoding=None)

        return mark_safe(creole2html(value))
Exemple #5
0
def wikify(text, wiki_root):
    parser = Parser(dialect_base(wiki_root))
    return parser.render(text)
Exemple #6
0
"""
The hot lead that formats pages to whatever we like.
This is here in its own module because we want to 
a) be able to test it nice like (even though we don't yet)
b) work out ways to have the format() method come
   from multiple places, if we don't want creole or
   what have you
"""

from creoleparser.dialects import Creole10
from creoleparser.core import Parser

# FIXME: this url should come from config!!!
dialect = Creole10( wiki_links_base_url='/content/name/', wiki_links_space_char=' ' )
parser = Parser( dialect=dialect )

def format(content):
    """Turn creole text into html. For the time being does not
    support CamelCase. This should be fixed."""
    return parser(content)