diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-05-02 16:31:49 +0100 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-05-02 16:31:49 +0100 |
commit | bc9c81a84f8b8ba33098d12104c39b171deb649e (patch) | |
tree | 3c42e4e605a41ea34bae90e9c3d7a5efb027c751 /src | |
parent | 3976034a4b99a750946d2f5242158db2fef33d35 (diff) | |
download | subplot-bc9c81a84f8b8ba33098d12104c39b171deb649e.tar.gz |
resources: Use new path checking order
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/sp-codegen.rs | 2 | ||||
-rw-r--r-- | src/bin/sp-docgen.rs | 7 | ||||
-rw-r--r-- | src/bin/subplot.rs | 40 | ||||
-rw-r--r-- | src/resource.rs | 18 |
4 files changed, 61 insertions, 6 deletions
diff --git a/src/bin/sp-codegen.rs b/src/bin/sp-codegen.rs index 3fbfe1c..fae84af 100644 --- a/src/bin/sp-codegen.rs +++ b/src/bin/sp-codegen.rs @@ -13,7 +13,7 @@ fn main() -> Result<()> { eprintln!("warning: Use of `sp-codegen` is deprecated."); eprintln!("warning: You should use `subplot codegen` instead."); - opt.resources.handle(); + opt.resources.handle(opt.filename.parent()); let mut doc = cli::load_document(&opt.filename, Style::default())?; doc.lint()?; if !doc.check_named_files_exist()? { diff --git a/src/bin/sp-docgen.rs b/src/bin/sp-docgen.rs index e4b2403..398f126 100644 --- a/src/bin/sp-docgen.rs +++ b/src/bin/sp-docgen.rs @@ -37,7 +37,12 @@ fn main() -> Result<()> { eprintln!("warning: Use of `sp-docgen` is deprecated."); eprintln!("warning: You should use `subplot docgen` instead."); - opt.resources.handle(); + opt.resources.handle( + opt.filenames + .get(0) + .map(PathBuf::as_path) + .and_then(Path::parent), + ); let mut pandoc = pandoc::new(); let first_file = &opt.filenames[0]; diff --git a/src/bin/subplot.rs b/src/bin/subplot.rs index 4a650ee..e2a79d8 100644 --- a/src/bin/subplot.rs +++ b/src/bin/subplot.rs @@ -34,6 +34,11 @@ impl Toplevel { fn run(&self) -> Result<()> { self.command.run() } + + fn handle_resources(&self) { + let doc_path = self.command.doc_path(); + self.resources.handle(doc_path); + } } #[derive(Debug, StructOpt)] @@ -55,6 +60,16 @@ impl Cmd { Cmd::Codegen(c) => c.run(), } } + + fn doc_path(&self) -> Option<&Path> { + match self { + Cmd::Extract(e) => e.doc_path(), + Cmd::Filter(f) => f.doc_path(), + Cmd::Metadata(m) => m.doc_path(), + Cmd::Docgen(d) => d.doc_path(), + Cmd::Codegen(c) => c.doc_path(), + } + } } fn long_version() -> Result<String> { @@ -114,6 +129,10 @@ struct Extract { } impl Extract { + fn doc_path(&self) -> Option<&Path> { + self.filename.parent() + } + fn run(&self) -> Result<()> { let doc = cli::load_document(&self.filename, Style::default())?; @@ -164,6 +183,13 @@ struct Filter { } impl Filter { + fn doc_path(&self) -> Option<&Path> { + self.input + .as_ref() + .map(PathBuf::as_path) + .and_then(Path::parent) + } + fn run(&self) -> Result<()> { let mut buffer = String::new(); if let Some(filename) = &self.input { @@ -208,6 +234,10 @@ struct Metadata { } impl Metadata { + fn doc_path(&self) -> Option<&Path> { + self.filename.parent() + } + fn run(&self) -> Result<()> { let mut doc = cli::load_document(&self.filename, Style::default())?; let meta = cli::Metadata::try_from(&mut doc)?; @@ -238,6 +268,10 @@ struct Docgen { } impl Docgen { + fn doc_path(&self) -> Option<&Path> { + self.input.parent() + } + fn run(&self) -> Result<()> { let mut style = Style::default(); if self.output.extension() == Some(&OsString::from("pdf")) { @@ -334,6 +368,10 @@ struct Codegen { } impl Codegen { + fn doc_path(&self) -> Option<&Path> { + self.filename.parent() + } + fn run(&self) -> Result<()> { let mut doc = cli::load_document(&self.filename, Style::default())?; doc.lint()?; @@ -373,7 +411,7 @@ fn main() { let argparser = argparser.long_version(version.as_str()); let args = argparser.get_matches(); let args = Toplevel::from_clap(&args); - args.resources.handle(); + args.handle_resources(); match args.run() { Ok(_) => {} Err(e) => { diff --git a/src/resource.rs b/src/resource.rs index a03086c..2951654 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -28,10 +28,13 @@ pub struct ResourceOpts { impl ResourceOpts { /// Handle any supplied resource related arguments - pub fn handle(&self) { + pub fn handle<P: AsRef<Path>>(&self, doc_path: Option<P>) { for rpath in &self.resources { add_search_path(rpath); } + if let Some(doc_path) = doc_path.as_ref() { + add_search_path(doc_path); + } } } @@ -61,7 +64,17 @@ pub fn set_template(template: &str) { } /// Open a file for reading, honouring search paths established during -/// startup, and falling back to potentially embedded file content +/// startup, and falling back to potentially embedded file content. +/// +/// The search path sequence is: +/// +/// First any path given to `--resources` in the order of the arguments +/// Second, it's relative to the input document. +/// Third we look in `FALLBACK_PATH` if it's defined +/// Finally we check for an embedded file. +/// +/// Then we repeat all the above, inserting the template name in the subpath +/// too. pub fn open<P: AsRef<Path>>(subpath: P) -> io::Result<Box<dyn Read>> { let subpath = subpath.as_ref(); match internal_open(subpath) { @@ -100,7 +113,6 @@ fn internal_open(subpath: &Path) -> io::Result<Box<dyn Read>> { let search_paths = SEARCH_PATHS.lock().expect("Unable to lock SEARCH_PATHS"); let search_paths = search_paths.iter().map(|p| p.as_path()); let search_paths = std::iter::empty() - .chain(Some(Path::new("."))) .chain(search_paths) .chain(internal_fallback_path()); let mut ret = Err(io::Error::new( |