Commit c65650fc authored by Ingo Meyer's avatar Ingo Meyer

Merge branch 'develop'

parents e4b8af52 1315d901
__version_info__ = (0, 1, 3)
__version_info__ = (0, 1, 4)
__version__ = '.'.join(map(str, __version_info__))
import base64
import requests
import urlparse
import sys
from io import BytesIO
from PIL import Image
from docutils import nodes
from docutils.parsers.rst import Directive
......@@ -8,10 +11,23 @@ from docutils.nodes import fully_normalize_name, whitespace_normalize_name
from docutils.parsers.rst.roles import set_classes
def convert_png_to_html_base64(png_filepath):
with open(png_filepath.encode(sys.getfilesystemencoding()), "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
return "data:image/png;base64," + encoded_string
def is_url(url):
return urlparse.urlparse(url).scheme not in ("", "file")
def convert_png_to_pillow_image_and_html_base64(png_filepath_or_url):
if is_url(png_filepath_or_url):
png_url = png_filepath_or_url
response = requests.get(png_url)
response.raise_for_status()
image_content = response.content
else:
png_filepath = png_filepath_or_url
with open(png_filepath.encode(sys.getfilesystemencoding()), "rb") as image_file:
image_content = image_file.read()
image = Image.open(BytesIO(image_content))
encoded_string = "data:image/png;base64," + base64.b64encode(image_content)
return image, encoded_string
class EmbeddedImage(Directive):
......@@ -40,15 +56,11 @@ class EmbeddedImage(Directive):
% (self.name, self.options["align"], '", "'.join(self.align_values))
)
messages = []
reference = directives.uri(convert_png_to_html_base64(image_filepath))
image, encoded_string = convert_png_to_pillow_image_and_html_base64(image_filepath)
reference = directives.uri(encoded_string)
self.options["uri"] = reference
if not any(attr in self.options for attr in ("height", "width", "scale")):
try:
image = Image.open(image_filepath.encode(sys.getfilesystemencoding()))
except (IOError, UnicodeEncodeError):
pass # TODO: warn?
else:
self.options["width"] = "{}px".format(image.size[0])
self.options["width"] = "{}px".format(image.size[0])
reference_node = None
if "target" in self.options:
block = states.escape2null(self.options["target"]).splitlines()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment