Implement the new fields for bold, italic, underline, and strike-through.

This commit is contained in:
Tom Alexander 2023-12-08 15:51:38 -05:00
parent 3fb7cb82cd
commit 8fd9ff3848
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 86 additions and 30 deletions

View File

@ -225,6 +225,8 @@ mod tests {
let input = OrgSource::new("foo *bar* baz"); let input = OrgSource::new("foo *bar* baz");
let radio_target_match = vec![Object::Bold(Bold { let radio_target_match = vec![Object::Bold(Bold {
source: "*bar*", source: "*bar*",
contents: "bar",
post_blank: Some(" "),
children: vec![Object::PlainText(PlainText { source: "bar" })], children: vec![Object::PlainText(PlainText { source: "bar" })],
})]; })];
let global_settings = GlobalSettings { let global_settings = GlobalSettings {

View File

@ -3,11 +3,13 @@ use nom::bytes::complete::tag;
use nom::character::complete::anychar; use nom::character::complete::anychar;
use nom::character::complete::multispace1; use nom::character::complete::multispace1;
use nom::character::complete::one_of; use nom::character::complete::one_of;
use nom::character::complete::space0; use nom::character::complete::space1;
use nom::combinator::all_consuming; use nom::combinator::all_consuming;
use nom::combinator::consumed;
use nom::combinator::map; use nom::combinator::map;
use nom::combinator::map_parser; use nom::combinator::map_parser;
use nom::combinator::not; use nom::combinator::not;
use nom::combinator::opt;
use nom::combinator::peek; use nom::combinator::peek;
use nom::combinator::recognize; use nom::combinator::recognize;
use nom::combinator::verify; use nom::combinator::verify;
@ -76,12 +78,14 @@ fn bold<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>, context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Bold<'s>> { ) -> Res<OrgSource<'s>, Bold<'s>> {
let (remaining, children) = text_markup_object("*")(context, input)?; let (remaining, (contents, children, post_blank)) = text_markup_object("*")(context, input)?;
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
Ok(( Ok((
remaining, remaining,
Bold { Bold {
source: source.into(), source: source.into(),
contents: contents.into(),
post_blank: post_blank.map(Into::<&str>::into),
children, children,
}, },
)) ))
@ -95,12 +99,14 @@ fn italic<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>, context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Italic<'s>> { ) -> Res<OrgSource<'s>, Italic<'s>> {
let (remaining, children) = text_markup_object("/")(context, input)?; let (remaining, (contents, children, post_blank)) = text_markup_object("/")(context, input)?;
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
Ok(( Ok((
remaining, remaining,
Italic { Italic {
source: source.into(), source: source.into(),
contents: contents.into(),
post_blank: post_blank.map(Into::<&str>::into),
children, children,
}, },
)) ))
@ -114,12 +120,14 @@ fn underline<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>, context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Underline<'s>> { ) -> Res<OrgSource<'s>, Underline<'s>> {
let (remaining, children) = text_markup_object("_")(context, input)?; let (remaining, (contents, children, post_blank)) = text_markup_object("_")(context, input)?;
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
Ok(( Ok((
remaining, remaining,
Underline { Underline {
source: source.into(), source: source.into(),
contents: contents.into(),
post_blank: post_blank.map(Into::<&str>::into),
children, children,
}, },
)) ))
@ -133,12 +141,14 @@ fn strike_through<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>, context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, StrikeThrough<'s>> { ) -> Res<OrgSource<'s>, StrikeThrough<'s>> {
let (remaining, children) = text_markup_object("+")(context, input)?; let (remaining, (contents, children, post_blank)) = text_markup_object("+")(context, input)?;
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
Ok(( Ok((
remaining, remaining,
StrikeThrough { StrikeThrough {
source: source.into(), source: source.into(),
contents: contents.into(),
post_blank: post_blank.map(Into::<&str>::into),
children, children,
}, },
)) ))
@ -187,8 +197,10 @@ fn text_markup_object(
) -> impl for<'b, 'g, 'r, 's> Fn( ) -> impl for<'b, 'g, 'r, 's> Fn(
RefContext<'b, 'g, 'r, 's>, RefContext<'b, 'g, 'r, 's>,
OrgSource<'s>, OrgSource<'s>,
) -> Res<OrgSource<'s>, Vec<Object<'s>>> ) -> Res<
+ '_ { OrgSource<'s>,
(OrgSource<'s>, Vec<Object<'s>>, Option<OrgSource<'s>>),
> + '_ {
move |context, input: OrgSource<'_>| _text_markup_object(context, input, marker_symbol) move |context, input: OrgSource<'_>| _text_markup_object(context, input, marker_symbol)
} }
@ -200,7 +212,7 @@ fn _text_markup_object<'b, 'g, 'r, 's, 'c>(
context: RefContext<'b, 'g, 'r, 's>, context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
marker_symbol: &'c str, marker_symbol: &'c str,
) -> Res<OrgSource<'s>, Vec<Object<'s>>> { ) -> Res<OrgSource<'s>, (OrgSource<'s>, Vec<Object<'s>>, Option<OrgSource<'s>>)> {
let (remaining, _) = pre(context, input)?; let (remaining, _) = pre(context, input)?;
let (remaining, open) = tag(marker_symbol)(remaining)?; let (remaining, open) = tag(marker_symbol)(remaining)?;
let (remaining, _peek_not_whitespace) = let (remaining, _peek_not_whitespace) =
@ -215,7 +227,7 @@ fn _text_markup_object<'b, 'g, 'r, 's, 'c>(
let initial_context = ContextElement::document_context(); let initial_context = ContextElement::document_context();
let initial_context = Context::new(context.get_global_settings(), List::new(&initial_context)); let initial_context = Context::new(context.get_global_settings(), List::new(&initial_context));
let (remaining, children) = map_parser( let (remaining, (contents, children)) = consumed(map_parser(
verify( verify(
parser_with_context!(text_until_exit)(&parser_context), parser_with_context!(text_until_exit)(&parser_context),
|text| text.len() > 0, |text| text.len() > 0,
@ -225,7 +237,7 @@ fn _text_markup_object<'b, 'g, 'r, 's, 'c>(
&initial_context, &initial_context,
)))(i) )))(i)
}), }),
)(remaining)?; ))(remaining)?;
{ {
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
@ -240,9 +252,9 @@ fn _text_markup_object<'b, 'g, 'r, 's, 'c>(
} }
let (remaining, _close) = text_markup_end_specialized(context, remaining)?; let (remaining, _close) = text_markup_end_specialized(context, remaining)?;
let (remaining, _trailing_whitespace) = let (remaining, post_blank) =
maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?; maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?;
Ok((remaining, children)) Ok((remaining, (contents, children, post_blank)))
} }
fn text_markup_string( fn text_markup_string(
@ -382,13 +394,15 @@ impl<'x> RematchObject<'x> for Bold<'x> {
_context: RefContext<'b, 'g, 'r, 's>, _context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Object<'s>> { ) -> Res<OrgSource<'s>, Object<'s>> {
let (remaining, children) = let (remaining, (contents, children, post_blank)) =
_rematch_text_markup_object(_context, input, "*", &self.children)?; _rematch_text_markup_object(_context, input, "*", &self.children)?;
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
Ok(( Ok((
remaining, remaining,
Object::Bold(Bold { Object::Bold(Bold {
source: source.into(), source: source.into(),
contents: contents.into(),
post_blank: post_blank.map(Into::<&str>::into),
children, children,
}), }),
)) ))
@ -405,13 +419,15 @@ impl<'x> RematchObject<'x> for Italic<'x> {
_context: RefContext<'b, 'g, 'r, 's>, _context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Object<'s>> { ) -> Res<OrgSource<'s>, Object<'s>> {
let (remaining, children) = let (remaining, (contents, children, post_blank)) =
_rematch_text_markup_object(_context, input, "/", &self.children)?; _rematch_text_markup_object(_context, input, "/", &self.children)?;
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
Ok(( Ok((
remaining, remaining,
Object::Italic(Italic { Object::Italic(Italic {
source: source.into(), source: source.into(),
contents: contents.into(),
post_blank: post_blank.map(Into::<&str>::into),
children, children,
}), }),
)) ))
@ -428,13 +444,15 @@ impl<'x> RematchObject<'x> for Underline<'x> {
_context: RefContext<'b, 'g, 'r, 's>, _context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Object<'s>> { ) -> Res<OrgSource<'s>, Object<'s>> {
let (remaining, children) = let (remaining, (contents, children, post_blank)) =
_rematch_text_markup_object(_context, input, "_", &self.children)?; _rematch_text_markup_object(_context, input, "_", &self.children)?;
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
Ok(( Ok((
remaining, remaining,
Object::Underline(Underline { Object::Underline(Underline {
source: source.into(), source: source.into(),
contents: contents.into(),
post_blank: post_blank.map(Into::<&str>::into),
children, children,
}), }),
)) ))
@ -451,13 +469,15 @@ impl<'x> RematchObject<'x> for StrikeThrough<'x> {
_context: RefContext<'b, 'g, 'r, 's>, _context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Object<'s>> { ) -> Res<OrgSource<'s>, Object<'s>> {
let (remaining, children) = let (remaining, (contents, children, post_blank)) =
_rematch_text_markup_object(_context, input, "+", &self.children)?; _rematch_text_markup_object(_context, input, "+", &self.children)?;
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
Ok(( Ok((
remaining, remaining,
Object::StrikeThrough(StrikeThrough { Object::StrikeThrough(StrikeThrough {
source: source.into(), source: source.into(),
contents: contents.into(),
post_blank: post_blank.map(Into::<&str>::into),
children, children,
}), }),
)) ))
@ -473,7 +493,7 @@ fn _rematch_text_markup_object<'b, 'g, 'r, 's, 'x>(
input: OrgSource<'s>, input: OrgSource<'s>,
marker_symbol: &'static str, marker_symbol: &'static str,
original_match_children: &'x Vec<Object<'x>>, original_match_children: &'x Vec<Object<'x>>,
) -> Res<OrgSource<'s>, Vec<Object<'s>>> { ) -> Res<OrgSource<'s>, (OrgSource<'s>, Vec<Object<'s>>, Option<OrgSource<'s>>)> {
let (remaining, _) = pre(context, input)?; let (remaining, _) = pre(context, input)?;
let (remaining, open) = tag(marker_symbol)(remaining)?; let (remaining, open) = tag(marker_symbol)(remaining)?;
let (remaining, _peek_not_whitespace) = peek(not(multispace1))(remaining)?; let (remaining, _peek_not_whitespace) = peek(not(multispace1))(remaining)?;
@ -484,6 +504,7 @@ fn _rematch_text_markup_object<'b, 'g, 'r, 's, 'x>(
}); });
let parser_context = context.with_additional_node(&parser_context); let parser_context = context.with_additional_node(&parser_context);
let contents_begin = remaining;
let (remaining, children) = let (remaining, children) =
// TODO: This doesn't really check the exit matcher between each object. I think it may be possible to construct an org document that parses incorrectly with the current code. // TODO: This doesn't really check the exit matcher between each object. I think it may be possible to construct an org document that parses incorrectly with the current code.
rematch_target(&parser_context, original_match_children, remaining)?; rematch_target(&parser_context, original_match_children, remaining)?;
@ -499,8 +520,10 @@ fn _rematch_text_markup_object<'b, 'g, 'r, 's, 'x>(
))); )));
} }
} }
let contents_end = remaining;
let contents = contents_begin.get_until(contents_end);
let (remaining, _close) = text_markup_end_specialized(context, remaining)?; let (remaining, _close) = text_markup_end_specialized(context, remaining)?;
let (remaining, _trailing_whitespace) = space0(remaining)?; let (remaining, post_blank) = opt(space1)(remaining)?;
Ok((remaining, children)) Ok((remaining, (contents, children, post_blank)))
} }

View File

@ -81,14 +81,21 @@ pub(crate) fn maybe_consume_object_trailing_whitespace_if_not_exiting<'b, 'g, 'r
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Option<OrgSource<'s>>> { ) -> Res<OrgSource<'s>, Option<OrgSource<'s>>> {
// We have to check exit matcher after each character because description list tags need to end with a space unconsumed (" ::"). // We have to check exit matcher after each character because description list tags need to end with a space unconsumed (" ::").
let (remaining, _) = many_till( let (remaining, post_blank) = recognize(many_till(
one_of(" \t"), one_of(" \t"),
alt(( alt((
peek(recognize(none_of(" \t"))), peek(recognize(none_of(" \t"))),
parser_with_context!(exit_matcher_parser)(context), parser_with_context!(exit_matcher_parser)(context),
)), )),
)(input)?; ))(input)?;
Ok((remaining, None)) Ok((
remaining,
if post_blank.len() == 0 {
None
} else {
Some(post_blank)
},
))
} }
#[cfg_attr( #[cfg_attr(

View File

@ -43,24 +43,32 @@ pub enum Object<'s> {
#[derive(Debug)] #[derive(Debug)]
pub struct Bold<'s> { pub struct Bold<'s> {
pub source: &'s str, pub source: &'s str,
pub contents: &'s str,
pub post_blank: Option<&'s str>,
pub children: Vec<Object<'s>>, pub children: Vec<Object<'s>>,
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Italic<'s> { pub struct Italic<'s> {
pub source: &'s str, pub source: &'s str,
pub contents: &'s str,
pub post_blank: Option<&'s str>,
pub children: Vec<Object<'s>>, pub children: Vec<Object<'s>>,
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Underline<'s> { pub struct Underline<'s> {
pub source: &'s str, pub source: &'s str,
pub contents: &'s str,
pub post_blank: Option<&'s str>,
pub children: Vec<Object<'s>>, pub children: Vec<Object<'s>>,
} }
#[derive(Debug)] #[derive(Debug)]
pub struct StrikeThrough<'s> { pub struct StrikeThrough<'s> {
pub source: &'s str, pub source: &'s str,
pub contents: &'s str,
pub post_blank: Option<&'s str>,
pub children: Vec<Object<'s>>, pub children: Vec<Object<'s>>,
} }
@ -523,11 +531,15 @@ impl<'s> StandardProperties<'s> for Bold<'s> {
} }
fn get_contents<'b>(&'b self) -> Option<&'s str> { fn get_contents<'b>(&'b self) -> Option<&'s str> {
todo!() Some(self.contents)
} }
fn get_post_blank(&self) -> PostBlank { fn get_post_blank(&self) -> PostBlank {
todo!() self.post_blank
.map(|post_blank| post_blank.chars().count())
.unwrap_or(0)
.try_into()
.expect("Too much post-blank to fit into a PostBlank.")
} }
} }
@ -537,11 +549,15 @@ impl<'s> StandardProperties<'s> for Italic<'s> {
} }
fn get_contents<'b>(&'b self) -> Option<&'s str> { fn get_contents<'b>(&'b self) -> Option<&'s str> {
todo!() Some(self.contents)
} }
fn get_post_blank(&self) -> PostBlank { fn get_post_blank(&self) -> PostBlank {
todo!() self.post_blank
.map(|post_blank| post_blank.chars().count())
.unwrap_or(0)
.try_into()
.expect("Too much post-blank to fit into a PostBlank.")
} }
} }
@ -551,11 +567,15 @@ impl<'s> StandardProperties<'s> for Underline<'s> {
} }
fn get_contents<'b>(&'b self) -> Option<&'s str> { fn get_contents<'b>(&'b self) -> Option<&'s str> {
todo!() Some(self.contents)
} }
fn get_post_blank(&self) -> PostBlank { fn get_post_blank(&self) -> PostBlank {
todo!() self.post_blank
.map(|post_blank| post_blank.chars().count())
.unwrap_or(0)
.try_into()
.expect("Too much post-blank to fit into a PostBlank.")
} }
} }
@ -565,11 +585,15 @@ impl<'s> StandardProperties<'s> for StrikeThrough<'s> {
} }
fn get_contents<'b>(&'b self) -> Option<&'s str> { fn get_contents<'b>(&'b self) -> Option<&'s str> {
todo!() Some(self.contents)
} }
fn get_post_blank(&self) -> PostBlank { fn get_post_blank(&self) -> PostBlank {
todo!() self.post_blank
.map(|post_blank| post_blank.chars().count())
.unwrap_or(0)
.try_into()
.expect("Too much post-blank to fit into a PostBlank.")
} }
} }