Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
html.rs 1.17 KiB
Newer Older
peterrabbit's avatar
peterrabbit committed
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()
    }
}