End list items when the following line is indented less than or equal to the current item.

This commit is contained in:
Tom Alexander 2023-03-19 13:05:37 -04:00
parent 6d19eeb0f4
commit bf3464c65c
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 7 additions and 9 deletions

View File

@ -1,6 +1,4 @@
use crate::parser::document; use crate::parser::document;
use crate::parser::item;
use crate::parser::ContextTree;
use tracing::Level; use tracing::Level;
use tracing_subscriber::fmt::format::FmtSpan; use tracing_subscriber::fmt::format::FmtSpan;
@ -20,10 +18,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.with_span_events(FmtSpan::ENTER | FmtSpan::EXIT) .with_span_events(FmtSpan::ENTER | FmtSpan::EXIT)
.finish(); .finish();
tracing::subscriber::set_global_default(subscriber)?; tracing::subscriber::set_global_default(subscriber)?;
// let parsed = document(TEST_DOC); let parsed = document(TEST_DOC);
// println!("{}\n\n\n", TEST_DOC); println!("{}\n\n\n", TEST_DOC);
let initial_context: ContextTree<'_, '_> = ContextTree::new();
let parsed = item(&initial_context, " 1. foo\n2. bar");
println!("{:#?}", parsed); println!("{:#?}", parsed);
Ok(()) Ok(())
} }

View File

@ -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, check_box))),
opt(tuple((space, item_tag))), opt(tuple((space, item_tag))),
space, space,
// TODO: This context should probably be something involving the item
context_many_till(&list_item_context, text_element, item_end), context_many_till(&list_item_context, text_element, item_end),
))(remaining)?; ))(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> { 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 item_matcher = parser_with_context!(item)(&context);
let line_indented_matcher = parser_with_context!(line_indented_lte)(&context);
alt(( alt((
paragraph_end, 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)))), recognize(tuple((line_ending, peek(item_matcher)))),
))(i) ))(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( let matched = recognize(verify(
tuple((line_ending::<&str, _>, space0, anychar)), tuple((space0::<&str, _>, anychar)),
|(_newline, _space0, _anychar)| _space0.len() <= *current_item_indent_level, |(_space0, _anychar)| _space0.len() <= *current_item_indent_level,
))(i)?; ))(i)?;
Ok(matched) Ok(matched)