| 1 |
""" |
| 2 |
Copyright (c) 2015, Waylan Limberg |
| 3 |
All rights reserved. |
| 4 |
|
| 5 |
Redistribution and use in source and binary forms, with or without modification, |
| 6 |
are permitted provided that the following conditions are met: |
| 7 |
|
| 8 |
1. Redistributions of source code must retain the above copyright notice, this |
| 9 |
list of conditions and the following disclaimer. |
| 10 |
|
| 11 |
2. Redistributions in binary form must reproduce the above copyright notice, this |
| 12 |
list of conditions and the following disclaimer in the documentation and/or other |
| 13 |
materials provided with the distribution. |
| 14 |
|
| 15 |
3. Neither the name of the copyright holder nor the names of its contributors may |
| 16 |
be used to endorse or promote products derived from this software without |
| 17 |
specific prior written permission. |
| 18 |
|
| 19 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 22 |
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 23 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 24 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 26 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 27 |
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 28 |
OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
|
| 30 |
MultiMarkdown Meta-Data |
| 31 |
|
| 32 |
Extracts, parses and transforms MultiMarkdown style data from documents. |
| 33 |
|
| 34 |
""" |
| 35 |
from __future__ import annotations |
| 36 |
|
| 37 |
import re |
| 38 |
from typing import Any |
| 39 |
|
| 40 |
import yaml |
| 41 |
|
| 42 |
try: |
| 43 |
from yaml import CSafeLoader as SafeLoader |
| 44 |
except ImportError: # pragma: no cover |
| 45 |
from yaml import SafeLoader # type: ignore |
| 46 |
|
| 47 |
##################################################################### |
| 48 |
# Data Parser # |
| 49 |
##################################################################### |
| 50 |
|
| 51 |
YAML_RE = re.compile(r'^-{3}[ \t]*\n(.*?\n)(?:\.{3}|-{3})[ \t]*\n', re.UNICODE | re.DOTALL) |
| 52 |
META_RE = re.compile(r'^[ ]{0,3}(?P<key>[A-Za-z0-9_-]+):\s*(?P<value>.*)') |
| 53 |
META_MORE_RE = re.compile(r'^([ ]{4}|\t)(\s*)(?P<value>.*)') |
| 54 |
|
| 55 |
|
| 56 |
def get_data(doc: str) -> tuple[str, dict[str, Any]]: |
| 57 |
""" |
| 58 |
Extract meta-data from a text document. |
| 59 |
|
| 60 |
Returns a tuple of document and a data dict. |
| 61 |
""" |
| 62 |
data = {} |
| 63 |
|
| 64 |
# First try YAML |
| 65 |
if m := YAML_RE.match(doc): |
| 66 |
try: |
| 67 |
data = yaml.load(m.group(1), SafeLoader) |
| 68 |
if isinstance(data, dict): |
| 69 |
doc = doc[m.end() :].lstrip('\n') |
| 70 |
else: |
| 71 |
data = {} # type: ignore[unreachable] |
| 72 |
except Exception: |
| 73 |
pass |
| 74 |
return doc, data |
| 75 |
|
| 76 |
# No YAML delimiters. Try MultiMarkdown style |
| 77 |
lines = doc.replace('\r\n', '\n').replace('\r', '\n').split('\n') |
| 78 |
|
| 79 |
key = None |
| 80 |
while lines: |
| 81 |
line = lines.pop(0) |
| 82 |
|
| 83 |
if line.strip() == '': |
| 84 |
break # blank line - done |
| 85 |
if m1 := META_RE.match(line): |
| 86 |
key = m1.group('key').lower().strip() |
| 87 |
value = m1.group('value').strip() |
| 88 |
if key in data: |
| 89 |
data[key] += f' {value}' |
| 90 |
else: |
| 91 |
data[key] = value |
| 92 |
else: |
| 93 |
m2 = META_MORE_RE.match(line) |
| 94 |
if m2 and key: |
| 95 |
# Add another line to existing key |
| 96 |
data[key] += ' {}'.format(m2.group('value').strip()) |
| 97 |
else: |
| 98 |
lines.insert(0, line) |
| 99 |
break # no meta data - done |
| 100 |
return '\n'.join(lines).lstrip('\n'), data |