Skip to content

Commit

Permalink
support for custom jinja filters
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsca committed Jul 7, 2020
1 parent 8b337ca commit e849026
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion clay/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .version import __version__


m = Manager(f"<b>Clay v{__version__}")
m = Manager(f"<b>Clay v{__version__}", catch_errors=False)


@m.command(help="Creates a new Clay project at `dest`.")
Expand Down
18 changes: 16 additions & 2 deletions clay/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pathlib import Path

import hecto
import jinja2
import yaml

from .request import Request
Expand Down Expand Up @@ -47,6 +48,13 @@ def thumbnail(path, *args, **kwargs):
return "/" + path.lstrip("/")


@jinja2.contextfilter
def shuffle(context, value):
iter = value[:]
random.shuffle(iter)
return iter


JINJA_GLOBALS = {
"now": utcnow,
"dir": dir,
Expand All @@ -59,6 +67,10 @@ def thumbnail(path, *args, **kwargs):
"thumbnail": thumbnail,
}

JINJA_FILTERS = {
"shuffle": shuffle,
}

JINJA_EXTENSIONS = ("jinja2.ext.with_", IncludeWith)


Expand All @@ -82,7 +94,8 @@ def __init__(self, source_path, exclude=None, include=None):
self.render = JinjaRender(
self.source_path,
data=JINJA_GLOBALS.copy(),
extensions=self.jinja_extensions
filters_=JINJA_FILTERS,
extensions=self.jinja_extensions,
)

must_exclude = make_matcher(self.config["exclude"])
Expand Down Expand Up @@ -138,8 +151,9 @@ def build(self, build_folder="build", quiet=False):
"block_end_string": "%}",
"variable_start_string": "{{",
"variable_end_string": "}}",
'extensions': self.jinja_extensions,
"extensions": self.jinja_extensions,
},
jinja_filters=JINJA_FILTERS,
render_as=self._render_as,
get_context=self._get_context,
force=True,
Expand Down
4 changes: 2 additions & 2 deletions clay/utils/jinja_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@


class JinjaRender(object):
def __init__(self, src_path, data=None, extensions=None, envops=None):
def __init__(self, src_path, data=None, filters_=None, **envops):
self.src_path = str(src_path)
_envops = ENVOPS_DEFAULT.copy()
_envops.update(envops or {})
_envops.setdefault("loader", jinja2.FileSystemLoader(self.src_path))
_envops["extensions"] = extensions or []
self.env = SandboxedEnvironment(**_envops)
self.env.filters.update(filters_ or {})
self.env.globals.update(**(data or {}))

def __call__(self, fullpath, **data):
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = clay
version= 3.6
version= 3.7
author = Juan-Pablo Scaletti
author_email = juanpablo@jpscaletti.com
description = An amazing web prototyping tool.
Expand Down

0 comments on commit e849026

Please sign in to comment.