use std::fs::File;
use std::fs;
use std::io::prelude::*;
use std::path::{PathBuf};
use std::error::Error;
use maud::{DOCTYPE, html, Markup, PreEscaped};
use crate::encoder::Base;
fn head(
css: String,
icons: Vec<String>,
) -> Markup {
let viewport = concat!(
"width=device-width, ",
"initial-scale=1.0, ",
"maximum-scale=1.0, ",
"user-scalable=1"
);
html! {
(DOCTYPE)
meta charset = "utf-8";
meta name = "viewport" content = (viewport);
title { "htmlpacker" }
(favicons(icons))
style { (css) }
}
}
fn favicons(icons: Vec<String>) -> Markup {
html! {
link rel="icon" type="image/svg+xml" href=(format!("data:image/svg+xml;base64,{}", icons[0]));
}
}
fn scripts(
js: Vec<String>
) -> Markup {
html! {
@for script in &js {
script {
(PreEscaped(script))
}
}
}
}
fn binary(
bin: Vec<Base>,
) -> Markup {
html! {
@for b in &bin {
pre id=(b.id) hash=(b.hash) style="display: none;" {
(b.text)
}
}
}
}
pub fn page(
css: String,
icons: Vec<String>,
js: Vec<String>,
bin: Vec<Base>,
)
-> Markup {
html! {
(head(css, icons))
body {
(binary(
bin,
))
(scripts(
js
))
}
}
}
pub fn save_html(
html: String,
output: PathBuf,
) -> Result<(), Box<dyn Error>> {
if let Some(parent) = output.parent() {
fs::create_dir_all(parent)?;
}
let mut file = File::create(output)?;
file.write_all(html.as_bytes())?;
Ok(())
}