api/modules: seperate frontmatter parsing from extraction
This commit is contained in:
@@ -184,7 +184,7 @@ def set_machine_disk_schema(
|
|||||||
raise ClanError(msg, description=f"Valid options: {ph.options}")
|
raise ClanError(msg, description=f"Valid options: {ph.options}")
|
||||||
|
|
||||||
header = f"""# ---
|
header = f"""# ---
|
||||||
# schema = "{schema_name}";
|
# schema = "{schema_name}"
|
||||||
# ---
|
# ---
|
||||||
# This file was automatically generated!
|
# This file was automatically generated!
|
||||||
# CHANGING this configuration requires wiping and reinstalling the machine
|
# CHANGING this configuration requires wiping and reinstalling the machine
|
||||||
|
|||||||
@@ -59,19 +59,12 @@ class Frontmatter:
|
|||||||
raise ValueError(msg)
|
raise ValueError(msg)
|
||||||
|
|
||||||
|
|
||||||
def extract_frontmatter(readme_content: str, err_scope: str) -> tuple[Frontmatter, str]:
|
def parse_frontmatter(readme_content: str) -> tuple[dict[str, Any] | None, str]:
|
||||||
"""
|
"""
|
||||||
Extracts TOML frontmatter from a README file content.
|
Extracts TOML frontmatter from a string
|
||||||
|
|
||||||
Parameters:
|
|
||||||
- readme_content (str): The content of the README file as a string.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
- str: The extracted frontmatter as a string.
|
|
||||||
- str: The content of the README file without the frontmatter.
|
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
- ValueError: If the README does not contain valid frontmatter.
|
- ClanError: If the toml frontmatter is invalid
|
||||||
"""
|
"""
|
||||||
# Pattern to match YAML frontmatter enclosed by triple-dashed lines
|
# Pattern to match YAML frontmatter enclosed by triple-dashed lines
|
||||||
frontmatter_pattern = r"^---\s+(.*?)\s+---\s?+(.*)$"
|
frontmatter_pattern = r"^---\s+(.*?)\s+---\s?+(.*)$"
|
||||||
@@ -92,11 +85,32 @@ def extract_frontmatter(readme_content: str, err_scope: str) -> tuple[Frontmatte
|
|||||||
msg = f"Error parsing TOML frontmatter: {e}"
|
msg = f"Error parsing TOML frontmatter: {e}"
|
||||||
raise ClanError(
|
raise ClanError(
|
||||||
msg,
|
msg,
|
||||||
description=f"Invalid TOML frontmatter. {err_scope}",
|
description="Invalid TOML frontmatter",
|
||||||
location="extract_frontmatter",
|
location="extract_frontmatter",
|
||||||
) from e
|
) from e
|
||||||
|
|
||||||
return Frontmatter(**frontmatter_parsed), remaining_content
|
return frontmatter_parsed, remaining_content
|
||||||
|
return None, readme_content
|
||||||
|
|
||||||
|
|
||||||
|
def extract_frontmatter(readme_content: str, err_scope: str) -> tuple[Frontmatter, str]:
|
||||||
|
"""
|
||||||
|
Extracts TOML frontmatter from a README file content.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- readme_content (str): The content of the README file as a string.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- str: The extracted frontmatter as a string.
|
||||||
|
- str: The content of the README file without the frontmatter.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
- ValueError: If the README does not contain valid frontmatter.
|
||||||
|
"""
|
||||||
|
frontmatter_raw, remaining_content = parse_frontmatter(readme_content)
|
||||||
|
|
||||||
|
if frontmatter_raw:
|
||||||
|
return Frontmatter(**frontmatter_raw), remaining_content
|
||||||
|
|
||||||
# If no frontmatter is found, raise an error
|
# If no frontmatter is found, raise an error
|
||||||
msg = "Invalid README: Frontmatter not found."
|
msg = "Invalid README: Frontmatter not found."
|
||||||
|
|||||||
Reference in New Issue
Block a user