-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path__init__.py
More file actions
57 lines (44 loc) · 1.48 KB
/
__init__.py
File metadata and controls
57 lines (44 loc) · 1.48 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
This package contains common utilities for markdown used across the various plugins
for Markdown.
"""
import re
_PROPS_RE = re.compile(
r"""\s?((?P<name>[^\s\=]+)=(?P<quot>"|')(?P<value>[^\"\']+)(?P=quot))""",
re.DOTALL | re.MULTILINE,
)
def parse_props(
line: str, prefix: str = "", bool_attrs: bool = False
) -> dict[str, str]:
"""
Parses a line describing properties, in this form:
prop1="value1" prop2="value2"
It also parses boolean attributes, if bool_attrs is set to true:
id="example" class="foo another" multiple
into a dictionary of names and values. It accepts an optional prefix, to filter
properties names.
"""
props = {}
for match in _PROPS_RE.findall(line):
name: str = match[1]
value: str = match[3]
if prefix:
if name.startswith(prefix):
props[name[len(prefix) :]] = value
else:
props[name] = value
if bool_attrs:
for word in _PROPS_RE.sub("", line).split():
if "=" in word:
name, value = word.split("=")
props[name] = value
else:
props[word] = True
return props
def extract_props(line: str, prefix: str = "") -> tuple[str, dict[str, str]]:
"""
Extracts properties like the `parse_props` function, but
also returns the original line with properties removed.
"""
props = parse_props(line, prefix)
return _PROPS_RE.sub("", line).strip(), props