BlogPlugin: handling of generated blog posts
This commit is contained in:
parent
ffcabcaa15
commit
fe49397c21
|
|
@ -52,40 +52,45 @@ class Post(Page):
|
||||||
def __init__(self, file: File, config: MkDocsConfig):
|
def __init__(self, file: File, config: MkDocsConfig):
|
||||||
super().__init__(None, file, config)
|
super().__init__(None, file, config)
|
||||||
|
|
||||||
# Resolve path relative to docs directory
|
if file.generated_by is None:
|
||||||
docs = os.path.relpath(config.docs_dir)
|
# Resolve path relative to docs directory
|
||||||
path = os.path.relpath(file.abs_src_path, docs)
|
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
|
# Read contents and metadata immediately
|
||||||
with open(file.abs_src_path, encoding = "utf-8-sig") as f:
|
self.markdown = file.content_string
|
||||||
self.markdown = f.read()
|
|
||||||
|
|
||||||
# Sadly, MkDocs swallows any exceptions that occur during parsing.
|
# Sadly, MkDocs swallows any exceptions that occur during parsing.
|
||||||
# Since we want to provide the best possible user experience, we
|
# Since we want to provide the best possible user experience, we
|
||||||
# need to catch errors early and display them nicely. We decided to
|
# need to catch errors early and display them nicely. We decided to
|
||||||
# drop support for MkDocs' MultiMarkdown syntax, because it is not
|
# drop support for MkDocs' MultiMarkdown syntax, because it is not
|
||||||
# correctly implemented anyway. When using MultiMarkdown syntax, all
|
# correctly implemented anyway. When using MultiMarkdown syntax, all
|
||||||
# date formats are returned as strings and list are not properly
|
# date formats are returned as strings and list are not properly
|
||||||
# supported. Thus, we just use the relevants parts of `get_data`.
|
# supported. Thus, we just use the relevants parts of `get_data`.
|
||||||
match: Match = YAML_RE.match(self.markdown)
|
match: Match = YAML_RE.match(self.markdown)
|
||||||
if not match:
|
if not match:
|
||||||
raise PluginError(
|
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"
|
f"Expected metadata to be defined but found nothing"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Extract metadata and parse as YAML
|
# Extract metadata and parse as YAML
|
||||||
try:
|
try:
|
||||||
self.meta = yaml.load(match.group(1), SafeLoader) or {}
|
self.meta = yaml.load(match.group(1), SafeLoader) or {}
|
||||||
self.markdown = self.markdown[match.end():].lstrip("\n")
|
self.markdown = self.markdown[match.end():].lstrip("\n")
|
||||||
|
|
||||||
# The post's metadata could not be parsed because of a syntax error,
|
# The post's metadata could not be parsed because of a syntax error,
|
||||||
# which we display to the author with a nice error message
|
# which we display to the author with a nice error message
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise PluginError(
|
raise PluginError(
|
||||||
f"Error reading metadata of post '{path}' in '{docs}':\n"
|
f"Error reading metadata of post '{path}' {file_origin}:\n"
|
||||||
f"{e}"
|
f"{e}"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Initialize post configuration, but remove all keys that this plugin
|
# Initialize post configuration, but remove all keys that this plugin
|
||||||
# doesn't care about, or they will be reported as invalid configuration
|
# doesn't care about, or they will be reported as invalid configuration
|
||||||
|
|
@ -103,7 +108,7 @@ class Post(Page):
|
||||||
log.warning(w)
|
log.warning(w)
|
||||||
for k, e in errors:
|
for k, e in errors:
|
||||||
raise PluginError(
|
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}"
|
f"{e}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -52,40 +52,45 @@ class Post(Page):
|
||||||
def __init__(self, file: File, config: MkDocsConfig):
|
def __init__(self, file: File, config: MkDocsConfig):
|
||||||
super().__init__(None, file, config)
|
super().__init__(None, file, config)
|
||||||
|
|
||||||
# Resolve path relative to docs directory
|
if file.generated_by is None:
|
||||||
docs = os.path.relpath(config.docs_dir)
|
# Resolve path relative to docs directory
|
||||||
path = os.path.relpath(file.abs_src_path, docs)
|
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
|
# Read contents and metadata immediately
|
||||||
with open(file.abs_src_path, encoding = "utf-8-sig") as f:
|
self.markdown = file.content_string
|
||||||
self.markdown = f.read()
|
|
||||||
|
|
||||||
# Sadly, MkDocs swallows any exceptions that occur during parsing.
|
# Sadly, MkDocs swallows any exceptions that occur during parsing.
|
||||||
# Since we want to provide the best possible user experience, we
|
# Since we want to provide the best possible user experience, we
|
||||||
# need to catch errors early and display them nicely. We decided to
|
# need to catch errors early and display them nicely. We decided to
|
||||||
# drop support for MkDocs' MultiMarkdown syntax, because it is not
|
# drop support for MkDocs' MultiMarkdown syntax, because it is not
|
||||||
# correctly implemented anyway. When using MultiMarkdown syntax, all
|
# correctly implemented anyway. When using MultiMarkdown syntax, all
|
||||||
# date formats are returned as strings and list are not properly
|
# date formats are returned as strings and list are not properly
|
||||||
# supported. Thus, we just use the relevants parts of `get_data`.
|
# supported. Thus, we just use the relevants parts of `get_data`.
|
||||||
match: Match = YAML_RE.match(self.markdown)
|
match: Match = YAML_RE.match(self.markdown)
|
||||||
if not match:
|
if not match:
|
||||||
raise PluginError(
|
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"
|
f"Expected metadata to be defined but found nothing"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Extract metadata and parse as YAML
|
# Extract metadata and parse as YAML
|
||||||
try:
|
try:
|
||||||
self.meta = yaml.load(match.group(1), SafeLoader) or {}
|
self.meta = yaml.load(match.group(1), SafeLoader) or {}
|
||||||
self.markdown = self.markdown[match.end():].lstrip("\n")
|
self.markdown = self.markdown[match.end():].lstrip("\n")
|
||||||
|
|
||||||
# The post's metadata could not be parsed because of a syntax error,
|
# The post's metadata could not be parsed because of a syntax error,
|
||||||
# which we display to the author with a nice error message
|
# which we display to the author with a nice error message
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise PluginError(
|
raise PluginError(
|
||||||
f"Error reading metadata of post '{path}' in '{docs}':\n"
|
f"Error reading metadata of post '{path}' {file_origin}:\n"
|
||||||
f"{e}"
|
f"{e}"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Initialize post configuration, but remove all keys that this plugin
|
# Initialize post configuration, but remove all keys that this plugin
|
||||||
# doesn't care about, or they will be reported as invalid configuration
|
# doesn't care about, or they will be reported as invalid configuration
|
||||||
|
|
@ -103,7 +108,7 @@ class Post(Page):
|
||||||
log.warning(w)
|
log.warning(w)
|
||||||
for k, e in errors:
|
for k, e in errors:
|
||||||
raise PluginError(
|
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}"
|
f"{e}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue