Compare commits
No commits in common. "c322d6147a2ee7f59c59a565788b230a18f971ea" and "87b6b3362db84884fe967702f8c843ec477c589a" have entirely different histories.
c322d6147a
...
87b6b3362d
@ -1,5 +1,4 @@
|
|||||||
import re
|
import re
|
||||||
from urllib.parse import urlparse, parse_qs
|
|
||||||
from types import SimpleNamespace
|
from types import SimpleNamespace
|
||||||
|
|
||||||
import mimetypes
|
import mimetypes
|
||||||
@ -91,59 +90,16 @@ def set_link_target_blank(text):
|
|||||||
def embed_youtube(text):
|
def embed_youtube(text):
|
||||||
soup = BeautifulSoup(text, "html.parser")
|
soup = BeautifulSoup(text, "html.parser")
|
||||||
for element in soup.find_all("a"):
|
for element in soup.find_all("a"):
|
||||||
# Check if the link is a YouTube link
|
if element.attrs["href"].startswith("https://www.you"):
|
||||||
url = urlparse(element["href"])
|
video_name = element.attrs["href"].split("/")[-1]
|
||||||
if (
|
if "v=" in element.attrs["href"]:
|
||||||
url.hostname in ["www.youtu.be", "youtu.be"]
|
video_name = element.attrs["href"].split("?v=")[1].split("&")[0]
|
||||||
or url.hostname
|
# if "si=" in element.attrs["href"]:
|
||||||
in [
|
# video_name = "?v=" + element.attrs["href"].split("/")[-1]
|
||||||
"www.youtube.com",
|
# if "t=" in element.attrs["href"]:
|
||||||
"youtube.com",
|
# video_name += "&t=" + element.attrs["href"].split("&t=")[1].split("&")[0]
|
||||||
"www.youtube-nocookie.com",
|
embed_template = f'<iframe width="560" height="315" style="display:block" src="https://www.youtube.com/embed/{video_name}" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>'
|
||||||
"youtube-nocookie.com",
|
|
||||||
]
|
|
||||||
and any(url.path.startswith(p) for p in ["/watch", "/embed"])
|
|
||||||
):
|
|
||||||
queries = parse_qs(url.query)
|
|
||||||
if "v" in queries:
|
|
||||||
video_name = queries["v"][0]
|
|
||||||
else:
|
|
||||||
video_name = url.path.split("/")[-1]
|
|
||||||
|
|
||||||
queries.pop("v", None)
|
|
||||||
|
|
||||||
start_time = queries.get("t", None)
|
|
||||||
if start_time:
|
|
||||||
queries.pop("t", None)
|
|
||||||
queries["start"] = []
|
|
||||||
for t in start_time:
|
|
||||||
if t.endswith("s"):
|
|
||||||
t = start_time[:-1]
|
|
||||||
if t.isdigit():
|
|
||||||
queries["start"].append(t)
|
|
||||||
else:
|
|
||||||
queries["start"].append(
|
|
||||||
str(
|
|
||||||
sum(
|
|
||||||
int(x) * 60**i
|
|
||||||
for i, x in enumerate(reversed(t.split(":")))
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
new_queries = "&".join(
|
|
||||||
[f"{key}={v}" for key, value in queries.items() for v in value]
|
|
||||||
)
|
|
||||||
|
|
||||||
base_url = (
|
|
||||||
"youtube-nocookie.com"
|
|
||||||
if "youtube-nocookie" in url.hostname
|
|
||||||
else "youtube.com"
|
|
||||||
)
|
|
||||||
|
|
||||||
embed_template = f'<iframe width="560" height="315" style="display:block" src="https://www.{base_url}/embed/{video_name}?{new_queries}" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>'
|
|
||||||
element.replace_with(BeautifulSoup(embed_template, "html.parser"))
|
element.replace_with(BeautifulSoup(embed_template, "html.parser"))
|
||||||
|
|
||||||
return str(soup)
|
return str(soup)
|
||||||
|
|
||||||
|
|
||||||
@ -152,41 +108,35 @@ def embed_image(text):
|
|||||||
for element in soup.find_all("a"):
|
for element in soup.find_all("a"):
|
||||||
file_mime = mimetypes.guess_type(element.attrs["href"])[0]
|
file_mime = mimetypes.guess_type(element.attrs["href"])[0]
|
||||||
|
|
||||||
if (
|
if file_mime and file_mime.startswith("image/") or any(
|
||||||
file_mime
|
ext in element.attrs["href"].lower() for ext in [
|
||||||
and file_mime.startswith("image/")
|
".png",
|
||||||
or any(
|
".jpg",
|
||||||
ext in element.attrs["href"].lower()
|
".jpeg",
|
||||||
for ext in [
|
".gif",
|
||||||
".png",
|
".webp",
|
||||||
".jpg",
|
".svg",
|
||||||
".jpeg",
|
".bmp",
|
||||||
".gif",
|
".tiff",
|
||||||
".webp",
|
".ico",
|
||||||
".svg",
|
".heif",
|
||||||
".bmp",
|
".heic",
|
||||||
".tiff",
|
]
|
||||||
".ico",
|
|
||||||
".heif",
|
|
||||||
".heic",
|
|
||||||
]
|
|
||||||
)
|
|
||||||
):
|
):
|
||||||
embed_template = f'<img src="{element.attrs["href"]}" title="{element.attrs["href"]}?width=420" alt="{element.attrs["href"]}" />'
|
embed_template = f'<img src="{element.attrs["href"]}" title="{element.attrs["href"]}?width=420" alt="{element.attrs["href"]}" />'
|
||||||
element.replace_with(BeautifulSoup(embed_template, "html.parser"))
|
element.replace_with(BeautifulSoup(embed_template, "html.parser"))
|
||||||
return str(soup)
|
return str(soup)
|
||||||
|
|
||||||
|
|
||||||
def enrich_image_rendering(text):
|
def enrich_image_rendering(text):
|
||||||
soup = BeautifulSoup(text, "html.parser")
|
soup = BeautifulSoup(text, "html.parser")
|
||||||
for element in soup.find_all("img"):
|
for element in soup.find_all("img"):
|
||||||
if element.attrs["src"].startswith("/"):
|
if element.attrs["src"].startswith("/" ):
|
||||||
element.attrs["src"] += "?width=240&height=240"
|
element.attrs["src"] += "?width=240&height=240"
|
||||||
picture_template = f'''
|
picture_template = f'''
|
||||||
<picture>
|
<picture>
|
||||||
<source srcset="{element.attrs["src"]}" type="{mimetypes.guess_type(element.attrs["src"])[0]}" />
|
<source srcset="{element.attrs["src"]}" type="{mimetypes.guess_type(element.attrs["src"])[0]}" />
|
||||||
<source srcset="{element.attrs["src"]}&format=webp" type="image/webp" />
|
<source srcset="{element.attrs["src"]}" type="image/webp" />
|
||||||
<img src="{element.attrs["src"]}&format=png" title="{element.attrs["src"]}" alt="{element.attrs["src"]}" />
|
<img src="{element.attrs["src"]}" title="{element.attrs["src"]}" alt="{element.attrs["src"]}" />
|
||||||
</picture>'''
|
</picture>'''
|
||||||
element.replace_with(BeautifulSoup(picture_template, "html.parser"))
|
element.replace_with(BeautifulSoup(picture_template, "html.parser"))
|
||||||
return str(soup)
|
return str(soup)
|
||||||
@ -295,6 +245,7 @@ class PythonExtension(Extension):
|
|||||||
).set_lineno(line_number)
|
).set_lineno(line_number)
|
||||||
|
|
||||||
def _to_html(self, md_file, caller):
|
def _to_html(self, md_file, caller):
|
||||||
|
|
||||||
def fn(source):
|
def fn(source):
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import mimetypes
|
import mimetypes
|
||||||
from os.path import isfile
|
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import pillow_heif.HeifImagePlugin
|
import pillow_heif.HeifImagePlugin
|
||||||
@ -18,9 +17,6 @@ class ChannelAttachmentView(BaseView):
|
|||||||
relative_url=relative_path
|
relative_url=relative_path
|
||||||
)
|
)
|
||||||
|
|
||||||
if not channel_attachment or not isfile(channel_attachment["path"]):
|
|
||||||
return web.HTTPNotFound()
|
|
||||||
|
|
||||||
original_format = mimetypes.guess_type(channel_attachment["path"])[0]
|
original_format = mimetypes.guess_type(channel_attachment["path"])[0]
|
||||||
format_ = self.request.query.get("format")
|
format_ = self.request.query.get("format")
|
||||||
width = self.request.query.get("width")
|
width = self.request.query.get("width")
|
||||||
@ -79,7 +75,7 @@ class ChannelAttachmentView(BaseView):
|
|||||||
|
|
||||||
setattr(response, "write", sync_writer)
|
setattr(response, "write", sync_writer)
|
||||||
|
|
||||||
image.save(response, format=format_, quality=100, optimize=True, save_all=True)
|
image.save(response, format=format_)
|
||||||
|
|
||||||
setattr(response, "write", naughty_steal)
|
setattr(response, "write", naughty_steal)
|
||||||
|
|
||||||
@ -92,7 +88,6 @@ class ChannelAttachmentView(BaseView):
|
|||||||
response.headers["Content-Disposition"] = (
|
response.headers["Content-Disposition"] = (
|
||||||
f'attachment; filename="{channel_attachment["name"]}"'
|
f'attachment; filename="{channel_attachment["name"]}"'
|
||||||
)
|
)
|
||||||
response.headers["Content-Type"] = original_format
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
async def post(self):
|
async def post(self):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user