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> {
|
pub fn resolve_to_page(&self, name: &str) -> Option<Page> {
|
||||||
let mut file_path = self.source_root.clone().join(Path::new(name));
|
let mut file_path = self.source_root.clone().join(Path::new(name));
|
||||||
match fs::metadata(&file_path) {
|
match fs::metadata(&file_path) {
|
||||||
|
|||||||
19
src/main.rs
19
src/main.rs
@@ -171,7 +171,7 @@ fn action(
|
|||||||
#[command(version, about, long_about = None)]
|
#[command(version, about, long_about = None)]
|
||||||
struct Args {
|
struct Args {
|
||||||
/// Path to wiki repository
|
/// Path to wiki repository
|
||||||
wiki_path: String,
|
wiki_path: Option<String>,
|
||||||
|
|
||||||
/// Render the wiki to a static website
|
/// Render the wiki to a static website
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
@@ -193,7 +193,9 @@ struct Args {
|
|||||||
#[rocket::main]
|
#[rocket::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
let historian = Historian::new(args.wiki_path);
|
|
||||||
|
if let Some(wiki_path) = args.wiki_path {
|
||||||
|
let historian = Historian::new(wiki_path);
|
||||||
|
|
||||||
let renderer = if let Some(template_path) = args.template_path {
|
let renderer = if let Some(template_path) = args.template_path {
|
||||||
PageRenderer::with_template_path(&template_path)
|
PageRenderer::with_template_path(&template_path)
|
||||||
@@ -225,4 +227,17 @@ async fn main() {
|
|||||||
.mount("/", routes![page, action])
|
.mount("/", routes![page, action])
|
||||||
.launch()
|
.launch()
|
||||||
.await;
|
.await;
|
||||||
|
return;
|
||||||
|
} else if let Some(resolve_links) = args.resolve_links {
|
||||||
|
if let Some((historian, page)) = Historian::resolve_from_file(&resolve_links) {
|
||||||
|
let linker = Linker::new(&historian);
|
||||||
|
println!("{}", linker.resolve_links(&page));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user