-
Notifications
You must be signed in to change notification settings - Fork 14
/
markdown_utils.py
80 lines (64 loc) · 2.46 KB
/
markdown_utils.py
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
73
74
75
76
77
78
79
80
import math
import os
import shutil
from typing import Any, Dict
import mistune
from mistune.renderers.markdown import MarkdownRenderer
class HeadingsRenderer(MarkdownRenderer):
def __init__(self, min_level: int):
super().__init__()
self.min_level = min_level
self.initial_level = math.inf
def heading(self, token: Dict[str, Any], state: Any):
if self.initial_level == math.inf:
self.initial_level = token["attrs"]["level"]
token["attrs"]["level"] = self.min_level + max(
token["attrs"]["level"] - self.initial_level, 0
)
return super().heading(token, state)
def limit_markdown_headings(source: str, min_level: int) -> str:
markdown = mistune.create_markdown(renderer=HeadingsRenderer(min_level))
return markdown(source)
def unescape_braces(text: str):
return text.replace("%7B", "{").replace("%7D", "}")
class ImagePathRewriterRenderer(MarkdownRenderer):
def __init__(self, prefix: str):
super().__init__()
self.prefix = prefix
def image(self, token, state):
url = token["attrs"]["url"]
if "%7B" in url:
pass
elif "://" not in url and not url.startswith("/"):
url = os.path.join(self.prefix, url)
elif ".." in url:
url = ""
elif url.startswith("/"):
url = os.path.join(self.prefix, "../" + url[1:])
token["attrs"]["url"] = url
return super().image(token, state)
def rewrite_image_paths(source: str, prefix: str) -> str:
markdown = mistune.create_markdown(renderer=ImagePathRewriterRenderer(prefix))
return unescape_braces(markdown(source))
class WebsiteImagePathRewriterRenderer(MarkdownRenderer):
def __init__(self, source_dir: str, target_dir: str):
super().__init__()
self.source_dir = source_dir
self.target_dir = target_dir
def image(self, token, state):
url = token["attrs"]["url"]
if "%7B" in url:
pass
elif ".." in url:
url = ""
elif "://" not in url and not url.startswith("/"):
filename = os.path.basename(url)
if url.startswith("/"):
url = "../" + url[1:]
shutil.copyfile(
os.path.join(self.source_dir, url),
os.path.join(self.target_dir, filename),
)
url = f"images/{filename}"
token["attrs"]["url"] = url
return super().image(token, state)