aboutsummaryrefslogtreecommitdiff
path: root/src/generator/html.rs
blob: ade3eeed75af4dc3d1a085cb2100de469e490c4e (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
use crate::template::{Context, find, parse};
use std::fs::write;
use std::io::{Error, ErrorKind::Other, Result};
use std::path::Path;

const FILENAME: &str = "index.html";

pub fn generate(
    _: &Path,
    template_directory: &Path,
    target: &Path,
    context: &Context,
) -> Result<()> {
    if let Some(template) = find(template_directory, FILENAME) {
        let parsed_template =
            parse(&template).ok_or_else(|| Error::new(Other, "Unable to parse HTML template"))?;
        let rendered_template = parsed_template.render(context)?;
        let location = target.join(FILENAME);
        write(location, rendered_template)?;
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::fs::create_dir_all;

    use super::*;

    use crate::template::Value;

    use test_utilities::*;

    #[test]
    fn test_generates_html_with_default_layout() {
        let test_dir = setup_test_dir();
        let static_dir = test_dir.join("static");
        let template_dir = test_dir.join("templates");
        let output_dir = test_dir.join("output");

        create_dir_all(&static_dir).expect("Could not create static directory");
        create_dir_all(&template_dir).expect("Could not create template directory");
        create_dir_all(&output_dir).expect("Could not create output directory");

        let context = HashMap::from([
            ("has_posts".to_string(), Value::Bool(true)),
            (
                "posts".to_string(),
                Value::Collection(vec![
                    HashMap::from([
                        ("index".to_string(), Value::Unsigned(2828)),
                        (
                            "html".to_string(),
                            Value::String("<p>Contextualization</p>".to_string()),
                        ),
                    ]),
                    HashMap::from([
                        ("index".to_string(), Value::Unsigned(2828)),
                        (
                            "html".to_string(),
                            Value::String("<p>Contexternalization</p>".to_string()),
                        ),
                    ]),
                ]),
            ),
        ]);

        generate(&static_dir, &template_dir, &output_dir, &context).expect("Generate failed");

        assert_file_contains(&output_dir.join("index.html"), "<p>Contextualization</p>");
        assert_file_contains(&output_dir.join("index.html"), "<p>Contexternalization</p>");
        cleanup_test_dir(&test_dir);
    }

    #[test]
    fn test_uses_custom_layout_if_available() {
        let test_dir = setup_test_dir();
        let static_dir = test_dir.join("static");
        let template_dir = test_dir.join("templates");
        let output_dir = test_dir.join("output");

        create_dir_all(&static_dir).expect("Could not create static directory");
        create_dir_all(&template_dir).expect("Could not create template directory");
        create_dir_all(&output_dir).expect("Could not create output directory");
        create_test_file(
            &template_dir.join("index.html"),
            "\
{{~ posts:post }}
    {{= post.html }}
{{~}}
",
        );
        let context = HashMap::from([(
            "posts".to_string(),
            Value::Collection(vec![
                HashMap::from([(
                    "html".to_string(),
                    Value::String("<p>Recontextualization</p>".to_string()),
                )]),
                HashMap::from([(
                    "html".to_string(),
                    Value::String("<p>Recontexternalization</p>".to_string()),
                )]),
            ]),
        )]);

        generate(&static_dir, &template_dir, &output_dir, &context).expect("Generate failed");

        assert_file_contents(
            &output_dir.join("index.html"),
            "\
<p>Recontextualization</p>

    <p>Recontexternalization</p>
",
        );
        cleanup_test_dir(&test_dir);
    }

    #[test]
    fn test_fails_if_html_is_malformed() {
        let test_dir = setup_test_dir();
        let static_dir = test_dir.join("static");
        let template_dir = test_dir.join("templates");
        let output_dir = test_dir.join("output");

        create_dir_all(&static_dir).expect("Could not create static directory");
        create_dir_all(&template_dir).expect("Could not create template directory");
        create_dir_all(&output_dir).expect("Could not create output directory");
        create_test_file(
            &template_dir.join("index.html"),
            "\
{{~ posts:post }}
    {{ post.html }}
{{~}}
",
        );
        let context = HashMap::new();

        let result = generate(&static_dir, &template_dir, &output_dir, &context);

        assert!(result.is_err());
        cleanup_test_dir(&test_dir);
    }

    #[test]
    fn test_fails_if_output_is_not_writable() {
        let test_dir = setup_test_dir();
        let static_dir = test_dir.join("static");
        let template_dir = test_dir.join("templates");
        let output_dir = test_dir.join("output");

        create_dir_all(&template_dir).expect("Could not create template directory");
        create_test_file(
            &template_dir.join("index.html"),
            "\
{{~ posts:post }}
    {{= post.html }}
{{~}}
",
        );
        let context = HashMap::from([(
            "posts".to_string(),
            Value::Collection(vec![
                HashMap::from([(
                    "html".to_string(),
                    Value::String("<p>Recontextualization</p>".to_string()),
                )]),
                HashMap::from([(
                    "html".to_string(),
                    Value::String("<p>Recontexternalization</p>".to_string()),
                )]),
            ]),
        )]);

        let result = generate(&static_dir, &template_dir, &output_dir, &context);

        assert!(result.is_err());
        cleanup_test_dir(&test_dir);
    }
}