Newer
Older
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
use crate::website::page::PageData;
use regex::{Captures, Regex};
use serde::{Deserialize, Serialize};
pub const HTML_DOC_TEMPLATE: &'static str = "
<html lang='{lang}'>
<head>
<meta charset='UTF-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<meta name='description' content='{description}'>
<title>{title}</title>
<link rel='stylesheet' href='{css}'>
</head>
<body>
{body}
</body>
<script src='{js}'></script>
</html>
";
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct HtmlDoc(String);
impl HtmlDoc {
pub fn from_page_data(page_data: &PageData) -> Self {
let re = Regex::new(r#"\{[a-z]+\}"#).unwrap();
let html = re
.replace_all(HTML_DOC_TEMPLATE, |captures: &Captures| {
let placeholder = captures.iter().next().unwrap().unwrap().as_str();
let placeholder = placeholder[1..placeholder.len() - 1].to_owned();
page_data.field_from_str_key(placeholder)
})
.to_string();
HtmlDoc(html)
}
pub fn to_string(&self) -> String {
self.0.clone()
}
}