Source

django-markitup / markitup / settings.py

Full commit
from django.conf import settings
from django.utils.functional import curry, wraps

def fallback_function(text):
    return text

def resolve_function(dotted_path, **params):
    if dotted_path is None:
        return fallback_function
    (module, func) = dotted_path.rsplit('.', 1)
    func = getattr(__import__(module, {}, {}, [func]), func)
    return wraps(func)(curry(func, **params))

def inject_callables(config):
    for key in ('filter', 'preview_filter'):
        if config[key]:
            dotted_path, params = config[key]
        else:
            dotted_path, params = None, {}
        config['%s_function' % key] = resolve_function(dotted_path, **params)

# deprecated/backward-compatible settings
MARKITUP_FILTER = getattr(settings, 'MARKITUP_FILTER', None)
MARKITUP_PREVIEW_FILTER = getattr(
    settings, 'MARKITUP_PREVIEW_FILTER', MARKITUP_FILTER)
MARKITUP_AUTO_PREVIEW = getattr(settings, 'MARKITUP_AUTO_PREVIEW', False)
MARKITUP_SET = getattr(settings, 'MARKITUP_SET', 'markitup/sets/default')
MARKITUP_SKIN = getattr(settings, 'MARKITUP_SKIN', 'markitup/skins/simple')

DEFAULT_CONFIG = {
    'filter': MARKITUP_FILTER,
    'preview_filter': MARKITUP_PREVIEW_FILTER,
    'auto_preview': MARKITUP_AUTO_PREVIEW,
    'set': MARKITUP_SET,
    'skin': MARKITUP_SKIN,
    }

DEFAULT_CONFIG_KEY = 'default'

configurations = getattr(
    settings, 'MARKITUP_CONFIG', (DEFAULT_CONFIG_KEY, DEFAULT_CONFIG))

MARKITUP_CONFIG = {}
for name, params in configurations:
    MARKITUP_CONFIG[name] = dict(DEFAULT_CONFIG, **params)
    # TODO: handle this more elegantly:
    if MARKITUP_CONFIG[name]['preview_filter'] is None:
        MARKITUP_CONFIG[name]['preview_filter'] = \
            MARKITUP_CONFIG[name]['filter']
    inject_callables(MARKITUP_CONFIG[name])

if not DEFAULT_CONFIG_KEY in MARKITUP_CONFIG: # if default key not present
    DEFAULT_CONFIG_KEY = configurations[0][0] # set first as default

def get_filter_function(config_key, preview=False, fail_silently=True):
    if config_key is None:
        config_key = DEFAULT_CONFIG_KEY
    try:
        function_key = \
            'preview_filter_function' if preview else 'filter_function'
        return MARKITUP_CONFIG[config_key][function_key]
    except KeyError:
        if fail_silently:
            return fallback_function
        else:
            raise

JQUERY_URL = getattr(
    settings, 'JQUERY_URL',
    'http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js')