From c785aa3ac8f322f8c39e5877a9db14c5b1f4e7a4 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Fri, 26 Apr 2024 19:28:28 +0200 Subject: [PATCH 1/2] print status --- src/main.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index fea5add..5365cae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,8 @@ use std::{ const SRC_DIR: &str = "write"; const OUT_DIR: &str = "site"; +const CONTENT_MARKER: &str = "CONTENT HERE"; + type Result = core::result::Result<(), Box>; fn main() -> Result { @@ -56,14 +58,13 @@ fn convert_file(path: &Path) -> Result { html += "\n"; state = S::None; continue; - } else { - if state == S::P { - html += "

\n"; - } - state = S::Code; - html += "
\n";
-				continue;
 			}
+			if state == S::P {
+				html += "

\n"; + } + state = S::Code; + html += "
\n";
+			continue;
 		}
 
 		if state == S::Code {
@@ -99,11 +100,11 @@ fn convert_file(path: &Path) -> Result {
 	}
 
 	let template = read_to_string("template.html")?;
-	let html = template.replace("CONTENT HERE", &html);
+	let html = template.replacen(CONTENT_MARKER, &html, 1);
 
-	let mut file = File::create(out_path)?;
+	let mut file = File::create(&out_path)?;
 	file.write_all(html.as_bytes())?;
-
+	println!("built {}", out_path.display());
 	Ok(())
 }
 

From 8736fa8638acd2842768580ca41115be9b29d27d Mon Sep 17 00:00:00 2001
From: CrispyPin 
Date: Fri, 26 Apr 2024 20:05:33 +0200
Subject: [PATCH 2/2] create source dir, output dir and template file if
 missing

---
 src/main.rs   | 22 +++++++++++++++++++---
 template.html | 14 ++++++++++++++
 2 files changed, 33 insertions(+), 3 deletions(-)
 create mode 100644 template.html

diff --git a/src/main.rs b/src/main.rs
index 5365cae..66f4847 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,7 @@
 use std::{
 	error::Error,
 	ffi::OsStr,
-	fs::{read_dir, read_to_string, File},
+	fs::{read_dir, read_to_string, DirBuilder, File},
 	io::Write,
 	path::{Path, PathBuf},
 };
@@ -10,15 +10,31 @@ const SRC_DIR: &str = "write";
 const OUT_DIR: &str = "site";
 const CONTENT_MARKER: &str = "CONTENT HERE";
 
+const TEMPLATE_FILE: &str = "template.html";
+const DEFAULT_TEMPLATE: &[u8] = include_bytes!("../template.html");
+
 type Result = core::result::Result<(), Box>;
 
 fn main() -> Result {
+	if !PathBuf::from(TEMPLATE_FILE).is_file() {
+		File::create(TEMPLATE_FILE)?.write_all(DEFAULT_TEMPLATE)?;
+		println!("created {TEMPLATE_FILE}");
+	}
+	if !PathBuf::from(SRC_DIR).is_dir() {
+		DirBuilder::new().create(SRC_DIR)?;
+		println!("created {SRC_DIR}/");
+	}
+	if !PathBuf::from(OUT_DIR).is_dir() {
+		DirBuilder::new().create(OUT_DIR)?;
+		println!("created {OUT_DIR}/");
+	}
+
 	let src_dir = PathBuf::from(SRC_DIR);
 	build_dir(&src_dir)
 }
 
 fn build_dir(dir: &Path) -> Result {
-	for entry in read_dir(dir).unwrap().flatten() {
+	for entry in read_dir(dir)?.flatten() {
 		let ftype = entry.file_type()?;
 		if ftype.is_dir() {
 			build_dir(&entry.path())?;
@@ -99,7 +115,7 @@ fn convert_file(path: &Path) -> Result {
 		}
 	}
 
-	let template = read_to_string("template.html")?;
+	let template = read_to_string(TEMPLATE_FILE)?;
 	let html = template.replacen(CONTENT_MARKER, &html, 1);
 
 	let mut file = File::create(&out_path)?;
diff --git a/template.html b/template.html
new file mode 100644
index 0000000..67f63de
--- /dev/null
+++ b/template.html
@@ -0,0 +1,14 @@
+
+
+
+
+	
+	
+	TITLE HERE
+
+
+
+	CONTENT HERE
+
+
+
\ No newline at end of file