···20202121## Usage
22222323-Current version: [](https://crates.io/crates/germ)
2323+Current version:
2424+[](https://crates.io/crates/germ)
24252526```toml
2627# Cargo.toml
···3940### Features
40414142| Feature | Description |
4242-|-----------|------------------------------------------------------------------|
4343+| --------- | ---------------------------------------------------------------- |
4344| `ast` | Construct AST trees from raw Gemtext. |
4445| `convert` | Convert from Gemtext to markup formats such as HTML or Markdown. |
4546| `request` | Make Gemini requests and get sane, structured results. |
4647| `meta` | Structure-ize a Gemini response's meta section |
4748| `macros` | Macros to aid with various Germ-related functionalities |
4949+| `quick` | Quick functions to create valid Gemtext elements from input |
48504951### Examples
5052
···11+// This file is part of Germ <https://github.com/gemrest/germ>.
22+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
33+//
44+// This program is free software: you can redistribute it and/or modify
55+// it under the terms of the GNU General Public License as published by
66+// the Free Software Foundation, version 3.
77+//
88+// This program is distributed in the hope that it will be useful, but
99+// WITHOUT ANY WARRANTY; without even the implied warranty of
1010+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1111+// General Public License for more details.
1212+//
1313+// You should have received a copy of the GNU General Public License
1414+// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515+//
1616+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717+// SPDX-License-Identifier: GPL-3.0-only
1818+1919+pub enum HeadingLevel {
2020+ One,
2121+ Two,
2222+ Three,
2323+}
2424+2525+#[must_use]
2626+pub fn heading(text: &str, level: &HeadingLevel) -> String {
2727+ format!(
2828+ "{} {}",
2929+ match level {
3030+ HeadingLevel::One => "#",
3131+ HeadingLevel::Two => "##",
3232+ HeadingLevel::Three => "###",
3333+ },
3434+ text
3535+ )
3636+}
3737+3838+#[must_use]
3939+pub fn list_item(text: &str) -> String { format!("* {}", text) }
4040+4141+#[must_use]
4242+pub fn list_items(items: &[&str]) -> String {
4343+ items
4444+ .iter()
4545+ .map(|item| list_item(item))
4646+ .collect::<Vec<_>>()
4747+ .join("\n")
4848+}
4949+5050+#[must_use]
5151+pub fn link(text: &str, location: Option<&str>) -> String {
5252+ format!(
5353+ "=> {}{}",
5454+ text,
5555+ location.map_or_else(|| "".to_string(), |l| format!(" {}", l))
5656+ )
5757+}
5858+5959+#[must_use]
6060+pub fn block_quote(text: &str) -> String { format!("> {}", text) }
6161+6262+#[must_use]
6363+pub fn preformatted_text(text: &str, alt_text: Option<&str>) -> String {
6464+ format!("```{}\n{}\n```", alt_text.unwrap_or(""), text)
6565+}
+71
crates/germ/tests/quick.rs
···11+// This file is part of Germ <https://github.com/gemrest/germ>.
22+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
33+//
44+// This program is free software: you can redistribute it and/or modify
55+// it under the terms of the GNU General Public License as published by
66+// the Free Software Foundation, version 3.
77+//
88+// This program is distributed in the hope that it will be useful, but
99+// WITHOUT ANY WARRANTY; without even the implied warranty of
1010+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1111+// General Public License for more details.
1212+//
1313+// You should have received a copy of the GNU General Public License
1414+// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515+//
1616+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717+// SPDX-License-Identifier: GPL-3.0-only
1818+1919+#[cfg(test)]
2020+mod test {
2121+ use germ::quick::{self, heading};
2222+2323+ #[test]
2424+ fn all_heading_levels() {
2525+ assert_eq!(heading("Soup", &germ::quick::HeadingLevel::One), "# Soup");
2626+ assert_eq!(
2727+ heading("Vegetables", &germ::quick::HeadingLevel::Two),
2828+ "## Vegetables"
2929+ );
3030+ assert_eq!(
3131+ heading("Fruits", &germ::quick::HeadingLevel::Three),
3232+ "### Fruits"
3333+ );
3434+ }
3535+3636+ #[test]
3737+ fn list_item() {
3838+ assert_eq!(quick::list_item("Soup"), "* Soup");
3939+ }
4040+4141+ #[test]
4242+ fn list_items() {
4343+ assert_eq!(
4444+ quick::list_items(&["Soup", "Vegetables", "Fruits"]),
4545+ "* Soup\n* Vegetables\n* Fruits"
4646+ );
4747+ }
4848+4949+ #[test]
5050+ fn link_variants() {
5151+ assert_eq!(quick::link("Soup", None), "=> Soup");
5252+ assert_eq!(
5353+ quick::link("Soup", Some("gemini://soup.com")),
5454+ "=> Soup gemini://soup.com"
5555+ );
5656+ }
5757+5858+ #[test]
5959+ fn block_quote() {
6060+ assert_eq!(quick::block_quote("Soup"), "> Soup");
6161+ }
6262+6363+ #[test]
6464+ fn preformatted_text_variants() {
6565+ assert_eq!(quick::preformatted_text("Soup", None), "```\nSoup\n```");
6666+ assert_eq!(
6767+ quick::preformatted_text("Vegetables", Some("Fruits")),
6868+ "```Fruits\nVegetables\n```"
6969+ );
7070+ }
7171+}