Skip to content

Fixed PNG syntax error. Fixed issue with .jpg filenames #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/jupyter_to_medium/_publish_to_medium.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import re
import urllib.parse
from pathlib import Path
from typing import Optional

import requests
import nbformat
Expand Down Expand Up @@ -170,13 +172,27 @@ def create_markdown(self):
self.resources.pop("converter")
me = MarkdownExporter()
md, self.resources = me.from_notebook_node(self.nb, self.resources)
md = self.fix_jpeg_ext(md)

image_data_dict = {
**self.resources["image_data_dict"],
**self.resources["outputs"],
}
return md, image_data_dict

def fix_jpeg_ext(self, md: Optional[str] = None):
# Medium gives errors if our file names end with .jpg and not .jpeg
for k in ["image_data_dict", "outputs"]:
d = self.resources[k]
file_names = list(d.keys())
for file_name in file_names:
if file_name.endswith(".jpg"):
d[file_name.replace(".jpg", ".jpeg")] = d.pop(file_name)
if md is not None:
md = re.sub(r"(!\[jpeg])\((.*)\.jpg\)", r"\1(\2.jpeg)", md)

return md

def gistify_markdown(self):
if self.gistify:
# list-ify the markdown --> ``` elements identify code blocks
Expand Down
21 changes: 10 additions & 11 deletions src/jupyter_to_medium/_screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,25 +111,24 @@ def take_screenshot(self):
with open(temp_html, "w") as f:
f.write(self.html)

with open(temp_img, "wb") as f:
args = ["--enable-logging", "--disable-gpu", "--headless"]
args = ["--enable-logging", "--disable-gpu", "--headless"]

if self.ss_width and self.ss_height:
args.append(f"--window-size={self.ss_width},{self.ss_height}")
if self.ss_width and self.ss_height:
args.append(f"--window-size={self.ss_width},{self.ss_height}")

args += [
"--hide-scrollbars",
f"--screenshot={str(temp_img)}",
str(temp_html),
]
args += [
"--hide-scrollbars",
f"--screenshot={str(temp_img)}",
str(temp_html),
]

subprocess.run(executable=self.chrome_path, args=args)
subprocess.run(executable=self.chrome_path, args=args)

with open(temp_img, "rb") as f:
img_bytes = f.read()

buffer = io.BytesIO(img_bytes)
img = mimage.imread(buffer)
img = mimage.imread(buffer, format="png")
return self.possibly_enlarge(img)

def possibly_enlarge(self, img):
Expand Down