| 1 |
# Meta Data Extension for Python-Markdown |
| 2 |
# ======================================= |
| 3 |
|
| 4 |
# This extension adds Meta Data handling to markdown. |
| 5 |
|
| 6 |
# See https://Python-Markdown.github.io/extensions/meta_data |
| 7 |
# for documentation. |
| 8 |
|
| 9 |
# Original code Copyright 2007-2008 [Waylan Limberg](https://github.com/waylan). |
| 10 |
|
| 11 |
# All changes Copyright 2008-2014 The Python Markdown Project |
| 12 |
|
| 13 |
# License: [BSD](https://opensource.org/licenses/bsd-license.php) |
| 14 |
|
| 15 |
""" |
| 16 |
This extension adds Meta Data handling to markdown. |
| 17 |
|
| 18 |
See the [documentation](https://Python-Markdown.github.io/extensions/meta_data) |
| 19 |
for details. |
| 20 |
""" |
| 21 |
|
| 22 |
from __future__ import annotations |
| 23 |
|
| 24 |
from . import Extension |
| 25 |
from ..preprocessors import Preprocessor |
| 26 |
import re |
| 27 |
import logging |
| 28 |
from typing import Any |
| 29 |
|
| 30 |
log = logging.getLogger('MARKDOWN') |
| 31 |
|
| 32 |
# Global Vars |
| 33 |
META_RE = re.compile(r'^[ ]{0,3}(?P<key>[A-Za-z0-9_-]+):\s*(?P<value>.*)') |
| 34 |
META_MORE_RE = re.compile(r'^[ ]{4,}(?P<value>.*)') |
| 35 |
BEGIN_RE = re.compile(r'^-{3}(\s.*)?') |
| 36 |
END_RE = re.compile(r'^(-{3}|\.{3})(\s.*)?') |
| 37 |
|
| 38 |
|
| 39 |
class MetaExtension (Extension): |
| 40 |
""" Meta-Data extension for Python-Markdown. """ |
| 41 |
|
| 42 |
def extendMarkdown(self, md): |
| 43 |
""" Register the processor. |
| 44 |
|
| 45 |
| Class Instance | Registry | Name | Priority | |
| 46 |
| --------------------------------------------------------------- | ------------------------------------------------------------- | ------ | :------: | |
| 47 |
| [`MetaPreprocessor`][markdown.extensions.meta.MetaPreprocessor] | [`preprocessors`][markdown.preprocessors.build_preprocessors] | `meta` | `27` | |
| 48 |
|
| 49 |
""" |
| 50 |
# flake8: noqa: E501 45-47 |
| 51 |
md.registerExtension(self) |
| 52 |
self.md = md |
| 53 |
md.preprocessors.register(MetaPreprocessor(md), 'meta', 27) |
| 54 |
|
| 55 |
def reset(self) -> None: |
| 56 |
self.md.Meta = {} |
| 57 |
|
| 58 |
|
| 59 |
class MetaPreprocessor(Preprocessor): |
| 60 |
""" Get Meta-Data. """ |
| 61 |
|
| 62 |
def run(self, lines: list[str]) -> list[str]: |
| 63 |
""" Parse Meta-Data and store in Markdown.Meta. """ |
| 64 |
meta: dict[str, Any] = {} |
| 65 |
key = None |
| 66 |
if lines and BEGIN_RE.match(lines[0]): |
| 67 |
lines.pop(0) |
| 68 |
while lines: |
| 69 |
line = lines.pop(0) |
| 70 |
m1 = META_RE.match(line) |
| 71 |
if line.strip() == '' or END_RE.match(line): |
| 72 |
break # blank line or end of YAML header - done |
| 73 |
if m1: |
| 74 |
key = m1.group('key').lower().strip() |
| 75 |
value = m1.group('value').strip() |
| 76 |
try: |
| 77 |
meta[key].append(value) |
| 78 |
except KeyError: |
| 79 |
meta[key] = [value] |
| 80 |
else: |
| 81 |
m2 = META_MORE_RE.match(line) |
| 82 |
if m2 and key: |
| 83 |
# Add another line to existing key |
| 84 |
meta[key].append(m2.group('value').strip()) |
| 85 |
else: |
| 86 |
lines.insert(0, line) |
| 87 |
break # no meta data - done |
| 88 |
self.md.Meta = meta |
| 89 |
return lines |
| 90 |
|
| 91 |
|
| 92 |
def makeExtension(**kwargs): # pragma: no cover |
| 93 |
return MetaExtension(**kwargs) |