Skip to content

Commit

Permalink
πŸ”– 0.7.5 (#49)
Browse files Browse the repository at this point in the history
* ✨ Implement a playground powered by pyscript

* πŸ’š [docs] Trigger docs deploy in CI

* ✨ Add filter `call` for wild mode

* πŸ”– 0.7.5
  • Loading branch information
pwwang authored May 24, 2022
1 parent dafb358 commit 74ab6be
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Deploy docs
run: |
mkdocs gh-deploy --clean --force
if: success() && github.ref == 'refs/heads/master'
if: success() && (github.ref == 'refs/heads/master' || contains(github.event.head_commit.message, '[docs]'))

fix-index:
needs: docs
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ A port of [liquid][19] template engine for python, on the shoulder of [jinja2][1
pip install -U liquidpy
```

## Playground

Powered by [pyscript][21]:

[https://pwwang.github.io/liquidpy/playground][22]

## Baisic usage

### Loading a template
Expand Down Expand Up @@ -89,3 +95,5 @@ liq.render()
[18]: https://jekyllrb.com/docs/liquid/
[19]: https://shopify.github.io/liquid/
[20]: https://jinja.palletsprojects.com/
[21]: https://pyscript.net/
[22]: https://pwwang.github.io/liquidpy/playground
5 changes: 5 additions & 0 deletions docs/changelog.md β†’ docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.7.5

- ✨ Implement a playground powered by pyscript
- ✨ Add filter `call` for `wild` mode

# 0.7.4

- βœ… Add tests regarding #47
Expand Down
60 changes: 60 additions & 0 deletions docs/playground/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />

<script src="https://cdn.tailwindcss.com"></script>
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<py-env>
- liquidpy
</py-env>
<title>Liquidpy playground</title>
</head>
<body>
<main class="mx-auto w-4/5 pt-16">
<h1 class="text-2xl text-bold">Liquidpy playground</h1>
<p class="text-xs">
This is a playground for Liquidpy, powered by <a class="text-blue-600" target="_blank" href="https://pyscript.net">pyscript</a>.
</p>
<p class="text-xs"><py-script>
import liquid
print(f"Liquidpy version: {liquid.__version__}")
</py-script>
</p>
<p>&nbsp;</p>
<div>
<div class="w-1/3 inline-block mr-4">
<h2>Template</h2>
<textarea id="template" class="w-full h-96 p-2 text-sm font-mono border-2 border-slate-600"></textarea>
</div>
<div class="w-1/4 inline-block mr-4">
<h2>Variables</h2>
<textarea id="variables" class="w-full h-96 p-2 text-sm font-mono border-2 border-slate-600"># one assignment per line</textarea>
</div>
<div class="w-1/4 inline-block">
<h2>Filters</h2>
<textarea id="filters" class="w-full h-96 p-2 text-sm font-mono border-2 border-slate-600"></textarea>
</div>
<div class="my-4">
<select id="mode" class="bg-cyan-500 text-white text-sm py-2 px-4 rounded-lg">
<option value="default">default</option>
<option value="jekyll">jekyll</option>
<option value="shopify">shopify</option>
<option value="wild">wild</option>
</select>

<button id="render" pys-onClick="render" class="bg-lime-600 hover:bg-lime-700 text-white text-sm py-2 px-4 rounded-lg">Render</button>
<button id="load_example" pys-onClick="load_example" class="bg-slate-600 hover:bg-slate-700 text-white text-sm py-2 px-4 rounded-lg">Load example</button>
</div>
<div class="my-2">
<h2>Rendered:</h2>
<textarea id="rendered" readonly style="width: 82.8rem" class="text-sm h-72 p-2 font-mono border-2 border-slate-600"></textarea>
</div>
</div>
<py-script src="./liquid.py"></py-script>
</main>
</body>
</html>
66 changes: 66 additions & 0 deletions docs/playground/liquid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from liquid import Liquid

EXAMPLE_TEMPLATE = "{{ a | upper }}"
EXAMPLE_VARIABLES = "a = 'hello world!'"
EXAMPLE_FILTERS = """\
def upper(value):
return value.upper()
"""

TEMPLATE_CONTAINER = Element("template")
VARIABLES_CONTAINER = Element("variables")
FILTERS_CONTAINER = Element("filters")
MODE_CONTAINER = Element("mode")
RENDERED_CONTAINER = Element("rendered")

def _remove_class(element, class_name):
element.element.classList.remove(class_name)


def _add_class(element, class_name):
element.element.classList.add(class_name)


def _error(message):
"""
Displays an error message.
"""
_add_class(RENDERED_CONTAINER, "bg-red-100")
RENDERED_CONTAINER.element.value = message


def load_example(*args, **kwargs):
"""
Loads the example template, variables and filters.
"""
TEMPLATE_CONTAINER.element.value = EXAMPLE_TEMPLATE
VARIABLES_CONTAINER.element.value = EXAMPLE_VARIABLES
FILTERS_CONTAINER.element.value = EXAMPLE_FILTERS


def render(*args, **kwargs):
"""
Renders the template with the variables and filters.
"""
template = TEMPLATE_CONTAINER.element.value
variables = {}
try:
exec(VARIABLES_CONTAINER.element.value, variables)
except Exception as e:
_error(f"Something wrong when evaluating variables: \n{e}")
return

filters = {}
try:
exec(FILTERS_CONTAINER.element.value, filters)
except Exception as e:
_error(f"Something wrong when evaluating filters: \n{e}")
return

mode = MODE_CONTAINER.element.value
_remove_class(RENDERED_CONTAINER, "bg-red-100")
try:
liq = Liquid(template, from_file=False, mode=mode, filters=filters)
RENDERED_CONTAINER.element.value = liq.render(**variables)
except Exception as e:
_error(f"Something wrong when rendering: \n{e}")
4 changes: 2 additions & 2 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# use_directory_urls doesn't work for newer versions
mkdocs==1.1.2
mkdocs-material==7.2.2
mkdocs
mkdocs-material
pymdown-extensions
mkapi-fix
2 changes: 1 addition & 1 deletion liquid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

patch_jinja()

__version__ = "0.7.4"
__version__ = "0.7.5"
21 changes: 20 additions & 1 deletion liquid/filters/wild.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
except ImportError:
from jinja2 import environmentfilter as pass_environment

from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Callable
from .manager import FilterManager

if TYPE_CHECKING:
Expand Down Expand Up @@ -67,3 +67,22 @@ def compile_out(func: Any, args: Any) -> Any:
if test_out:
return compile_out(true, true_args)
return compile_out(false, false_args)


@wild_filter_manager.register
def call(fn: Callable, *args, **kwargs) -> Any:
"""Call a function with passed arguments
Examples:
>>> {{ int | call: "1" | plus: 1 }}
>>> # 2
Args:
fn: The callable
*args: and
**kwargs: The arguments for the callable
Returns:
The result of calling the function
"""
return fn(*args, **kwargs)
21 changes: 18 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.masonry.api"

[tool.poetry]
name = "liquidpy"
version = "0.7.4"
version = "0.7.5"
description = "A port of liquid template engine for python"
authors = [ "pwwang <pwwang@pwwang.com>",]
license = "MIT"
Expand Down Expand Up @@ -39,7 +39,7 @@ target-version = ['py37', 'py38', 'py39']
include = '\.pyi?$'

[tool.pytest.ini_options]
addopts = "-vv --cov-config=.coveragerc --cov=liquid --cov-report xml:cov.xml --cov-report term-missing"
addopts = "-vv -p no:asyncio --cov-config=.coveragerc --cov=liquid --cov-report xml:cov.xml --cov-report term-missing"
console_output_style = "progress"
junit_family = "xunit1"

Expand Down
21 changes: 21 additions & 0 deletions tests/wild/test_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from liquid import Liquid
import pytest # noqa


def test_ifelse(set_default_wild):
tpl = """{{ a | ifelse: isinstance, (int, ),
"plus", (1, ),
"append", (".html", ) }}"""

out = Liquid(tpl).render(a=1)
assert out == "2"

out = Liquid(tpl).render(a="a")
assert out == "a.html"


def test_call(set_default_wild):
tpl = """{{ int | call: "1" | plus: 1 }}"""

out = Liquid(tpl).render()
assert out == "2"

0 comments on commit 74ab6be

Please sign in to comment.