Skip to main content

hexo_rs/
lib.rs

1//! hexo-rs: A fast static site generator with built-in vexo theme
2//!
3//! This crate provides a Rust implementation of a static site generator
4//! that uses Tera templates with an embedded vexo theme for rendering.
5
6pub mod commands;
7pub mod config;
8pub mod content;
9pub mod generator;
10pub mod helpers;
11pub mod server;
12pub mod templates;
13pub mod theme;
14
15use anyhow::Result;
16use std::path::Path;
17
18/// The main Hexo application
19#[derive(Clone)]
20pub struct Hexo {
21    /// Site configuration
22    pub config: config::SiteConfig,
23    /// Base directory
24    pub base_dir: std::path::PathBuf,
25    /// Source directory
26    pub source_dir: std::path::PathBuf,
27    /// Public (output) directory
28    pub public_dir: std::path::PathBuf,
29    /// Theme directory
30    pub theme_dir: std::path::PathBuf,
31}
32
33impl Hexo {
34    /// Create a new Hexo instance from a directory
35    pub fn new<P: AsRef<Path>>(base_dir: P) -> Result<Self> {
36        let base_dir = base_dir.as_ref().to_path_buf();
37        let config_path = base_dir.join("_config.yml");
38
39        let config = if config_path.exists() {
40            config::SiteConfig::load(&config_path)?
41        } else {
42            config::SiteConfig::default()
43        };
44
45        let source_dir = base_dir.join(&config.source_dir);
46        let public_dir = base_dir.join(&config.public_dir);
47        let theme_dir = base_dir.join("themes").join(&config.theme);
48
49        Ok(Self {
50            config,
51            base_dir,
52            source_dir,
53            public_dir,
54            theme_dir,
55        })
56    }
57
58    /// Initialize a new site
59    pub fn init(&self) -> Result<()> {
60        commands::init::run(self)
61    }
62
63    /// Generate the static site
64    pub fn generate(&self) -> Result<()> {
65        commands::generate::run(self)
66    }
67
68    /// Clean the public directory
69    pub fn clean(&self) -> Result<()> {
70        commands::clean::run(self)
71    }
72
73    /// Create a new post
74    pub fn new_post(&self, title: &str, layout: Option<&str>) -> Result<()> {
75        commands::new::run(self, title, layout)
76    }
77}