From 49898e1c1214470e2684e661b558ee198fba18f0 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Wed, 19 Jun 2024 07:13:56 +0000 Subject: feat: single Node gemtext content conversion --- src/ast/container.rs | 19 +++++++++++++++++++ src/ast/node.rs | 8 ++++++++ 2 files changed, 27 insertions(+) (limited to 'src') diff --git a/src/ast/container.rs b/src/ast/container.rs index 51931c6..d8f2817 100644 --- a/src/ast/container.rs +++ b/src/ast/container.rs @@ -85,6 +85,25 @@ impl Ast { Self { inner: ast } } + /// Build an AST tree from a [`Vec`] of [`Node`]s + /// + /// # Example + /// + /// ```rust + /// // This assertion converts the Gemtext "=> / Home\n" to an AST tree of one + /// // node, then converts the AST tree back to Gemtext, and compares it against + /// // the original Gemtext. + /// assert_eq!( + /// germ::ast::Ast::from_nodes( + /// germ::gemini_to_ast!("=> / Home\n").inner().to_vec() + /// ) + /// .to_gemtext(), + /// "=> / Home\n" + /// ); + /// ``` + #[must_use] + pub fn from_nodes(nodes: Vec) -> Self { Self { inner: nodes } } + #[must_use] pub fn to_gemtext(&self) -> String { let mut gemtext = String::new(); diff --git a/src/ast/node.rs b/src/ast/node.rs index e80ef84..1040d2b 100644 --- a/src/ast/node.rs +++ b/src/ast/node.rs @@ -171,3 +171,11 @@ pub enum Node { /// A whitespace line, a line which contains nothing but whitespace. Whitespace, } + +impl Node { + /// Obtain the Gemtext content of a single [`Node`] as a [`String`] + #[must_use] + pub fn content(&self) -> String { + super::Ast::from_nodes(vec![self.to_owned()]).to_gemtext() + } +} -- cgit v1.2.3