specify the note to export as a separate argument

This commit is contained in:
Victor Ananyev 2021-06-28 10:58:37 +02:00
parent 2dc7809367
commit 2e70d0d989
2 changed files with 43 additions and 14 deletions

View File

@ -211,6 +211,7 @@ pub enum PostprocessorResult {
pub struct Exporter<'a> {
root: PathBuf,
destination: PathBuf,
root_note: Option<PathBuf>,
frontmatter_strategy: FrontmatterStrategy,
vault_contents: Option<Vec<PathBuf>>,
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<PathBuf>) -> &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(())
}

View File

@ -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<PathBuf>,
#[options(help = "Destination file being linked to", free, required)]
destination: Option<PathBuf>,
#[options(
short = "t",
help = "Start scanning from this note"
)]
root_note: Option<PathBuf>,
#[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);