From fe03915e1bddf286af8ed50a9b69c52a553be5ea Mon Sep 17 00:00:00 2001 From: Fuwn Date: Wed, 19 Jun 2024 07:34:04 +0000 Subject: fix(ast): preserve newline structure at end of gemtext --- src/ast/container.rs | 18 +++++++++++++++--- src/ast/macros.rs | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) (limited to 'src') 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 { -- cgit v1.2.3