sync utils io module to front_matter

This commit is contained in:
tmshv committed 2020-03-10 17:24:11 +03:00
1 parent 6a5dffe567
commit 112496fd1e
2 files changed
+5 -12

No files matched your search

+1 -1
View File
@@ -10,7 +10,7 @@ from sync.models import Model
from sync import create_date, title_from_html, create_post
from sync.models.Image import Image
from utils.hash import hash_str
from utils.io import parse_yaml_front_matter
from utils.front_matter import parse_yaml_front_matter
from utils.text.transform import url_encode_text
logger = logging.getLogger(settings.name + '.Page')
@@ -1,12 +1,9 @@
import re
import yaml
FM_BOUNDARY = re.compile(r'^-{3,}', re.MULTILINE)
def parse_yaml_front_matter(data: str):
fm, content = split_yaml_front_matter(data)
fm = fm if fm else {}
@@ -16,7 +13,7 @@ def parse_yaml_front_matter(data: str):
}
def split_yaml_front_matter(text, **defaults):
def split_yaml_front_matter(text: str):
"""
Parse text with YAML frontmatter, return metadata and content.
Pass in optional metadata defaults as keyword args.
@@ -25,19 +22,15 @@ def split_yaml_front_matter(text, **defaults):
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
return None, None
# parse yaml, now that we have frontmatter
fm = yaml.safe_load(fm)
if isinstance(fm, dict):
metadata.update(fm)
return metadata, content.strip()
return fm, content.strip()
return None, None