Add an ability to resolve a file path to a historian instance and page object. Add a command for running resolve-links on a file directly, for use in an editor.
This commit is contained in:
30
src/lib.rs
30
src/lib.rs
@@ -58,6 +58,36 @@ impl Historian {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resolve_from_file(file_path_str: &str) -> Option<(Historian, Page)> {
|
||||
let file_path: PathBuf = std::path::absolute(file_path_str).unwrap();
|
||||
match fs::metadata(&file_path) {
|
||||
Err(_) => None,
|
||||
Ok(metadata) => {
|
||||
if !metadata.is_file() {
|
||||
None
|
||||
} else {
|
||||
let mut wiki_path = file_path.parent().unwrap();
|
||||
while wiki_path.parent() != None {
|
||||
let toml_path = wiki_path.join(DEFAULT_TOML_FILENAME);
|
||||
match fs::metadata(&toml_path) {
|
||||
Err(_) => {
|
||||
wiki_path = wiki_path.parent().unwrap();
|
||||
},
|
||||
Ok(_) => {
|
||||
let historian = Historian::new(wiki_path.to_str().unwrap().to_owned());
|
||||
let relative_page_path = diff_paths(&file_path, &wiki_path).unwrap();
|
||||
return relative_page_path.to_str()
|
||||
.and_then(|page_path| historian.resolve_to_page(page_path))
|
||||
.map(|page| (historian, page));
|
||||
}
|
||||
};
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resolve_to_page(&self, name: &str) -> Option<Page> {
|
||||
let mut file_path = self.source_root.clone().join(Path::new(name));
|
||||
match fs::metadata(&file_path) {
|
||||
|
||||
Reference in New Issue
Block a user