From 5171326d63dc94d2737d015dd94fe31383bb6182 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 31 Aug 2023 19:02:18 -0400 Subject: [PATCH 1/5] Create a test for special blocks. --- .../greater_element/greater_block/special_block.org | 5 +++++ src/parser/greater_block.rs | 10 +++++----- 2 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 org_mode_samples/greater_element/greater_block/special_block.org diff --git a/org_mode_samples/greater_element/greater_block/special_block.org b/org_mode_samples/greater_element/greater_block/special_block.org new file mode 100644 index 0000000..d3b6df1 --- /dev/null +++ b/org_mode_samples/greater_element/greater_block/special_block.org @@ -0,0 +1,5 @@ +#+begin_defun +foo + +{{{bar(baz)}}} +#+end_defun diff --git a/src/parser/greater_block.rs b/src/parser/greater_block.rs index 85dc7e3..ff7f59c 100644 --- a/src/parser/greater_block.rs +++ b/src/parser/greater_block.rs @@ -49,11 +49,11 @@ pub fn greater_block<'r, 's>( }), ))(remaining)?; let context_name = match Into::<&str>::into(name).to_lowercase().as_str() { - "center" => "center block", - "quote" => "quote block", - _ => "greater block", + "center" => "center block".to_owned(), + "quote" => "quote block".to_owned(), + name @ _ => format!("special block {}", name), }; - if immediate_in_section(context, context_name) { + if immediate_in_section(context, context_name.as_str()) { return Err(nom::Err::Error(CustomError::MyError(MyError( "Cannot nest objects of the same element".into(), )))); @@ -63,7 +63,7 @@ pub fn greater_block<'r, 's>( let (remaining, _nl) = line_ending(remaining)?; let parser_context = context .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) - .with_additional_node(ContextElement::Context(context_name)) + .with_additional_node(ContextElement::Context(context_name.as_str())) .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Alpha, exit_matcher: &exit_with_name, From 7a903acedc3cc8f70947178f9f44a76fbb7f6810 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 31 Aug 2023 19:04:44 -0400 Subject: [PATCH 2/5] Support special blocks in compare. --- src/compare/diff.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compare/diff.rs b/src/compare/diff.rs index 6e1386d..a633be8 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -737,7 +737,7 @@ fn compare_greater_block<'s>( let emacs_name = match rust.name.to_lowercase().as_str() { "center" => "center-block", "quote" => "quote-block", - _ => todo!(), + _ => "special-block", }; if assert_name(emacs, emacs_name).is_err() { this_status = DiffStatus::Bad; From 2a4d22bdd40fb3c262a257d99bce03c3edaa5dc6 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 31 Aug 2023 19:09:38 -0400 Subject: [PATCH 3/5] Add test for nesting of greater blocks. This shows that greater blocks of different types can be directly nested even if they are both special blocks (as long as they are different special blocks). --- .../greater_block/nested_special_block.org | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 org_mode_samples/greater_element/greater_block/nested_special_block.org diff --git a/org_mode_samples/greater_element/greater_block/nested_special_block.org b/org_mode_samples/greater_element/greater_block/nested_special_block.org new file mode 100644 index 0000000..b96dbee --- /dev/null +++ b/org_mode_samples/greater_element/greater_block/nested_special_block.org @@ -0,0 +1,12 @@ +#+begin_defun +foo +#+begin_lorem +ipsum +#+end_lorem +bar +#+begin_center +#+begin_quote +baz +#+end_quote +#+end_center +#+end_defun From 949d0989f473e17aa45bb172f260074a124358f4 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 31 Aug 2023 19:15:28 -0400 Subject: [PATCH 4/5] Add a test showing the current parser is broken with deeply nested greater blocks. --- .../greater_block/deeply_nested.org | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 org_mode_samples/greater_element/greater_block/deeply_nested.org diff --git a/org_mode_samples/greater_element/greater_block/deeply_nested.org b/org_mode_samples/greater_element/greater_block/deeply_nested.org new file mode 100644 index 0000000..4820474 --- /dev/null +++ b/org_mode_samples/greater_element/greater_block/deeply_nested.org @@ -0,0 +1,18 @@ +#+begin_defun +foo +#+begin_lorem +,#+begin_center +bar +,#+end_center +ipsum +#+end_lorem +baz +#+end_defun + +#+begin_center +#+begin_quote +#+begin_center +lorem +#+end_center +#+end_quote +#+end_center From 510985e97c8097f27f02d38a04b2206ea3e677e5 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 31 Aug 2023 19:17:46 -0400 Subject: [PATCH 5/5] Fix greater blocks by preventing nesting even if they are not the immediate parent. --- src/parser/greater_block.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/parser/greater_block.rs b/src/parser/greater_block.rs index ff7f59c..99ff850 100644 --- a/src/parser/greater_block.rs +++ b/src/parser/greater_block.rs @@ -12,6 +12,7 @@ use nom::multi::many_till; use nom::sequence::tuple; use super::org_source::OrgSource; +use super::util::in_section; use super::Context; use crate::error::CustomError; use crate::error::MyError; @@ -26,7 +27,6 @@ use crate::parser::source::SetSource; 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; use crate::parser::util::start_of_line; use crate::parser::Element; use crate::parser::Paragraph; @@ -53,7 +53,7 @@ pub fn greater_block<'r, 's>( "quote" => "quote block".to_owned(), name @ _ => format!("special block {}", name), }; - if immediate_in_section(context, context_name.as_str()) { + if in_section(context, context_name.as_str()) { return Err(nom::Err::Error(CustomError::MyError(MyError( "Cannot nest objects of the same element".into(), ))));