summaryrefslogtreecommitdiff
path: root/src/works/stories.rs
blob: ccc01e038bd525bc80b01e0f9c653a22407a1a44 (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
use chrono::NaiveDate;
use serde::{Deserialize, Serialize};

// stories and their information
// derive from JSON
// process to context for HTML
#[derive(Debug, Deserialize, Serialize)]
pub struct Stories(Vec<Story>);

#[derive(Debug, Deserialize, Serialize)]
struct Story {
    title: String,
    description: String,
    path_to_document: String,
    number_of_pages: u16,
    language: Language,
    last_update: NaiveDate,
}

// see src/works/mod.rs for information of Works trait
impl super::Works for Stories {
    // render single pages for every story with the extracted text and a download button
    fn render_single_pages(
        &self,
        tera_instance: &tera::Tera,
        template_name: &str,
    ) -> anyhow::Result<()> {
        // TODO: Extract story pdf text
        let mut result = Vec::new();
        for story in self.0.iter() {
            let mut context = tera::Context::new();
            context.insert("story", story);
            result.push(std::fs::write(
                format!(
                    // use story document name for the single page
                    // e.g. mit_gutem_gewissen.pdf -> mit_gutem_gewissen.html
                    "docs/stories/{}.html",
                    story.path_to_document.replace(".pdf", "")
                )
                .as_str(),
                tera_instance.render(format!("{}.html", template_name).as_str(), &context)?,
            ));
        }
        Ok(())
    }
}

// language of story content
#[derive(Deserialize, Serialize, Debug)]
pub enum Language {
    German,
    English,
}