Skip to content

Commit 4d05127

Browse files
authored
Merge pull request #176 from jg-rp/mkdocs
Bundle i18n and i10n tags and filters and update docs
2 parents b85c2d2 + 17edc78 commit 4d05127

File tree

153 files changed

+9220
-531
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

153 files changed

+9220
-531
lines changed

CHANGES.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@
22

33
## Version 2.0.0 (unreleased)
44

5-
This major release:
5+
This is a major release with several breaking changes. As well as API changes listed below, we:
66

7-
- Drops support for Python version 3.7 and 3.8.
8-
- Introduces API changes [discussed here](https://github.com/jg-rp/liquid/discussions/137).
7+
- Drop support for Python version 3.7 and 3.8.
98
- Promotes rendering behavior from `liquid.future.Environment` to be the default, so as to improve Shopify/liquid compatibility by default.
10-
- Fixes variable/identifier/path parsing described in [issue #39](https://github.com/jg-rp/liquid/issues/39).
11-
- Improves Liquid syntax error messages and exposes source index, line numbers and column numbers through methods on Liquid exceptions. See [#53](https://github.com/jg-rp/liquid/issues/53).
12-
- Changes comment tag parsing to better match Shopify/Liquid. See [#133](https://github.com/jg-rp/liquid/issues/133).
13-
- Removes `BoundTemplate.analyze_with_context()`. Shout if you need contextual analysis and we'll restore this feature.
14-
- Removes the `cache_size` argument to `liquid.Environment` and `liquid.Template`. Template caching is now handled by template loaders.
15-
- Removes the `expression_cache_size` argument to `liquid.Environment` and `liquid.Template`. Environment-level expression caching is no longer available as it does not play nicely with detailed error messages. If you need to cache parsing of Liquid expressions, it is now recommended to implement a cache per tag, where it makes sense to do so for your use case.
16-
- Makes markupsafe>=3 a dependency. Previously markupsafe was an optional dependency. Version 3 of markupsafe brings some subtle changes to the `replace`, `replace_first` and `replace_last` filters when they receive a "safe" string wrapped in `Markup()`.
17-
- Adds new filters `reject`, `has`, `find` and `find_index`.
18-
- Adds the new `doc` tag.
9+
- Fixe variable/identifier/path parsing described in [issue #39](https://github.com/jg-rp/liquid/issues/39).
10+
- Improve Liquid syntax error messages and exposes source index, line numbers and column numbers through methods on Liquid exceptions. See [#53](https://github.com/jg-rp/liquid/issues/53).
11+
- Change comment tag parsing to better match Shopify/Liquid. See [#133](https://github.com/jg-rp/liquid/issues/133).
12+
- Remove `BoundTemplate.analyze_with_context()`. Shout if you need contextual analysis and we'll restore this feature.
13+
- Remove the `cache_size` argument to `liquid.Environment` and `liquid.Template`. Template caching is now handled by template loaders.
14+
- Remove the `expression_cache_size` argument to `liquid.Environment` and `liquid.Template`. Environment-level expression caching is no longer available as it does not play nicely with detailed error messages. If you need to cache parsing of Liquid expressions, it is now recommended to implement a cache per tag, where it makes sense to do so for your use case.
15+
- Make markupsafe>=3 a dependency. Previously markupsafe was an optional dependency. Version 3 of markupsafe brings some subtle changes to the `replace`, `replace_first` and `replace_last` filters when they receive a "safe" string wrapped in `Markup()`.
16+
- Add new filters `reject`, `has`, `find` and `find_index`.
17+
- Add the new `doc` tag.
1918

2019
### API changes
2120

21+
Also see the [migration guide].
22+
2223
#### Miscellaneous
2324

2425
- Added `liquid.parse(source)`, `liquid.render(source, **data)` and `liquid.render_async(source, **data)`. These are shorthand functions that use `liquid.DEFAULT_ENVIRONMENT`.
@@ -27,6 +28,9 @@ This major release:
2728
- Added `liquid.Environment.render(source, **data)` and `liquid.Environment.render_async(source, **data)`. These are convenience methods equivalent to `liquid.Environment.from_string(source).render(**data)`.
2829
- Renamed `liquid.Context` to `liquid.RenderContext`.
2930
- Change the `liquid.RenderContext` constructor (previously `liquid.Context`) to require an instance of `BoundTemplate` as its only positional argument instead of an instance of `Environment`. All other arguments are now keyword only.
31+
- Renamed `liquid.exceptions.Error` to `liquid.exceptions.LiquidError`.
32+
- Renamed `liquid.exceptions.TemplateNotFound` to `liquid.exceptions.TemplateNotFoundError`.
33+
- Renamed `liquid.exceptions.NoSuchFilterFunc` to `liquid.exceptions.UnknownFilterError`.
3034

3135
#### Template loaders
3236

README.md

Lines changed: 44 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
> [!IMPORTANT]
2-
> 🎉 Announcing the release of [Python Liquid2](https://github.com/jg-rp/python-liquid2) 🎉 See the [migration guide](https://jg-rp.github.io/python-liquid2/migration/) and the list of [new features](https://jg-rp.github.io/python-liquid2/migration/#new-features).
3-
4-
---
5-
61
<h1 align="center">Python Liquid</h1>
72

83
<p align="center">
9-
A Python engine for <a href="https://shopify.github.io/liquid/">Liquid</a>, the safe customer-facing template language for flexible web apps.
10-
<br>We follow <a href="https://github.com/Shopify/liquid">Shopify/Liquid</a> closely and test against the <a href="https://github.com/jg-rp/golden-liquid">Golden Liquid</a> test suite.
4+
Python Liquid is a Python engine for <a href="https://shopify.github.io/liquid/">Liquid</a>, the safe, customer-facing template language.
5+
<br>
6+
We follow <a href="https://github.com/Shopify/liquid">Shopify/Liquid</a> closely and test against the <a href="https://github.com/jg-rp/golden-liquid">Golden Liquid</a> test suite.
117
</p>
128
<p align="center">
139
<a href="https://github.com/jg-rp/liquid/blob/main/LICENSE">
@@ -46,10 +42,8 @@ A Python engine for <a href="https://shopify.github.io/liquid/">Liquid</a>, the
4642

4743
- [Install](#install)
4844
- [Links](#links)
49-
- [Example](#example)
5045
- [Related Projects](#related-projects)
51-
- [Compatibility](#compatibility)
52-
- [Benchmark](#benchmark)
46+
- [Quick Start](#example)
5347
- [Contributing](#contributing)
5448

5549
## Install
@@ -75,78 +69,70 @@ $ conda install -c conda-forge python-liquid
7569
## Links
7670

7771
- Documentation: https://jg-rp.github.io/liquid/
72+
- Documentation for Python Liquid version 1.x: https://jg-rp.github.io/python-liquid-docs-archive/
7873
- Change Log: https://github.com/jg-rp/liquid/blob/main/CHANGES.md
7974
- PyPi: https://pypi.org/project/python-liquid/
8075
- Source Code: https://github.com/jg-rp/liquid
8176
- Issue Tracker: https://github.com/jg-rp/liquid/issues
8277

83-
## Example
84-
85-
```python
86-
from liquid import Template
78+
## Related Projects
8779

88-
template = Template("Hello, {{ you }}!")
89-
print(template.render(you="World")) # "Hello, World!"
90-
print(template.render(you="Liquid")) # "Hello, Liquid!"
91-
```
80+
- [Python Liquid2](https://github.com/jg-rp/python-liquid2): A new Python engine for Liquid with [extra features](https://jg-rp.github.io/python-liquid2/migration/#new-features).
81+
- [LiquidScript](https://github.com/jg-rp/liquidscript): A JavaScript engine for Liquid with a similar high-level API to Python Liquid.
9282

93-
## Related Projects
83+
## Quick Start
9484

95-
- [Python Liquid2](https://github.com/jg-rp/python-liquid2): A new Python pakage for Liquid, with extra features.
96-
- [liquid-babel](https://github.com/jg-rp/liquid-babel): Internationalization and localization for Liquid templates.
97-
- [LiquidScript](https://github.com/jg-rp/liquidscript): A JavaScript and TypeScript engine for Liquid with a similar high-level API to Python Liquid.
98-
- [django-liquid](https://github.com/jg-rp/django-liquid): A Django template backend for Liquid. Render Liquid templates in your Django apps.
99-
- [Flask-Liquid](https://github.com/jg-rp/Flask-Liquid): A Flask extension for Liquid. Render Liquid templates in your Flask applications.
100-
- [golden-liquid](https://github.com/jg-rp/golden-liquid): A test suite for Liquid. See how various Liquid template engines compare to the reference implementation.
85+
### `render()`
10186

102-
## Compatibility
87+
This example renders a template from a string of text with the package-level `render()` function. The template has just one placeholder variable `you`, which we've given the value `"World"`.
10388

104-
We strive to be 100% compatible with the [reference implementation](https://shopify.github.io/liquid/) of Liquid, written in Ruby. That is, given an equivalent render context, a template rendered with Python Liquid should produce the same output as when rendered with Ruby Liquid.
89+
```python
90+
from liquid import render
10591

106-
See the [known issues page](https://jg-rp.github.io/liquid/known_issues) for details of known incompatibilities between Python Liquid and Ruby Liquid, and please help by raising an issue if you notice an incompatibility.
92+
print(render("Hello, {{ you }}!", you="World"))
93+
# Hello, World!
94+
```
10795

108-
## Benchmark
96+
### `parse()`
10997

110-
You can run the benchmark using `hatch run benchmark` (or `python -O scripts/performance.py` if you don't have `make`) from the root of the source tree. On my ropey desktop computer with a Ryzen 5 1500X and Python 3.11.0, we get the following results.
98+
Often you'll want to render the same template several times with different variables. We can parse source text without immediately rendering it using the `parse()` function. `parse()` returns a `BoundTemplate` instance with a `render()` method.
11199

112-
```text
113-
Best of 5 rounds with 100 iterations per round and 60 ops per iteration (6000 ops per round).
100+
```python
101+
from liquid import parse
114102

115-
lex template (not expressions): 1.2s (5020.85 ops/s, 83.68 i/s)
116-
lex and parse: 5.0s (1197.32 ops/s, 19.96 i/s)
117-
render: 1.4s (4152.92 ops/s, 69.22 i/s)
118-
lex, parse and render: 6.5s (922.08 ops/s, 15.37 i/s)
103+
template = parse("Hello, {{ you }}!")
104+
print(template.render(you="World")) # Hello, World!
105+
print(template.render(you="Liquid")) # Hello, Liquid!
119106
```
120107

121-
And PyPy3.7 gives us a decent increase in performance.
122-
123-
```text
124-
Best of 5 rounds with 100 iterations per round and 60 ops per iteration (6000 ops per round).
108+
### Configure
125109

126-
lex template (not expressions): 0.58s (10308.67 ops/s, 171.81 i/s)
127-
lex and parse: 3.6s (1661.20 ops/s, 27.69 i/s)
128-
render: 0.95s (6341.14 ops/s, 105.69 i/s)
129-
lex, parse and render: 4.6s (1298.18 ops/s, 21.64 i/s)
130-
```
110+
Both `parse()` and `render()` are convenience functions that use the default Liquid environment. For all but the simplest cases, you'll want to configure an instance of `Environment`, then load and render templates from that.
131111

132-
On the same machine, running `rake benchmark:run` from the root of the reference implementation source tree gives us these results.
112+
```python
113+
from liquid import CachingFileSystemLoader
114+
from liquid import Environment
133115

134-
```text
135-
/usr/bin/ruby ./performance/benchmark.rb lax
116+
env = Environment(
117+
autoescape=True,
118+
loader=CachingFileSystemLoader("./templates"),
119+
)
120+
```
136121

137-
Running benchmark for 10 seconds (with 5 seconds warmup).
122+
Then, using `env.parse()` or `env.get_template()`, we can create a `BoundTemplate` from a string or read from the file system, respectively.
138123

139-
Warming up --------------------------------------
140-
parse: 3.000 i/100ms
141-
render: 8.000 i/100ms
142-
parse & render: 2.000 i/100ms
143-
Calculating -------------------------------------
144-
parse: 39.072 (± 0.0%) i/s - 393.000 in 10.058789s
145-
render: 86.995 (± 1.1%) i/s - 872.000 in 10.024951s
146-
parse & render: 26.139 (± 0.0%) i/s - 262.000 in 10.023365s
124+
```python
125+
# ... continued from above
126+
template = env.parse("Hello, {{ you }}!")
127+
print(template.render(you="World")) # Hello, World!
128+
129+
# Try to load "./templates/index.html"
130+
another_template = env.get_template("index.html")
131+
data = {"some": {"thing": [1, 2, 3]}}
132+
result = another_template.render(**data)
147133
```
148134

149-
I've tried to match the benchmark workload to that of the reference implementation, so that we might compare results directly. The workload is meant to be representative of Shopify's use case, although I wouldn't be surprised if their usage has changed subtly since the benchmark fixture was designed.
135+
Unless you happen to have a relative folder called `templates` with a file called `index.html` within it, we would expect a `TemplateNotFoundError` to be raised when running the example above.
150136

151137
## Contributing
152138

contributing.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Contributing to Python Liquid
22

3-
Hi. Your contributions and questions are always welcome. Feel free to ask questions, report bugs or request features on the [issue tracker](https://github.com/jg-rp/liquid/issues) or on [Github Discussions](https://github.com/jg-rp/liquid/discussions).
3+
Your contributions and questions are always welcome. Feel free to ask questions, report bugs or request features on the [issue tracker](https://github.com/jg-rp/liquid/issues) or on [Github Discussions](https://github.com/jg-rp/liquid/discussions).
44

55
**Table of contents**
66

@@ -52,9 +52,7 @@ Then open `htmlcov/index.html` in your browser.
5252

5353
## Documentation
5454

55-
[Documentation](https://jg-rp.github.io/liquid/) is built using [Docusaurus](https://docusaurus.io/). Find the source in the [docs branch](https://github.com/jg-rp/liquid/tree/docs) of this repository.
56-
57-
The `docs` folder in the root of this repository contains docs generated from Python docstrings, hosted on [Read the Docs](https://liquid.readthedocs.io/en/latest/). The plan is to mirror this API documentation to the Docusaurus site with the help of [Griffe](https://mkdocstrings.github.io/griffe/).
55+
[Documentation](https://jg-rp.github.io/liquid/) is built using [Material for MkDocs](https://squidfunk.github.io/mkdocs-material/getting-started/). Find the source in the `docs` folder of this repository.
5856

5957
## Style Guides
6058

@@ -64,6 +62,6 @@ There are no hard rules for git commit messages, although you might like to indi
6462

6563
### Python Style
6664

67-
All Python files are formatted using [Black](https://github.com/psf/black), with its default configuration.
65+
All Python files are formatted using [Ruff](https://github.com/astral-sh/ruff), configured in `pyproject.toml`
6866

6967
Docstrings must use [Google style docstrings](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html).

docs/.overrides/main.html

Lines changed: 0 additions & 5 deletions
This file was deleted.

docs/analysis.md

Lines changed: 0 additions & 10 deletions
This file was deleted.

docs/api.md

Lines changed: 0 additions & 10 deletions
This file was deleted.

docs/api/ast.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
::: liquid.Node
2+
::: liquid.BlockNode
3+
::: liquid.ConditionalBlockNode
4+
::: liquid.ast.Partial
5+
::: liquid.ast.PartialScope

docs/api/builtin.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
::: liquid.builtin.register

docs/api/convenience.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
::: liquid.parse
2+
::: liquid.render
3+
::: liquid.render_async
4+
::: liquid.extract_liquid

docs/api/environment.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
::: liquid.Environment

0 commit comments

Comments
 (0)