From 74ab6be554f2d5a8ced4305280a9d3b26bef0185 Mon Sep 17 00:00:00 2001 From: pwwang <1188067+pwwang@users.noreply.github.com> Date: Mon, 23 May 2022 23:04:16 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=96=200.7.5=20(#49)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ✨ Implement a playground powered by pyscript * 💚 [docs] Trigger docs deploy in CI * ✨ Add filter `call` for wild mode * 🔖 0.7.5 --- .github/workflows/docs.yml | 2 +- README.md | 8 ++++ docs/{changelog.md => CHANGELOG.md} | 5 +++ docs/playground/index.html | 60 ++++++++++++++++++++++++++ docs/playground/liquid.py | 66 +++++++++++++++++++++++++++++ docs/requirements.txt | 4 +- liquid/__init__.py | 2 +- liquid/filters/wild.py | 21 ++++++++- poetry.lock | 21 +++++++-- pyproject.toml | 4 +- tests/wild/test_filters.py | 21 +++++++++ 11 files changed, 204 insertions(+), 10 deletions(-) rename docs/{changelog.md => CHANGELOG.md} (97%) create mode 100644 docs/playground/index.html create mode 100644 docs/playground/liquid.py create mode 100644 tests/wild/test_filters.py diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index ae839d9..04821a5 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -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 diff --git a/README.md b/README.md index 72ab73a..98a3cdb 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/docs/changelog.md b/docs/CHANGELOG.md similarity index 97% rename from docs/changelog.md rename to docs/CHANGELOG.md index ec08831..bd0fc9e 100644 --- a/docs/changelog.md +++ b/docs/CHANGELOG.md @@ -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 diff --git a/docs/playground/index.html b/docs/playground/index.html new file mode 100644 index 0000000..64b56ae --- /dev/null +++ b/docs/playground/index.html @@ -0,0 +1,60 @@ + + + + + + + + + + + + - liquidpy + + Liquidpy playground + + +
+

Liquidpy playground

+

+ This is a playground for Liquidpy, powered by pyscript. +

+

+ import liquid + print(f"Liquidpy version: {liquid.__version__}") + +

+

 

+
+
+

Template

+ +
+
+

Variables

+ +
+
+

Filters

+ +
+
+ + + + +
+
+

Rendered:

+ +
+
+ +
+ + diff --git a/docs/playground/liquid.py b/docs/playground/liquid.py new file mode 100644 index 0000000..f6fd5cc --- /dev/null +++ b/docs/playground/liquid.py @@ -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}") diff --git a/docs/requirements.txt b/docs/requirements.txt index 02fdddd..680698e 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -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 diff --git a/liquid/__init__.py b/liquid/__init__.py index 1ffeb96..5043cc6 100644 --- a/liquid/__init__.py +++ b/liquid/__init__.py @@ -4,4 +4,4 @@ patch_jinja() -__version__ = "0.7.4" +__version__ = "0.7.5" diff --git a/liquid/filters/wild.py b/liquid/filters/wild.py index 0fc9c38..a52471a 100644 --- a/liquid/filters/wild.py +++ b/liquid/filters/wild.py @@ -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: @@ -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) diff --git a/poetry.lock b/poetry.lock index f2d7319..4f1ae7a 100644 --- a/poetry.lock +++ b/poetry.lock @@ -83,7 +83,7 @@ i18n = ["Babel (>=2.7)"] [[package]] name = "markdown" -version = "3.3.6" +version = "3.3.7" description = "Python implementation of Markdown." category = "main" optional = true @@ -376,8 +376,8 @@ jinja2 = [ {file = "Jinja2-3.0.3.tar.gz", hash = "sha256:611bb273cd68f3b993fabdc4064fc858c5b47a973cb5aa7999ec1ba405c87cd7"}, ] markdown = [ - {file = "Markdown-3.3.6-py3-none-any.whl", hash = "sha256:9923332318f843411e9932237530df53162e29dc7a4e2b91e35764583c46c9a3"}, - {file = "Markdown-3.3.6.tar.gz", hash = "sha256:76df8ae32294ec39dcf89340382882dfa12975f87f45c3ed1ecdb1e8cefc7006"}, + {file = "Markdown-3.3.7-py3-none-any.whl", hash = "sha256:f5da449a6e1c989a4cea2631aa8ee67caa5a2ef855d551c88f9e309f4634c621"}, + {file = "Markdown-3.3.7.tar.gz", hash = "sha256:cbb516f16218e643d8e0a95b309f77eb118cb138d39a4f27851e6a63581db874"}, ] markupsafe = [ {file = "MarkupSafe-2.0.1-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:d8446c54dc28c01e5a2dbac5a25f071f6653e6e40f3a8818e8b45d790fe6ef53"}, @@ -385,6 +385,9 @@ markupsafe = [ {file = "MarkupSafe-2.0.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2d7d807855b419fc2ed3e631034685db6079889a1f01d5d9dac950f764da3dad"}, {file = "MarkupSafe-2.0.1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:add36cb2dbb8b736611303cd3bfcee00afd96471b09cda130da3581cbdc56a6d"}, {file = "MarkupSafe-2.0.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:168cd0a3642de83558a5153c8bd34f175a9a6e7f6dc6384b9655d2697312a646"}, + {file = "MarkupSafe-2.0.1-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:4dc8f9fb58f7364b63fd9f85013b780ef83c11857ae79f2feda41e270468dd9b"}, + {file = "MarkupSafe-2.0.1-cp310-cp310-musllinux_1_1_i686.whl", hash = "sha256:20dca64a3ef2d6e4d5d615a3fd418ad3bde77a47ec8a23d984a12b5b4c74491a"}, + {file = "MarkupSafe-2.0.1-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:cdfba22ea2f0029c9261a4bd07e830a8da012291fbe44dc794e488b6c9bb353a"}, {file = "MarkupSafe-2.0.1-cp310-cp310-win32.whl", hash = "sha256:99df47edb6bda1249d3e80fdabb1dab8c08ef3975f69aed437cb69d0a5de1e28"}, {file = "MarkupSafe-2.0.1-cp310-cp310-win_amd64.whl", hash = "sha256:e0f138900af21926a02425cf736db95be9f4af72ba1bb21453432a07f6082134"}, {file = "MarkupSafe-2.0.1-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:f9081981fe268bd86831e5c75f7de206ef275defcb82bc70740ae6dc507aee51"}, @@ -396,6 +399,9 @@ markupsafe = [ {file = "MarkupSafe-2.0.1-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:bf5d821ffabf0ef3533c39c518f3357b171a1651c1ff6827325e4489b0e46c3c"}, {file = "MarkupSafe-2.0.1-cp36-cp36m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:0d4b31cc67ab36e3392bbf3862cfbadac3db12bdd8b02a2731f509ed5b829724"}, {file = "MarkupSafe-2.0.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:baa1a4e8f868845af802979fcdbf0bb11f94f1cb7ced4c4b8a351bb60d108145"}, + {file = "MarkupSafe-2.0.1-cp36-cp36m-musllinux_1_1_aarch64.whl", hash = "sha256:deb993cacb280823246a026e3b2d81c493c53de6acfd5e6bfe31ab3402bb37dd"}, + {file = "MarkupSafe-2.0.1-cp36-cp36m-musllinux_1_1_i686.whl", hash = "sha256:63f3268ba69ace99cab4e3e3b5840b03340efed0948ab8f78d2fd87ee5442a4f"}, + {file = "MarkupSafe-2.0.1-cp36-cp36m-musllinux_1_1_x86_64.whl", hash = "sha256:8d206346619592c6200148b01a2142798c989edcb9c896f9ac9722a99d4e77e6"}, {file = "MarkupSafe-2.0.1-cp36-cp36m-win32.whl", hash = "sha256:6c4ca60fa24e85fe25b912b01e62cb969d69a23a5d5867682dd3e80b5b02581d"}, {file = "MarkupSafe-2.0.1-cp36-cp36m-win_amd64.whl", hash = "sha256:b2f4bf27480f5e5e8ce285a8c8fd176c0b03e93dcc6646477d4630e83440c6a9"}, {file = "MarkupSafe-2.0.1-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:0717a7390a68be14b8c793ba258e075c6f4ca819f15edfc2a3a027c823718567"}, @@ -407,6 +413,9 @@ markupsafe = [ {file = "MarkupSafe-2.0.1-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e9936f0b261d4df76ad22f8fee3ae83b60d7c3e871292cd42f40b81b70afae85"}, {file = "MarkupSafe-2.0.1-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:2a7d351cbd8cfeb19ca00de495e224dea7e7d919659c2841bbb7f420ad03e2d6"}, {file = "MarkupSafe-2.0.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:60bf42e36abfaf9aff1f50f52644b336d4f0a3fd6d8a60ca0d054ac9f713a864"}, + {file = "MarkupSafe-2.0.1-cp37-cp37m-musllinux_1_1_aarch64.whl", hash = "sha256:d6c7ebd4e944c85e2c3421e612a7057a2f48d478d79e61800d81468a8d842207"}, + {file = "MarkupSafe-2.0.1-cp37-cp37m-musllinux_1_1_i686.whl", hash = "sha256:f0567c4dc99f264f49fe27da5f735f414c4e7e7dd850cfd8e69f0862d7c74ea9"}, + {file = "MarkupSafe-2.0.1-cp37-cp37m-musllinux_1_1_x86_64.whl", hash = "sha256:89c687013cb1cd489a0f0ac24febe8c7a666e6e221b783e53ac50ebf68e45d86"}, {file = "MarkupSafe-2.0.1-cp37-cp37m-win32.whl", hash = "sha256:a30e67a65b53ea0a5e62fe23682cfe22712e01f453b95233b25502f7c61cb415"}, {file = "MarkupSafe-2.0.1-cp37-cp37m-win_amd64.whl", hash = "sha256:611d1ad9a4288cf3e3c16014564df047fe08410e628f89805e475368bd304914"}, {file = "MarkupSafe-2.0.1-cp38-cp38-macosx_10_9_universal2.whl", hash = "sha256:5bb28c636d87e840583ee3adeb78172efc47c8b26127267f54a9c0ec251d41a9"}, @@ -419,6 +428,9 @@ markupsafe = [ {file = "MarkupSafe-2.0.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6fcf051089389abe060c9cd7caa212c707e58153afa2c649f00346ce6d260f1b"}, {file = "MarkupSafe-2.0.1-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:5855f8438a7d1d458206a2466bf82b0f104a3724bf96a1c781ab731e4201731a"}, {file = "MarkupSafe-2.0.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:3dd007d54ee88b46be476e293f48c85048603f5f516008bee124ddd891398ed6"}, + {file = "MarkupSafe-2.0.1-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:aca6377c0cb8a8253e493c6b451565ac77e98c2951c45f913e0b52facdcff83f"}, + {file = "MarkupSafe-2.0.1-cp38-cp38-musllinux_1_1_i686.whl", hash = "sha256:04635854b943835a6ea959e948d19dcd311762c5c0c6e1f0e16ee57022669194"}, + {file = "MarkupSafe-2.0.1-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:6300b8454aa6930a24b9618fbb54b5a68135092bc666f7b06901f897fa5c2fee"}, {file = "MarkupSafe-2.0.1-cp38-cp38-win32.whl", hash = "sha256:023cb26ec21ece8dc3907c0e8320058b2e0cb3c55cf9564da612bc325bed5e64"}, {file = "MarkupSafe-2.0.1-cp38-cp38-win_amd64.whl", hash = "sha256:984d76483eb32f1bcb536dc27e4ad56bba4baa70be32fa87152832cdd9db0833"}, {file = "MarkupSafe-2.0.1-cp39-cp39-macosx_10_9_universal2.whl", hash = "sha256:2ef54abee730b502252bcdf31b10dacb0a416229b72c18b19e24a4509f273d26"}, @@ -431,6 +443,9 @@ markupsafe = [ {file = "MarkupSafe-2.0.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:c47adbc92fc1bb2b3274c4b3a43ae0e4573d9fbff4f54cd484555edbf030baf1"}, {file = "MarkupSafe-2.0.1-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:37205cac2a79194e3750b0af2a5720d95f786a55ce7df90c3af697bfa100eaac"}, {file = "MarkupSafe-2.0.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:1f2ade76b9903f39aa442b4aadd2177decb66525062db244b35d71d0ee8599b6"}, + {file = "MarkupSafe-2.0.1-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:4296f2b1ce8c86a6aea78613c34bb1a672ea0e3de9c6ba08a960efe0b0a09047"}, + {file = "MarkupSafe-2.0.1-cp39-cp39-musllinux_1_1_i686.whl", hash = "sha256:9f02365d4e99430a12647f09b6cc8bab61a6564363f313126f775eb4f6ef798e"}, + {file = "MarkupSafe-2.0.1-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:5b6d930f030f8ed98e3e6c98ffa0652bdb82601e7a016ec2ab5d7ff23baa78d1"}, {file = "MarkupSafe-2.0.1-cp39-cp39-win32.whl", hash = "sha256:10f82115e21dc0dfec9ab5c0223652f7197feb168c940f3ef61563fc2d6beb74"}, {file = "MarkupSafe-2.0.1-cp39-cp39-win_amd64.whl", hash = "sha256:693ce3f9e70a6cf7d2fb9e6c9d8b204b6b39897a2c4a1aa65728d5ac97dcc1d8"}, {file = "MarkupSafe-2.0.1.tar.gz", hash = "sha256:594c67807fb16238b30c44bdf74f36c02cdf22d1c8cda91ef8a0ed8dabf5620a"}, diff --git a/pyproject.toml b/pyproject.toml index 09b822a..17a095e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 ",] license = "MIT" @@ -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" diff --git a/tests/wild/test_filters.py b/tests/wild/test_filters.py new file mode 100644 index 0000000..aa89378 --- /dev/null +++ b/tests/wild/test_filters.py @@ -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"