Skip to main content

ftml/render/html/element/
embed.rs

1/*
2 * render/html/element/embed.rs
3 *
4 * ftml - Library to parse Wikidot text
5 * Copyright (C) 2019-2026 Wikijump Team
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21use super::prelude::*;
22use crate::tree::Embed;
23
24pub fn render_embed(ctx: &mut HtmlContext, embed: &Embed) {
25    debug!(
26        "Rendering embed (variant '{}', url '{}')",
27        embed.name(),
28        embed.direct_url(),
29    );
30
31    ctx.html()
32        .div()
33        .attr(attr!(
34            "class" => "wj-embed",
35        ))
36        .inner(|ctx| match embed {
37            Embed::Youtube { video_id } => {
38                let url = format!("https://www.youtube.com/embed/{video_id}");
39
40                ctx.html().iframe().attr(attr!(
41                    "src" => &url,
42                    "frameborder" => "0",
43                    "allow" => "accelerometer; autoplay; "
44                               "clipboard-write; encrypted-media; "
45                               "gyroscope; picture-in-picture",
46                    "allowfullscreen",
47                ));
48            }
49
50            Embed::Vimeo { video_id } => {
51                let url = format!("https://player.vimeo.com/video/{video_id}");
52
53                ctx.html().iframe().attr(attr!(
54                    "src" => &url,
55                    "frameborder" => "0",
56                    "allow" => "autoplay; fullscreen; picture-inpicture",
57                    "allowfullscreen",
58                ));
59            }
60
61            Embed::GithubGist { username, hash } => {
62                let url = format!("https://gist.github.com/{username}/{hash}.js");
63
64                ctx.html().script().attr(attr!("src" => &url));
65            }
66
67            Embed::GitlabSnippet { snippet_id } => {
68                let url = format!("https://gitlab.com/-/snippets/{snippet_id}.js");
69
70                ctx.html().script().attr(attr!("src" => &url));
71            }
72        });
73}