def youtube(self, src, alt="", title=None): youtube_match = YOUTUBE_RE.match(src) video_tag = ( f'<span class="ratio-16-9">' f'<iframe src="https://www.youtube.com/embed/{escape_html(youtube_match.group(1))}' f'?autoplay=0&controls=1&showinfo=1&vq=hd1080" frameborder="0"></iframe>' f"</span>") caption = f"<figcaption>{escape_html(title)}</figcaption>" if title else "" return f"<figure>{video_tag}{caption}</figure>"
def link(self, link, text=None, title=None): if IMAGE_RE.match(link): return self.image(link, text or "", title or "") if YOUTUBE_RE.match(link): return self.youtube(link, text, title) if VIDEO_RE.match(link): return self.video(link, text, title) if TWITTER_RE.match(link): return self.tweet(link, text, title) return super().link(link, text, title)
def embed(self, src, alt="", title=None): if IMAGE_RE.match(src): return self.simple_image(src, alt, title) if YOUTUBE_RE.match(src): return self.youtube(src, alt, title) if VIDEO_RE.match(src): return self.video(src, alt, title) if TWITTER_RE.match(src): return self.tweet(src, alt, title) return None
def youtube(self, src, alt="", title=None): youtube_match = YOUTUBE_RE.match(src) playlist = "" if youtube_match.group(2): playlist = f"list={escape_html(youtube_match.group(2))}&listType=playlist&" video_tag = ( f'<span class="ratio-16-9">' f'<iframe loading="lazy" src="https://www.youtube.com/embed/{escape_html(youtube_match.group(1) or "")}' f'?{playlist}autoplay=0&controls=1&showinfo=1&vq=hd1080"' f'allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; fullscreen"' f'allowfullscreen></iframe>' f"</span>") caption = f"<figcaption>{escape_html(title)}</figcaption>" if title else "" return f"<figure>{video_tag}{caption}</figure>"
def image(self, src, alt="", title=None): if IMAGE_RE.match(src): return self.just_img(src, alt, title) if YOUTUBE_RE.match(src): return self.youtube(src, alt, title) if VIDEO_RE.match(src): return self.video(src, alt, title) if TWITTER_RE.match(src): return self.tweet(src, alt, title) # if its not an image or video, display as a link return f'<a href="{src}">{src}</a>'
def youtube_id(value): youtube_match = YOUTUBE_RE.match(value) if youtube_match: return youtube_match.group(1) return ""
def youtube(self, src, alt="", title=None): youtube_match = YOUTUBE_RE.match(src) youtube_id = escape_html(youtube_match.group(1) or "") return f'<a href="{escape_html(src)}"><span class="ratio-16-9 video-preview" ' \ f'style="background-image: url(\'https://img.youtube.com/vi/{escape_html(youtube_id)}/0.jpg\');">' \ f'</span></a><br>{escape_html(title or "")}'
"youtube.com": """<i class="fab fa-youtube"></i>""", "youtu.be": """<i class="fab fa-youtube"></i>""", "github.com": """<i class="fab fa-github"></i>""", "twitter.com": """<i class="fab fa-twitter"></i>""", "facebook.com": """<i class="fab fa-facebook"></i>""", "www.patreon.com": """<i class="fab fa-patreon"></i>""", "apple.com": """<i class="fab fa-apple"></i>""", "vk.com": """<i class="fab fa-vk"></i>""", "medium.com": """<i class="fab fa-medium"></i>""", } CUSTOM_PARSERS = { "www.youtube.com": { "template": loader.get_template("posts/embeds/youtube.html"), "data": lambda post: { "src": YOUTUBE_RE.match(post.url).group(1) if YOUTUBE_RE.match(post.url) else None } }, "github.com": { "template": loader.get_template("posts/embeds/github.html"), "data": lambda post: { "metadata": post.metadata } }, "www.patreon.com": { "do_not_parse": True }, }
CUSTOM_ICONS = { "www.youtube.com": """<i class="fab fa-youtube"></i>""", "youtube.com": """<i class="fab fa-youtube"></i>""", "youtu.be": """<i class="fab fa-youtube"></i>""", "github.com": """<i class="fab fa-github"></i>""", "twitter.com": """<i class="fab fa-twitter"></i>""", "facebook.com": """<i class="fab fa-facebook"></i>""", "www.patreon.com": """<i class="fab fa-patreon"></i>""", "apple.com": """<i class="fab fa-apple"></i>""", "vk.com": """<i class="fab fa-vk"></i>""", "medium.com": """<i class="fab fa-medium"></i>""", } CUSTOM_PARSERS = { "www.youtube.com": { "template": loader.get_template("posts/embeds/youtube.html"), "data": lambda post: { "src": YOUTUBE_RE.match(post.url).group(1) } }, "github.com": { "template": loader.get_template("posts/embeds/github.html"), "data": lambda post: { "metadata": post.metadata } }, "www.patreon.com": { "do_not_parse": True }, }