BlogPlugin: handling of generated blog posts
This commit is contained in:
parent
ffcabcaa15
commit
fe49397c21
|
|
@ -52,13 +52,18 @@ class Post(Page):
|
|||
def __init__(self, file: File, config: MkDocsConfig):
|
||||
super().__init__(None, file, config)
|
||||
|
||||
if file.generated_by is None:
|
||||
# Resolve path relative to docs directory
|
||||
docs = os.path.relpath(config.docs_dir)
|
||||
path = os.path.relpath(file.abs_src_path, docs)
|
||||
file_origin = f" in '{docs}'"
|
||||
else:
|
||||
# Path for generated posts
|
||||
path = file.src_path
|
||||
file_origin = f" generated by '{file.generated_by}'"
|
||||
|
||||
# Read contents and metadata immediately
|
||||
with open(file.abs_src_path, encoding = "utf-8-sig") as f:
|
||||
self.markdown = f.read()
|
||||
self.markdown = file.content_string
|
||||
|
||||
# Sadly, MkDocs swallows any exceptions that occur during parsing.
|
||||
# Since we want to provide the best possible user experience, we
|
||||
|
|
@ -70,7 +75,7 @@ class Post(Page):
|
|||
match: Match = YAML_RE.match(self.markdown)
|
||||
if not match:
|
||||
raise PluginError(
|
||||
f"Error reading metadata of post '{path}' in '{docs}':\n"
|
||||
f"Error reading metadata of post '{path}' {file_origin}:\n"
|
||||
f"Expected metadata to be defined but found nothing"
|
||||
)
|
||||
|
||||
|
|
@ -83,7 +88,7 @@ class Post(Page):
|
|||
# which we display to the author with a nice error message
|
||||
except Exception as e:
|
||||
raise PluginError(
|
||||
f"Error reading metadata of post '{path}' in '{docs}':\n"
|
||||
f"Error reading metadata of post '{path}' {file_origin}:\n"
|
||||
f"{e}"
|
||||
)
|
||||
|
||||
|
|
@ -103,7 +108,7 @@ class Post(Page):
|
|||
log.warning(w)
|
||||
for k, e in errors:
|
||||
raise PluginError(
|
||||
f"Error reading metadata '{k}' of post '{path}' in '{docs}':\n"
|
||||
f"Error reading metadata '{k}' of post '{path}' {file_origin}:\n"
|
||||
f"{e}"
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -52,13 +52,18 @@ class Post(Page):
|
|||
def __init__(self, file: File, config: MkDocsConfig):
|
||||
super().__init__(None, file, config)
|
||||
|
||||
if file.generated_by is None:
|
||||
# Resolve path relative to docs directory
|
||||
docs = os.path.relpath(config.docs_dir)
|
||||
path = os.path.relpath(file.abs_src_path, docs)
|
||||
file_origin = f" in '{docs}'"
|
||||
else:
|
||||
# Path for generated posts
|
||||
path = file.src_path
|
||||
file_origin = f" generated by '{file.generated_by}'"
|
||||
|
||||
# Read contents and metadata immediately
|
||||
with open(file.abs_src_path, encoding = "utf-8-sig") as f:
|
||||
self.markdown = f.read()
|
||||
self.markdown = file.content_string
|
||||
|
||||
# Sadly, MkDocs swallows any exceptions that occur during parsing.
|
||||
# Since we want to provide the best possible user experience, we
|
||||
|
|
@ -70,7 +75,7 @@ class Post(Page):
|
|||
match: Match = YAML_RE.match(self.markdown)
|
||||
if not match:
|
||||
raise PluginError(
|
||||
f"Error reading metadata of post '{path}' in '{docs}':\n"
|
||||
f"Error reading metadata of post '{path}' {file_origin}:\n"
|
||||
f"Expected metadata to be defined but found nothing"
|
||||
)
|
||||
|
||||
|
|
@ -83,7 +88,7 @@ class Post(Page):
|
|||
# which we display to the author with a nice error message
|
||||
except Exception as e:
|
||||
raise PluginError(
|
||||
f"Error reading metadata of post '{path}' in '{docs}':\n"
|
||||
f"Error reading metadata of post '{path}' {file_origin}:\n"
|
||||
f"{e}"
|
||||
)
|
||||
|
||||
|
|
@ -103,7 +108,7 @@ class Post(Page):
|
|||
log.warning(w)
|
||||
for k, e in errors:
|
||||
raise PluginError(
|
||||
f"Error reading metadata '{k}' of post '{path}' in '{docs}':\n"
|
||||
f"Error reading metadata '{k}' of post '{path}' {file_origin}:\n"
|
||||
f"{e}"
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue