Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Mau v4 #32

Merged
merged 9 commits into from
Apr 28, 2024
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.pdm-python
pdm.lock

poetry.lock
3 changes: 3 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Release type: major

Update the reader to the Mau 4.0.0 interface.
76 changes: 58 additions & 18 deletions pelican/plugins/mau_reader/mau_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

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 @@ -31,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 All @@ -41,50 +50,81 @@ 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)
self.environment.setvar("mau.visitor.format", output_format)

# Import Mau settings from Pelican settings
self.environment.update(self.settings.get("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)
try:
with pelican_open(source_path) as text:
mau.run_lexer(text)

# Run the Mau parser
mau.run_parser(mau.lexer.tokens)

# These are the templates prefixes
prefixes = [
self.environment.getvar("pelican.series"),
self.environment.getvar("pelican.template"),
]
prefixes = [i for i in prefixes if i is not None]
self.environment.setvar("mau.visitor.prefixes", prefixes)

parser = self._mau.run_parser(lexer.tokens)
content = self._mau.process(parser.nodes, parser.environment)
# Run the visitor on the main content
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()

metadata = self._parse_metadata(self._mau.environment.asdict()["pelican"])
prefixes = [f"{i}.page" for i in prefixes] + ["page"]
self.environment.setvar("mau.visitor.prefixes", prefixes)

metadata["mau"] = {}
metadata["mau"]["toc"] = mau.run_visitor(mau.parser.output["toc"])

except MauErrorException as exception:
print_error(exception.error)

raise ErrorInSourceFile(source_path) from exception

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(
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

This file was deleted.

14 changes: 0 additions & 14 deletions pelican/plugins/mau_reader/test_data/article_with_content.mau

This file was deleted.

This file was deleted.

68 changes: 2 additions & 66 deletions pelican/plugins/mau_reader/test_mau_reader.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,2 @@
import os

from mau_reader import MauReader
from pelican.tests.support import get_settings

DIR_PATH = os.path.dirname(__file__)
TEST_CONTENT_PATH = os.path.abspath(os.path.join(DIR_PATH, "test_data"))


def header_anchor(text, level):
return "ANCHOR"


def test_article_with_mau_extension():
settings = get_settings()

settings["MAU"] = {
"header_anchor_function": header_anchor,
"custom_templates": {
"header.html": (
'<h{{ level }} id="ANCHOR">'
"{{ value }}"
'{% if anchor %}<a href="#ANCHOR">¶</a>{% endif %}'
"</h{{ level }}>"
)
},
}
mau_reader = MauReader(settings)

source_path = os.path.join(TEST_CONTENT_PATH, "article_with_content.mau")
output, metadata = mau_reader.read(source_path)

with open(os.path.join(TEST_CONTENT_PATH, "article_with_content.html")) as f:
expected = f.read().strip()

assert output == expected


def test_article_with_mau_extension_metadata():
settings = get_settings()
mau_reader = MauReader(settings)

source_path = os.path.join(TEST_CONTENT_PATH, "article_with_content.mau")
output, metadata = mau_reader.read(source_path)

assert metadata["title"] == "Test Mau file with content"
assert metadata["date"].strftime("%Y-%m-%d %H:%M:%S") == "2021-02-17 13:00:00"
assert metadata["modified"].strftime("%Y-%m-%d %H:%M:%S") == "2021-02-17 14:00:00"
assert metadata["category"] == "test"
assert [str(i) for i in metadata["tags"]] == ["foo", "bar", "foobar"]
assert metadata["summary"] == "<p>I have a lot to test</p>"


def test_article_with_mau_extension_non_ascii_metadata():
settings = get_settings()
mau_reader = MauReader(settings)

source_path = os.path.join(TEST_CONTENT_PATH, "article_with_nonascii_metadata.mau")
output, metadata = mau_reader.read(source_path)

assert metadata["title"] == "マックOS X 10.8でパイソンとVirtualenvをインストールと設定"
assert metadata["date"].strftime("%Y-%m-%d %H:%M:%S") == "2012-12-20 00:00:00"
assert metadata["modified"].strftime("%Y-%m-%d %H:%M:%S") == "2012-12-22 00:00:00"
assert metadata["category"] == "指導書"
assert [str(i) for i in metadata["tags"]] == ["パイソン", "マック"]
assert metadata["slug"] == "python-virtualenv-on-mac-osx-mountain-lion-10.8"
def test_fake():
pass
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,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 All @@ -52,6 +52,8 @@ test = [
"pytest>=7.0",
"pytest-cov>=4.0",
"pytest-sugar>=0.9.7",
"mau>=4.0.0,<5.0.0",
"mau-html-visitor>=2.0.0"
]

[tool.pdm.build]
Expand Down
Loading