1pub 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#[derive(Clone)]
20pub struct Hexo {
21 pub config: config::SiteConfig,
23 pub base_dir: std::path::PathBuf,
25 pub source_dir: std::path::PathBuf,
27 pub public_dir: std::path::PathBuf,
29 pub theme_dir: std::path::PathBuf,
31}
32
33impl Hexo {
34 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 pub fn init(&self) -> Result<()> {
60 commands::init::run(self)
61 }
62
63 pub fn generate(&self) -> Result<()> {
65 commands::generate::run(self)
66 }
67
68 pub fn clean(&self) -> Result<()> {
70 commands::clean::run(self)
71 }
72
73 pub fn new_post(&self, title: &str, layout: Option<&str>) -> Result<()> {
75 commands::new::run(self, title, layout)
76 }
77}