def run(self, lines):
""" Parse Meta-Data and store in Markdown.Meta. """
meta = {}
key = None
yaml_block = []
have_yaml = False
if lines and YAML_BEGIN_RE.match(lines[0]):
have_yaml = True
lines.pop(0)
if self.config['yaml'] and not yaml: # pragma: no cover
log.warning('Document with YAML header, but PyYAML unavailable')
while lines:
line = lines.pop(0)
m1 = META_RE.match(line)
if line.strip() == '' or have_yaml and YAML_END_RE.match(line):
break # blank line or end of YAML header - done
elif have_yaml and self.config['yaml'] and yaml:
yaml_block.append(line)
elif m1:
key = m1.group('key').lower().strip()
value = m1.group('value').strip()
try:
meta[key].append(value)
except KeyError:
meta[key] = [value]
else:
m2 = META_MORE_RE.match(line)
if m2 and key:
# Add another line to existing key
meta[key].append(m2.group('value').strip())
else:
lines.insert(0, line)
break # no meta data - done
if yaml_block:
meta = yaml.load('\n'.join(yaml_block), SafeLoader)
self.markdown.Meta = meta
return lines
评论列表
文章目录