From ee9198d001187127b04f83b19ebeadc2b5dbe8b7 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 30 May 2020 17:17:49 +0200 Subject: [PATCH] Fix path resolution --- README.md | 9 +++++++++ src/main.rs | 5 +++-- src/parser.rs | 17 ++++++++++++++--- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e26c4bd..d979e89 100644 --- a/README.md +++ b/README.md @@ -33,4 +33,13 @@ Multiline Quote with metadata (e.g. Author) [Trivernis - 2020]> This is a quote with metadata +``` + +### Imports + +Imports can be used to import a different document to be attached to the main document. +Imports are parsed via multithreading. + +``` +<[path] ``` \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index c2997bc..4ffb031 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,8 +5,9 @@ use std::time::Instant; fn main() { let start = Instant::now(); let mut parser = Parser::new( - read_to_string("test/document.md").unwrap(), - Some("test/".to_string()), + read_to_string("/home/trivernis/Documents/Programming/Rust/markdown-rs/test/document.md") + .unwrap(), + Some("/home/trivernis/Documents/Programming/Rust/markdown-rs/test/document.md".to_string()), ); let document = parser.parse(); println!("Total duration: {:?}", start.elapsed()); diff --git a/src/parser.rs b/src/parser.rs index a4688ce..23960c7 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -58,6 +58,13 @@ impl Parser { let mut text: Vec = text.chars().collect(); text.append(&mut vec!['\n', ' ', '\n']); // push space and newline of eof. it fixes stuff and I don't know why. let current_char = text.get(0).unwrap().clone(); + if let Some(path) = path.clone() { + let path_info = Path::new(&path); + paths + .lock() + .unwrap() + .push(path_info.to_str().unwrap().to_string()) + } Self { index: 0, text, @@ -184,6 +191,10 @@ impl Parser { fn transform_path(&mut self, path: String) -> String { let mut path = path; + let first_path_info = Path::new(&path); + if first_path_info.is_absolute() { + return first_path_info.to_str().unwrap().to_string(); + } if let Some(selfpath) = &self.path { let path_info = Path::new(&selfpath); if path_info.is_file() { @@ -192,18 +203,18 @@ impl Parser { } } } - return path; + let path_info = Path::new(&path); + return path_info.to_str().unwrap().to_string(); } /// starts up a new thread to parse the imported document fn import_document(&mut self, path: String) -> Result>, ParseError> { - let mut path = self.transform_path(path); + let path = self.transform_path(path); let path_info = Path::new(&path); if !path_info.exists() || !path_info.is_file() { println!("Import of \"{}\" failed: The file doesn't exist.", path); return Err(ParseError::new(self.index)); } - path = path_info.to_str().unwrap().to_string(); { let mut paths = self.paths.lock().unwrap(); if paths.iter().find(|item| **item == path) != None {