crabmail

Static HTML email archive viewer in Rust
git clone git://git.alexwennerberg.com/crabmail
Log | Files | Refs | README | LICENSE

commit bec7324fa76016d46778b561593c7896d84dfa17
parent 0c84fa8203856fe05519e10bf81a08f6dd68769f
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Thu,  6 May 2021 22:14:32 -0700

Add simple index template

Diffstat:
Msrc/main.rs | 37+++++++++++++++++++++++++++++++++++--
1 file changed, 35 insertions(+), 2 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -1,5 +1,7 @@ use anyhow::Result; use mailparse::{parse_mail, MailHeaderMap, ParsedMail}; +use std::fs::OpenOptions; +use std::io::prelude::*; use std::path::Path; const HELP: &str = "\ @@ -23,7 +25,7 @@ fn main() -> Result<()> { } // TODO configurable let out_dir = pargs - .opt_value_from_os_str(["-o", "--output"], parse_path)? + .opt_value_from_os_str(["-d", "--dir"], parse_path)? .unwrap_or("site".into()); // Create if does not exist let in_mboxes = pargs.values_from_os_str(["-m", "--mbox"], parse_path)?; @@ -31,10 +33,41 @@ fn main() -> Result<()> { println!("Please provide one or more input files with the -m flag"); std::process::exit(1); } - std::fs::create_dir(out_dir).ok(); + std::fs::create_dir(&out_dir).ok(); + write_index(&out_dir)?; Ok(()) } fn parse_path(s: &std::ffi::OsStr) -> Result<std::path::PathBuf, &'static str> { Ok(s.into()) } + +// TODO set lang, title, etc +const HEADER: &[u8] = br#"<!DOCTYPE html> +<html> +<head> +<meta charset="utf-8"> +<link rel="stylesheet" type="text/css" href="/style.css"> +</head> +<body> +<main> +"#; + +const FOOTER: &[u8] = br#" +</main> +</body> +</html> +"#; + +// TODO write wrapper +fn write_index(out_dir: &Path) -> Result<()> { + let mut file = OpenOptions::new() + .create(true) + .write(true) + .truncate(true) + .open(out_dir.join("index.html"))?; + file.write_all(HEADER)?; + file.write_all(b"<h1>Hello world</h1>")?; + file.write_all(FOOTER)?; + Ok(()) +}