Set Document path and category based on file path.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use std::path::Path;
|
||||
|
||||
use nom::combinator::all_consuming;
|
||||
use nom::combinator::opt;
|
||||
use nom::multi::many0;
|
||||
@@ -25,30 +27,71 @@ use crate::types::Object;
|
||||
|
||||
/// Parse a full org-mode document.
|
||||
///
|
||||
/// This is the main entry point for Organic. It will parse the full contents of the input string as an org-mode document.
|
||||
/// This is a main entry point for Organic. It will parse the full contents of the input string as an org-mode document without an underlying file attached.
|
||||
#[allow(dead_code)]
|
||||
pub fn parse<'s>(input: &'s str) -> Result<Document<'s>, Box<dyn std::error::Error>> {
|
||||
parse_with_settings(input, &GlobalSettings::default())
|
||||
parse_file_with_settings::<&Path>(input, &GlobalSettings::default(), None)
|
||||
}
|
||||
|
||||
/// Parse a full org-mode document.
|
||||
///
|
||||
/// This is a main entry point for Organic. It will parse the full contents of the input string as an org-mode document at the file_path.
|
||||
///
|
||||
/// file_path is not used for reading the file contents. It is only used for determining the document category and filling in the path attribute on the Document.
|
||||
#[allow(dead_code)]
|
||||
pub fn parse_file<'s, P: AsRef<Path>>(
|
||||
input: &'s str,
|
||||
file_path: Option<P>,
|
||||
) -> Result<Document<'s>, Box<dyn std::error::Error>> {
|
||||
parse_file_with_settings(input, &GlobalSettings::default(), file_path)
|
||||
}
|
||||
|
||||
/// Parse a full org-mode document with starting settings.
|
||||
///
|
||||
/// This is the secondary entry point for Organic. It will parse the full contents of the input string as an org-mode document starting with the settings you supplied.
|
||||
/// This is a secondary entry point for Organic. It will parse the full contents of the input string as an org-mode document starting with the settings you supplied without an underlying file attached.
|
||||
///
|
||||
/// This will not prevent additional settings from being learned during parsing, for example when encountering a "#+TODO".
|
||||
#[allow(dead_code)]
|
||||
pub fn parse_with_settings<'g, 's>(
|
||||
input: &'s str,
|
||||
global_settings: &'g GlobalSettings<'g, 's>,
|
||||
) -> Result<Document<'s>, Box<dyn std::error::Error>> {
|
||||
parse_file_with_settings::<&Path>(input, global_settings, None)
|
||||
}
|
||||
|
||||
/// Parse a full org-mode document with starting settings.
|
||||
///
|
||||
/// This is the secondary entry point for Organic. It will parse the full contents of the input string as an org-mode document at the file_path starting with the settings you supplied.
|
||||
///
|
||||
/// This will not prevent additional settings from being learned during parsing, for example when encountering a "#+TODO".
|
||||
///
|
||||
/// file_path is not used for reading the file contents. It is only used for determining the document category and filling in the path attribute on the Document.
|
||||
#[allow(dead_code)]
|
||||
pub fn parse_file_with_settings<'g, 's, P: AsRef<Path>>(
|
||||
input: &'s str,
|
||||
global_settings: &'g GlobalSettings<'g, 's>,
|
||||
file_path: Option<P>,
|
||||
) -> Result<Document<'s>, Box<dyn std::error::Error>> {
|
||||
let initial_context = ContextElement::document_context();
|
||||
let initial_context = Context::new(global_settings, List::new(&initial_context));
|
||||
let wrapped_input = OrgSource::new(input);
|
||||
let ret =
|
||||
let mut doc =
|
||||
all_consuming(parser_with_context!(document_org_source)(&initial_context))(wrapped_input)
|
||||
.map_err(|err| err.to_string())
|
||||
.map(|(_remaining, parsed_document)| parsed_document);
|
||||
Ok(ret?)
|
||||
.map(|(_remaining, parsed_document)| parsed_document)?;
|
||||
if let Some(file_path) = file_path {
|
||||
let full_path = file_path.as_ref().canonicalize()?;
|
||||
if doc.category.is_none() {
|
||||
let category = full_path
|
||||
.file_prefix()
|
||||
.expect("File should have a name.")
|
||||
.to_str()
|
||||
.expect("File name should be valid utf-8.");
|
||||
doc.category = Some(category.to_owned());
|
||||
}
|
||||
doc.path = Some(full_path);
|
||||
}
|
||||
Ok(doc)
|
||||
}
|
||||
|
||||
/// Parse a full org-mode document.
|
||||
|
||||
@@ -45,5 +45,7 @@ mod text_markup;
|
||||
mod timestamp;
|
||||
mod util;
|
||||
pub use document::parse;
|
||||
pub use document::parse_file;
|
||||
pub use document::parse_file_with_settings;
|
||||
pub use document::parse_with_settings;
|
||||
pub(crate) use org_source::OrgSource;
|
||||
|
||||
Reference in New Issue
Block a user