🦠 The Definitive Gemini Protocol Toolkit
gemini gemini-protocol gemtext parser zero-dependency toolkit ast converter html markdown cli networking
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(germ): quick gemtext functions

Fuwn ba0e8077 539c48e0

+146 -4
+4 -2
README.md
··· 20 20 21 21 ## Usage 22 22 23 - Current version: [![crates.io](https://img.shields.io/crates/v/germ.svg)](https://crates.io/crates/germ) 23 + Current version: 24 + [![crates.io](https://img.shields.io/crates/v/germ.svg)](https://crates.io/crates/germ) 24 25 25 26 ```toml 26 27 # Cargo.toml ··· 39 40 ### Features 40 41 41 42 | Feature | Description | 42 - |-----------|------------------------------------------------------------------| 43 + | --------- | ---------------------------------------------------------------- | 43 44 | `ast` | Construct AST trees from raw Gemtext. | 44 45 | `convert` | Convert from Gemtext to markup formats such as HTML or Markdown. | 45 46 | `request` | Make Gemini requests and get sane, structured results. | 46 47 | `meta` | Structure-ize a Gemini response's meta section | 47 48 | `macros` | Macros to aid with various Germ-related functionalities | 49 + | `quick` | Quick functions to create valid Gemtext elements from input | 48 50 49 51 ### Examples 50 52
+2 -1
crates/germ/Cargo.toml
··· 2 2 3 3 [package] 4 4 name = "germ" 5 - version = "0.3.0" 5 + version = "0.3.1" 6 6 authors = ["Fuwn <contact@fuwn.me>"] 7 7 edition = "2021" 8 8 description = "The Ultimate Gemini Toolkit." ··· 21 21 macros = ["ast", "convert"] 22 22 meta = [] 23 23 request = ["rustls", "url", "anyhow"] 24 + quick = [] 24 25 25 26 [dependencies] 26 27 anyhow = { version = "1.0.57", optional = true } # `Result`
+4 -1
crates/germ/src/lib.rs
··· 27 27 clippy::nursery, 28 28 clippy::pedantic 29 29 )] 30 - #![doc = include_str!("../../../../README.md")] 30 + #![doc = include_str!("../../../README.md")] 31 31 #![recursion_limit = "128"] 32 32 33 33 #[cfg(feature = "ast")] ··· 41 41 42 42 #[cfg(feature = "meta")] 43 43 pub mod meta; 44 + 45 + #[cfg(feature = "quick")] 46 + pub mod quick;
+65
crates/germ/src/quick.rs
··· 1 + // This file is part of Germ <https://github.com/gemrest/germ>. 2 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 3 + // 4 + // This program is free software: you can redistribute it and/or modify 5 + // it under the terms of the GNU General Public License as published by 6 + // the Free Software Foundation, version 3. 7 + // 8 + // This program is distributed in the hope that it will be useful, but 9 + // WITHOUT ANY WARRANTY; without even the implied warranty of 10 + // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 + // General Public License for more details. 12 + // 13 + // You should have received a copy of the GNU General Public License 14 + // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 + // 16 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 + // SPDX-License-Identifier: GPL-3.0-only 18 + 19 + pub enum HeadingLevel { 20 + One, 21 + Two, 22 + Three, 23 + } 24 + 25 + #[must_use] 26 + pub fn heading(text: &str, level: &HeadingLevel) -> String { 27 + format!( 28 + "{} {}", 29 + match level { 30 + HeadingLevel::One => "#", 31 + HeadingLevel::Two => "##", 32 + HeadingLevel::Three => "###", 33 + }, 34 + text 35 + ) 36 + } 37 + 38 + #[must_use] 39 + pub fn list_item(text: &str) -> String { format!("* {}", text) } 40 + 41 + #[must_use] 42 + pub fn list_items(items: &[&str]) -> String { 43 + items 44 + .iter() 45 + .map(|item| list_item(item)) 46 + .collect::<Vec<_>>() 47 + .join("\n") 48 + } 49 + 50 + #[must_use] 51 + pub fn link(text: &str, location: Option<&str>) -> String { 52 + format!( 53 + "=> {}{}", 54 + text, 55 + location.map_or_else(|| "".to_string(), |l| format!(" {}", l)) 56 + ) 57 + } 58 + 59 + #[must_use] 60 + pub fn block_quote(text: &str) -> String { format!("> {}", text) } 61 + 62 + #[must_use] 63 + pub fn preformatted_text(text: &str, alt_text: Option<&str>) -> String { 64 + format!("```{}\n{}\n```", alt_text.unwrap_or(""), text) 65 + }
+71
crates/germ/tests/quick.rs
··· 1 + // This file is part of Germ <https://github.com/gemrest/germ>. 2 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 3 + // 4 + // This program is free software: you can redistribute it and/or modify 5 + // it under the terms of the GNU General Public License as published by 6 + // the Free Software Foundation, version 3. 7 + // 8 + // This program is distributed in the hope that it will be useful, but 9 + // WITHOUT ANY WARRANTY; without even the implied warranty of 10 + // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 + // General Public License for more details. 12 + // 13 + // You should have received a copy of the GNU General Public License 14 + // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 + // 16 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 + // SPDX-License-Identifier: GPL-3.0-only 18 + 19 + #[cfg(test)] 20 + mod test { 21 + use germ::quick::{self, heading}; 22 + 23 + #[test] 24 + fn all_heading_levels() { 25 + assert_eq!(heading("Soup", &germ::quick::HeadingLevel::One), "# Soup"); 26 + assert_eq!( 27 + heading("Vegetables", &germ::quick::HeadingLevel::Two), 28 + "## Vegetables" 29 + ); 30 + assert_eq!( 31 + heading("Fruits", &germ::quick::HeadingLevel::Three), 32 + "### Fruits" 33 + ); 34 + } 35 + 36 + #[test] 37 + fn list_item() { 38 + assert_eq!(quick::list_item("Soup"), "* Soup"); 39 + } 40 + 41 + #[test] 42 + fn list_items() { 43 + assert_eq!( 44 + quick::list_items(&["Soup", "Vegetables", "Fruits"]), 45 + "* Soup\n* Vegetables\n* Fruits" 46 + ); 47 + } 48 + 49 + #[test] 50 + fn link_variants() { 51 + assert_eq!(quick::link("Soup", None), "=> Soup"); 52 + assert_eq!( 53 + quick::link("Soup", Some("gemini://soup.com")), 54 + "=> Soup gemini://soup.com" 55 + ); 56 + } 57 + 58 + #[test] 59 + fn block_quote() { 60 + assert_eq!(quick::block_quote("Soup"), "> Soup"); 61 + } 62 + 63 + #[test] 64 + fn preformatted_text_variants() { 65 + assert_eq!(quick::preformatted_text("Soup", None), "```\nSoup\n```"); 66 + assert_eq!( 67 + quick::preformatted_text("Vegetables", Some("Fruits")), 68 + "```Fruits\nVegetables\n```" 69 + ); 70 + } 71 + }