diff --git a/Cargo.lock b/Cargo.lock index 6661898..e2d0909 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2127,7 +2127,7 @@ checksum = "ae524f056d7d770e174287294f562e95044c68e88dec909a00d2094805db9d75" [[package]] name = "snekdown" -version = "0.31.0" +version = "0.31.1" dependencies = [ "asciimath-rs", "base64 0.12.3", diff --git a/Cargo.toml b/Cargo.toml index c43fd3d..52283ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "snekdown" -version = "0.31.0" +version = "0.31.1" authors = ["trivernis "] edition = "2018" license-file = "LICENSE" diff --git a/README.md b/README.md index ebb20ab..6206761 100644 --- a/README.md +++ b/README.md @@ -37,27 +37,50 @@ SUBCOMMANDS: watch Watch the document and its imports and render on change ``` -### Rendering / Watching +### Rendering ``` Parse and render the document USAGE: - snekdown render [FLAGS] [OPTIONS] + snekdown render [OPTIONS] FLAGS: - -h, --help Prints help information - --no-cache Don't use the cache - -V, --version Prints version information + -h, --help Prints help information + -V, --version Prints version information OPTIONS: -f, --format the output format [default: html] +ARGS: + Path to the input file + Path for the output file + +``` + +### Watching + +``` +Watch the document and its imports and render on change + +USAGE: + snekdown watch [OPTIONS] + +FLAGS: + -h, --help Prints help information + -V, --version Prints version information + +OPTIONS: + --debounce The amount of time in milliseconds to wait after changes before rendering [default: + 500] + -f, --format the output format [default: html] + ARGS: Path to the input file Path for the output file ``` + ## Syntax ### Images diff --git a/src/main.rs b/src/main.rs index 236a2de..288e5a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,17 +16,17 @@ use std::sync::mpsc::channel; use std::time::{Duration, Instant}; use structopt::StructOpt; -#[derive(StructOpt, Debug)] +#[derive(StructOpt, Debug, Clone)] struct Opt { #[structopt(subcommand)] sub_command: SubCommand, } -#[derive(StructOpt, Debug)] +#[derive(StructOpt, Debug, Clone)] #[structopt()] enum SubCommand { /// Watch the document and its imports and render on change. - Watch(RenderOptions), + Watch(WatchOptions), /// Parse and render the document. Render(RenderOptions), @@ -35,7 +35,7 @@ enum SubCommand { ClearCache, } -#[derive(StructOpt, Debug)] +#[derive(StructOpt, Debug, Clone)] #[structopt()] struct RenderOptions { /// Path to the input file @@ -49,10 +49,17 @@ struct RenderOptions { /// the output format #[structopt(short, long, default_value = "html")] format: String, +} + +#[derive(StructOpt, Debug, Clone)] +#[structopt()] +struct WatchOptions { + /// The amount of time in milliseconds to wait after changes before rendering + #[structopt(long, default_value = "500")] + debounce: u64, - /// Don't use the cache - #[structopt(long)] - no_cache: bool, + #[structopt(flatten)] + render_options: RenderOptions, } fn main() { @@ -101,16 +108,17 @@ fn get_level_style(level: Level) -> colored::Color { } /// Watches a file with all of its imports and renders on change -fn watch(opt: &RenderOptions) { - let parser = render(opt); +fn watch(opt: &WatchOptions) { + let parser = render(&opt.render_options); let (tx, rx) = channel(); - let mut watcher = watcher(tx, Duration::from_millis(250)).unwrap(); + let mut watcher = watcher(tx, Duration::from_millis(opt.debounce)).unwrap(); + for path in parser.get_paths() { watcher.watch(path, RecursiveMode::NonRecursive).unwrap(); } while let Ok(_) = rx.recv() { println!("---"); - let parser = render(opt); + let parser = render(&opt.render_options); for path in parser.get_paths() { watcher.watch(path, RecursiveMode::NonRecursive).unwrap(); } @@ -129,11 +137,7 @@ fn render(opt: &RenderOptions) -> Parser { } let start = Instant::now(); - let mut parser = Parser::with_defaults( - ParserOptions::default() - .add_path(opt.input.clone()) - .use_cache(!opt.no_cache), - ); + let mut parser = Parser::with_defaults(ParserOptions::default().add_path(opt.input.clone())); let document = parser.parse(); log::info!("Parsing + Processing took: {:?}", start.elapsed()); diff --git a/src/parser/mod.rs b/src/parser/mod.rs index f4b1cf9..7085440 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -58,13 +58,6 @@ impl ParserOptions { self } - - /// If external sources should be cached when after downloaded - pub fn use_cache(self, value: bool) -> Self { - self.document.downloads.lock().use_cache = value; - - self - } } pub struct Parser { @@ -159,18 +152,6 @@ impl Parser { ); return Err(self.ctm.assert_error(None)); } - { - let mut paths = self.options.paths.lock().unwrap(); - if paths.iter().find(|item| **item == path) != None { - log::warn!( - "Import of \"{}\" failed: Already imported.\n\t--> {}\n", - path.to_str().unwrap(), - self.get_position_string(), - ); - return Err(self.ctm.assert_error(None)); - } - paths.push(path.clone()); - } let anchor = Arc::new(RwLock::new(ImportAnchor::new())); let anchor_clone = Arc::clone(&anchor); let wg = self.wg.clone(); @@ -277,6 +258,18 @@ impl Parser { } } } + { + let mut paths = self.options.paths.lock().unwrap(); + if paths.iter().find(|item| **item == path).is_some() { + log::warn!( + "Import of \"{}\" failed: Already imported.\n\t--> {}\n", + path.to_str().unwrap(), + self.get_position_string(), + ); + return ImportType::None; + } + paths.push(path.clone()); + } match args.get("type").map(|e| e.as_string().to_lowercase()) { Some(s) if s == "stylesheet".to_string() => { ImportType::Stylesheet(self.import_stylesheet(path)) diff --git a/src/utils/downloads.rs b/src/utils/downloads.rs index c8d86f3..39c300f 100644 --- a/src/utils/downloads.rs +++ b/src/utils/downloads.rs @@ -10,7 +10,6 @@ use std::sync::Arc; #[derive(Clone, Debug)] pub struct DownloadManager { downloads: Vec>>, - pub use_cache: bool, } impl DownloadManager { @@ -18,14 +17,12 @@ impl DownloadManager { pub fn new() -> Self { Self { downloads: Vec::new(), - use_cache: true, } } /// Adds a new pending download pub fn add_download(&mut self, path: String) -> Arc> { - let mut download = PendingDownload::new(path.clone()); - download.use_cache = self.use_cache; + let download = PendingDownload::new(path.clone()); let pending = Arc::new(Mutex::new(download)); self.downloads.push(Arc::clone(&pending)); log::debug!("Added download {}", path);