Skip to content

Commit afe00a2

Browse files
BlogPlugin: handling of generated blog posts
1 parent ffcabca commit afe00a2

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

material/plugins/blog/structure/__init__.py

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from .config import PostConfig
4141
from .markdown import ExcerptTreeprocessor
4242

43+
4344
# -----------------------------------------------------------------------------
4445
# Classes
4546
# -----------------------------------------------------------------------------
@@ -52,40 +53,45 @@ class Post(Page):
5253
def __init__(self, file: File, config: MkDocsConfig):
5354
super().__init__(None, file, config)
5455

55-
# Resolve path relative to docs directory
56-
docs = os.path.relpath(config.docs_dir)
57-
path = os.path.relpath(file.abs_src_path, docs)
56+
if file.generated_by is None:
57+
# Resolve path relative to docs directory
58+
docs = os.path.relpath(config.docs_dir)
59+
path = os.path.relpath(file.abs_src_path, docs)
60+
file_origin = f" in '{docs}'"
61+
else:
62+
# Path for generated posts
63+
path = file.src_path
64+
file_origin = f" generated by '{file.generated_by}'"
5865

5966
# Read contents and metadata immediately
60-
with open(file.abs_src_path, encoding = "utf-8-sig") as f:
61-
self.markdown = f.read()
62-
63-
# Sadly, MkDocs swallows any exceptions that occur during parsing.
64-
# Since we want to provide the best possible user experience, we
65-
# need to catch errors early and display them nicely. We decided to
66-
# drop support for MkDocs' MultiMarkdown syntax, because it is not
67-
# correctly implemented anyway. When using MultiMarkdown syntax, all
68-
# date formats are returned as strings and list are not properly
69-
# supported. Thus, we just use the relevants parts of `get_data`.
70-
match: Match = YAML_RE.match(self.markdown)
71-
if not match:
72-
raise PluginError(
73-
f"Error reading metadata of post '{path}' in '{docs}':\n"
74-
f"Expected metadata to be defined but found nothing"
75-
)
76-
77-
# Extract metadata and parse as YAML
78-
try:
79-
self.meta = yaml.load(match.group(1), SafeLoader) or {}
80-
self.markdown = self.markdown[match.end():].lstrip("\n")
81-
82-
# The post's metadata could not be parsed because of a syntax error,
83-
# which we display to the author with a nice error message
84-
except Exception as e:
85-
raise PluginError(
86-
f"Error reading metadata of post '{path}' in '{docs}':\n"
87-
f"{e}"
88-
)
67+
self.markdown = file.content_string
68+
69+
# Sadly, MkDocs swallows any exceptions that occur during parsing.
70+
# Since we want to provide the best possible user experience, we
71+
# need to catch errors early and display them nicely. We decided to
72+
# drop support for MkDocs' MultiMarkdown syntax, because it is not
73+
# correctly implemented anyway. When using MultiMarkdown syntax, all
74+
# date formats are returned as strings and list are not properly
75+
# supported. Thus, we just use the relevants parts of `get_data`.
76+
match: Match = YAML_RE.match(self.markdown)
77+
if not match:
78+
raise PluginError(
79+
f"Error reading metadata of post '{path}' {file_origin}:\n"
80+
f"Expected metadata to be defined but found nothing"
81+
)
82+
83+
# Extract metadata and parse as YAML
84+
try:
85+
self.meta = yaml.load(match.group(1), SafeLoader) or {}
86+
self.markdown = self.markdown[match.end():].lstrip("\n")
87+
88+
# The post's metadata could not be parsed because of a syntax error,
89+
# which we display to the author with a nice error message
90+
except Exception as e:
91+
raise PluginError(
92+
f"Error reading metadata of post '{path}' {file_origin}:\n"
93+
f"{e}"
94+
)
8995

9096
# Initialize post configuration, but remove all keys that this plugin
9197
# doesn't care about, or they will be reported as invalid configuration
@@ -103,7 +109,7 @@ def __init__(self, file: File, config: MkDocsConfig):
103109
log.warning(w)
104110
for k, e in errors:
105111
raise PluginError(
106-
f"Error reading metadata '{k}' of post '{path}' in '{docs}':\n"
112+
f"Error reading metadata '{k}' of post '{path}' {file_origin}:\n"
107113
f"{e}"
108114
)
109115

0 commit comments

Comments
 (0)