diff options
| author | Fuwn <[email protected]> | 2024-06-19 07:34:04 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-06-19 07:34:04 +0000 |
| commit | fe03915e1bddf286af8ed50a9b69c52a553be5ea (patch) | |
| tree | cc687ba81d6672c3645eb730fdb18f85ea0e9289 | |
| parent | feat: single Node gemtext content conversion (diff) | |
| download | germ-fe03915e1bddf286af8ed50a9b69c52a553be5ea.tar.xz germ-fe03915e1bddf286af8ed50a9b69c52a553be5ea.zip | |
fix(ast): preserve newline structure at end of gemtext
| -rw-r--r-- | src/ast/container.rs | 18 | ||||
| -rw-r--r-- | src/ast/macros.rs | 2 | ||||
| -rw-r--r-- | tests/ast.rs | 8 |
3 files changed, 20 insertions, 8 deletions
diff --git a/src/ast/container.rs b/src/ast/container.rs index d8f2817..1e6c63f 100644 --- a/src/ast/container.rs +++ b/src/ast/container.rs @@ -82,6 +82,14 @@ impl Ast { )); } + if source.chars().last().map_or(false, |c| c == '\n') { + if let Some(last) = ast.last() { + if !matches!(last, Node::Whitespace) { + ast.push(Node::Whitespace); + } + } + } + Self { inner: ast } } @@ -95,10 +103,10 @@ impl Ast { /// // the original Gemtext. /// assert_eq!( /// germ::ast::Ast::from_nodes( - /// germ::gemini_to_ast!("=> / Home\n").inner().to_vec() + /// germ::gemini_to_ast!("=> / Home").inner().to_vec() /// ) /// .to_gemtext(), - /// "=> / Home\n" + /// "=> / Home" /// ); /// ``` #[must_use] @@ -145,6 +153,10 @@ impl Ast { } } + if gemtext.ends_with('\n') && !gemtext.ends_with("\n\n") { + gemtext.pop(); + } + gemtext } @@ -279,7 +291,7 @@ impl Ast { break; } - // This as a catchall, it does a number of things. + // This as a catchall. It does a number of things. _ => { if *in_preformatted { // If we are in a preformatted line context, add the line to the diff --git a/src/ast/macros.rs b/src/ast/macros.rs index 086402c..435fd3b 100644 --- a/src/ast/macros.rs +++ b/src/ast/macros.rs @@ -26,7 +26,7 @@ /// germ::gemini_to_ast!("=> / A link!").to_gemtext(), /// // `to_gemtext` appends a newline to all responses, so let's make sure we /// // account for that. -/// format!("{}\n", "=> / A link!"), +/// "=> / A link!", /// ); #[macro_export] macro_rules! gemini_to_ast { diff --git a/tests/ast.rs b/tests/ast.rs index 9bb7fce..eedd546 100644 --- a/tests/ast.rs +++ b/tests/ast.rs @@ -88,7 +88,7 @@ That was a link without text."#; Ast::from_string(EXAMPLE_GEMTEXT).to_gemtext(), // `to_gemtext` appends a newline to all responses, so let's make sure we // account for that. - format!("{}\n", EXAMPLE_GEMTEXT), + EXAMPLE_GEMTEXT ); } @@ -98,16 +98,16 @@ That was a link without text."#; germ::gemini_to_ast!(EXAMPLE_GEMTEXT).to_gemtext(), // `to_gemtext` appends a newline to all responses, so let's make sure we // account for that. - format!("{}\n", EXAMPLE_GEMTEXT), + EXAMPLE_GEMTEXT ); } #[test] fn gemtext_to_ast_then_node_to_ast_to_gemtext() { assert_eq!( - Ast::from_nodes(germ::gemini_to_ast!("=> / Home\n").inner().to_vec()) + Ast::from_nodes(germ::gemini_to_ast!("=> / Home").inner().to_vec()) .to_gemtext(), - "=> / Home\n" + "=> / Home" ); } } |