Skip to content

Commit

Permalink
improve support for PageBlockEmbed
Browse files Browse the repository at this point in the history
  • Loading branch information
adbenitez committed Oct 23, 2022
1 parent 0e1ce47 commit 0b4dd88
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
4 changes: 2 additions & 2 deletions simplebot_tgchan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from telethon.tl.functions.messages import ImportChatInviteRequest
from telethon.tl.types import PeerChannel

from .instantview import blocks2html
from .instantview import page2html
from .orm import Channel, Subscription, init, session_scope
from .subcommands import login
from .util import get_client, getdefault, set_config, sync
Expand Down Expand Up @@ -230,7 +230,7 @@ async def tg2dc(bot: DeltaBot, client: TelegramClient, msg, dbchan: Channel) ->
if msg.file and msg.file.size <= int(getdefault(bot, "max_size")):
args["filename"] = await msg.download_media(tempdir)
if msg.web_preview and msg.web_preview.cached_page:
args["html"] = await blocks2html(
args["html"] = await page2html(
msg.web_preview.cached_page.blocks,
client=client,
msg=msg,
Expand Down
25 changes: 22 additions & 3 deletions simplebot_tgchan/instantview.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,19 @@ async def _download_image(logger, client, img) -> str:
return ""


async def blocks2html(blocks: list, client=None, msg=None, logger=None) -> str:
async def page2html(blocks: list, client=None, msg=None, logger=None) -> str:
html_text = await blocks2html(blocks, client=client, msg=msg, logger=logger)
return (
'<!DOCTYPE html><html><meta charset="UTF-8">'
'<meta name="viewport" content="width=device-width, initial-scale=1.0">'
f"</head><body>{html_text}</body></html>"
)


async def blocks2html(blocks: list, **kwargs) -> str:
html_text = ""
for block in blocks:
html_text += await block2html(block, client=client, msg=msg, logger=logger)
html_text += await block2html(block, **kwargs)
return html_text


Expand Down Expand Up @@ -246,7 +255,17 @@ async def PageBlockEmbedPost2HTML(block, **kwargs) -> str:


async def PageBlockEmbed2HTML(block, **kwargs) -> str: # noqa
return f'<video controls><source src="{block.url}"></video>' if block.url else ""
src = f'src="{block.url}"' if block.url else ""
srcdoc = f'srcdoc="{html.escape(block.html)}"' if block.html else ""
title = await block2html(block.caption, **kwargs)
style = ""
if block.full_width:
style += "width:100%;"
elif block.w:
style += f"width:{block.w};"
if block.h:
style += f"height:{block.h};"
return f'<iframe {src} {srcdoc} title="{title}" style="{style}"></iframe>'


async def PageBlockAuthorDate2HTML(block, **kwargs) -> str:
Expand Down

0 comments on commit 0b4dd88

Please # to comment.