From 2e70d0d989e86f8e817b0300f5da85e994a502cf Mon Sep 17 00:00:00 2001 From: Victor Ananyev Date: Mon, 28 Jun 2021 10:58:37 +0200 Subject: [PATCH] specify the note to export as a separate argument --- src/lib.rs | 48 +++++++++++++++++++++++++++++++++++------------- src/main.rs | 9 ++++++++- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fe6616e..d40d2fe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -211,6 +211,7 @@ pub enum PostprocessorResult { pub struct Exporter<'a> { root: PathBuf, destination: PathBuf, + root_note: Option, frontmatter_strategy: FrontmatterStrategy, vault_contents: Option>, walk_options: WalkOptions<'a>, @@ -245,6 +246,7 @@ impl<'a> Exporter<'a> { Exporter { root: source, destination, + root_note: None, frontmatter_strategy: FrontmatterStrategy::Auto, walk_options: WalkOptions::default(), process_embeds_recursively: true, @@ -253,6 +255,22 @@ impl<'a> Exporter<'a> { } } + /// Set the note to start the export from. + /// + /// Path should be relative to the vault root + /// If the path includes path to the root as a prefix, + /// the prefix will be removed. + pub fn set_root_note(&mut self, path: Option) -> &mut Exporter<'a> { + if let Some(note_path) = path.clone() { + let trimmed_path_res = note_path.strip_prefix(self.root.clone()); + self.root_note = match trimmed_path_res { + Ok(trimmed_path) => Some(PathBuf::from(trimmed_path)), + Err(_) => path + }; + } + self + } + /// Set the [`WalkOptions`] to be used for this exporter. pub fn walk_options(&mut self, options: WalkOptions<'a>) -> &mut Exporter<'a> { self.walk_options = options; @@ -330,19 +348,23 @@ impl<'a> Exporter<'a> { self.root.as_path(), self.walk_options.clone(), )?); - self.vault_contents - .as_ref() - .unwrap() - .clone() - .into_par_iter() - .try_for_each(|file| { - let relative_path = file - .strip_prefix(&self.root.clone()) - .expect("file should always be nested under root") - .to_path_buf(); - let destination = &self.destination.join(&relative_path); - self.export_note(&file, destination) - })?; + + match self.root_note.clone() { + None => self.vault_contents + .as_ref() + .unwrap() + .clone() + .into_par_iter() + .try_for_each(|file| { + let relative_path = file + .strip_prefix(&self.root.clone()) + .expect("file should always be nested under root") + .to_path_buf(); + let destination = &self.destination.join(&relative_path); + self.export_note(&file, destination) + })?, + Some(path) => self.export_note(&self.root.join(path.clone()), &self.destination.join(path.clone()))? + }; Ok(()) } diff --git a/src/main.rs b/src/main.rs index 24eca47..253b3b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,12 +13,18 @@ struct Opts { #[options(help = "Display version information")] version: bool, - #[options(help = "Source file containing reference", free, required)] + #[options(help = "Path to the vault root", free, required)] source: Option, #[options(help = "Destination file being linked to", free, required)] destination: Option, + #[options( + short = "t", + help = "Start scanning from this note" + )] + root_note: Option, + #[options( help = "Frontmatter strategy (one of: always, never, auto)", no_short, @@ -75,6 +81,7 @@ fn main() { }; let mut exporter = Exporter::new(source, destination); + exporter.set_root_note(args.root_note); exporter.frontmatter_strategy(args.frontmatter_strategy); exporter.process_embeds_recursively(!args.no_recursive_embeds); exporter.walk_options(walk_options);