Fix path resolution

pull/1/head
trivernis 4 years ago
parent 15b1a1e6b7
commit ee9198d001

@ -33,4 +33,13 @@ Multiline
Quote with metadata (e.g. Author) Quote with metadata (e.g. Author)
[Trivernis - 2020]> This is a quote with metadata [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]
``` ```

@ -5,8 +5,9 @@ use std::time::Instant;
fn main() { fn main() {
let start = Instant::now(); let start = Instant::now();
let mut parser = Parser::new( let mut parser = Parser::new(
read_to_string("test/document.md").unwrap(), read_to_string("/home/trivernis/Documents/Programming/Rust/markdown-rs/test/document.md")
Some("test/".to_string()), .unwrap(),
Some("/home/trivernis/Documents/Programming/Rust/markdown-rs/test/document.md".to_string()),
); );
let document = parser.parse(); let document = parser.parse();
println!("Total duration: {:?}", start.elapsed()); println!("Total duration: {:?}", start.elapsed());

@ -58,6 +58,13 @@ impl Parser {
let mut text: Vec<char> = text.chars().collect(); let mut text: Vec<char> = text.chars().collect();
text.append(&mut vec!['\n', ' ', '\n']); // push space and newline of eof. it fixes stuff and I don't know why. 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(); 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 { Self {
index: 0, index: 0,
text, text,
@ -184,6 +191,10 @@ impl Parser {
fn transform_path(&mut self, path: String) -> String { fn transform_path(&mut self, path: String) -> String {
let mut path = path; 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 { if let Some(selfpath) = &self.path {
let path_info = Path::new(&selfpath); let path_info = Path::new(&selfpath);
if path_info.is_file() { 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 /// starts up a new thread to parse the imported document
fn import_document(&mut self, path: String) -> Result<Arc<Mutex<ImportAnchor>>, ParseError> { fn import_document(&mut self, path: String) -> Result<Arc<Mutex<ImportAnchor>>, ParseError> {
let mut path = self.transform_path(path); let path = self.transform_path(path);
let path_info = Path::new(&path); let path_info = Path::new(&path);
if !path_info.exists() || !path_info.is_file() { if !path_info.exists() || !path_info.is_file() {
println!("Import of \"{}\" failed: The file doesn't exist.", path); println!("Import of \"{}\" failed: The file doesn't exist.", path);
return Err(ParseError::new(self.index)); return Err(ParseError::new(self.index));
} }
path = path_info.to_str().unwrap().to_string();
{ {
let mut paths = self.paths.lock().unwrap(); let mut paths = self.paths.lock().unwrap();
if paths.iter().find(|item| **item == path) != None { if paths.iter().find(|item| **item == path) != None {

Loading…
Cancel
Save