24 lines
650 B
Python
24 lines
650 B
Python
from typing import Any
|
|
|
|
from yaml import Loader, load
|
|
|
|
from jwebsite.content import Content, ContentField
|
|
from jwebsite.context import Context
|
|
|
|
|
|
def load_extension(context: Context) -> None:
|
|
context.add_filters(yaml=_load_yaml)
|
|
|
|
|
|
def _load_yaml(content: Any) -> ContentField:
|
|
if not isinstance(content, Content) or not isinstance(content.data, (str, bytes)):
|
|
raise ValueError("yaml filter can only accept byte or string content")
|
|
|
|
data = content.data
|
|
if isinstance(data, bytes):
|
|
data = data.decode("utf-8")
|
|
|
|
assert isinstance(data, str)
|
|
|
|
return ContentField(content.path, load(data, Loader), content.language)
|