Compare commits

..

No commits in common. "e47901a67f71c03870e37ce5049db20bc821aca0" and "60d1ecfa759c1fe6abf8a815c3bb9f06b87c418d" have entirely different histories.

11 changed files with 71 additions and 194 deletions

View File

@ -1,32 +1,3 @@
* Empty
:PROPERTIES:
:END:
* Single new line
:PROPERTIES:
:END:
* Single line with spaces
:PROPERTIES:
:END:
* Many lines, first line without spaces
:PROPERTIES:
:END:
* Many lines, first line with spaces
:PROPERTIES:
:END:
* Many lines, first line with spaces, later line with spaces
:PROPERTIES:
:END:

View File

@ -13,9 +13,7 @@ use nom::sequence::tuple;
use super::affiliated_keyword::parse_affiliated_keywords;
use super::org_source::OrgSource;
use super::paragraph::empty_paragraph;
use super::util::maybe_consume_trailing_whitespace_if_not_exiting;
use crate::context::bind_context;
use crate::context::parser_with_context;
use crate::context::ContextElement;
use crate::context::ExitClass;
@ -24,6 +22,7 @@ use crate::context::RefContext;
use crate::error::CustomError;
use crate::error::Res;
use crate::parser::element_parser::element;
use crate::parser::util::blank_line;
use crate::parser::util::exit_matcher_parser;
use crate::parser::util::get_consumed;
use crate::parser::util::immediate_in_section;
@ -32,6 +31,7 @@ use crate::parser::util::WORD_CONSTITUENT_CHARACTERS;
use crate::types::Drawer;
use crate::types::Element;
use crate::types::Keyword;
use crate::types::Paragraph;
#[cfg_attr(
feature = "tracing",
@ -107,14 +107,23 @@ fn children<'b, 'g, 'r, 's>(
let element_matcher = parser_with_context!(element(true))(context);
let exit_matcher = parser_with_context!(exit_matcher_parser)(context);
if let Ok((remaining, (_not_exit, empty_para))) =
tuple((not(exit_matcher), bind_context!(empty_paragraph, context)))(input)
let (remaining, children) = match tuple((
not(exit_matcher),
blank_line,
many_till(blank_line, exit_matcher),
))(input)
{
return Ok((remaining, vec![Element::Paragraph(empty_para)]));
Ok((remain, (_not_immediate_exit, first_line, (_trailing_whitespace, _exit_contents)))) => {
let source = get_consumed(input, remain);
let element = Element::Paragraph(Paragraph::of_text(source.into(), first_line.into()));
(remain, vec![element])
}
let (remaining, (children, _exit_contents)) = many_till(element_matcher, exit_matcher)(input)?;
Err(_) => {
let (remaining, (children, _exit_contents)) =
many_till(element_matcher, exit_matcher)(input)?;
(remaining, children)
}
};
Ok((remaining, children))
}

View File

@ -66,7 +66,6 @@ fn _heading<'b, 'g, 'r, 's>(
let (remaining, pre_headline) = headline(context, input, parent_star_count)?;
let section_matcher = bind_context!(section, context);
let heading_matcher = bind_context!(heading(pre_headline.star_count), context);
let contents_begin = remaining;
let (remaining, maybe_section) =
opt(map(section_matcher, DocumentElement::Section))(remaining)?;
let (remaining, _ws) = opt(tuple((start_of_line, many0(blank_line))))(remaining)?;
@ -92,7 +91,6 @@ fn _heading<'b, 'g, 'r, 's>(
};
let is_archived = pre_headline.tags.contains(&"ARCHIVE");
let contents = get_consumed(contents_begin, remaining);
let source = get_consumed(input, remaining);
Ok((
remaining,
@ -114,11 +112,6 @@ fn _heading<'b, 'g, 'r, 's>(
scheduled,
deadline,
closed,
contents: if contents.len() > 0 {
Some(Into::<&str>::into(contents))
} else {
None
},
},
))
}

View File

@ -38,7 +38,7 @@ where
space0,
alt((line_ending, eof)),
)))(remaining)?;
let (remaining, post_blank) =
let (remaining, _trailing_ws) =
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
let source = get_consumed(input, remaining);
Ok((
@ -49,7 +49,6 @@ where
context.get_global_settings(),
affiliated_keywords,
),
post_blank: post_blank.map(Into::<&str>::into),
},
))
}

View File

@ -1,8 +1,6 @@
use nom::branch::alt;
use nom::character::complete::space1;
use nom::combinator::consumed;
use nom::combinator::eof;
use nom::combinator::opt;
use nom::combinator::recognize;
use nom::combinator::verify;
use nom::multi::many1;
@ -15,7 +13,6 @@ use super::org_source::OrgSource;
use super::util::blank_line;
use super::util::get_consumed;
use super::util::maybe_consume_trailing_whitespace_if_not_exiting;
use super::util::org_line_ending;
use crate::context::parser_with_context;
use crate::context::ContextElement;
use crate::context::ExitClass;
@ -75,57 +72,6 @@ where
))
}
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
pub(crate) fn empty_paragraph<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Paragraph<'s>> {
// If it is just a single newline then source, contents, and post-blank are "\n".
// If it has multiple newlines then contents is the first "\n" and post-blank is all the new lines.
// If there are any spaces on the first line then post-blank excludes the first line.
let exit_matcher = parser_with_context!(exit_matcher_parser)(context);
let (remaining, first_line_with_spaces) =
opt(recognize(tuple((space1, org_line_ending))))(input)?;
let post_blank_begin = remaining;
if let Some(first_line_with_spaces) = first_line_with_spaces {
let (remaining, _additional_lines) =
recognize(many_till(blank_line, exit_matcher))(remaining)?;
let post_blank = get_consumed(post_blank_begin, remaining);
let source = get_consumed(input, remaining);
Ok((
remaining,
Paragraph::of_text_full(
Into::<&str>::into(source),
Into::<&str>::into(first_line_with_spaces),
Some(Into::<&str>::into(first_line_with_spaces)),
Some(Into::<&str>::into(post_blank)),
),
))
} else {
let (remaining, first_line) = blank_line(remaining)?;
let (remaining, _additional_lines) =
recognize(many_till(blank_line, exit_matcher))(remaining)?;
let post_blank = get_consumed(post_blank_begin, remaining);
let source = get_consumed(input, remaining);
Ok((
remaining,
Paragraph::of_text_full(
Into::<&str>::into(source),
Into::<&str>::into(first_line),
Some(Into::<&str>::into(first_line)),
Some(Into::<&str>::into(post_blank)),
),
))
}
}
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
@ -153,7 +99,6 @@ mod tests {
use crate::context::List;
use crate::parser::element_parser::element;
use crate::parser::org_source::OrgSource;
use crate::parser::paragraph::empty_paragraph;
use crate::types::StandardProperties;
#[test]
@ -170,17 +115,4 @@ mod tests {
assert_eq!(first_paragraph.get_source(), "foo bar baz\n\n");
assert_eq!(second_paragraph.get_source(), "lorem ipsum");
}
#[test]
fn paragraph_whitespace() {
let input = OrgSource::new("\n");
let global_settings = GlobalSettings::default();
let initial_context = ContextElement::document_context();
let initial_context = Context::new(&global_settings, List::new(&initial_context));
let paragraph_matcher = bind_context!(empty_paragraph, &initial_context);
let (remaining, paragraph) = paragraph_matcher(input).expect("Parse paragraph");
assert_eq!(Into::<&str>::into(remaining), "");
assert_eq!(paragraph.get_source(), "\n");
assert_eq!(paragraph.get_contents(), Some("\n"));
}
}

View File

@ -33,7 +33,7 @@ pub(crate) fn planning<'b, 'g, 'r, 's>(
many1(parser_with_context!(planning_parameter)(context))(remaining)?;
let (remaining, _trailing_ws) = tuple((space0, org_line_ending))(remaining)?;
let (remaining, post_blank) =
let (remaining, _trailing_ws) =
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
let source = get_consumed(input, remaining);
@ -62,7 +62,6 @@ pub(crate) fn planning<'b, 'g, 'r, 's>(
scheduled,
deadline,
closed,
post_blank: post_blank.map(Into::<&str>::into),
},
))
}

View File

@ -6,7 +6,6 @@ use nom::character::complete::anychar;
use nom::character::complete::line_ending;
use nom::character::complete::space0;
use nom::character::complete::space1;
use nom::combinator::consumed;
use nom::combinator::eof;
use nom::combinator::opt;
use nom::combinator::recognize;
@ -65,11 +64,14 @@ pub(crate) fn property_drawer<'b, 'g, 'r, 's>(
let parser_context = context.with_additional_node(&contexts[0]);
let parser_context = parser_context.with_additional_node(&contexts[1]);
let parser_context = parser_context.with_additional_node(&contexts[2]);
let (remaining, (contents, children)) =
consumed(parser_with_context!(children)(&parser_context))(remaining)?;
let node_property_matcher = parser_with_context!(node_property)(&parser_context);
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
let (remaining, (children, _exit_contents)) =
many_till(node_property_matcher, exit_matcher)(remaining)?;
let (remaining, _end) = property_drawer_end(&parser_context, remaining)?;
let (remaining, post_blank) =
let (remaining, _trailing_ws) =
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
let source = get_consumed(input, remaining);
@ -78,31 +80,10 @@ pub(crate) fn property_drawer<'b, 'g, 'r, 's>(
PropertyDrawer {
source: source.into(),
children,
contents: if contents.len() > 0 {
Some(contents.into())
} else {
None
},
post_blank: post_blank.map(Into::<&str>::into),
},
))
}
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
fn children<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Vec<NodeProperty<'s>>> {
let node_property_matcher = parser_with_context!(node_property)(context);
let exit_matcher = parser_with_context!(exit_matcher_parser)(context);
let (remaining, (children, _exit_contents)) =
many_till(node_property_matcher, exit_matcher)(input)?;
Ok((remaining, children))
}
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(_context))

View File

@ -1,3 +1,4 @@
use nom::combinator::consumed;
use nom::combinator::opt;
use nom::combinator::recognize;
use nom::combinator::verify;
@ -58,12 +59,12 @@ pub(crate) fn zeroth_section<'b, 'g, 'r, 's>(
many0(blank_line),
)))(input)?;
let (remaining, (mut children, _exit_contents)) = verify(
let (remaining, (contents, (mut children, _exit_contents))) = consumed(verify(
many_till(element_matcher, exit_matcher),
|(children, _exit_contents)| {
!children.is_empty() || comment_and_property_drawer_element.is_some()
},
)(remaining)?;
))(remaining)?;
if let Some((comment, property_drawer, _ws)) = comment_and_property_drawer_element {
children.insert(0, Element::PropertyDrawer(property_drawer));
@ -80,6 +81,7 @@ pub(crate) fn zeroth_section<'b, 'g, 'r, 's>(
remaining,
Section {
source: source.into(),
contents: Some(contents.into()),
post_blank: post_blank.map(Into::<&str>::into),
children,
},
@ -116,12 +118,12 @@ pub(crate) fn section<'b, 'g, 'r, 's>(
remaining = remain;
input = remain;
}
let (remaining, (mut children, _exit_contents)) = verify(
let (remaining, (contents, (mut children, _exit_contents))) = consumed(verify(
many_till(element_matcher, exit_matcher),
|(children, _exit_contents)| {
!children.is_empty() || property_drawer_element.is_some() || planning_element.is_some()
},
)(remaining)?;
))(remaining)?;
if let Some(ele) = property_drawer_element.map(Element::PropertyDrawer) {
children.insert(0, ele);
}
@ -137,6 +139,7 @@ pub(crate) fn section<'b, 'g, 'r, 's>(
remaining,
Section {
source: source.into(),
contents: Some(contents.into()),
post_blank: post_blank.map(Into::<&str>::into),
children,
},

View File

@ -35,12 +35,12 @@ pub struct Heading<'s> {
pub scheduled: Option<Timestamp<'s>>,
pub deadline: Option<Timestamp<'s>>,
pub closed: Option<Timestamp<'s>>,
pub contents: Option<&'s str>,
}
#[derive(Debug)]
pub struct Section<'s> {
pub source: &'s str,
pub contents: Option<&'s str>,
pub post_blank: Option<&'s str>,
pub children: Vec<Element<'s>>,
}
@ -101,7 +101,7 @@ impl<'s> StandardProperties<'s> for Section<'s> {
}
fn get_contents<'b>(&'b self) -> Option<&'s str> {
Some(self.source)
self.contents
}
fn get_post_blank(&self) -> PostBlank {
@ -119,7 +119,32 @@ impl<'s> StandardProperties<'s> for Heading<'s> {
}
fn get_contents<'b>(&'b self) -> Option<&'s str> {
self.contents
let first_child = self.children.first();
let last_child = self.children.last();
match (first_child, last_child) {
(None, None) => None,
(None, Some(_)) | (Some(_), None) => unreachable!(),
(Some(first_child), Some(last_child)) => {
let first_child_offset = first_child.get_source().as_ptr() as usize;
let last_child_offset = {
let last_child_source = last_child.get_source();
last_child_source.as_ptr() as usize + last_child_source.len()
};
let source_offset = self.source.as_ptr() as usize;
debug_assert!(super::lesser_element::is_slice_of(
self.source,
first_child.get_source()
));
debug_assert!(super::lesser_element::is_slice_of(
self.source,
last_child.get_source()
));
Some(
&self.source[(first_child_offset - source_offset)
..(last_child_offset - first_child_offset)],
)
}
}
}
fn get_post_blank(&self) -> PostBlank {

View File

@ -103,8 +103,6 @@ pub struct Drawer<'s> {
pub struct PropertyDrawer<'s> {
pub source: &'s str,
pub children: Vec<NodeProperty<'s>>,
pub contents: Option<&'s str>,
pub post_blank: Option<&'s str>,
}
#[derive(Debug)]
@ -260,15 +258,11 @@ impl<'s> StandardProperties<'s> for PropertyDrawer<'s> {
}
fn get_contents<'b>(&'b self) -> Option<&'s str> {
self.contents
todo!()
}
fn get_post_blank(&self) -> PostBlank {
self.post_blank
.map(|text| text.lines().count())
.unwrap_or(0)
.try_into()
.expect("Too much post-blank to fit into a PostBlank.")
todo!()
}
}
@ -278,11 +272,11 @@ impl<'s> StandardProperties<'s> for NodeProperty<'s> {
}
fn get_contents<'b>(&'b self) -> Option<&'s str> {
None
todo!()
}
fn get_post_blank(&self) -> PostBlank {
0
todo!()
}
}

View File

@ -141,7 +141,6 @@ pub struct Planning<'s> {
pub scheduled: Option<Timestamp<'s>>,
pub deadline: Option<Timestamp<'s>>,
pub closed: Option<Timestamp<'s>>,
pub post_blank: Option<&'s str>,
}
#[derive(Debug)]
@ -156,7 +155,6 @@ pub struct FixedWidthArea<'s> {
pub struct HorizontalRule<'s> {
pub source: &'s str,
pub affiliated_keywords: AffiliatedKeywords<'s>,
pub post_blank: Option<&'s str>,
}
#[derive(Debug)]
@ -204,29 +202,10 @@ impl<'s> Paragraph<'s> {
///
/// This is used for elements that support an "empty" content like greater blocks.
pub(crate) fn of_text(source: &'s str, body: &'s str) -> Self {
// TODO: This should be replaced with of_text_full.
Paragraph {
source,
contents: None,
post_blank: None,
affiliated_keywords: AffiliatedKeywords::default(),
children: vec![Object::PlainText(PlainText { source: body })],
}
}
/// Generate a paragraph of the passed in text with no additional properties.
///
/// This is used for elements that support an "empty" content like greater blocks.
pub(crate) fn of_text_full(
source: &'s str,
body: &'s str,
contents: Option<&'s str>,
post_blank: Option<&'s str>,
) -> Self {
Paragraph {
source,
contents,
post_blank,
contents: None, // TODO
post_blank: None, // TODO
affiliated_keywords: AffiliatedKeywords::default(),
children: vec![Object::PlainText(PlainText { source: body })],
}
@ -411,15 +390,11 @@ impl<'s> StandardProperties<'s> for Planning<'s> {
}
fn get_contents<'b>(&'b self) -> Option<&'s str> {
None
todo!()
}
fn get_post_blank(&self) -> PostBlank {
self.post_blank
.map(|text| text.lines().count())
.unwrap_or(0)
.try_into()
.expect("Too much post-blank to fit into a PostBlank.")
todo!()
}
}
@ -447,15 +422,11 @@ impl<'s> StandardProperties<'s> for HorizontalRule<'s> {
}
fn get_contents<'b>(&'b self) -> Option<&'s str> {
None
todo!()
}
fn get_post_blank(&self) -> PostBlank {
self.post_blank
.map(|text| text.lines().count())
.unwrap_or(0)
.try_into()
.expect("Too much post-blank to fit into a PostBlank.")
todo!()
}
}