-
peterrabbit authoredpeterrabbit authored
html.rs 1.40 KiB
use super::page::Page;
use regex::{Captures, Regex};
use serde::{Deserialize, Serialize};
// const CSS_LINK_FRAGMENT: &'static str = "<link rel='stylesheet' href='{url}'>";
// const SCRIPT_FRAGMENT: &'static str = "<script src='{url}'></script>";
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>
{css}
</head>
<body>
{body}
</body>
{js}
</html>
";
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct HtmlDoc(String);
impl HtmlDoc {
pub fn from_page(page: &Page) -> 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(); // strip the brackets
page.text_from_key(placeholder)
})
.to_string();
HtmlDoc(html)
}
pub fn empty() -> Self {
HtmlDoc(String::new())
}
}
impl std::fmt::Display for HtmlDoc {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}