organic/src/parser/horizontal_rule.rs

46 lines
1.5 KiB
Rust
Raw Normal View History

2023-04-21 22:23:59 -04:00
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::character::complete::line_ending;
use nom::character::complete::space0;
use nom::combinator::eof;
use nom::combinator::recognize;
use nom::combinator::verify;
use nom::multi::many0;
2023-04-21 22:23:59 -04:00
use nom::multi::many1_count;
use nom::sequence::tuple;
use super::keyword::affiliated_keyword;
use super::org_source::OrgSource;
use super::util::get_consumed;
2023-10-04 21:21:26 -04:00
use super::util::get_name;
use super::util::maybe_consume_trailing_whitespace_if_not_exiting;
2023-09-03 12:23:18 -04:00
use crate::context::RefContext;
2023-04-21 22:23:59 -04:00
use crate::error::Res;
use crate::parser::util::start_of_line;
2023-09-03 12:23:18 -04:00
use crate::types::HorizontalRule;
2023-04-21 22:23:59 -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 horizontal_rule<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, HorizontalRule<'s>> {
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
start_of_line(remaining)?;
let (remaining, _rule) = recognize(tuple((
2023-04-21 22:23:59 -04:00
space0,
verify(many1_count(tag("-")), |dashes| *dashes >= 5),
space0,
alt((line_ending, eof)),
)))(remaining)?;
let (remaining, _trailing_ws) =
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
let source = get_consumed(input, remaining);
Ok((
remaining,
HorizontalRule {
source: source.into(),
2023-10-04 21:21:26 -04:00
name: get_name(&affiliated_keywords),
},
))
2023-04-21 22:23:59 -04:00
}