summaryrefslogtreecommitdiff
path: root/src/story.rs
blob: c5371ee910c35db7ee460c72d20897ca7a9c0f67 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use crate::page;
use anyhow::Result;
use maud::{html, Markup};
use serde::Deserialize;
use toml::value::Date;

impl page::Page for Vec<Story> {
    fn page_title() -> String {
        String::from("Stories")
    }

    fn render_content(&self) -> Markup {
        html!(
            @for story in self {
                (story.render_as_card())
            }
        )
    }

    fn prepare_content(&mut self) -> Result<()> {
        self.sort_by(|a, b| {
            let date1 = a.update_date.unwrap_or(a.upload_date);
            let date2 = b.update_date.unwrap_or(b.upload_date);
            date2.cmp(&date1)
        });
        for story in self {
            std::fs::copy(&story.path, story.format_local_pdf_path())?;
        }
        Ok(())
    }

    fn content_style() -> Markup {
        html!(
        """
h2 {
    font-size: 1.8rem;
    margin-bottom: 2rem;
    color: #2F3C57;
}

hr {
    margin: 1rem;
}


.story-card {
    text-align: left;
    font-size: 1.2rem;
    hyphens: auto;
    ul {
        display: flex;
        justify-content: start;
        list-style: none;
        flex-wrap: wrap;
        
        li {
            font-size: 1rem;
            padding: 0.8rem;
            margin: 0.8rem;
            background-color: #2F3C57;
            color: #ffd1a3;
            outline: 0.2rem solid #2F3C57;
            
            a {
                text-decoration: underline #E1CCEB;
                color: #E1CCEB;
            }
        }
    }

    hr {
        margin: 2rem 0;
    }

    p {
        margin-top: 1rem;
    }

    padding: 3rem;
    margin: 3rem;
    background-color: #ffd1a3;
    border-bottom: 0.1rem dotted black;

    .content-warning {
        background-color: #EC7981;
        margin: 2rem 0;
        padding: 0.3rem;
        width: fit-content;
    }
}
        """
        )
    }
}

#[derive(Deserialize, Debug)]
pub enum StoryLanguage {
    German,
    English,
}

impl std::fmt::Display for StoryLanguage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

#[derive(Deserialize)]
pub struct Story {
    pub name: String,
    pub description: String,
    pub language: StoryLanguage,
    pub upload_date: Date,
    pub update_date: Option<Date>,
    pub path: String,
    pub warnings: Option<Vec<String>>,
}

impl Story {
    fn render_as_card(&self) -> Markup {
        html!(div class="story-card" lang=(self.html_lang_value()) {
            card {
                h2 { (self.name) }
                @if let Some(content_warnings) = &self.render_content_warnings() {
                    (content_warnings)
                }
                p { (self.description) }
                hr {}
                ul lang="en" {
                    li { "Language: " (self.language) }
                    li { "Uploaded on: " (self.upload_date) }
                    @if let Some(update_date) = self.update_date {
                        li { "Updated on: " (update_date) }
                    }
                    li {
                        a href=(&self.format_pdf_path()) download
                        {
                            "PDF"
                        }
                    }
                }
            }
        })
    }

    pub fn format_pdf_path(&self) -> String {
        let snake_case_name = self.name.to_lowercase().replace(" ", "_");
        format!("stories/{}.pdf", snake_case_name)
    }

    fn format_local_pdf_path(&self) -> String {
        format!("dist/{}", self.format_pdf_path())
    }

    pub fn html_lang_value(&self) -> String {
        match self.language {
            StoryLanguage::German => String::from("de"),
            StoryLanguage::English => String::from("en"),
        }
    }

    pub fn render_content_warnings(&self) -> Option<Markup> {
        if let Some(warnings) = &self.warnings {
            let (summary, prefix) = match self.language {
                StoryLanguage::German => (
                    String::from("Vorsicht ist geboten."),
                    String::from("Die Geschichte beinhaltet: "),
                ),
                StoryLanguage::English => (
                    String::from("Reader discretion is advised."),
                    String::from("The story contains: "),
                ),
            };
            let details = format!("{}{}", prefix, warnings.join(", "));
            Some(html!(
                details {
                    summary {
                        strong { (summary) }
                    }
                    p class="content-warning" { (details) }
                }
            ))
        } else {
            None
        }
    }
}