Skip to content

Commit

Permalink
Initial support for Mau v4
Browse files Browse the repository at this point in the history
  • Loading branch information
lgiordani committed Mar 17, 2024
1 parent e0c2069 commit e31887c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
37 changes: 23 additions & 14 deletions pelican/plugins/mau_reader/mau_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

try:
from mau import Mau, load_visitors
from mau.environment.environment import Environment

visitor_classes = load_visitors()
mau_enabled = True
Expand Down Expand Up @@ -41,50 +42,58 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def read(self, source_path):
self.environment = Environment()
config = self.settings.get("MAU", {})

output_format = config.get("output_format", "html")

if output_format not in visitors:
raise OutputFormatNotSupported(output_format)

custom_templates = config.get("custom_templates", {})
templates_directory = config.get("templates_directory", None)
visitor_class = visitors[output_format]
self.environment.setvar("mau.visitor.class", visitor_class)

# Import Mau settings from Pelican settings
self.environment.update(self.settings.get("MAU", {}), "mau")

self._source_path = source_path
self._mau = Mau(

mau = Mau(
source_path,
visitor_class=visitor_class,
config=config,
custom_templates=custom_templates,
templates_directory=templates_directory,
self.environment,
)

with pelican_open(source_path) as text:
lexer = self._mau.run_lexer(text)
mau.run_lexer(text)

parser = self._mau.run_parser(lexer.tokens)
content = self._mau.process(parser.nodes, parser.environment)
mau.run_parser(mau.lexer.tokens)
content = mau.run_visitor(mau.parser.output["content"])

if visitor_class.transform:
content = visitor_class.transform(content)

metadata = self._parse_metadata(self._mau.environment.asdict()["pelican"])
metadata = self._parse_metadata(
self.environment.getnamespace("pelican").asdict()
)

return content, metadata

def _parse_metadata(self, meta):
"""Return the dict containing document metadata."""
formatted_fields = self.settings["FORMATTED_FIELDS"]

mau = Mau(
self._source_path,
self.environment,
)

output = {}
for name, value in meta.items():
name = name.lower()
if name in formatted_fields:
lexer = self._mau.run_lexer(value)
parser = self._mau.run_parser(lexer.tokens)
formatted = self._mau.process(parser.nodes, parser.environment)
mau.run_lexer(value)
mau.run_parser(mau.lexer.tokens)
formatted = mau.run_visitor(mau.parser.output["content"])
output[name] = self.process_metadata(name, formatted)
elif len(value) > 1:
# handle list metadata as list of string
Expand Down
12 changes: 9 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "pelican-mau-reader"
version = "3.0.1"
version = "4.0.0"
description = "Pelican plugin that converts Mau-formatted content into HTML"
authors = [{name = "Leonardo Giordani", email = "giordani.leonardo@gmail.com"}, {name = "Justin Mayer", email = "entroP@gmail.com"}]
license = {text = "MIT"}
Expand All @@ -25,8 +25,8 @@ classifiers = [
requires-python = ">=3.8.1,<4.0"
dependencies = [
"pelican>=4.5",
"mau>=3.0.0,<4.0.0",
"mau-html-visitor>=1.0.0"
"mau>=4.0.0,<5.0.0",
"mau-html-visitor>=2.0.0"
]

[project.urls]
Expand Down Expand Up @@ -104,3 +104,9 @@ known-first-party = ["pelican"]
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.sdist]
include = ["pelican/plugins/mau_reader/*.py"]

[tool.hatch.build.targets.wheel]
include = ["pelican/plugins/mau_reader/*.py"]

0 comments on commit e31887c

Please sign in to comment.