Commit 5c0015cd authored by Timothy J. Baek's avatar Timothy J. Baek
Browse files

fix: frontmatter

parent abf212c2
...@@ -10,17 +10,32 @@ def extract_frontmatter(file_path): ...@@ -10,17 +10,32 @@ def extract_frontmatter(file_path):
Extract frontmatter as a dictionary from the specified file path. Extract frontmatter as a dictionary from the specified file path.
""" """
frontmatter = {} frontmatter = {}
frontmatter_pattern = re.compile(r"^([a-z_]+):\s*(.*)\s*$", re.IGNORECASE) frontmatter_started = False
frontmatter_ended = False
frontmatter_pattern = re.compile(r"^\s*([a-z_]+):\s*(.*)\s*$", re.IGNORECASE)
try:
with open(file_path, "r", encoding="utf-8") as file: with open(file_path, "r", encoding="utf-8") as file:
for line in file: for line in file:
if line.strip() == '"""': if '"""' in line:
# End of frontmatter section if not frontmatter_started:
frontmatter_started = True
continue # skip the line with the opening triple quotes
else:
frontmatter_ended = True
break break
if frontmatter_started and not frontmatter_ended:
match = frontmatter_pattern.match(line) match = frontmatter_pattern.match(line)
if match: if match:
key, value = match.groups() key, value = match.groups()
frontmatter[key] = value frontmatter[key.strip()] = value.strip()
except FileNotFoundError:
print(f"Error: The file {file_path} does not exist.")
return {}
except Exception as e:
print(f"An error occurred: {e}")
return {}
return frontmatter return frontmatter
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment