Ejemplo n.º 1
0
def do_tojson(eval_ctx, value, indent=None):
    """Dumps a structure to JSON so that it's safe to use in ``<script>``
    tags.  It accepts the same arguments and returns a JSON string.  Note that
    this is available in template through the ``|tojson`` filter which will
    also mark the result as safe.  Due to how this function escapes certain
    characters this is safe even if used outside of ``<script>`` tags.

    The following characters are escaped in strings:

    -   ``<``
    -   ``>``
    -   ``&``
    -   ``'``

    This makes it safe to embed such strings in any place in HTML with the
    notable exception of double quoted attributes.  In that case single
    quote your attributes or HTML escape it in addition.

    The indent parameter can be used to enable pretty printing.  Set it to
    the number of spaces that the structures should be indented with.

    Note that this filter is for use in HTML contexts only.

    .. versionadded:: 2.9
    """
    policies = eval_ctx.environment.policies
    dumper = policies['json.dumps_function']
    options = policies['json.dumps_kwargs']
    if indent is not None:
        options = dict(options)
        options['indent'] = indent
    return htmlsafe_json_dumps(value, dumper=dumper, **options)
Ejemplo n.º 2
0
def do_tojson(eval_ctx, value, indent=None):
    """Dumps a structure to JSON so that it's safe to use in ``<script>``
    tags.  It accepts the same arguments and returns a JSON string.  Note that
    this is available in templates through the ``|tojson`` filter which will
    also mark the result as safe.  Due to how this function escapes certain
    characters this is safe even if used outside of ``<script>`` tags.

    The following characters are escaped in strings:

    -   ``<``
    -   ``>``
    -   ``&``
    -   ``'``

    This makes it safe to embed such strings in any place in HTML with the
    notable exception of double quoted attributes.  In that case single
    quote your attributes or HTML escape it in addition.

    The indent parameter can be used to enable pretty printing.  Set it to
    the number of spaces that the structures should be indented with.

    Note that this filter is for use in HTML contexts only.

    .. versionadded:: 2.9
    """
    policies = eval_ctx.environment.policies
    dumper = policies['json.dumps_function']
    options = policies['json.dumps_kwargs']
    if indent is not None:
        options = dict(options)
        options['indent'] = indent
    return htmlsafe_json_dumps(value, dumper=dumper, **options)
Ejemplo n.º 3
0
    def test_builtin_tile(self):
        """Test custom maptiles."""

        default_tiles = [
            "OpenStreetMap",
            "Stamen Terrain",
            "Stamen Toner",
            "Mapbox Bright",
            "Mapbox Control Room",
            "CartoDB positron",
            "CartoDB dark_matter",
        ]
        for tiles in default_tiles:
            m = folium.Map(location=[45.5236, -122.6750], tiles=tiles)
            tiles = "".join(tiles.lower().strip().split())
            url = "tiles/{}/tiles.txt".format
            attr = "tiles/{}/attr.txt".format
            url = m._env.get_template(url(tiles)).render()
            attr = m._env.get_template(attr(tiles)).render()

            assert m._children[tiles].tiles == url
            assert htmlsafe_json_dumps(attr) in m._parent.render()

        bounds = m.get_bounds()
        assert bounds == [[None, None], [None, None]], bounds
Ejemplo n.º 4
0
    def test_builtin_tile(self):
        """Test custom maptiles."""

        default_tiles = [
            "OpenStreetMap",
            "Stamen Terrain",
            "Stamen Toner",
            "Mapbox Bright",
            "Mapbox Control Room",
            "CartoDB positron",
            "CartoDB dark_matter",
        ]
        for tiles in default_tiles:
            m = folium.Map(location=[45.5236, -122.6750], tiles=tiles)
            tiles = "".join(tiles.lower().strip().split())
            url = "tiles/{}/tiles.txt".format
            attr = "tiles/{}/attr.txt".format
            url = m._env.get_template(url(tiles)).render()
            attr = m._env.get_template(attr(tiles)).render()

            assert m._children[tiles].tiles == url
            assert htmlsafe_json_dumps(attr) in m._parent.render()

        bounds = m.get_bounds()
        assert bounds == [[None, None], [None, None]], bounds
Ejemplo n.º 5
0
def _filter_tojson(context: Context,
                   data: Any,
                   indent: Optional[int] = None) -> str:
    """
    Converts a value to a JSON string safe for use in an HTML document.

    This mimics Jinja2's built-in JSON filter, but uses Betty's own JSON encoder.
    """
    return htmlsafe_json_dumps(
        data,
        indent=indent,
        dumper=lambda *args, **kwargs: _filter_json(context, *args, **kwargs))
Ejemplo n.º 6
0
 def _filter_tojson(context, data, indent=None):
     return htmlsafe_json_dumps(data,
                                indent=indent,
                                dumper=lambda *args, **kwargs:
                                _filter_json(context, *args, **kwargs))