Commit 613de438 authored by Ingo Heimbach's avatar Ingo Heimbach

Merge branch 'develop'

parents 9f988a73 16844e60
__version_info__ = (0, 2, 1)
__version_info__ = (0, 2, 2)
__version__ = '.'.join(map(str, __version_info__))
......@@ -21,13 +21,16 @@ from .output_mode import get_output_mode
TMP_IMAGE_FILENAME = "_downloaded_image{i:04d}.{ext}"
TMP_QPDF_OUT_FILENAME = "_qpdf_out.pdf"
OUT_PDF_FILENAME = "_converted_image{i:04d}.pdf"
OUT_PNG_FILENAME = "_converted_image{i:04d}.png"
def is_url(url):
return urlparse.urlparse(url).scheme not in ("", "file")
def load_and_convert_image(image_filepath_or_url, convert_to_base64=True, convert_svg_to_pdf=False):
def load_and_convert_image(
image_filepath_or_url, convert_to_base64=True, convert_svg_to_pdf=False, convert_gif_to_png=False
):
self = load_and_convert_image
if not hasattr(self, "image_number"):
self.image_number = 1
......@@ -35,9 +38,12 @@ def load_and_convert_image(image_filepath_or_url, convert_to_base64=True, conver
image_content = None
image_filepath = None
image_is_pdf = None
image_is_png = None
image_is_svg = image_filepath_or_url.lower().endswith(".svg")
image_is_gif = image_filepath_or_url.lower().endswith(".gif")
image_will_be_converted = (image_is_svg and convert_svg_to_pdf) or (image_is_gif and convert_gif_to_png)
image_is_remote = is_url(image_filepath_or_url)
if image_is_svg and convert_svg_to_pdf and (image_is_remote or convert_to_base64):
if image_will_be_converted and (image_is_remote or convert_to_base64):
tmp_dir = tempfile.mkdtemp()
else:
tmp_dir = None
......@@ -46,8 +52,8 @@ def load_and_convert_image(image_filepath_or_url, convert_to_base64=True, conver
response = requests.get(image_url)
response.raise_for_status()
image_content = response.content
if not convert_to_base64 or (image_is_svg and convert_svg_to_pdf):
image_filepath = TMP_IMAGE_FILENAME.format(i=self.image_number, ext=os.path.splitext(image_url)[1])
if not convert_to_base64 or image_will_be_converted:
image_filepath = TMP_IMAGE_FILENAME.format(i=self.image_number, ext=os.path.splitext(image_url)[1][1:])
if tmp_dir is not None:
image_filepath = os.path.join(tmp_dir, image_filepath)
with open(image_filepath.encode(sys.getfilesystemencoding()), "wb") as image_file:
......@@ -55,28 +61,39 @@ def load_and_convert_image(image_filepath_or_url, convert_to_base64=True, conver
else:
image_filepath = image_filepath_or_url
try:
if image_is_svg and convert_svg_to_pdf:
image_content = None
out_pdf_filename = OUT_PDF_FILENAME.format(i=self.image_number)
if convert_to_base64:
out_pdf_filename = os.path.join(tmp_dir, out_pdf_filename)
with open(os.devnull, "w") as devnull:
subprocess.check_call(
["rsvg-convert", "-f", "pdf", "-o", out_pdf_filename, image_filepath],
stdout=devnull,
stderr=devnull,
)
try:
if image_will_be_converted:
if image_is_svg:
image_content = None
out_pdf_filename = OUT_PDF_FILENAME.format(i=self.image_number)
if convert_to_base64:
out_pdf_filename = os.path.join(tmp_dir, out_pdf_filename)
with open(os.devnull, "w") as devnull:
subprocess.check_call(
["qpdf", out_pdf_filename, TMP_QPDF_OUT_FILENAME], stdout=devnull, stderr=devnull
["rsvg-convert", "-f", "pdf", "-o", out_pdf_filename, image_filepath],
stdout=devnull,
stderr=devnull,
)
os.remove(out_pdf_filename)
shutil.move(TMP_QPDF_OUT_FILENAME, out_pdf_filename)
except subprocess.CalledProcessError:
pass
image_filepath = out_pdf_filename
image_is_svg = False
image_is_pdf = True
try:
subprocess.check_call(
["qpdf", out_pdf_filename, TMP_QPDF_OUT_FILENAME], stdout=devnull, stderr=devnull
)
os.remove(out_pdf_filename)
shutil.move(TMP_QPDF_OUT_FILENAME, out_pdf_filename)
except subprocess.CalledProcessError:
pass
image_filepath = out_pdf_filename
image_is_svg = False
image_is_pdf = True
elif image_is_gif:
image_content = None
out_png_filename = OUT_PNG_FILENAME.format(i=self.image_number)
if convert_to_base64:
out_png_filename = os.path.join(tmp_dir, out_png_filename)
with open(os.devnull, "w") as devnull:
subprocess.check_call(["convert", image_filepath, out_png_filename], stdout=devnull, stderr=devnull)
image_filepath = out_png_filename
image_is_gif = False
image_is_png = True
if image_content is None:
with open(image_filepath.encode(sys.getfilesystemencoding()), "rb") as image_file:
image_content = image_file.read()
......@@ -131,7 +148,10 @@ class EmbeddedImage(Directive):
messages = []
image = None
image, image_reference = load_and_convert_image(
image_filepath, convert_to_base64=is_output_mode_html, convert_svg_to_pdf=not is_output_mode_html
image_filepath,
convert_to_base64=is_output_mode_html,
convert_svg_to_pdf=not is_output_mode_html,
convert_gif_to_png=not is_output_mode_html,
)
reference = directives.uri(image_reference)
self.options["uri"] = reference
......
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