Move the text element parser into the text module.
This commit is contained in:
49
src/parser/document.rs
Normal file
49
src/parser/document.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
//! A single element of text.
|
||||
use super::bold::bold;
|
||||
use super::combinator::context_many1;
|
||||
use super::error::Res;
|
||||
use super::link::link;
|
||||
use super::paragraph::paragraph;
|
||||
use super::parser_context::ContextElement;
|
||||
use super::parser_context::ContextTree;
|
||||
use super::text::line_break;
|
||||
use super::text::space;
|
||||
use super::text::span;
|
||||
use super::text::symbol;
|
||||
use super::text::Paragraph;
|
||||
use super::text::TextElement;
|
||||
use super::token::Token;
|
||||
use super::Context;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use nom::branch::alt;
|
||||
use nom::combinator::map;
|
||||
use nom::combinator::not;
|
||||
use nom::IResult;
|
||||
|
||||
type UnboundMatcher<'r, 's, I, O, E> = dyn Fn(Context<'r, 's>, I) -> IResult<I, O, E>;
|
||||
|
||||
pub fn document(input: &str) -> Res<&str, Vec<Paragraph>> {
|
||||
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
||||
let (remaining, tokens) = context_many1(&initial_context, paragraph)(input)?;
|
||||
let paragraphs = tokens
|
||||
.into_iter()
|
||||
.map(|token| match token {
|
||||
Token::TextElement(_) => unreachable!(),
|
||||
Token::Paragraph(paragraph) => paragraph,
|
||||
})
|
||||
.collect();
|
||||
Ok((remaining, paragraphs))
|
||||
}
|
||||
|
||||
pub fn in_section<'s, 'r, 'x>(context: Context<'r, 's>, section_name: &'x str) -> bool {
|
||||
for thing in context.iter() {
|
||||
match thing.get_data() {
|
||||
ContextElement::ExitMatcherNode(_) => {}
|
||||
ContextElement::PreviousElementNode(_) => {}
|
||||
ContextElement::Context(name) if *name == section_name => return true,
|
||||
ContextElement::Context(_) => {}
|
||||
ContextElement::StartOfParagraph => {} // TODO: If we specialize this to bold then this would be a good spot to stop scanning
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
Reference in New Issue
Block a user