Merge pull request 'Add support for embedded players' (#64) from BordedDev/snek:feat/support-player into main

Reviewed-on: #64
Reviewed-by: retoor <retoor@noreply@molodetz.nl>
This commit is contained in:
retoor 2025-07-06 01:00:13 +02:00
commit 30e8c17000

View File

@ -402,17 +402,17 @@ def embed_url(text):
)
page_image = (
get_element_options(head_info, None, "image", "image", "image")
or get_element_options(
head_info, None, "image:url", "image:url", "image:url"
)
or get_element_options(
get_element_options(
head_info,
None,
"image:secure_url",
"image:secure_url",
"image:secure_url",
)
or get_element_options(
head_info, None, "image:url", "image:url", "image:url"
)
or get_element_options(head_info, None, "image", "image", "image")
)
page_image_height = get_element_options(
head_info, None, "image:height", "image:height", "image:height"
@ -425,17 +425,17 @@ def embed_url(text):
)
page_video = (
get_element_options(head_info, None, "video", "video", "video")
or get_element_options(
head_info, None, "video:url", "video:url", "video:url"
)
or get_element_options(
get_element_options(
head_info,
None,
"video:secure_url",
"video:secure_url",
"video:secure_url",
)
or get_element_options(
head_info, None, "video:url", "video:url", "video:url"
)
or get_element_options(head_info, None, "video", "video", "video")
)
page_video_height = get_element_options(
head_info, None, "video:height", "video:height", "video:height"
@ -443,21 +443,28 @@ def embed_url(text):
page_video_width = get_element_options(
head_info, None, "video:width", "video:width", "video:width"
)
page_video_type = get_element_options(
head_info, None, "video:type", "video:type", "video:type"
)
page_audio = (
get_element_options(head_info, None, "audio", "audio", "audio")
or get_element_options(
head_info, None, "audio:url", "audio:url", "audio:url"
)
or get_element_options(
get_element_options(
head_info,
None,
"audio:secure_url",
"audio:secure_url",
"audio:secure_url",
)
or get_element_options(
head_info, None, "audio:url", "audio:url", "audio:url"
)
or get_element_options(head_info, None, "audio", "audio", "audio")
)
page_player = get_element_options(head_info, twitter="player")
page_player_width = get_element_options(head_info, twitter="player:width")
page_player_height = get_element_options(head_info, twitter="player:height")
(get_element_options(head_info, twitter="card") or "summary_large_image")
attachment_base = BeautifulSoup(str(element), "html.parser")
@ -470,7 +477,32 @@ def embed_url(text):
render_element = attachment
if page_image:
if page_player:
style = {
"width": page_player_width + "px" if page_player_width else None,
"height": f"{(page_player_height or '400')}px",
}
style_string = "; ".join(
f"{key}: {value}" for key, value in style.items() if value
)
player_template = f'<iframe src="{page_player}" style="{style_string}" title="{page_name}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>'
render_element.append(BeautifulSoup(player_template, "html.parser"))
elif page_video:
style = {
"width": page_video_width + "px" if page_video_width else None,
"height": f"{(page_video_height or '400')}px",
}
style_string = "; ".join(
f"{key}: {value}" for key, value in style.items() if value
)
if not page_video_type or page_video_type.startswith("video/"):
video_template = f'<video style="{style_string}" controls><source src="{page_video}" type="{page_video_type}">Your browser does not support the video tag.</video>'
else:
video_template = f'<iframe style="{style_string}" src="{page_video}" title="{page_name}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>'
render_element.append(BeautifulSoup(video_template, "html.parser"))
elif page_image:
style = {
"width": page_image_width + "px" if page_image_width else None,
"height": page_image_height + "px" if page_image_height else None,
@ -483,18 +515,6 @@ def embed_url(text):
image_template = f'<span><img src="{page_image}" alt="{page_image_alt or page_name}" title="{page_name}" width="1" height="1" style="{style_string}" /></span>'
render_element.append(BeautifulSoup(image_template, "html.parser"))
if page_video:
style = {
"width": page_video_width + "px" if page_video_width else None,
"height": page_video_height + "px" if page_video_height else None,
}
style_string = "; ".join(
f"{key}: {value}" for key, value in style.items() if value
)
video_template = f'<video controls style="{style_string}"><source src="{page_video}">Your browser does not support the video tag.</video>'
render_element.append(BeautifulSoup(video_template, "html.parser"))
if page_audio:
audio_template = f'<audio controls><source src="{page_audio}">Your browser does not support the audio tag.</audio>'
render_element.append(BeautifulSoup(audio_template, "html.parser"))
@ -608,4 +628,3 @@ class PythonExtension(Extension):
return "".join(to_write)
return str(fn(caller()))