-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmeta_compat.py
40 lines (30 loc) · 1.37 KB
/
meta_compat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from itertools import takewhile
import operator
from flask_flatpages import FlatPages
from flask_flatpages.page import Page
from werkzeug.utils import import_string
class MetaCompat(FlatPages):
def _parse(self, content, path, _rel_path):
"""Parse a flatpage file with support for --- yaml delimiters
"""
lines = iter(content.split('\n'))
# Read lines until an empty line is encountered.
meta_lines = list(takewhile(operator.methodcaller('strip'), lines))
# Handle ---s compatibl with Jekyl, github
if meta_lines[0] == '---':
meta_lines = meta_lines[1:]
if meta_lines[-1] == '---':
meta_lines = meta_lines[:-1]
meta = '\n'.join(meta_lines)
# The rest is the content. `lines` is an iterator so it continues
# where `itertools.takewhile` left it.
content = '\n'.join(lines)
# Now we ready to get HTML renderer function
html_renderer = self.config('html_renderer')
# If function is not callable yet, import it
if not callable(html_renderer):
html_renderer = import_string(html_renderer)
# Make able to pass custom arguments to renderer function
html_renderer = self._smart_html_renderer(html_renderer)
# Initialize and return Page instance
return Page(path, meta, content, html_renderer, folder='')