Flask Creole

Helpers and filters for Creole usage in Flask.

Quickstart

Install:

easy_install Flask-Creole

Code:

hg clone http://bitbucket.org/aafshar/flask-creole-main

Usage as a template filter

Load the extension:

from flaskext.creole import Creole
creole = Creole(app)

Now in your templates you can use the filters:

{{ article.body|creole2html }}

Usage outside templates

You can also use this functionality outside templates:

from flaskext.creole import Creole
creole = Creole(app)
article.html = creole.creole2html(article.body)

Adding your own macros

You can add your own macros to extend creoleparser.

Load the extension and declare the macro:

from flaskext.creole import Creole
creole = Creole(app)

@creole.macro
def hello(name, environ, body, is_block, *args, **kw):
    return 'Hello World'

Now use it in some markup:

{{ '<<hello>>'|creole2html }}

API

class flaskext.creole.Creole(app, dialect_base=<function creole11_base at 0x8f98a04>, parser_method='xml', **dialect_kw)

Flask extension to add template filters for using creole

Parameters:
  • app – The flask application
  • dialect_base – The creoleparser dialect base to use (default creole11_base)
  • parser_method – The parser_method to use (default ‘xml’)
  • dialect_kw – Keyword arguments passed to the dialect on instantiation.
add_macro(name, func)

Add a named macro to the macros.

Parameters:
  • name – The name of the macro
  • func – The macro handler function (see macro docstring for definition of the handler function signature).
creole2html(markup)

Convert creole markup to html

Parameters:
  • markup – The creole markup to convert to html.
dispatch_macro(macro_name, arg_string, body, is_block, environ)

Dispatch a macro.

Called internally, usually.

macro(func_or_name=None)

Decorator to define a function as a creole macro.

The function should take the signature:

def macro_function(name, environ, body, is_block, *args, **kw):
    ...
name:
The name used to call the macro.
environ:
The Creoleparser environment object
body:
The contents of the macro
is_block:
Whether the macro has been called as a block macro or an inline macro.

Where *args and **kw can be replaced by specific macro argument names, or omitted entirely if the macro has no arguments.

This decorator may be called with a name to use for the macro, and if omitted the decorated function name will be used instead.

All the following are acceptable:

creole = Creole(app)

@creole.macro
def macro_function(name, environ, body, is_block, *args, **kw):
    ...

@creole.macro('my_macro_name')
def macro_function(name, environ, body, is_block, *args, **kw):
    ...

@creole.macro()
def macro_function(name, environ, body, is_block, *args, **kw):
    ...