organic/src/parser/fixed_width_area.rs

86 lines
3.1 KiB
Rust
Raw Normal View History

2023-04-21 22:11:06 -04:00
use nom::branch::alt;
use nom::bytes::complete::tag;
2023-10-05 03:58:42 -04:00
use nom::character::complete::anychar;
2023-04-21 22:11:06 -04:00
use nom::character::complete::space0;
use nom::combinator::not;
use nom::combinator::recognize;
2023-04-21 22:11:06 -04:00
use nom::multi::many0;
2023-10-05 03:58:42 -04:00
use nom::multi::many_till;
2023-04-21 22:11:06 -04:00
use nom::sequence::preceded;
use nom::sequence::tuple;
use super::keyword::affiliated_keyword;
use super::org_source::OrgSource;
2023-10-04 21:21:26 -04:00
use super::util::get_name;
use super::util::org_line_ending;
2023-09-03 12:23:18 -04:00
use crate::context::parser_with_context;
use crate::context::RefContext;
2023-04-21 22:04:22 -04:00
use crate::error::Res;
2023-04-21 22:11:06 -04:00
use crate::parser::util::exit_matcher_parser;
use crate::parser::util::get_consumed;
use crate::parser::util::start_of_line;
2023-09-03 12:23:18 -04:00
use crate::types::FixedWidthArea;
2023-04-21 22:04:22 -04:00
2023-08-10 20:04:59 -04:00
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
2023-09-11 13:13:28 -04:00
pub(crate) fn fixed_width_area<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, FixedWidthArea<'s>> {
let (input, affiliated_keywords) = many0(affiliated_keyword)(input)?;
2023-04-21 22:11:06 -04:00
let fixed_width_area_line_matcher = parser_with_context!(fixed_width_area_line)(context);
let exit_matcher = parser_with_context!(exit_matcher_parser)(context);
2023-10-05 03:58:42 -04:00
let (remaining, first_line) = fixed_width_area_line_matcher(input)?;
let (remaining, mut remaining_lines) =
2023-04-21 22:11:06 -04:00
many0(preceded(not(exit_matcher), fixed_width_area_line_matcher))(remaining)?;
let source = get_consumed(input, remaining);
2023-10-05 03:58:42 -04:00
let mut value = Vec::with_capacity(remaining_lines.len() + 1);
let last_line = remaining_lines.pop();
if let Some(last_line) = last_line {
value.push(Into::<&str>::into(first_line));
value.extend(remaining_lines.into_iter().map(Into::<&str>::into));
let last_line = Into::<&str>::into(last_line);
// Trim the line ending from the final line.
value.push(&last_line[..(last_line.len() - 1)])
} else {
// Trim the line ending from the only line.
let only_line = Into::<&str>::into(first_line);
value.push(&only_line[..(only_line.len() - 1)])
}
Ok((
remaining,
FixedWidthArea {
source: source.into(),
2023-10-04 21:21:26 -04:00
name: get_name(&affiliated_keywords),
2023-10-05 03:58:42 -04:00
value,
},
))
2023-04-21 22:11:06 -04:00
}
2023-08-10 20:04:59 -04:00
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn fixed_width_area_line<'b, 'g, 'r, 's>(
_context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
start_of_line(input)?;
2023-10-05 03:58:42 -04:00
let (remaining, _) = tuple((space0, tag(":")))(input)?;
if let Ok((remaining, line_break)) = org_line_ending(remaining) {
return Ok((remaining, line_break));
}
let (remaining, _) = tag(" ")(remaining)?;
let (remaining, value) = recognize(many_till(anychar, org_line_ending))(remaining)?;
Ok((remaining, value))
2023-04-21 22:04:22 -04:00
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
2023-09-11 13:13:28 -04:00
pub(crate) fn detect_fixed_width_area<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> {
let (input, _) = many0(affiliated_keyword)(input)?;
tuple((
start_of_line,
space0,
tag(":"),
2023-10-05 03:58:42 -04:00
alt((tag(" "), org_line_ending)),
))(input)?;
Ok((input, ()))
}