-
Notifications
You must be signed in to change notification settings - Fork 1
/
markdown_imaged.py
34 lines (26 loc) · 1.11 KB
/
markdown_imaged.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
import os.path
import urllib.parse
import requests
import rfc6266
import settings
import utilities
from markdown import Extension
from markdown.inlinepatterns import ImagePattern, IMAGE_LINK_RE
class ImageDownloadPattern(ImagePattern):
def handleMatch(self, match):
el = super(ImageDownloadPattern, self).handleMatch(match)
urlparts = urllib.parse.urlparse(el.attrib["src"])
if urlparts.netloc:
response = requests.get(urlparts.geturl())
response.raise_for_status()
filename = rfc6266.parse_requests_response(response).filename_unsafe
with open(os.path.join(settings.get("folder"), filename), "wb") as f:
f.write(response.content)
el.attrib["src"] = filename
utilities.fix_image(os.path.join(settings.get("folder"), filename), settings.get("features")["width"])
return el
class ImageDownload(Extension):
def extendMarkdown(self, md, md_globals):
md.inlinePatterns['image_link'] = ImageDownloadPattern(IMAGE_LINK_RE, md)
def makeExtension(configs={}):
return ImageDownload(configs=configs)