This repository was archived by the owner on Jun 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutil.py
More file actions
72 lines (57 loc) · 1.69 KB
/
util.py
File metadata and controls
72 lines (57 loc) · 1.69 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
General utilities
"""
import re
from urllib.parse import urlparse
from markdown.util import ETX, STX
from .error import CMS7Error
from .hyphenate import hyphenate_word
_NOTHING = object()
def meta_get_one(md, key, default=_NOTHING):
from .source import MarkdownSource
source = None
if isinstance(md, MarkdownSource):
source = md
md = md.meta
if key in md:
return md[key][0]
if default is not _NOTHING:
return default
if source:
raise CMS7Error('{!s} missing required metadata key: {!r}'.format(source.source, key))
else:
raise KeyError(key)
def is_relative_url(url):
url = urlparse(url)
if url.scheme or url.netloc or url.path.startswith('/'):
return False
if url.path == '':
return False
return True
WORD_RE = re.compile(r"[A-Za-z0-9'-]+")
def _hyphenate(text):
le = 0
quoted = False
for match in WORD_RE.finditer(text):
plain = text[le:match.start()]
le = match.end()
yield plain
"""
python-markdown uses STX...ETX pairs to demarcate magical processing
tokens. we avoid messing those up here by keeping track of which of
the two special characters we last saw.
we never have to deal with them inside the match, since WORD_RE can't
match them.
"""
stx, etx = plain.rfind(STX), plain.rfind(ETX)
if stx > etx:
quoted = True
elif etx > -1:
quoted = False
if not quoted:
yield '\u00ad'.join(hyphenate_word(match.group()))
else:
yield match.group()
yield text[le:]
def hyphenate(text):
return ''.join(_hyphenate(text))