sync util update
This commit is contained in:
1 parent
fe409c04a4
commit
261e842802
5 files changed
+134
-42
No files matched your search
+36
-12
@@ -1,8 +1,13 @@
|
||||
import re
|
||||
|
||||
import frontmatter
|
||||
import yaml
|
||||
from markdown import markdown
|
||||
|
||||
from utils.text.split import split, split_with
|
||||
|
||||
# FM_BOUNDARY = re.compile(r'^-{3,}$', re.MULTILINE)
|
||||
FM_BOUNDARY = re.compile(r'^-{3,}', re.MULTILINE)
|
||||
|
||||
def read_file(file):
|
||||
with open(file, 'rb') as f:
|
||||
@@ -23,19 +28,11 @@ def read_md(file):
|
||||
|
||||
|
||||
def read_yaml_md(file):
|
||||
data = read_file(file)
|
||||
yaml_data, md_data = split_with(['\n\n', '\r\n\r\n'])(data)
|
||||
yaml, content = parse_yaml_front_matter(read_file(file))
|
||||
return yaml, markdown(content)
|
||||
|
||||
if not yaml_data and not md_data:
|
||||
return (read_yaml(file), None)
|
||||
|
||||
try:
|
||||
y = yaml.load(yaml_data)
|
||||
except:
|
||||
y = None
|
||||
|
||||
m = markdown(md_data) if md_data else None
|
||||
return y, m
|
||||
# data = frontmatter.load(file)
|
||||
# return data.metadata, markdown(data.content)
|
||||
|
||||
|
||||
def read_md_yaml(file):
|
||||
@@ -52,3 +49,30 @@ def read_md_yaml(file):
|
||||
|
||||
m = markdown(md_data) if md_data else None
|
||||
return y, m
|
||||
|
||||
|
||||
def parse_yaml_front_matter(text, **defaults):
|
||||
"""
|
||||
Parse text with YAML frontmatter, return metadata and content.
|
||||
Pass in optional metadata defaults as keyword args.
|
||||
|
||||
If frontmatter is not found, returns an empty metadata dictionary
|
||||
and original text content.
|
||||
"""
|
||||
|
||||
# metadata starts with defaults
|
||||
metadata = defaults.copy()
|
||||
|
||||
# split on the first two triple-dashes
|
||||
try:
|
||||
_, fm, content = FM_BOUNDARY.split(text, 2)
|
||||
except ValueError:
|
||||
# if we can't split, bail
|
||||
return metadata, text
|
||||
|
||||
# parse yaml, now that we have frontmatter
|
||||
fm = yaml.safe_load(fm)
|
||||
if isinstance(fm, dict):
|
||||
metadata.update(fm)
|
||||
|
||||
return metadata, content.strip()
|
||||
Reference in new issue
Block a user