Move link to its own module.
This commit is contained in:
parent
0b35491047
commit
58f2f4e6aa
@ -39,10 +39,7 @@ fn can_start_bold<'s, 'r>(context: Context<'r, 's>) -> bool {
|
||||
_preceded_by_whitespace(context) && !_in_section(context, "bold")
|
||||
}
|
||||
|
||||
pub fn context_bold_start<'s, 'r>(
|
||||
context: Context<'r, 's>,
|
||||
input: &'s str,
|
||||
) -> Res<&'s str, &'s str> {
|
||||
fn context_bold_start<'s, 'r>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||||
if can_start_bold(context) {
|
||||
recognize(bold_start)(input)
|
||||
} else {
|
||||
@ -53,7 +50,7 @@ pub fn context_bold_start<'s, 'r>(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn context_bold_end<'s, 'r>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||||
fn context_bold_end<'s, 'r>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||||
let (remaining, actual_match) = recognize(bold_end)(input)?;
|
||||
peek(alt((
|
||||
// Must have whitespace after the end asterisk or it must be the end of that section (as checked by the exit matcher)
|
||||
@ -66,10 +63,10 @@ pub fn context_bold_end<'s, 'r>(context: Context<'r, 's>, input: &'s str) -> Res
|
||||
Ok((remaining, actual_match))
|
||||
}
|
||||
|
||||
pub fn bold_start(input: &str) -> Res<&str, TextElement> {
|
||||
fn bold_start(input: &str) -> Res<&str, TextElement> {
|
||||
map(symbol("*"), TextElement::Symbol)(input)
|
||||
}
|
||||
|
||||
pub fn bold_end(input: &str) -> Res<&str, TextElement> {
|
||||
fn bold_end(input: &str) -> Res<&str, TextElement> {
|
||||
map(symbol("*"), TextElement::Symbol)(input)
|
||||
}
|
||||
|
60
src/parser/link.rs
Normal file
60
src/parser/link.rs
Normal file
@ -0,0 +1,60 @@
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
|
||||
use super::combinator::context_many_till;
|
||||
use super::error::CustomError;
|
||||
use super::error::MyError;
|
||||
use super::parser_context::ChainBehavior;
|
||||
use super::parser_context::ContextElement;
|
||||
use super::parser_context::ExitMatcherNode;
|
||||
use super::text::symbol;
|
||||
use super::text::Link;
|
||||
use super::text::Res;
|
||||
use super::text::TextElement;
|
||||
use super::text_element_parser::_in_section;
|
||||
use super::text_element_parser::flat_text_element;
|
||||
use super::Context;
|
||||
use nom::combinator::map;
|
||||
use nom::combinator::recognize;
|
||||
use nom::sequence::tuple;
|
||||
|
||||
pub fn flat_link<'s, 'r>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, Link<'s>> {
|
||||
let link_start = parser_with_context!(context_link_start)(&context);
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
exit_matcher: ChainBehavior::AndParent(Some(&context_link_end)),
|
||||
}))
|
||||
.with_additional_node(ContextElement::Context("link"));
|
||||
let (remaining, captured) = recognize(tuple((link_start, |i| {
|
||||
context_many_till(&parser_context, flat_text_element, context_link_end)(i)
|
||||
})))(i)?;
|
||||
let ret = Link { contents: captured };
|
||||
Ok((remaining, ret))
|
||||
}
|
||||
|
||||
fn can_start_link<'s, 'r>(context: Context<'r, 's>) -> bool {
|
||||
!_in_section(context, "link")
|
||||
}
|
||||
|
||||
fn context_link_start<'s, 'r>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||||
if can_start_link(context) {
|
||||
recognize(link_start)(input)
|
||||
} else {
|
||||
// TODO: Make this a specific error instead of just a generic MyError
|
||||
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
||||
"Cannot start link",
|
||||
))));
|
||||
}
|
||||
}
|
||||
|
||||
fn context_link_end<'s, 'r>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||||
let (remaining, actual_match) = recognize(link_end)(input)?;
|
||||
Ok((remaining, actual_match))
|
||||
}
|
||||
|
||||
fn link_start(input: &str) -> Res<&str, TextElement> {
|
||||
map(symbol("["), TextElement::Symbol)(input)
|
||||
}
|
||||
|
||||
fn link_end(input: &str) -> Res<&str, TextElement> {
|
||||
map(symbol("]"), TextElement::Symbol)(input)
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
mod bold;
|
||||
mod combinator;
|
||||
mod error;
|
||||
mod link;
|
||||
mod list;
|
||||
mod parser_context;
|
||||
mod parser_with_context;
|
||||
|
@ -100,14 +100,6 @@ fn blank_line(input: &str) -> Res<&str, BlankLine> {
|
||||
)(input)
|
||||
}
|
||||
|
||||
pub fn link_start(input: &str) -> Res<&str, TextElement> {
|
||||
map(symbol("["), TextElement::Symbol)(input)
|
||||
}
|
||||
|
||||
pub fn link_end(input: &str) -> Res<&str, TextElement> {
|
||||
map(symbol("]"), TextElement::Symbol)(input)
|
||||
}
|
||||
|
||||
pub fn paragraph_end(input: &str) -> Res<&str, &str> {
|
||||
alt((
|
||||
recognize(tuple((
|
||||
|
@ -5,19 +5,15 @@ use crate::parser::text::paragraph_end;
|
||||
use super::bold::flat_bold;
|
||||
use super::combinator::context_many1;
|
||||
use super::combinator::context_many_till;
|
||||
use super::error::CustomError;
|
||||
use super::error::MyError;
|
||||
use super::link::flat_link;
|
||||
use super::parser_context::ChainBehavior;
|
||||
use super::parser_context::ContextElement;
|
||||
use super::parser_context::ContextTree;
|
||||
use super::parser_context::ExitMatcherNode;
|
||||
use super::text::line_break;
|
||||
use super::text::link_end;
|
||||
use super::text::link_start;
|
||||
use super::text::space;
|
||||
use super::text::span;
|
||||
use super::text::symbol;
|
||||
use super::text::Link;
|
||||
use super::text::Paragraph;
|
||||
use super::text::Res;
|
||||
use super::text::TextElement;
|
||||
@ -27,8 +23,6 @@ use nom::branch::alt;
|
||||
use nom::combinator::eof;
|
||||
use nom::combinator::map;
|
||||
use nom::combinator::not;
|
||||
use nom::combinator::recognize;
|
||||
use nom::sequence::tuple;
|
||||
use nom::IResult;
|
||||
|
||||
type UnboundMatcher<'r, 's, I, O, E> = dyn Fn(Context<'r, 's>, I) -> IResult<I, O, E>;
|
||||
@ -53,10 +47,6 @@ pub fn context_paragraph_end<'s, 'r>(
|
||||
paragraph_end(input)
|
||||
}
|
||||
|
||||
fn can_start_link<'s, 'r>(context: Context<'r, 's>) -> bool {
|
||||
!_in_section(context, "link")
|
||||
}
|
||||
|
||||
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() {
|
||||
@ -103,25 +93,6 @@ pub fn _preceded_by_whitespace<'s, 'r>(context: Context<'r, 's>) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
pub fn context_link_start<'s, 'r>(
|
||||
context: Context<'r, 's>,
|
||||
input: &'s str,
|
||||
) -> Res<&'s str, &'s str> {
|
||||
if can_start_link(context) {
|
||||
recognize(link_start)(input)
|
||||
} else {
|
||||
// TODO: Make this a specific error instead of just a generic MyError
|
||||
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
||||
"Cannot start link",
|
||||
))));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn context_link_end<'s, 'r>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||||
let (remaining, actual_match) = recognize(link_end)(input)?;
|
||||
Ok((remaining, actual_match))
|
||||
}
|
||||
|
||||
pub fn paragraph<'s, 'r>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, Paragraph<'s>> {
|
||||
// Add a not(eof) check because many_till cannot match a zero-length string
|
||||
not(eof)(i)?;
|
||||
@ -168,17 +139,3 @@ pub fn flat_text_element<'s, 'r>(
|
||||
map(line_break, TextElement::LineBreak),
|
||||
))(i)
|
||||
}
|
||||
|
||||
fn flat_link<'s, 'r>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, Link<'s>> {
|
||||
let link_start = parser_with_context!(context_link_start)(&context);
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
exit_matcher: ChainBehavior::AndParent(Some(&context_link_end)),
|
||||
}))
|
||||
.with_additional_node(ContextElement::Context("link"));
|
||||
let (remaining, captured) = recognize(tuple((link_start, |i| {
|
||||
context_many_till(&parser_context, flat_text_element, context_link_end)(i)
|
||||
})))(i)?;
|
||||
let ret = Link { contents: captured };
|
||||
Ok((remaining, ret))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user