use std::fmt::Write as _;
use std::path::Path;
use motabhai_core::Error;
macro_rules! files {
($dir:literal: $($path:literal),* $(,)?) => {
&[$(($path, include_str!(concat!("../", $dir, "/", $path)))),*]
};
}
const FILES: &[(&str, &str)] = &[
(".gitignore", include_str!("../starter/gitignore")),
(
".github/workflows/deploy.yml",
include_str!("../starter/github/workflows/deploy.yml"),
),
("motabhai.toml", include_str!("../starter/motabhai.toml")),
(
"models/page.toml",
include_str!("../starter/models/page.toml"),
),
(
"models/blog.toml",
include_str!("../starter/models/blog.toml"),
),
(
"models/blog-post.toml",
include_str!("../starter/models/blog-post.toml"),
),
(
"models/projects.toml",
include_str!("../starter/models/projects.toml"),
),
(
"models/project.toml",
include_str!("../starter/models/project.toml"),
),
(
"content/index.md",
include_str!("../starter/content/index.md"),
),
(
"content/about/index.md",
include_str!("../starter/content/about/index.md"),
),
(
"content/blog/index.md",
include_str!("../starter/content/blog/index.md"),
),
(
"content/blog/hello-world/index.md",
include_str!("../starter/content/blog/hello-world/index.md"),
),
(
"content/projects/index.md",
include_str!("../starter/content/projects/index.md"),
),
(
"content/projects/example-project/index.md",
include_str!("../starter/content/projects/example-project/index.md"),
),
(
"databags/menu.toml",
include_str!("../starter/databags/menu.toml"),
),
];
const TAILWIND_FILES: &[(&str, &str)] = files!("starter-tailwind":
"motabhai.toml",
"assets/css/main.css",
"templates/layout.html",
"templates/blog-post.html",
);
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, clap::ValueEnum)]
pub enum Theme {
#[default]
Plain,
EngineeringFantasy,
}
impl Theme {
const ALL: [Self; 2] = [Self::Plain, Self::EngineeringFantasy];
#[must_use]
pub fn name(self) -> &'static str {
match self {
Self::Plain => "plain",
Self::EngineeringFantasy => "engineering-fantasy",
}
}
fn files(self) -> &'static [(&'static str, &'static str)] {
match self {
Self::Plain => files!("themes/plain":
"assets/css/main.css",
"templates/layout.html",
"templates/page.html",
"templates/blog.html",
"templates/blog-post.html",
"templates/tag.html",
"templates/projects.html",
"templates/project.html",
),
Self::EngineeringFantasy => files!("themes/engineering-fantasy":
"assets/css/main.css",
"templates/layout.html",
"templates/page.html",
"templates/blog.html",
"templates/blog-post.html",
"templates/card.html",
"templates/tag.html",
"templates/projects.html",
"templates/project.html",
),
}
}
}
const NAME_PLACEHOLDER: &str = "__SITE_NAME__";
const VERSION_PLACEHOLDER: &str = "__TAILWIND_VERSION__";
const MOTABHAI_VERSION_PLACEHOLDER: &str = "__MOTABHAI_VERSION__";
pub const TAILWIND_VERSION: &str = "4.3.5";
const THEME_PLACEHOLDER: &str = "__THEME__";
pub fn create(dir: &Path, tailwind: bool, theme: Theme) -> miette::Result<()> {
let not_empty = dir.exists()
&& (!dir.is_dir()
|| std::fs::read_dir(dir)
.map_err(|e| Error::io(dir, e))?
.next()
.is_some());
if not_empty {
return Err(Error::NotEmpty {
dir: dir.to_path_buf(),
}
.into());
}
let absolute = std::path::absolute(dir).map_err(|e| Error::io(dir, e))?;
let name = absolute
.file_name()
.map_or_else(|| "My site".into(), |n| n.to_string_lossy());
let replacements: &[(&str, &str)] = if tailwind { TAILWIND_FILES } else { &[] };
let added = replacements
.iter()
.filter(|(path, _)| FILES.iter().all(|(p, _)| p != path));
for (path, text) in FILES.iter().chain(added) {
let text = replacements
.iter()
.find(|(p, _)| p == path)
.map_or(*text, |(_, t)| t);
let text = text
.replace(NAME_PLACEHOLDER, &toml_string(&name))
.replace(THEME_PLACEHOLDER, &toml_string(theme.name()))
.replace(VERSION_PLACEHOLDER, TAILWIND_VERSION)
.replace(MOTABHAI_VERSION_PLACEHOLDER, env!("CARGO_PKG_VERSION"));
write(&dir.join(path), &text)?;
}
for shipped in Theme::ALL {
let theme_dir = dir.join(motabhai_core::THEMES_DIR).join(shipped.name());
for (path, text) in shipped.files() {
write(&theme_dir.join(path), text)?;
}
}
println!("created {}", dir.display());
println!("next: cd {} && motabhai build", dir.display());
Ok(())
}
fn write(path: &Path, text: &str) -> miette::Result<()> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(|e| Error::io(parent, e))?;
}
Ok(std::fs::write(path, text).map_err(|e| Error::io(path, e))?)
}
fn toml_string(text: &str) -> String {
let mut quoted = String::with_capacity(text.len() + 2);
quoted.push('"');
for c in text.chars() {
match c {
'"' => quoted.push_str("\\\""),
'\\' => quoted.push_str("\\\\"),
c if c.is_control() => {
let _ = write!(quoted, "\\u{:04X}", u32::from(c));
}
c => quoted.push(c),
}
}
quoted.push('"');
quoted
}