From fa9f282194c0f99dc6aca5a59c1e02abc71053da Mon Sep 17 00:00:00 2001 From: quirinecker Date: Mon, 10 Nov 2025 17:54:09 +0100 Subject: [PATCH] updated some things like help text, fixing custom docs dir --- res/test/docki.config.toml | 3 +- src/app/build/mod.rs | 6 +- src/app/commands/build.rs | 2 +- .../commands/executions/build_execution.rs | 7 ++- src/app/commands/serve.rs | 16 +++--- src/app/config/arguments.rs | 18 +++++- src/app/config/config.rs | 56 ++++++++++--------- 7 files changed, 63 insertions(+), 45 deletions(-) diff --git a/res/test/docki.config.toml b/res/test/docki.config.toml index c3364d6..88b2a2a 100644 --- a/res/test/docki.config.toml +++ b/res/test/docki.config.toml @@ -1,2 +1,3 @@ -docs_dir = "./asciidocs" +docs_dir = "./docs" port = 6969 +offline_reveal = false diff --git a/src/app/build/mod.rs b/src/app/build/mod.rs index 8a67f60..d14dde7 100644 --- a/src/app/build/mod.rs +++ b/src/app/build/mod.rs @@ -6,11 +6,11 @@ use super::fs_util; pub mod asciidoctor; -pub fn docki_build(in_path: &str, offline_reveal: bool) -> DockiBuildResult { - let out_path = in_path.replace("/docs/", "/dist/"); +pub fn docki_build(in_path: &str, offline_reveal: bool, docs_dir: &str) -> DockiBuildResult { + let out_path = in_path.replace(docs_dir, "./dist"); let convert_out_path = out_path.replace(".adoc", ".html"); - if in_path.starts_with("./docs/slides/") && in_path.ends_with(".adoc") { + if in_path.starts_with(format!("{}/slides/", docs_dir).as_str()) && in_path.ends_with(".adoc") { if let Err(err) = build_slide(&in_path, &convert_out_path, offline_reveal) { return DockiBuildResult::Err(err); } diff --git a/src/app/commands/build.rs b/src/app/commands/build.rs index 83e8fcd..45fd69d 100644 --- a/src/app/commands/build.rs +++ b/src/app/commands/build.rs @@ -3,6 +3,6 @@ use crate::app::config::config::Config; use super::executions::build_execution::BuildExecution; pub async fn build(config: &Config) -> () { - let mut build_execution = BuildExecution::new(&config.docs_dir); + let mut build_execution = BuildExecution::new(&config.input_dir); build_execution.execute(&config).await.expect("build failed") } diff --git a/src/app/commands/executions/build_execution.rs b/src/app/commands/executions/build_execution.rs index 98b4847..0628d93 100644 --- a/src/app/commands/executions/build_execution.rs +++ b/src/app/commands/executions/build_execution.rs @@ -1,5 +1,5 @@ use std::{ - fmt::format, io::Cursor, path::PathBuf + io::Cursor, path::PathBuf }; use crate::app::{ @@ -73,15 +73,16 @@ impl BuildExecution { return Err(result.unwrap_err()) }; + let reveal_dir = format!("{}/slides/revealjs", path); let paths = paths.into_iter() - .filter(|path| offline_reveal || !path.starts_with("./docs/slides/revealjs")) + .filter(|path| offline_reveal || !path.starts_with(reveal_dir.as_str())) .collect::>(); self.goal = paths.len(); for (index, in_path) in paths.iter().enumerate() { self.progress = index + 1; - let result = docki_build(&in_path, offline_reveal); + let result = docki_build(&in_path, offline_reveal, &self.docs_dir); match result { DockiBuildResult::Err(err) => { diff --git a/src/app/commands/serve.rs b/src/app/commands/serve.rs index 9ecbfea..3726c30 100644 --- a/src/app/commands/serve.rs +++ b/src/app/commands/serve.rs @@ -12,11 +12,11 @@ use crate::app::{ build::{DockiBuildResult, docki_build}, commands::build::build pub async fn serve(config: &Config) { build(config).await; - tokio::join!(watch_and_build(&config.docs_dir), start_server(config.port)); + tokio::join!(watch_and_build(&config.input_dir), start_server(config.port)); } async fn watch_and_build(docs_dir: &str) { - watch(Path::new(docs_dir)) + watch(Path::new(docs_dir), docs_dir) .await .expect("something went wrong") } @@ -36,27 +36,27 @@ async fn start_server(port: u16) { }; } -async fn watch(path: &Path) -> notify::Result<()> { +async fn watch(path: &Path, docs_dir: &str) -> notify::Result<()> { let (mut watcher, mut rx) = watcher()?; watcher.watch(path.as_ref(), RecursiveMode::Recursive)?; while let Some(res) = rx.next().await { let event = res.expect("watching failed"); - file_change(event) + file_change(event, docs_dir) } Ok(()) } -fn file_change(event: Event) { +fn file_change(event: Event, docs_dir: &str) { match event.kind { - EventKind::Modify(ModifyKind::Data(_)) => build_file(event.paths), + EventKind::Modify(ModifyKind::Data(_)) => build_file(event.paths, docs_dir), _ => (), } } -fn build_file(paths: Vec) { +fn build_file(paths: Vec, docs_dir: &str) { let invalid_path_message = "changed path is invalid"; let in_path = paths .first() @@ -67,7 +67,7 @@ fn build_file(paths: Vec) { .expect(invalid_path_message); let in_path = format!("./{}", in_path); - let result = docki_build(&in_path, false); + let result = docki_build(&in_path, false, docs_dir); match result { DockiBuildResult::Slide(out_path) => display_rebuilding_status("Slide", &in_path, &out_path), diff --git a/src/app/config/arguments.rs b/src/app/config/arguments.rs index 88701e0..0283aeb 100644 --- a/src/app/config/arguments.rs +++ b/src/app/config/arguments.rs @@ -1,13 +1,27 @@ -use super::config::Config; use clap::{Parser, Subcommand}; +use nu_ansi_term::{AnsiGenericString, Style}; + +fn github_hyperlink() -> AnsiGenericString<'static, str> { + return Style::new() + .bold() + .underline() + .paint("https://github.com/quirinecker/docki") + .hyperlink("https://github.com/quirinecker/docki") +} #[derive(Parser)] +#[command(after_help = format!("More information like defaults can be found at {}", github_hyperlink()))] pub struct Args { #[command(subcommand)] pub command: CommandArg, + /// The directory where the documentation is located #[arg(short, long)] - pub docs_dir: Option, + pub input_dir: Option, + + /// The directory where the documentation will be built + #[arg(short, long)] + pub output_dir: Option, } #[derive(Subcommand)] diff --git a/src/app/config/config.rs b/src/app/config/config.rs index 119a792..e8f5288 100644 --- a/src/app/config/config.rs +++ b/src/app/config/config.rs @@ -5,44 +5,46 @@ use crate::app::config::arguments::CommandArg; #[derive(Deserialize, Debug)] #[serde(default)] pub struct Config { - pub port: u16, - pub docs_dir: String, - pub offline_reveal: bool, + pub port: u16, + pub input_dir: String, + pub offline_reveal: bool, + pub output_dir: String } impl Config { pub fn load() -> Result { - let s = config::Config::builder() .add_source(config::File::with_name("./docki.config.toml")) .build()?; s.try_deserialize() } - pub fn merge_with_args(self, args: &super::arguments::Args) -> Self { - Self { - port: match args.command { - CommandArg::Serve { port } => port.unwrap_or(self.port), - _ => self.port, - }, - docs_dir: args.docs_dir.clone().unwrap_or(self.docs_dir), - offline_reveal: { - if let CommandArg::Build { offline_reveal } = args.command { - offline_reveal - } else { - self.offline_reveal - } - } - } - } + pub fn merge_with_args(self, args: &super::arguments::Args) -> Self { + Self { + port: match args.command { + CommandArg::Serve { port } => port.unwrap_or(self.port), + _ => self.port, + }, + input_dir: args.input_dir.clone().unwrap_or(self.input_dir), + output_dir: args.output_dir.clone().unwrap_or(self.output_dir), + offline_reveal: { + if let CommandArg::Build { offline_reveal } = args.command { + offline_reveal + } else { + self.offline_reveal + } + }, + } + } } impl Default for Config { - fn default() -> Self { - Self { - port: 8080, - docs_dir: "./docs".to_string(), - offline_reveal: false, - } - } + fn default() -> Self { + Self { + port: 8080, + input_dir: "./docs".to_string(), + output_dir: "./dist".to_string(), + offline_reveal: false, + } + } }