Skip to content
Merged
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
18 changes: 9 additions & 9 deletions down2.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,18 @@ def _shutdown_browser() -> None:
)


def convert(input_typ: str, input: str | bytes, output_typ: str, opt: dict[str, dict] | None = None) -> str | bytes:
def convert(input_typ: str, content: str | bytes, output_typ: str, opt: dict[str, dict] | None = None) -> str | bytes:
conv = mappings.get((input_typ, output_typ))
if conv is None:
raise ValueError(f"unsupported conversion: {input_typ} -> {output_typ}")
if callable(conv):
kwargs = {}
if opt is not None and input_typ in opt:
kwargs = opt[input_typ]
return conv(input, **kwargs)
return conv(content, **kwargs)
else:
# indirect convertion
inp = convert(input_typ, input, conv, opt=opt)
inp = convert(input_typ, content, conv, opt=opt)
return convert(conv, inp, output_typ, opt=opt)


Expand Down Expand Up @@ -322,7 +322,7 @@ def web_to_img(pagefn: str, typ: str) -> bytes:


def render_to_img(
mime: str, input: str | bytes, typ: str, width: int = 1000, height: int = 2000, asset_base: str | None = None
mime: str, content: str | bytes, typ: str, width: int = 1000, height: int = 2000, asset_base: str | None = None
) -> bytes:
"""
Render content that is renderable in a browser to image.
Expand All @@ -332,7 +332,7 @@ def render_to_img(
Args:
mime(str): a full mime type such as ``image/jpeg`` or a shortcut ``jpg``.

input(str): content of the input, such as jpeg data or svg source file.
content(str): content to render, such as jpeg data or svg source.

typ(string): specifies output image type such as "png", "jpg"

Expand All @@ -347,22 +347,22 @@ def render_to_img(
"""

if "html" in mime:
input = r'<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>' + input
content = r'<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>' + content

if asset_base is not None:
base_uri = pathlib.Path(asset_base).as_uri()
input = '<base href="{}/">'.format(base_uri) + input
content = '<base href="{}/">'.format(base_uri) + content

m = mimetypes.get(mime) or mime
suffix = mime_to_suffix.get(m, mime)

with tempfile.TemporaryDirectory() as tdir:
fn = os.path.join(tdir, "xxx." + suffix)
flags = "w"
if isinstance(input, bytes):
if isinstance(content, bytes):
flags = "wb"
with open(fn, flags) as f:
f.write(input)
f.write(content)

browser = _get_browser()
page = browser.new_page(
Expand Down