Merge pull request #9885 from gavindsouza/feat-frontmatter

feat: handle YAML frontmatter via internal API
This commit is contained in:
mergify[bot] 2020-04-11 13:45:51 +00:00 committed by GitHub
commit b0452becc8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 12 deletions

View file

@ -2,13 +2,17 @@
# MIT License. See license.txt
from __future__ import unicode_literals
import frappe, os
from frappe.website.utils import (can_cache, delete_page_cache, extract_title,
extract_comment_tag)
from frappe.model.document import get_controller
from six import text_type
import io
import os
import re
import yaml
import frappe
from frappe.model.document import get_controller
from frappe.website.utils import can_cache, delete_page_cache, extract_comment_tag, extract_title
def resolve_route(path):
"""Returns the page route object based on searching in pages and generators.
@ -229,10 +233,26 @@ def get_page_info(path, app, start, basepath=None, app_path=None, fname=None):
return page_info
def get_frontmatter(string):
"""
Reference: https://github.com/jonbeebe/frontmatter
"""
fmatter = ""
body = ""
result = re.compile(r'^\s*(?:---|\+\+\+)(.*?)(?:---|\+\+\+)\s*(.+)$', re.S | re.M).search(string)
if result:
fmatter = result.group(1)
body = result.group(2)
return {
"attributes": yaml.safe_load(fmatter),
"body": body,
}
def setup_source(page_info):
'''Get the HTML source of the template'''
from frontmatter import Frontmatter
jenv = frappe.get_jenv()
source = jenv.loader.get_source(jenv, page_info.template)[0]
html = ''
@ -241,11 +261,11 @@ def setup_source(page_info):
# extract frontmatter block if exists
try:
# values will be used to update page_info
res = Frontmatter.read(source)
res = get_frontmatter(source)
if res['attributes']:
page_info.update(res['attributes'])
source = res['body']
except Exception as e:
except Exception:
pass
if page_info.template.endswith('.md'):
@ -367,4 +387,4 @@ def get_doctypes_with_web_view():
return frappe.cache().get_value('doctypes_with_web_view', _get)
def get_start_folders():
return frappe.local.flags.web_pages_folders or ('www', 'templates/pages')
return frappe.local.flags.web_pages_folders or ('www', 'templates/pages')

View file

@ -12,7 +12,6 @@ cryptography==2.8
dropbox==9.1.0
email-reply-parser==0.5.9
Faker==2.0.4
frontmatter==3.0.6
future==0.18.2
GitPython==2.1.15
gitdb2==2.0.6;python_version<'3.4'
@ -49,7 +48,7 @@ pypng==0.0.20
PyQRCode==1.2.1
python-dateutil==2.8.1
pytz==2019.3
PyYAML==5.1
PyYAML==5.3.1
rauth==0.7.3
redis>=3.0
requests-oauthlib==1.3.0