Compare commits
No commits in common. "8736fa8638acd2842768580ca41115be9b29d27d" and "000ec1e114a5ef18f974b84df446d948f3ee620c" have entirely different histories.
8736fa8638
...
000ec1e114
2 changed files with 13 additions and 44 deletions
43
src/main.rs
43
src/main.rs
|
@ -1,40 +1,22 @@
|
||||||
use std::{
|
use std::{
|
||||||
error::Error,
|
error::Error,
|
||||||
ffi::OsStr,
|
ffi::OsStr,
|
||||||
fs::{read_dir, read_to_string, DirBuilder, File},
|
fs::{read_dir, read_to_string, File},
|
||||||
io::Write,
|
io::Write,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
const SRC_DIR: &str = "write";
|
const SRC_DIR: &str = "write";
|
||||||
const OUT_DIR: &str = "site";
|
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<dyn Error>>;
|
type Result = core::result::Result<(), Box<dyn Error>>;
|
||||||
|
|
||||||
fn main() -> Result {
|
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);
|
let src_dir = PathBuf::from(SRC_DIR);
|
||||||
build_dir(&src_dir)
|
build_dir(&src_dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_dir(dir: &Path) -> Result {
|
fn build_dir(dir: &Path) -> Result {
|
||||||
for entry in read_dir(dir)?.flatten() {
|
for entry in read_dir(dir).unwrap().flatten() {
|
||||||
let ftype = entry.file_type()?;
|
let ftype = entry.file_type()?;
|
||||||
if ftype.is_dir() {
|
if ftype.is_dir() {
|
||||||
build_dir(&entry.path())?;
|
build_dir(&entry.path())?;
|
||||||
|
@ -74,13 +56,14 @@ fn convert_file(path: &Path) -> Result {
|
||||||
html += "</pre>\n";
|
html += "</pre>\n";
|
||||||
state = S::None;
|
state = S::None;
|
||||||
continue;
|
continue;
|
||||||
|
} else {
|
||||||
|
if state == S::P {
|
||||||
|
html += "</p>\n";
|
||||||
|
}
|
||||||
|
state = S::Code;
|
||||||
|
html += "<pre>\n";
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
if state == S::P {
|
|
||||||
html += "</p>\n";
|
|
||||||
}
|
|
||||||
state = S::Code;
|
|
||||||
html += "<pre>\n";
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if state == S::Code {
|
if state == S::Code {
|
||||||
|
@ -115,12 +98,12 @@ fn convert_file(path: &Path) -> Result {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let template = read_to_string(TEMPLATE_FILE)?;
|
let template = read_to_string("template.html")?;
|
||||||
let html = template.replacen(CONTENT_MARKER, &html, 1);
|
let html = template.replace("CONTENT HERE", &html);
|
||||||
|
|
||||||
let mut file = File::create(&out_path)?;
|
let mut file = File::create(out_path)?;
|
||||||
file.write_all(html.as_bytes())?;
|
file.write_all(html.as_bytes())?;
|
||||||
println!("built {}", out_path.display());
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>TITLE HERE</title>
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
CONTENT HERE
|
|
||||||
</body>
|
|
||||||
|
|
||||||
</html>
|
|
Loading…
Reference in a new issue