Take into account the source directory when parsing org-mode in Organic.
Previously only the emacs code was doing this.
This commit is contained in:
		
							parent
							
								
									275b4b53d1
								
							
						
					
					
						commit
						d3c733c5ad
					
				| @ -1,14 +1,24 @@ | ||||
| use std::fmt::Debug; | ||||
| use std::path::PathBuf; | ||||
| 
 | ||||
| pub trait FileAccessInterface: Debug { | ||||
|     fn read_file(&self, path: &str) -> Result<String, std::io::Error>; | ||||
| } | ||||
| 
 | ||||
| #[derive(Debug, Clone)] | ||||
| pub struct LocalFileAccessInterface; | ||||
| pub struct LocalFileAccessInterface { | ||||
|     pub working_directory: Option<PathBuf>, | ||||
| } | ||||
| 
 | ||||
| impl FileAccessInterface for LocalFileAccessInterface { | ||||
|     fn read_file(&self, path: &str) -> Result<String, std::io::Error> { | ||||
|         Ok(std::fs::read_to_string(path)?) | ||||
|         let final_path = self | ||||
|             .working_directory | ||||
|             .as_ref() | ||||
|             .map(PathBuf::as_path) | ||||
|             .map(|pb| pb.join(path)) | ||||
|             .unwrap_or_else(|| PathBuf::from(path)); | ||||
|         eprintln!("Reading file: {}", final_path.display()); | ||||
|         Ok(std::fs::read_to_string(final_path)?) | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -14,7 +14,9 @@ impl<'g, 's> GlobalSettings<'g, 's> { | ||||
|     pub fn new() -> GlobalSettings<'g, 's> { | ||||
|         GlobalSettings { | ||||
|             radio_targets: Vec::new(), | ||||
|             file_access: &LocalFileAccessInterface, | ||||
|             file_access: &LocalFileAccessInterface { | ||||
|                 working_directory: None, | ||||
|             }, | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -19,3 +19,6 @@ mod context; | ||||
| mod error; | ||||
| pub mod parser; | ||||
| pub mod types; | ||||
| 
 | ||||
| pub use context::GlobalSettings; | ||||
| pub use context::LocalFileAccessInterface; | ||||
|  | ||||
							
								
								
									
										28
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								src/main.rs
									
									
									
									
									
								
							| @ -103,10 +103,18 @@ fn run_parse_on_file<P: AsRef<Path>>(org_path: P) -> Result<(), Box<dyn std::err | ||||
|     let org_path = org_path.as_ref(); | ||||
|     eprintln!("Using emacs version: {}", get_emacs_version()?.trim()); | ||||
|     eprintln!("Using org-mode version: {}", get_org_mode_version()?.trim()); | ||||
|     // TODO: This should take into account the original file path when parsing in Organic, not just in emacs.
 | ||||
|     let parent_directory = org_path | ||||
|         .parent() | ||||
|         .ok_or("Should be contained inside a directory.")?; | ||||
|     let org_contents = std::fs::read_to_string(org_path)?; | ||||
|     let org_contents = org_contents.as_str(); | ||||
|     let rust_parsed = parse(org_contents)?; | ||||
|     let global_settings = GlobalSettings { | ||||
|         radio_targets: Vec::new(), | ||||
|         file_access: &LocalFileAccessInterface { | ||||
|             working_directory: Some(parent_directory.to_path_buf()), | ||||
|         }, | ||||
|     }; | ||||
|     let rust_parsed = parse_with_settings(org_contents, &global_settings)?; | ||||
|     let org_sexp = emacs_parse_file_org_document(org_path)?; | ||||
|     let (_remaining, parsed_sexp) = | ||||
|         sexp_with_padding(org_sexp.as_str()).map_err(|e| e.to_string())?; | ||||
| @ -128,14 +136,26 @@ fn run_parse_on_file<P: AsRef<Path>>(org_path: P) -> Result<(), Box<dyn std::err | ||||
| 
 | ||||
| #[cfg(not(feature = "compare"))] | ||||
| fn run_parse_on_file<P: AsRef<Path>>(org_path: P) -> Result<(), Box<dyn std::error::Error>> { | ||||
|     use organic::parser::parse_with_settings; | ||||
|     use organic::GlobalSettings; | ||||
|     use organic::LocalFileAccessInterface; | ||||
| 
 | ||||
|     let org_path = org_path.as_ref(); | ||||
|     eprintln!( | ||||
|         "This program was built with compare disabled. Only parsing with organic, not comparing." | ||||
|     ); | ||||
|     // TODO: This should take into account the original file path when parsing
 | ||||
|     let parent_directory = org_path | ||||
|         .parent() | ||||
|         .ok_or("Should be contained inside a directory.")?; | ||||
|     let org_contents = std::fs::read_to_string(org_path)?; | ||||
|     let org_contents = org_contents.as_str(); | ||||
|     let rust_parsed = parse(org_contents)?; | ||||
|     let global_settings = GlobalSettings { | ||||
|         radio_targets: Vec::new(), | ||||
|         file_access: &LocalFileAccessInterface { | ||||
|             working_directory: Some(parent_directory.to_path_buf()), | ||||
|         }, | ||||
|     }; | ||||
|     let rust_parsed = parse_with_settings(org_contents, &global_settings)?; | ||||
|     println!("{:#?}", rust_parsed); | ||||
|     Ok(()) | ||||
| } | ||||
|  | ||||
| @ -47,14 +47,24 @@ fn _filtered_keyword<'s, F: Matcher>( | ||||
| ) -> Res<OrgSource<'s>, Keyword<'s>> { | ||||
|     start_of_line(input)?; | ||||
|     // TODO: When key is a member of org-element-parsed-keywords, value can contain the standard set objects, excluding footnote references.
 | ||||
|     let (remaining, (consumed_input, (_, _, parsed_key, _, parsed_value, _))) = consumed(tuple(( | ||||
|         space0, | ||||
|         tag("#+"), | ||||
|         key_parser, | ||||
|         tag(":"), | ||||
|         alt((recognize(tuple((space1, is_not("\r\n")))), space0)), | ||||
|         alt((line_ending, eof)), | ||||
|     )))(input)?; | ||||
|     let (remaining, (consumed_input, (_, _, parsed_key, _))) = | ||||
|         consumed(tuple((space0, tag("#+"), key_parser, tag(":"))))(input)?; | ||||
|     match tuple((space0, alt((line_ending, eof))))(remaining) { | ||||
|         Ok((remaining, _)) => { | ||||
|             return Ok(( | ||||
|                 remaining, | ||||
|                 Keyword { | ||||
|                     source: consumed_input.into(), | ||||
|                     key: parsed_key.into(), | ||||
|                     value: "".into(), | ||||
|                 }, | ||||
|             )); | ||||
|         } | ||||
|         err @ Err(_) => err?, | ||||
|     }; | ||||
|     let (remaining, _ws) = space1(remaining)?; | ||||
|     let (remaining, parsed_value) = is_not("\r\n")(remaining)?; | ||||
|     let (remaining, _ws) = tuple((space0, alt((line_ending, eof))))(remaining)?; | ||||
|     Ok(( | ||||
|         remaining, | ||||
|         Keyword { | ||||
|  | ||||
| @ -46,4 +46,5 @@ mod token; | ||||
| mod util; | ||||
| pub use document::document; | ||||
| pub use document::parse; | ||||
| pub use document::parse_with_settings; | ||||
| pub use org_source::OrgSource; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Tom Alexander
						Tom Alexander