Esempio n. 1
0
 def tests(self, env, context, builtins=True):
     """List the tests."""
     from inspect import getdoc
     strip = set()
     if not builtins:
         from jinja.defaults import DEFAULT_TESTS
         strip = set(DEFAULT_TESTS.values())
     tests = env.tests.items()
     tests.sort(lambda a, b: cmp(a[0].lower(), b[0].lower()))
     result = []
     for name, f in tests:
         if f in strip:
             continue
         doc = '\n'.join(['    ' + x for x in (getdoc(f) or '').splitlines()])
         result.append('`%s`\n\n%s' % (name, doc))
     return '\n\n'.join(result)
Esempio n. 2
0
 def tests(self, env, context, builtins=True):
     """List the tests."""
     from inspect import getdoc
     strip = set()
     if not builtins:
         from jinja.defaults import DEFAULT_TESTS
         strip = set(DEFAULT_TESTS.values())
     tests = env.tests.items()
     tests.sort(lambda a, b: cmp(a[0].lower(), b[0].lower()))
     result = []
     for name, f in tests:
         if f in strip:
             continue
         doc = '\n'.join(
             ['    ' + x for x in (getdoc(f) or '').splitlines()])
         result.append('`%s`\n\n%s' % (name, doc))
     return '\n\n'.join(result)
    def __init__(
        self,
        block_start_string="{%",
        block_end_string="%}",
        variable_start_string="{{",
        variable_end_string="}}",
        comment_start_string="{#",
        comment_end_string="#}",
        trim_blocks=False,
        auto_escape=False,
        default_filters=None,
        template_charset="utf-8",
        charset="utf-8",
        namespace=None,
        loader=None,
        filters=None,
        tests=None,
        context_class=Context,
        undefined_singleton=SilentUndefined,
        disable_regexps=False,
        friendly_traceback=True,
        translator_factory=None,
        template_translator=PythonTranslator,
    ):
        """
        Here the possible initialization parameters:

        ========================= ============================================
        `block_start_string` *    the string marking the begin of a block.
                                  this defaults to ``'{%'``.
        `block_end_string` *      the string marking the end of a block.
                                  defaults to ``'%}'``.
        `variable_start_string` * the string marking the begin of a print
                                  statement. defaults to ``'{{'``.
        `comment_start_string` *  the string marking the begin of a
                                  comment. defaults to ``'{#'``.
        `comment_end_string` *    the string marking the end of a comment.
                                  defaults to ``'#}'``.
        `trim_blocks` *           If this is set to ``True`` the first newline
                                  after a block is removed (block, not
                                  variable tag!). Defaults to ``False``.
        `auto_escape`             If this is set to ``True`` Jinja will
                                  automatically escape all variables using xml
                                  escaping methods. If you don't want to
                                  escape a string you have to wrap it in a
                                  ``Markup`` object from the
                                  ``jinja.datastructure`` module. If
                                  `auto_escape` is ``True`` there will be also
                                  a ``Markup`` object in the template
                                  namespace to define partial html fragments.
                                  Note that we do not recommend this feature.
        `default_filters`         list of tuples in the form (``filter_name``,
                                  ``arguments``) where ``filter_name`` is the
                                  name of a registered filter and
                                  ``arguments`` a tuple with the filter
                                  arguments. The filters specified here will
                                  always be applied when printing data to the
                                  template. *new in Jinja 1.1*
        `template_charset`        The charset of the templates. Defaults
                                  to ``'utf-8'``.
        `charset`                 Charset of all string input data. Defaults
                                  to ``'utf-8'``.
        `namespace`               Global namespace for all templates.
        `loader`                  Specify a template loader.
        `filters`                 dict of filters or the default filters if
                                  not defined.
        `tests`                   dict of tests of the default tests if not
                                  defined.
        `context_class`           the context class this template should use.
                                  See the `Context` documentation for more
                                  details.
        `undefined_singleton`     The singleton value that is used for missing
                                  variables. *new in Jinja 1.1*
        `disable_regexps`         Disable support for regular expresssions.
        `friendly_traceback`      Set this to `False` to disable the developer
                                  friendly traceback rewriting. Whenever an
                                  runtime or syntax error occours jinja will
                                  try to make a developer friendly traceback
                                  that shows the error in the template line.
                                  This however can be annoying when debugging
                                  broken functions that are called from the
                                  template. *new in Jinja 1.1*
        `translator_factory`      A callback function that is called with
                                  the context as first argument to get the
                                  translator for the current instance.
                                  *new in Jinja 1.2*
        `template_translator`     An class that defines a static method called
                                  process which can be used to process the 
                                  template's AST into a compiled python module.
                                  *new in Jinja 1.2*
        ========================= ============================================

        All of these variables except those marked with a star (*) are
        modifiable after environment initialization.
        """

        # lexer / parser information
        self.block_start_string = block_start_string
        self.block_end_string = block_end_string
        self.variable_start_string = variable_start_string
        self.variable_end_string = variable_end_string
        self.comment_start_string = comment_start_string
        self.comment_end_string = comment_end_string
        self.trim_blocks = trim_blocks

        # other stuff
        self.template_charset = template_charset
        self.charset = charset
        self.loader = loader
        if filters is None:
            filters = DEFAULT_FILTERS.copy()
        self.filters = filters
        if tests is None:
            tests = DEFAULT_TESTS.copy()
        self.tests = tests
        self.default_filters = default_filters or []
        self.context_class = context_class
        self.undefined_singleton = undefined_singleton
        self.disable_regexps = disable_regexps
        self.friendly_traceback = friendly_traceback

        # global namespace
        if namespace is None:
            namespace = DEFAULT_NAMESPACE.copy()
        self.globals = namespace

        # jinja 1.0 compatibility
        if auto_escape:
            self.default_filters.append(("escape", (True,)))
            self.globals["Markup"] = Markup

        # and here the translator factory
        self.translator_factory = translator_factory

        # and here the AST translator
        self.template_translator = template_translator

        # create lexer
        self.lexer = Lexer(self)
    def __init__(self,
                 block_start_string='{%',
                 block_end_string='%}',
                 variable_start_string='{{',
                 variable_end_string='}}',
                 comment_start_string='{#',
                 comment_end_string='#}',
                 trim_blocks=False,
                 auto_escape=False,
                 default_filters=None,
                 template_charset='utf-8',
                 charset='utf-8',
                 namespace=None,
                 loader=None,
                 filters=None,
                 tests=None,
                 context_class=Context,
                 undefined_singleton=SilentUndefined,
                 disable_regexps=False,
                 friendly_traceback=True,
                 translator_factory=None):
        """
        Here the possible initialization parameters:

        ========================= ============================================
        `block_start_string` *    the string marking the begin of a block.
                                  this defaults to ``'{%'``.
        `block_end_string` *      the string marking the end of a block.
                                  defaults to ``'%}'``.
        `variable_start_string` * the string marking the begin of a print
                                  statement. defaults to ``'{{'``.
        `comment_start_string` *  the string marking the begin of a
                                  comment. defaults to ``'{#'``.
        `comment_end_string` *    the string marking the end of a comment.
                                  defaults to ``'#}'``.
        `trim_blocks` *           If this is set to ``True`` the first newline
                                  after a block is removed (block, not
                                  variable tag!). Defaults to ``False``.
        `auto_escape`             If this is set to ``True`` Jinja will
                                  automatically escape all variables using xml
                                  escaping methods. If you don't want to
                                  escape a string you have to wrap it in a
                                  ``Markup`` object from the
                                  ``jinja.datastructure`` module. If
                                  `auto_escape` is ``True`` there will be also
                                  a ``Markup`` object in the template
                                  namespace to define partial html fragments.
                                  Note that we do not recommend this feature.
        `default_filters`         list of tuples in the form (``filter_name``,
                                  ``arguments``) where ``filter_name`` is the
                                  name of a registered filter and
                                  ``arguments`` a tuple with the filter
                                  arguments. The filters specified here will
                                  always be applied when printing data to the
                                  template. *new in Jinja 1.1*
        `template_charset`        The charset of the templates. Defaults
                                  to ``'utf-8'``.
        `charset`                 Charset of all string input data. Defaults
                                  to ``'utf-8'``.
        `namespace`               Global namespace for all templates.
        `loader`                  Specify a template loader.
        `filters`                 dict of filters or the default filters if
                                  not defined.
        `tests`                   dict of tests of the default tests if not
                                  defined.
        `context_class`           the context class this template should use.
                                  See the `Context` documentation for more
                                  details.
        `undefined_singleton`     The singleton value that is used for missing
                                  variables. *new in Jinja 1.1*
        `disable_regexps`         Disable support for regular expresssions.
        `friendly_traceback`      Set this to `False` to disable the developer
                                  friendly traceback rewriting. Whenever an
                                  runtime or syntax error occours jinja will
                                  try to make a developer friendly traceback
                                  that shows the error in the template line.
                                  This however can be annoying when debugging
                                  broken functions that are called from the
                                  template. *new in Jinja 1.1*
        `translator_factory`      A callback function that is called with
                                  the context as first argument to get the
                                  translator for the current instance.
                                  *new in Jinja 1.2*
        ========================= ============================================

        All of these variables except those marked with a star (*) are
        modifiable after environment initialization.
        """

        # lexer / parser information
        self.block_start_string = block_start_string
        self.block_end_string = block_end_string
        self.variable_start_string = variable_start_string
        self.variable_end_string = variable_end_string
        self.comment_start_string = comment_start_string
        self.comment_end_string = comment_end_string
        self.trim_blocks = trim_blocks

        # other stuff
        self.template_charset = template_charset
        self.charset = charset
        self.loader = loader
        if filters is None:
            filters = DEFAULT_FILTERS.copy()
        self.filters = filters
        if tests is None:
            tests = DEFAULT_TESTS.copy()
        self.tests = tests
        self.default_filters = default_filters or []
        self.context_class = context_class
        self.undefined_singleton = undefined_singleton
        self.disable_regexps = disable_regexps
        self.friendly_traceback = friendly_traceback

        # global namespace
        if namespace is None:
            namespace = DEFAULT_NAMESPACE.copy()
        self.globals = namespace

        # jinja 1.0 compatibility
        if auto_escape:
            self.default_filters.append(('escape', (True, )))
            self.globals['Markup'] = Markup

        # and here the translator factory
        self.translator_factory = translator_factory

        # create lexer
        self.lexer = Lexer(self)