diff options
| -rw-r--r-- | crates/germ/src/ast/macros.rs | 4 | ||||
| -rw-r--r-- | crates/germ/src/convert.rs | 3 | ||||
| -rw-r--r-- | crates/germ/src/convert/macros.rs | 80 | ||||
| -rw-r--r-- | crates/germ/tests/ast.rs | 4 | ||||
| -rw-r--r-- | crates/germ/tests/convert.rs | 28 |
5 files changed, 112 insertions, 7 deletions
diff --git a/crates/germ/src/ast/macros.rs b/crates/germ/src/ast/macros.rs index b072daa..369e79e 100644 --- a/crates/germ/src/ast/macros.rs +++ b/crates/germ/src/ast/macros.rs @@ -18,9 +18,9 @@ //! Macros to aid with various Germ-related functionalities. -/// Convert a Gemini token tree into an `Ast` +/// Convert Gemtext an `Ast` /// -/// # Example +/// # Examples /// /// ```rust /// // Using a value diff --git a/crates/germ/src/convert.rs b/crates/germ/src/convert.rs index c68696e..2e323e1 100644 --- a/crates/germ/src/convert.rs +++ b/crates/germ/src/convert.rs @@ -23,6 +23,9 @@ use crate::ast::Ast; mod html; mod markdown; +#[cfg(feature = "macros")] +mod macros; + /// Different targets to convert Gemtext to pub enum Target { /// Convert Gemtext to HTML diff --git a/crates/germ/src/convert/macros.rs b/crates/germ/src/convert/macros.rs new file mode 100644 index 0000000..ca29050 --- /dev/null +++ b/crates/germ/src/convert/macros.rs @@ -0,0 +1,80 @@ +// This file is part of Germ <https://github.com/gemrest/germ>. +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +/// Convert Gemtext into HTML +/// +/// # Examples +/// +/// ```rust +/// // Using a value +/// assert_eq!( +/// germ::gemini_to_html!("=> /to hello !"), +/// "<a href=\"/to\">hello !</a><br>", +/// ); +/// +/// // Using raw Gemtext +/// assert_eq!( +/// germ::gemini_to_html! { => /to hello ! }, +/// "<a href=\"/to\">hello !</a><br>", +/// ); +/// ``` +#[macro_export] +macro_rules! gemini_to_html { + ($gemini:expr) => { + germ::convert::from_ast( + &germ::gemini_to_ast!($gemini), + &germ::convert::Target::HTML, + ) + }; + ($($gemini:tt)*) => { + germ::convert::from_ast( + &germ::gemini_to_ast!{ $($gemini)* }, + &germ::convert::Target::HTML, + ) + }; +} + +/// Convert Gemtext into Markdown +/// +/// # Examples +/// +/// ```rust +/// assert_eq!( +/// // Using a value +/// germ::gemini_to_md!("=> /to hello !"), +/// "[hello !](/to)\n", +/// ); +/// +/// // Using raw Gemtext +/// assert_eq!(germ::gemini_to_md! { => /to hello ! }, "[hello !](/to)\n",); +/// ``` +#[macro_export] +macro_rules! gemini_to_md { + ($gemini:expr) => { + germ::convert::from_ast( + &germ::gemini_to_ast!($gemini), + &germ::convert::Target::Markdown, + ) + }; + ($($gemini:tt)*) => { + germ::convert::from_ast( + &germ::gemini_to_ast!{ $($gemini)* }, + &germ::convert::Target::Markdown, + ) + }; +} diff --git a/crates/germ/tests/ast.rs b/crates/germ/tests/ast.rs index 302b9c9..c91d4b7 100644 --- a/crates/germ/tests/ast.rs +++ b/crates/germ/tests/ast.rs @@ -108,7 +108,7 @@ That was a link without text."#; } #[test] - fn gemtext_to_ast_then_ast_to_gemtext_macro() { + fn gemtext_to_ast_then_ast_to_gemtext_macro_expression() { assert_eq!( germ::gemini_to_ast!(EXAMPLE_GEMTEXT).to_gemtext(), // `to_gemtext` appends a newline to all responses, so let's make sure we @@ -118,7 +118,7 @@ That was a link without text."#; } #[test] - fn gemtext_to_ast_then_ast_to_gemtext_macro_simple() { + fn gemtext_to_ast_then_ast_to_gemtext_macro_block() { assert_eq!( germ::gemini_to_ast! { => / A link! diff --git a/crates/germ/tests/convert.rs b/crates/germ/tests/convert.rs index 40f337d..de1baf2 100644 --- a/crates/germ/tests/convert.rs +++ b/crates/germ/tests/convert.rs @@ -18,7 +18,11 @@ #[cfg(test)] mod test { - use germ::convert::{from_string, Target}; + use germ::{ + convert::{from_string, Target}, + gemini_to_html, + gemini_to_md, + }; #[test] fn convert_from_string_to_html_single_line() { @@ -34,9 +38,17 @@ mod test { } #[test] - fn convert_from_string_to_html_single_link() { + fn convert_from_string_to_html_single_link_macro_expression() { assert_eq!( - from_string("=> /to hello !", &Target::HTML), + gemini_to_html!("=> /to hello !"), + "<a href=\"/to\">hello !</a><br>", + ); + } + + #[test] + fn convert_from_string_to_html_single_link_macro_block() { + assert_eq!( + gemini_to_html! { => /to hello ! }, "<a href=\"/to\">hello !</a><br>", ); } @@ -58,4 +70,14 @@ mod test { "[hello !](/to)\n", ); } + + #[test] + fn convert_from_string_to_markdown_single_macro_expression() { + assert_eq!(gemini_to_md!("=> /to hello !"), "[hello !](/to)\n",); + } + + #[test] + fn convert_from_string_to_markdown_single_macro_block() { + assert_eq!(gemini_to_md! { => /to hello ! }, "[hello !](/to)\n",); + } } |