Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions markata/plugins/default_post_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,8 @@

</style>

{% for head in config.get('head', []) %}
{{ head.get('text', '') }}
{% endfor %}
{{ config.get('head', {}).pop('text') if 'text' in config.get('head',{}).keys() }}{% for tag, meta in config.get('head', {}).items() %}{% for _meta in meta %}
<{{ tag }} {% for attr, value in _meta.items() %}{{ attr }}="{{ value }}"{% endfor %}/> {% endfor %}{% endfor %}
</head>

<nav>
Expand Down
73 changes: 73 additions & 0 deletions markata/plugins/post_template.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,63 @@
"""


## Add head configuration

This snippet allows users to configure their head in `markata.toml`.

``` html
{{ config.get('head', {}).pop('text') if 'text' in config.get('head',{}).keys() }}{% for tag, meta in config.get('head', {}).items() %}{% for _meta in meta %}
<{{ tag }} {% for attr, value in _meta.items() %}{{ attr }}="{{ value }}"{% endfor %}/> {% endfor %}{% endfor %}
```

Users can specify any sort of tag in their `markata.toml`

```
[[markata.head.meta]]
name = "og:type"
content = "article"

[[markata.head.meta]]
name = "og:author"
content = "Waylon Walker"
```

The above configuration becomes this once rendered.

``` html
<meta name='og:type' content='article' />
<meta name='og:Author' content='Waylon Walker' />
```

Optionally users can also specify plain text to be appended to the head of
their documents. This works well for things that involve full blocks.

``` toml
[[markata.head.text]]
value = '''
<script>
console.log('hello world')
</script>
'''

[[markata.head.text]]
value='''
html {
font-family: "Space Mono", monospace;
background: var(--color-bg);
color: var(--color-text);
}
'''
```



"""
from pathlib import Path
from typing import TYPE_CHECKING

from jinja2 import Template, Undefined
from more_itertools import flatten

from markata.hookspec import hook_impl

Expand All @@ -14,6 +70,23 @@ def _fail_with_undefined_error(self, *args, **kwargs):
return ""


@hook_impl
def configure(markata: "Markata") -> None:
"""
Massages the configuration limitations of toml to allow a little bit easier
experience to the end user making configurations while allowing an simpler
jinja template. This enablees the use of the `markata.head.text` list in
configuration.
"""

raw_text = markata.config.get("head", {}).get("text", "")

if isinstance(raw_text, list):
markata.config["head"]["text"] = "\n".join(
flatten([t.values() for t in raw_text])
)


@hook_impl
def render(markata: "Markata") -> None:
if "post_template" in markata.config:
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ dev = [
"pre-commit",
"pytest",
"pytest-cov",
"pytest-rich",
"pytest-mock",
"pytest-tmp-files",
]
Expand Down Expand Up @@ -124,7 +125,7 @@ ignore_errors = true

[tool.isort]
profile = "black"
known_third_party = ["PIL", "anyconfig", "bs4", "checksumdir", "commonmark", "conftest", "dateutil", "diskcache", "feedgen", "frontmatter", "jinja2", "markdown", "more_itertools", "pathspec", "pkg_resources", "pluggy", "pydantic", "pytest", "pytz", "rich", "slugify", "textual", "typer", "yaml"]
known_third_party = ["PIL", "anyconfig", "bs4", "checksumdir", "commonmark", "conftest", "dateutil", "diskcache", "feedgen", "frontmatter", "jinja2", "markdown", "more_itertools", "pathspec", "pkg_resources", "pluggy", "pydantic", "pytest", "pytz", "rich", "slugify", "textual", "toml", "typer", "yaml"]

[tool.hatch.version]
path = "markata/__about__.py"
Expand All @@ -150,6 +151,7 @@ dependencies = [
"isort",
"pytest",
"pytest-cov",
"pytest-rich",
"pytest-tmp-files",
"seed-isort-config",
]
Expand All @@ -162,6 +164,7 @@ dependencies = [
"isort",
"pytest",
"pytest-cov",
"pytest-rich",
"pytest-tmp-files",
"seed-isort-config",
]
Expand Down
47 changes: 47 additions & 0 deletions tests/plugins/test_default_post_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from pathlib import Path

import toml
from conftest import set_directory

from markata import Markata
from markata.plugins import post_template


def test_head_config_text_str(tmp_path):
with set_directory(tmp_path):
m = Markata()
m.config["head"] = {}
m.config["head"]["text"] = "here"
post_template.configure(m)
assert m.config["head"]["text"] == "here"


def test_head_config_text_dict(tmp_path):
with set_directory(tmp_path):
m = Markata()
m.config["head"] = {}
m.config["head"]["text"] = [{"value": "one"}, {"value": "two"}]
post_template.configure(m)
assert m.config["head"]["text"] == "one\ntwo"


def test_head_config_text_str_toml(tmp_path):
with set_directory(tmp_path):
Path("markata.toml").write_text(
toml.dumps({"markata": {"head": {"text": "here"}}})
)
m = Markata()
post_template.configure(m)
assert m.config["head"]["text"] == "here"


def test_head_config_text_list_toml(tmp_path):
with set_directory(tmp_path):
Path("markata.toml").write_text(
toml.dumps(
{"markata": {"head": {"text": [{"value": "one"}, {"value": "two"}]}}}
)
)
m = Markata()
post_template.configure(m)
assert m.config["head"]["text"] == "one\ntwo"