# Path: djinja/template/base.py
# class Library(object):
#     def __init__(self):
#         self.filters = {}
#         self.extensions = []
#         self.globals = {}
#         self.tests = {}
#     def tag(self, func,name=None):
#         if name==None:
#             name = getattr(func, "_decorated_function", func).__name__
#         self.globals[name] = func
# 
#     def filter(self, func,name=None):
#         if name==None:
#             name = getattr(func, "_decorated_function", func).__name__
#         self.filters[name] = func
# 
#     def extension(self, ext):
#         self.extensions.append(ext)
#     
#     def set(self,*args,**kwargs):
#         for k in kwargs.keys(): #is a object with a name
#             self[k] = kwargs[k]
#         for a in args:
#             self.tag(a) #is a function
#         
#     def inclusion_tag(self,template,func,takes_context=False):
#         if takes_context:
#             import jinja2
#             @jinja2.contextfunction
#             def tag(context,*args,**kwargs):
#                 from django.template.loader import render_to_string
#                 return render_to_string(template, func(dict_from_context(context),*args,**kwargs))
#             
#         else:
#             def tag(*args,**kwargs):
#                 from django.template.loader import render_to_string
#                 return render_to_string(template, func(*args,**kwargs))
#         
#         #raise Exception(getattr(func, "_decorated_function", func))
#         self.tag(tag,name=getattr(func, "_decorated_function", func).__name__) 
#     
#     def __setitem__(self, item, value):
#         self.globals[item] = value
#     
#     def __getitem__(self, item, value): #for reciprocity with __setitem__
#         return globals[item]
# 
# Path: djinja/template/base.py
# def import_library(taglib_module):
#     """Load a template tag library module.
# 
#     Verifies that the library contains a 'register' attribute, and
#     returns that attribute as the representation of the library
#     """
#     app_path, taglib = taglib_module.rsplit('.',1)
#     app_module = import_module(app_path)
#     try:
#         mod = import_module(taglib_module)
#     except ImportError, e:
#         # If the ImportError is because the taglib submodule does not exist, that's not
#         # an error that should be raised. If the submodule exists and raised an ImportError
#         # on the attempt to load it, that we want to raise.
#         if not module_has_submodule(app_module, taglib):
#             return None
#         else:
#             raise InvalidTemplateLibrary("ImportError raised loading %s: %s" % (taglib_module, e))
#     try:
#         return mod.register
#     except AttributeError:
#         raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % taglib_module)


from djinja.template.base import Library
from jinja2.runtime import Undefined
from django.utils.safestring import mark_safe
from django.utils.encoding import force_unicode
from django.conf import settings
    from django.core.urlresolvers import reverse, NoReverseMatch
    from django.utils.timesince import timesince
    from django.conf import settings
    from django.utils.dateformat import format
    from django.conf import settings
    from django.utils.dateformat import time_format
    from django.utils.text import truncate_words
    from django.utils.text import truncate_html_words
from django.utils.safestring import SafeUnicode, SafeData, EscapeData
from jinja2 import Markup, environmentfilter
    from django.template.defaultfilters import floatformat
from djinja.template.base import import_library
    import urllib
    def conversion_wrapper(*args, **kwargs):
        result = filter_func(*args, **kwargs)
        return _convert(result)
    if hasattr(filter_func, 'needs_autoescape'):
        @environmentfilter
        def autoescape_wrapper(environment, *args, **kwargs):
            kwargs['autoescape'] = environment.autoescape
            return conversion_wrapper(*args, **kwargs)
        return autoescape_wrapper
    else:
        return conversion_wrapper


@register.filter
def floatformat(value, arg=-1):
    """Builds on top of Django's own version, but adds strict error
    checking, staying with the philosophy.
    """
    arg = int(arg)  # raise exception
    result = django_filter_to_jinja2(floatformat)(value, arg)
    if result == '':  # django couldn't handle the value
        raise ValueError(value)
    return result

@register.filter
def jsencode (value):
    value = 'document.write("%s")' % value
    encoded = urllib.quote(unicode(value).encode('utf-8'))
    return '<script>eval(decodeURIComponent("%s"));</script>'% encoded


# Path: djinja/template/base.py
# class Library(object):
#     def __init__(self):
#         self.filters = {}
#         self.extensions = []
#         self.globals = {}
#         self.tests = {}
#     def tag(self, func,name=None):
#         if name==None:
#             name = getattr(func, "_decorated_function", func).__name__
#         self.globals[name] = func
# 
#     def filter(self, func,name=None):
#         if name==None:
#             name = getattr(func, "_decorated_function", func).__name__
#         self.filters[name] = func
# 
#     def extension(self, ext):
#         self.extensions.append(ext)
#     
#     def set(self,*args,**kwargs):
#         for k in kwargs.keys(): #is a object with a name
#             self[k] = kwargs[k]
#         for a in args:
#             self.tag(a) #is a function
#         
#     def inclusion_tag(self,template,func,takes_context=False):
#         if takes_context:
#             import jinja2
#             @jinja2.contextfunction
#             def tag(context,*args,**kwargs):
#                 from django.template.loader import render_to_string
#                 return render_to_string(template, func(dict_from_context(context),*args,**kwargs))
#             
#         else:
#             def tag(*args,**kwargs):
#                 from django.template.loader import render_to_string
#                 return render_to_string(template, func(*args,**kwargs))
#         
#         #raise Exception(getattr(func, "_decorated_function", func))
#         self.tag(tag,name=getattr(func, "_decorated_function", func).__name__) 
#     
#     def __setitem__(self, item, value):
#         self.globals[item] = value
#     
#     def __getitem__(self, item, value): #for reciprocity with __setitem__
#         return globals[item]
# 
# Path: djinja/template/base.py
# def import_library(taglib_module):
#     """Load a template tag library module.
# 
#     Verifies that the library contains a 'register' attribute, and
#     returns that attribute as the representation of the library
#     """
#     app_path, taglib = taglib_module.rsplit('.',1)
#     app_module = import_module(app_path)
#     try:
#         mod = import_module(taglib_module)
#     except ImportError, e:
#         # If the ImportError is because the taglib submodule does not exist, that's not
#         # an error that should be raised. If the submodule exists and raised an ImportError
#         # on the attempt to load it, that we want to raise.
#         if not module_has_submodule(app_module, taglib):
#             return None
#         else:
#             raise InvalidTemplateLibrary("ImportError raised loading %s: %s" % (taglib_module, e))
#     try:
#         return mod.register
#     except AttributeError:
#         raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % taglib_module)


# Path: djinja/template/defaultfilters.py
from djinja.template.base import Library
from jinja2.runtime import Undefined
from django.utils.safestring import mark_safe
from django.utils.encoding import force_unicode
from django.conf import settings
    from django.core.urlresolvers import reverse, NoReverseMatch
    from django.utils.timesince import timesince
    from django.conf import settings
    from django.utils.dateformat import format
    from django.conf import settings
    from django.utils.dateformat import time_format
    from django.utils.text import truncate_words
    from django.utils.text import truncate_html_words
from django.utils.safestring import SafeUnicode, SafeData, EscapeData
from jinja2 import Markup, environmentfilter
    from django.template.defaultfilters import floatformat
from djinja.template.base import import_library
    import urllib

    def conversion_wrapper(*args, **kwargs):
        result = filter_func(*args, **kwargs)
        return _convert(result)
    if hasattr(filter_func, 'needs_autoescape'):
        @environmentfilter
        def autoescape_wrapper(environment, *args, **kwargs):
            kwargs['autoescape'] = environment.autoescape
            return conversion_wrapper(*args, **kwargs)
        return autoescape_wrapper
    else:
        return conversion_wrapper


@register.filter
def floatformat(value, arg=-1):
    """Builds on top of Django's own version, but adds strict error
    checking, staying with the philosophy.
    """
    arg = int(arg)  # raise exception
    result = django_filter_to_jinja2(floatformat)(value, arg)
    if result == '':  # django couldn't handle the value
        raise ValueError(value)
    return result

@register.filter
def jsencode (value):
    value = 'document.write("%s")' % value
    encoded = urllib.quote(unicode(value).encode('utf-8'))
    return '<script>eval(decodeURIComponent("%s"));</script>'% encoded


django_filters = import_library('django.template.defaultfilters').filters