Skip to content

Commit

Permalink
0.8.1 (#54)
Browse files Browse the repository at this point in the history
* 🏗️ Enable automatic setup file creation with Poetry

* ⬆️ Upgrade dependent to the latest

* ✨ Add `*` modifier for variable block to keep intial indention for multiline strings for all modes

* 📝 Update doc for "indent modifier"

* 🔖 0.8.1
  • Loading branch information
pwwang authored Jun 4, 2023
1 parent d6d7518 commit 757b9bc
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 148 deletions.
9 changes: 9 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Change Log

## 0.8.1

- 📝 Fix badges in README.md
- 👷 Use latest actions in CI
- 🏗️ Enable automatic setup file creation with Poetry
- ⬆️ Upgrade dependencies to the latest
- ✨ Add `*` modifier for variable block to keep intial indention for multiline strings for all modes
- 📝 Update doc for "indent modifier"

## 0.8.0

- ⬆️ Upgrade deps including markdown, regex, and python-slugify
Expand Down
20 changes: 20 additions & 0 deletions docs/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,23 @@ You can do arbitrary things with the wild mode, like executing python code and a
See details on:

- https://pwwang.github.com/liquidpy/wild

## `*` modifier for `{{` to keep initial indention along multiple lines

```python
tpl = """\
if True:
{{* body }}
"""
body = """\
print('hello')
print('world')
"""
print(Liquid(tpl, from_file=False).render(body=body))
```

```
if True:
print('hello')
print('world')
```
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.8.0"
__version__ = "0.8.1"
22 changes: 20 additions & 2 deletions liquid/exts/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


re_e = re.escape
re_c = lambda rex: re.compile(rex, re.M | re.S)
re_c = lambda rex: re.compile(rex, re.DOTALL | re.MULTILINE)

# A unique id to encode the start strings
ENCODING_ID = id(Extension)
Expand All @@ -35,12 +35,30 @@ def preprocess( # type: ignore
start strings ('{{', '{#', '{%') so that the body won't be tokenized
by jinjia.
"""
# Turn
# " {{* ... }}" to
# " {{* ... | indent(2) }}"
# to keep the indent for multiline variables
variable_start_re = re_e(self.environment.variable_start_string)
variable_end_re = re_e(self.environment.variable_end_string)
indent_re = re_c(
fr"^([ \t]+){variable_start_re}\*"
"(.*?)"
fr"(\-{variable_end_re}|\+{variable_end_re}|{variable_end_re})"
)
source = indent_re.sub(
lambda m: (
f"{m.group(1)}{self.environment.variable_start_string}"
f"{m.group(2)} | indent({m.group(1)!r}){m.group(3)}"
),
source,
)

if not self.__class__.raw_tags: # pragma: no cover
return super().preprocess(source, name, filename=filename)

block_start_re = re_e(self.environment.block_start_string)
block_end_re = re_e(self.environment.block_end_string)
variable_start_re = re_e(self.environment.variable_start_string)
comment_start_re = re_e(self.environment.comment_start_string)
to_encode = re_c(
f"({block_start_re}|{variable_start_re}|{comment_start_re})"
Expand Down
277 changes: 133 additions & 144 deletions poetry.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ build-backend = "poetry.masonry.api"

[tool.poetry]
name = "liquidpy"
version = "0.8.0"
version = "0.8.1"
description = "A port of liquid template engine for python"
authors = [ "pwwang <[email protected]>",]
license = "MIT"
readme = "README.md"
homepage = "https://github.com/pwwang/liquidpy"
repository = "https://github.com/pwwang/liquidpy"

[tool.poetry.build]
generate-setup-file = true

[[tool.poetry.packages]]
include = "liquid"

Expand Down
15 changes: 15 additions & 0 deletions tests/standard/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,18 @@ def test_wscontrol(set_default_standard):
Liquid(tpl).render()
== "Wow, John G. Chalmers-Smith, you have a long name!"
)


def test_indention_keeping(set_default_standard):
tpl = """
1
{{* var }}
2
"""
out = Liquid(tpl).render({"var": "a\n b"})
assert out == """
1
a
b
2
"""

0 comments on commit 757b9bc

Please sign in to comment.