1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use super::*;

/// Embed images.
#[derive(Default, serde::Deserialize)]
#[serde(default)]
pub struct Img {
    /// Image `<caption>`.
    pub label: String,
    /// Pop-up modal image, boolean `false` by default.
    pub pop: bool,
    /// This item is sized. (*flatten*)
    #[serde(flatten)]
    pub size: Sized,
}

/// Embed videos.
#[derive(serde::Deserialize)]
#[serde(default)]
pub struct Video {
    /// Allow controls, boolean `true` by default.
    controls: bool,
    /// Allow autoplay, boolean `false` by default.
    autoplay: bool,
    /// Video type, default to "video/mp4".
    pub r#type: String,
    /// This item is sized. (*flatten*)
    #[serde(flatten)]
    pub size: Sized,
}

impl Default for Video {
    fn default() -> Self {
        Self {
            controls: true,
            autoplay: false,
            r#type: "video/mp4".to_string(),
            size: Sized::default(),
        }
    }
}

/// Embed `<iframe>` structures, such as YouTube videos.
///
/// Please be aware that `<iframe>` maybe slow down your web browser and cause
/// security issues!
#[derive(Default, serde::Deserialize)]
#[serde(default)]
pub struct IFrame {
    /// This item is sized. (*flatten*)
    #[serde(flatten)]
    pub size: Sized,
}

impl ToHtml for Img {
    fn to_html(self, _ctx: &Ctx) -> String {
        let Self { label, pop, size } = self;
        let pop = if pop {
            " class=\"img-pop\" onclick=\"show_modal(this)\" title=\"click to pop-up the image\""
        } else {
            ""
        };
        let s = format!("<img alt=\"{}\"{}{}/>", label, pop, size);
        if label.is_empty() {
            s
        } else {
            format!("<figure>{}<figcaption>{}</figcaption></figure>", s, label)
        }
    }
}

impl ToHtml for Video {
    fn to_html(self, _ctx: &Ctx) -> String {
        let Self { controls, autoplay, r#type, size } = self;
        let (src, size) = size.size();
        let controls = if controls { " controls" } else { "" };
        let autoplay = if autoplay { " autoplay" } else { "" };
        format!(
            "<video{}{}{}><source{} type=\"{}\"></video>",
            size, controls, autoplay, src, r#type
        )
    }
}

impl ToHtml for IFrame {
    fn to_html(self, _ctx: &Ctx) -> String {
        let Self { size } = self;
        format!("<iframe{}></iframe>", size)
    }
}