Only do a single pre-pass on the full document pulling out both setupfile and all other in-buffer settings.
Previously we made a separate pass just to find setupfile and then we pulled the in-buffer settings from everything.
This commit is contained in:
parent
d3c733c5ad
commit
9060f9b26d
@ -20,7 +20,6 @@ use nom::sequence::tuple;
|
||||
|
||||
use super::in_buffer_settings::scan_for_in_buffer_settings;
|
||||
use super::org_source::OrgSource;
|
||||
use super::setup_file::scan_for_setup_file;
|
||||
use super::token::AllTokensIterator;
|
||||
use super::token::Token;
|
||||
use super::util::exit_matcher_parser;
|
||||
@ -111,41 +110,31 @@ fn document_org_source<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Document<'s>> {
|
||||
let setup_file = scan_for_setup_file(input);
|
||||
let setup_file = if setup_file.is_ok() {
|
||||
let (_remaining, setup_file) = setup_file.expect("If-statement proves this is okay.");
|
||||
let setup_file_contents = context
|
||||
.get_global_settings()
|
||||
.file_access
|
||||
.read_file(Into::<&str>::into(setup_file))
|
||||
.map_err(|err| nom::Err::<CustomError<OrgSource<'_>>>::Failure(err.into()))?;
|
||||
Some(setup_file_contents)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let setup_file_settings = setup_file
|
||||
.as_ref()
|
||||
.map(|input| input.as_str().into())
|
||||
.map(scan_for_in_buffer_settings)
|
||||
.map_or(Ok(None), |r| r.map(Some))
|
||||
.map_err(|_err| {
|
||||
nom::Err::Error(CustomError::MyError(MyError(
|
||||
"TODO: make this take an owned string so I can dump err.to_string() into it."
|
||||
.into(),
|
||||
)))
|
||||
})?;
|
||||
let mut final_settings = Vec::new();
|
||||
let (_, document_settings) = scan_for_in_buffer_settings(input)?;
|
||||
let mut final_settings = Vec::with_capacity(
|
||||
document_settings.len()
|
||||
+ match setup_file_settings {
|
||||
Some((_, ref setup_file_settings)) => setup_file_settings.len(),
|
||||
None => 0,
|
||||
},
|
||||
);
|
||||
if let Some((_, setup_file_settings)) = setup_file_settings {
|
||||
final_settings.extend(setup_file_settings.into_iter());
|
||||
let setup_files: Vec<String> = document_settings
|
||||
.iter()
|
||||
.filter(|kw| kw.key.eq_ignore_ascii_case("setupfile"))
|
||||
.map(|kw| kw.value)
|
||||
.map(|setup_file| {
|
||||
context
|
||||
.get_global_settings()
|
||||
.file_access
|
||||
.read_file(setup_file)
|
||||
.map_err(|err| nom::Err::<CustomError<OrgSource<'_>>>::Failure(err.into()))
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
for setup_file in setup_files.iter().map(String::as_str) {
|
||||
let (_, setup_file_settings) =
|
||||
scan_for_in_buffer_settings(setup_file.into()).map_err(|_err| {
|
||||
nom::Err::Error(CustomError::MyError(MyError(
|
||||
"TODO: make this take an owned string so I can dump err.to_string() into it."
|
||||
.into(),
|
||||
)))
|
||||
})?;
|
||||
final_settings.extend(setup_file_settings);
|
||||
}
|
||||
|
||||
final_settings.extend(document_settings);
|
||||
// TODO: read the keywords into settings and apply them to the GlobalSettings.
|
||||
|
||||
let (remaining, document) =
|
||||
|
@ -34,7 +34,6 @@ mod planning;
|
||||
mod property_drawer;
|
||||
mod radio_link;
|
||||
mod regular_link;
|
||||
mod setup_file;
|
||||
pub mod sexp;
|
||||
mod statistics_cookie;
|
||||
mod subscript_and_superscript;
|
||||
|
@ -1,22 +0,0 @@
|
||||
use nom::bytes::complete::tag_no_case;
|
||||
use nom::character::complete::anychar;
|
||||
use nom::combinator::map;
|
||||
use nom::multi::many_till;
|
||||
|
||||
use super::keyword::filtered_keyword;
|
||||
use super::OrgSource;
|
||||
use crate::error::Res;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn scan_for_setup_file<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, &'s str> {
|
||||
let (remaining, setup) = map(
|
||||
many_till(anychar, filtered_keyword(setupfile_key)),
|
||||
|(_, kw)| kw.value,
|
||||
)(input)?;
|
||||
Ok((remaining, setup))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn setupfile_key<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
tag_no_case("setupfile")(input)
|
||||
}
|
Loading…
Reference in New Issue
Block a user