Skip to content

Commit

Permalink
docs: document Jinja filter plugins (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfraney authored Feb 29, 2024
1 parent 94150b8 commit e531346
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
8 changes: 6 additions & 2 deletions docs/content/plugins/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ Blurry ships with a simple plugin infrastructure that makes it easy to write and

## How to write a plugin

See [Plugins: write a Markdown plugin](./write-a-markdown-plugin.md) and [Plugins: write an HTML plugin](./write-an-html-plugin.md) for more information.
See the docs for the type of plugin you'd like to write:

- [Plugins: write a Markdown plugin](./write-a-markdown-plugin.md)
- [Plugins: write an HTML plugin](./write-an-html-plugin.md)
- [Plugins: write a Jinja filter plugin](./write-a-jinja-filter-plugin.md)

## How to register a plugin

Expand All @@ -37,5 +41,5 @@ It registers a Blurry Markdown plugin in its `pyproject.toml` file using [Poetry
blur_blurry_name = "blurry_plugin_blur_blurry_name:blur_blurry_name"
```

Blurry [dogfoods](https://en.wikipedia.org/wiki/Eating_your_own_dog_food) its own plugin architecture for HTML and Markdown plugins, too.
Blurry [dogfoods](https://en.wikipedia.org/wiki/Eating_your_own_dog_food) its own plugin architecture, too.
See which plugins are registered in [Blurry's `pyproject.toml` file](https://github.com/blurry-dev/blurry/blob/main/pyproject.toml).
43 changes: 43 additions & 0 deletions docs/content/plugins/write-a-jinja-filter-plugin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
+++
"@type" = "WebPage"
name = "Plugins: write a Jinja filter plugin"
abstract = "Documentation for Blurry's Jinja filter plugins"
datePublished = 2024-02-29
+++

# Plugins: write an Jinja filter plugin

Blurry makes it easy to add [custom Jinja filters](https://jinja.palletsprojects.com/en/3.1.x/api/#custom-filters) to your site.
What is a Jinja filter?
From the Jinja docs:

> Filters are Python functions that take the value to the left of the filter as the first argument and produce a new value. Arguments passed to the filter are passed after the value.
>
> For example, the filter `{{ 42|myfilter(23) }}` is called behind the scenes as myfilter(42, 23).
## Example: "stars"

This function outputs a number of Unicode stars corresponding to the input, rounding to the nearest half-star.

```python
import math

STAR_EMPTY = ""
STAR_FILLED = ""
STAR_HALF_FILLED = ""


def float_to_stars(num: float, max: int = 5) -> str:
whole_stars = math.floor(num) * STAR_FILLED
part_star = STAR_HALF_FILLED if num - len(whole_stars) > 0 else ""
non_empty_stars = f"{whole_stars}{part_star}"

return f"{non_empty_stars:{STAR_EMPTY}{'<'}{max}}"
```

To use it, add the appropriate plugin syntax to your `pyproject.toml` file:

```toml
[tool.poetry.plugins."blurry.jinja_filter_plugins"]
stars = "{{ yourproject }}:float_to_stars"
```
1 change: 1 addition & 0 deletions docs/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<li><a href="/plugins/intro/" rel="noreferrer">Intro</a></li>
<li><a href="/plugins/write-a-markdown-plugin/" rel="noreferrer">Write a Markdown plugin</a></li>
<li><a href="/plugins/write-an-html-plugin/" rel="noreferrer">Write an HTML plugin</a></li>
<li><a href="/plugins/write-a-jinja-filter-plugin/" rel="noreferrer">Write a Jinja filter plugin</a></li>
</ul>
</li>
</ul>
Expand Down

0 comments on commit e531346

Please sign in to comment.