mirror of
https://github.com/open-webui/open-webui
synced 2025-02-19 19:38:52 +00:00
fix: frontmatter
This commit is contained in:
parent
abf212c28f
commit
5c0015cd66
@ -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)
|
||||||
|
|
||||||
with open(file_path, "r", encoding="utf-8") as file:
|
try:
|
||||||
for line in file:
|
with open(file_path, "r", encoding="utf-8") as file:
|
||||||
if line.strip() == '"""':
|
for line in file:
|
||||||
# End of frontmatter section
|
if '"""' in line:
|
||||||
break
|
if not frontmatter_started:
|
||||||
match = frontmatter_pattern.match(line)
|
frontmatter_started = True
|
||||||
if match:
|
continue # skip the line with the opening triple quotes
|
||||||
key, value = match.groups()
|
else:
|
||||||
frontmatter[key] = value
|
frontmatter_ended = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if frontmatter_started and not frontmatter_ended:
|
||||||
|
match = frontmatter_pattern.match(line)
|
||||||
|
if match:
|
||||||
|
key, value = match.groups()
|
||||||
|
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
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user