From bf3464c65c95771803a4edcdd3932c230198a7b6 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 19 Mar 2023 13:05:37 -0400 Subject: [PATCH] End list items when the following line is indented less than or equal to the current item. --- src/main.rs | 8 ++------ src/parser/plain_list.rs | 8 +++++--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2ac52e53..a50a86c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,4 @@ use crate::parser::document; -use crate::parser::item; -use crate::parser::ContextTree; use tracing::Level; use tracing_subscriber::fmt::format::FmtSpan; @@ -20,10 +18,8 @@ fn main() -> Result<(), Box> { .with_span_events(FmtSpan::ENTER | FmtSpan::EXIT) .finish(); tracing::subscriber::set_global_default(subscriber)?; - // let parsed = document(TEST_DOC); - // println!("{}\n\n\n", TEST_DOC); - let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let parsed = item(&initial_context, " 1. foo\n2. bar"); + let parsed = document(TEST_DOC); + println!("{}\n\n\n", TEST_DOC); println!("{:#?}", parsed); Ok(()) } diff --git a/src/parser/plain_list.rs b/src/parser/plain_list.rs index 07fd7d1b..c579f5b8 100644 --- a/src/parser/plain_list.rs +++ b/src/parser/plain_list.rs @@ -44,7 +44,6 @@ pub fn item<'r, 's>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, ListIt opt(tuple((space, check_box))), opt(tuple((space, item_tag))), space, - // TODO: This context should probably be something involving the item context_many_till(&list_item_context, text_element, item_end), ))(remaining)?; @@ -113,8 +112,11 @@ fn tag_separator<'s>(i: &'s str) -> Res<&'s str, &'s str> { pub fn item_end<'r, 's>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, &'s str> { let item_matcher = parser_with_context!(item)(&context); + let line_indented_matcher = parser_with_context!(line_indented_lte)(&context); alt(( paragraph_end, + recognize(tuple((line_ending, peek(line_indented_matcher)))), + // TODO: Do we still need the item_matcher entry here? If we remove it, then child items should become part of the body of the parent item which would match the description on https://orgmode.org/worg/org-syntax.html recognize(tuple((line_ending, peek(item_matcher)))), ))(i) } @@ -125,8 +127,8 @@ fn line_indented_lte<'r, 's>(context: Context<'r, 's>, i: &'s str) -> Res<&'s st )?; let matched = recognize(verify( - tuple((line_ending::<&str, _>, space0, anychar)), - |(_newline, _space0, _anychar)| _space0.len() <= *current_item_indent_level, + tuple((space0::<&str, _>, anychar)), + |(_space0, _anychar)| _space0.len() <= *current_item_indent_level, ))(i)?; Ok(matched)