Something went wrong. Try again.
Minimalist static .md → .html site generator. Declarative and highly customizable. Built with personal sites and the small web in mind! uteri.klastriz.com
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182//! `uteri init` — seed a site without building it.//!//! Writes `site.conf` and a starter page. Templates and styles come from the//! built-in theme; existing files are left alone and nothing is built.
use std::ffi::{OsStr, OsString};use std::fs;use std::io::Write;use std::path::{Path, PathBuf};
use crate::config::absolute;
const DEFAULT_CONFIG: &str = include_str!("../assets/site.conf");const STARTER: &str = include_str!("../assets/content/index.md");
/// Seed a site and return the directory it was created in.pub fn run(config: Option<&Path>) -> Result<PathBuf, String> { let cwd = std::env::current_dir() .map_err(|error| format!("cannot read the current directory: {error}"))?; let config_path = match config { Some(path) => absolute(path, &cwd), None => cwd.join("site.conf"), }; let root = config_path .parent() .map(Path::to_path_buf) .unwrap_or_else(|| cwd.clone());
seed(&config_path, DEFAULT_CONFIG)?; seed(&root.join("content").join("index.md"), STARTER)?;
Ok(root)}
/// Write `text` to `path` unless the file already exists.fn seed(path: &Path, text: &str) -> Result<(), String> { if path.exists() { eprintln!( "warning: {} already exists; leaving it unchanged", path.display() ); return Ok(()); } if let Some(parent) = path.parent() { fs::create_dir_all(parent).map_err(|error| format!("{}: {error}", parent.display()))?; } write_atomic(path, text.as_bytes())}
/// Write a complete file beside the target, then rename it into place.pub(crate) fn write_atomic(path: &Path, bytes: &[u8]) -> Result<(), String> { let parent = path.parent().unwrap_or_else(|| Path::new(".")); let name = path.file_name().unwrap_or(OsStr::new(""));
let mut temp_name = OsString::from("."); temp_name.push(name); temp_name.push(format!(".tmp-{}", std::process::id())); let temp = parent.join(temp_name);
let mut file = fs::File::create(&temp).map_err(|error| format!("{}: {error}", temp.display()))?; if let Err(error) = file.write_all(bytes) { drop(file); let _ = fs::remove_file(&temp); return Err(format!("{}: {error}", temp.display())); } if let Err(error) = file.sync_all() { drop(file); let _ = fs::remove_file(&temp); return Err(format!("{}: {error}", temp.display())); } drop(file);
match fs::rename(&temp, path) { Ok(()) => Ok(()), Err(error) => { let _ = fs::remove_file(&temp); Err(format!("{}: {error}", path.display())) } }}