diff --git a/org_mode_samples/statistics_cookie/simple.org b/org_mode_samples/statistics_cookie/simple.org new file mode 100644 index 0000000..77d522d --- /dev/null +++ b/org_mode_samples/statistics_cookie/simple.org @@ -0,0 +1,3 @@ +[70%] +[3/5] +[-3/5] diff --git a/src/parser/object_parser.rs b/src/parser/object_parser.rs index ce8c569..549aea8 100644 --- a/src/parser/object_parser.rs +++ b/src/parser/object_parser.rs @@ -21,6 +21,7 @@ use crate::parser::org_macro::org_macro; use crate::parser::plain_link::plain_link; use crate::parser::radio_link::radio_link; use crate::parser::radio_link::radio_target; +use crate::parser::statistics_cookie::statistics_cookie; use crate::parser::target::target; use crate::parser::text_markup::text_markup; @@ -29,10 +30,14 @@ pub fn standard_set_object<'r, 's>( context: Context<'r, 's>, input: &'s str, ) -> Res<&'s str, Object<'s>> { - // TODO: line breaks, targets (different from radio targets), statistics cookies, subscript and superscript, timestamps. + // TODO: subscript and superscript, timestamps. not(|i| context.check_exit_matcher(i))(input)?; alt(( + map( + parser_with_context!(statistics_cookie)(context), + Object::StatisticsCookie, + ), map(parser_with_context!(target)(context), Object::Target), map(parser_with_context!(line_break)(context), Object::LineBreak), map( @@ -100,6 +105,10 @@ pub fn any_object_except_plain_text<'r, 's>( ) -> Res<&'s str, Object<'s>> { // Used for exit matchers so this does not check exit matcher condition. alt(( + map( + parser_with_context!(statistics_cookie)(context), + Object::StatisticsCookie, + ), map(parser_with_context!(target)(context), Object::Target), map(parser_with_context!(line_break)(context), Object::LineBreak), map( @@ -145,8 +154,12 @@ pub fn regular_link_description_object_set<'r, 's>( context: Context<'r, 's>, input: &'s str, ) -> Res<&'s str, Object<'s>> { - // TODO: minimal set of objects as well as export snippets, and statistics cookies. It can also contain another link, but only when it is a plain or angle link. It can contain square brackets, but not ]] + // TODO: add export snippets. It can also contain another link, but only when it is a plain or angle link. It can contain square brackets, but not ]] alt(( + map( + parser_with_context!(statistics_cookie)(context), + Object::StatisticsCookie, + ), map( parser_with_context!(inline_source_block)(context), Object::InlineSourceBlock, diff --git a/src/parser/statistics_cookie.rs b/src/parser/statistics_cookie.rs index 43008a6..536fa03 100644 --- a/src/parser/statistics_cookie.rs +++ b/src/parser/statistics_cookie.rs @@ -1,6 +1,12 @@ +use nom::branch::alt; +use nom::bytes::complete::tag; +use nom::character::complete::space0; +use nom::combinator::recognize; +use nom::sequence::tuple; + use super::Context; use crate::error::Res; -use crate::parser::util::not_yet_implemented; +use crate::parser::parser_with_context::parser_with_context; use crate::parser::StatisticsCookie; #[tracing::instrument(ret, level = "debug")] @@ -8,6 +14,35 @@ pub fn statistics_cookie<'r, 's>( context: Context<'r, 's>, input: &'s str, ) -> Res<&'s str, StatisticsCookie<'s>> { - not_yet_implemented()?; - todo!() + alt(( + parser_with_context!(percent_statistics_cookie)(context), + parser_with_context!(fraction_statistics_cookie)(context), + ))(input) +} + +#[tracing::instrument(ret, level = "debug")] +pub fn percent_statistics_cookie<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, StatisticsCookie<'s>> { + let (remaining, source) = + recognize(tuple((tag("["), nom::character::complete::u64, tag("%]"))))(input)?; + let (remaining, _) = space0(remaining)?; + Ok((remaining, StatisticsCookie { source })) +} + +#[tracing::instrument(ret, level = "debug")] +pub fn fraction_statistics_cookie<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, StatisticsCookie<'s>> { + let (remaining, source) = recognize(tuple(( + tag("["), + nom::character::complete::u64, + tag("/"), + nom::character::complete::u64, + tag("]"), + )))(input)?; + let (remaining, _) = space0(remaining)?; + Ok((remaining, StatisticsCookie { source })) }