Skip to content

Commit

Permalink
Better error management
Browse files Browse the repository at this point in the history
Make custom filters available in metadata
  • Loading branch information
lgiordani committed Mar 20, 2024
1 parent e31887c commit 8b695b0
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions pelican/plugins/mau_reader/mau_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
try:
from mau import Mau, load_visitors
from mau.environment.environment import Environment
from mau.errors import MauErrorException, print_error

visitor_classes = load_visitors()
mau_enabled = True
Expand All @@ -32,6 +33,13 @@ def __init__(self, output_format):
)


class ErrorInSourceFile(Exception):
"""Exception to signal an error parsing the source file."""

def __init__(self, filename):
super().__init__(f"The file {filename} cannot be parsed")


class MauReader(BaseReader):
"""Mau Reader class method."""

Expand Down Expand Up @@ -63,23 +71,32 @@ def read(self, source_path):
self.environment,
)

with pelican_open(source_path) as text:
mau.run_lexer(text)
try:
with pelican_open(source_path) as text:
mau.run_lexer(text)

mau.run_parser(mau.lexer.tokens)
content = mau.run_visitor(mau.parser.output["content"])
mau.run_parser(mau.lexer.tokens)
content = mau.run_visitor(mau.parser.output["content"])

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

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

metadata["mau"] = {
"custom_filters": mau.parser.output["custom_filters"],
}
except MauErrorException as exception:
print_error(exception.error)

return content, metadata
raise ErrorInSourceFile(source_path) from exception
else:
return content, metadata

def _parse_metadata(self, meta):
def _parse_metadata(self):
"""Return the dict containing document metadata."""
meta = self.environment.getvar("pelican").asdict()

formatted_fields = self.settings["FORMATTED_FIELDS"]

mau = Mau(
Expand Down

0 comments on commit 8b695b0

Please sign in to comment.