From 50d118840448614b6309621faccc595a817782d4 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Tue, 28 Jun 2022 08:03:40 +0000 Subject: fix(crate): crate locations for readme --- Cargo.toml | 2 +- crates/germ-macros-impl/Cargo.toml | 21 --- crates/germ-macros-impl/src/lib.rs | 58 ------- crates/germ/Cargo.toml | 32 ---- crates/germ/examples/ast.rs | 52 ------ crates/germ/examples/ast_to_gemtext.rs | 53 ------ crates/germ/examples/convert.html | 6 - crates/germ/examples/convert.md | 28 ---- crates/germ/examples/html.rs | 54 ------ crates/germ/examples/markdown.rs | 57 ------- crates/germ/examples/meta.rs | 24 --- crates/germ/examples/request.rs | 24 --- crates/germ/src/ast.rs | 28 ---- crates/germ/src/ast/container.rs | 290 --------------------------------- crates/germ/src/ast/macros.rs | 50 ------ crates/germ/src/ast/node.rs | 173 -------------------- crates/germ/src/convert.rs | 72 -------- crates/germ/src/convert/html.rs | 75 --------- crates/germ/src/convert/macros.rs | 80 --------- crates/germ/src/convert/markdown.rs | 77 --------- crates/germ/src/lib.rs | 46 ------ crates/germ/src/meta.rs | 162 ------------------ crates/germ/src/quick.rs | 65 -------- crates/germ/src/request.rs | 73 --------- crates/germ/src/request/response.rs | 71 -------- crates/germ/src/request/status.rs | 110 ------------- crates/germ/src/request/verifier.rs | 39 ----- crates/germ/tests/ast.rs | 131 --------------- crates/germ/tests/convert.rs | 83 ---------- crates/germ/tests/meta.rs | 101 ------------ crates/germ/tests/quick.rs | 71 -------- crates/germ/tests/status.rs | 32 ---- germ-macros-impl/Cargo.toml | 21 +++ germ-macros-impl/src/lib.rs | 58 +++++++ germ/Cargo.toml | 32 ++++ germ/examples/ast.rs | 52 ++++++ germ/examples/ast_to_gemtext.rs | 53 ++++++ germ/examples/convert.html | 6 + germ/examples/convert.md | 28 ++++ germ/examples/html.rs | 54 ++++++ germ/examples/markdown.rs | 57 +++++++ germ/examples/meta.rs | 24 +++ germ/examples/request.rs | 24 +++ germ/src/ast.rs | 28 ++++ germ/src/ast/container.rs | 290 +++++++++++++++++++++++++++++++++ germ/src/ast/macros.rs | 50 ++++++ germ/src/ast/node.rs | 173 ++++++++++++++++++++ germ/src/convert.rs | 72 ++++++++ germ/src/convert/html.rs | 75 +++++++++ germ/src/convert/macros.rs | 80 +++++++++ germ/src/convert/markdown.rs | 77 +++++++++ germ/src/lib.rs | 46 ++++++ germ/src/meta.rs | 162 ++++++++++++++++++ germ/src/quick.rs | 65 ++++++++ germ/src/request.rs | 73 +++++++++ germ/src/request/response.rs | 71 ++++++++ germ/src/request/status.rs | 110 +++++++++++++ germ/src/request/verifier.rs | 39 +++++ germ/tests/ast.rs | 131 +++++++++++++++ germ/tests/convert.rs | 83 ++++++++++ germ/tests/meta.rs | 101 ++++++++++++ germ/tests/quick.rs | 71 ++++++++ germ/tests/status.rs | 32 ++++ 63 files changed, 2239 insertions(+), 2239 deletions(-) delete mode 100644 crates/germ-macros-impl/Cargo.toml delete mode 100644 crates/germ-macros-impl/src/lib.rs delete mode 100644 crates/germ/Cargo.toml delete mode 100644 crates/germ/examples/ast.rs delete mode 100644 crates/germ/examples/ast_to_gemtext.rs delete mode 100644 crates/germ/examples/convert.html delete mode 100644 crates/germ/examples/convert.md delete mode 100644 crates/germ/examples/html.rs delete mode 100644 crates/germ/examples/markdown.rs delete mode 100644 crates/germ/examples/meta.rs delete mode 100644 crates/germ/examples/request.rs delete mode 100644 crates/germ/src/ast.rs delete mode 100644 crates/germ/src/ast/container.rs delete mode 100644 crates/germ/src/ast/macros.rs delete mode 100644 crates/germ/src/ast/node.rs delete mode 100644 crates/germ/src/convert.rs delete mode 100644 crates/germ/src/convert/html.rs delete mode 100644 crates/germ/src/convert/macros.rs delete mode 100644 crates/germ/src/convert/markdown.rs delete mode 100644 crates/germ/src/lib.rs delete mode 100644 crates/germ/src/meta.rs delete mode 100644 crates/germ/src/quick.rs delete mode 100644 crates/germ/src/request.rs delete mode 100644 crates/germ/src/request/response.rs delete mode 100644 crates/germ/src/request/status.rs delete mode 100644 crates/germ/src/request/verifier.rs delete mode 100644 crates/germ/tests/ast.rs delete mode 100644 crates/germ/tests/convert.rs delete mode 100644 crates/germ/tests/meta.rs delete mode 100644 crates/germ/tests/quick.rs delete mode 100644 crates/germ/tests/status.rs create mode 100644 germ-macros-impl/Cargo.toml create mode 100644 germ-macros-impl/src/lib.rs create mode 100644 germ/Cargo.toml create mode 100644 germ/examples/ast.rs create mode 100644 germ/examples/ast_to_gemtext.rs create mode 100644 germ/examples/convert.html create mode 100644 germ/examples/convert.md create mode 100644 germ/examples/html.rs create mode 100644 germ/examples/markdown.rs create mode 100644 germ/examples/meta.rs create mode 100644 germ/examples/request.rs create mode 100644 germ/src/ast.rs create mode 100644 germ/src/ast/container.rs create mode 100644 germ/src/ast/macros.rs create mode 100644 germ/src/ast/node.rs create mode 100644 germ/src/convert.rs create mode 100644 germ/src/convert/html.rs create mode 100644 germ/src/convert/macros.rs create mode 100644 germ/src/convert/markdown.rs create mode 100644 germ/src/lib.rs create mode 100644 germ/src/meta.rs create mode 100644 germ/src/quick.rs create mode 100644 germ/src/request.rs create mode 100644 germ/src/request/response.rs create mode 100644 germ/src/request/status.rs create mode 100644 germ/src/request/verifier.rs create mode 100644 germ/tests/ast.rs create mode 100644 germ/tests/convert.rs create mode 100644 germ/tests/meta.rs create mode 100644 germ/tests/quick.rs create mode 100644 germ/tests/status.rs diff --git a/Cargo.toml b/Cargo.toml index 550daba..75b09f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,4 @@ # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [workspace] -members = ["crates/germ", "crates/germ-macros-impl"] +members = ["germ", "germ-macros-impl"] diff --git a/crates/germ-macros-impl/Cargo.toml b/crates/germ-macros-impl/Cargo.toml deleted file mode 100644 index 90bf092..0000000 --- a/crates/germ-macros-impl/Cargo.toml +++ /dev/null @@ -1,21 +0,0 @@ -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[package] -name = "germ-macros-impl" -version = "0.1.0" -authors = ["Fuwn "] -edition = "2021" -description = "Germ Macro Implementations" -documentation = "https://docs.rs/germ" -readme = "../../README.md" -homepage = "https://github.com/gemrest/germ" -repository = "https://github.com/gemrest/germ" -license = "GPL-3.0-only" -keywords = ["gemini", "parser", "lexer", "markdown", "converter"] -categories = ["encoding"] - -[lib] -proc-macro = true - -[dependencies] -quote = "1.0.18" # Quasi-quoting diff --git a/crates/germ-macros-impl/src/lib.rs b/crates/germ-macros-impl/src/lib.rs deleted file mode 100644 index 30fdd81..0000000 --- a/crates/germ-macros-impl/src/lib.rs +++ /dev/null @@ -1,58 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -#![deny( - warnings, - nonstandard_style, - unused, - future_incompatible, - rust_2018_idioms, - unsafe_code, - clippy::all, - clippy::nursery, - clippy::pedantic -)] -#![feature(proc_macro_hygiene, proc_macro_span)] -#![recursion_limit = "128"] - -use proc_macro::TokenStream; - -/// Convert Gemtext into a token tree -/// -/// # Panics -/// -/// May panic if the Gemini could not be properly handled, for any reason. -#[proc_macro] -pub fn gemini_to_tt(input: TokenStream) -> TokenStream { - let mut tokens = input.into_iter(); - let mut span = tokens.next().unwrap().span(); - - for token in tokens { - span = span.join(token.span()).unwrap(); - } - - let gemini = span - .source_text() - .unwrap() - .lines() - .map(|l| l.trim_start().to_string()) - .collect::>() - .join("\n"); - - quote::quote!(#gemini).into() -} diff --git a/crates/germ/Cargo.toml b/crates/germ/Cargo.toml deleted file mode 100644 index 9561438..0000000 --- a/crates/germ/Cargo.toml +++ /dev/null @@ -1,32 +0,0 @@ -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[package] -name = "germ" -version = "0.3.2" -authors = ["Fuwn "] -edition = "2021" -description = "The Ultimate Gemini Toolkit." -documentation = "https://docs.rs/germ" -readme = "../../README.md" -homepage = "https://github.com/gemrest/germ" -repository = "https://github.com/gemrest/germ" -license = "GPL-3.0-only" -keywords = ["gemini", "parser", "lexer", "markdown", "converter"] -categories = ["encoding"] - -[features] -ast = [] -convert = ["ast"] -default = ["ast", "convert", "meta", "request"] -macros = ["ast", "convert", "germ-macros-impl"] -meta = [] -request = ["rustls", "url", "anyhow"] -quick = [] - -[dependencies] -anyhow = { version = "1.0.57", optional = true } # `Result` -germ-macros-impl = { path = "../germ-macros-impl", version = "0.1.0", optional = true } # Germ's Macro Implementations -rustls = { version = "0.20.5", features = [ - "dangerous_configuration" -], optional = true } # TLS -url = { version = "2.2.2", optional = true } # URL Validation diff --git a/crates/germ/examples/ast.rs b/crates/germ/examples/ast.rs deleted file mode 100644 index 884bd91..0000000 --- a/crates/germ/examples/ast.rs +++ /dev/null @@ -1,52 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -const EXAMPLE_GEMTEXT: &str = r#"```This is alt-text -Here goes the pre-formatted text. - -This continues the pre-formatted text on a new line after a blank line. -``` - -# This is a heading - -This is some text. - -This is more text after a blank line. - -* This is a single list item. -* This is the next list item. - -* This is a new list. -* This is the next item on the new list. - -## This is a sub-heading - -> This is a blockquote. - -### This is a sub-sub-heading. - -=> gemini://gem.rest/ This is a link to GemRest -=> /somewhere - -That was a link without text."#; - -fn main() { - for node in germ::ast::Ast::from_string(EXAMPLE_GEMTEXT).inner() { - println!("{:?}", node); - } -} diff --git a/crates/germ/examples/ast_to_gemtext.rs b/crates/germ/examples/ast_to_gemtext.rs deleted file mode 100644 index 5ceef21..0000000 --- a/crates/germ/examples/ast_to_gemtext.rs +++ /dev/null @@ -1,53 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -const EXAMPLE_GEMTEXT: &str = r#"```This is alt-text -Here goes the pre-formatted text. - -This continues the pre-formatted text on a new line after a blank line. -``` - -# This is a heading - -This is some text. - -This is more text after a blank line. - -* This is a single list item. -* This is the next list item. - -* This is a new list. -* This is the next item on the new list. - -## This is a sub-heading - -> This is a blockquote. - -### This is a sub-sub-heading. - -=> gemini://gem.rest/ This is a link to GemRest -=> /somewhere - -That was a link without text."#; - -fn main() { - println!( - "{}", - germ::ast::Ast::from_string(EXAMPLE_GEMTEXT).to_gemtext() - ); -} diff --git a/crates/germ/examples/convert.html b/crates/germ/examples/convert.html deleted file mode 100644 index cdfde66..0000000 --- a/crates/germ/examples/convert.html +++ /dev/null @@ -1,6 +0,0 @@ -
Here goes the pre-formatted text.
-
-This continues the pre-formatted text on a new line after a blank line.
-

This is a heading

This is some text.

This is more text after a blank line.

  • This is a single list item.
  • -
  • This is the next list item.
  • This is a new list.
  • -
  • This is the next item on the new list.

This is a sub-heading

This is a blockquote.

This is a sub-sub-heading.

This is a link to GemRest
/somewhere

That was a link without text.

\ No newline at end of file diff --git a/crates/germ/examples/convert.md b/crates/germ/examples/convert.md deleted file mode 100644 index ea553ee..0000000 --- a/crates/germ/examples/convert.md +++ /dev/null @@ -1,28 +0,0 @@ -```This is alt-text -Here goes the pre-formatted text. - -This continues the pre-formatted text on a new line after a blank line. -``` - -# This is a heading - -This is some text. - -This is more text after a blank line. - -- This is a single list item. -- This is the next list item. - -- This is a new list. -- This is the next item on the new list. - -## This is a sub-heading - -> This is a blockquote. - -### This is a sub-sub-heading. - -[This is a link to GemRest](gemini://gem.rest/) - - -That was a link without text. diff --git a/crates/germ/examples/html.rs b/crates/germ/examples/html.rs deleted file mode 100644 index 541c01e..0000000 --- a/crates/germ/examples/html.rs +++ /dev/null @@ -1,54 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -const EXAMPLE_GEMTEXT: &str = r#"```This is alt-text -Here goes the pre-formatted text. - -This continues the pre-formatted text on a new line after a blank line. -``` - -# This is a heading - -This is some text. - -This is more text after a blank line. - -* This is a single list item. -* This is the next list item. - -* This is a new list. -* This is the next item on the new list. - -## This is a sub-heading - -> This is a blockquote. - -### This is a sub-sub-heading. - -=> gemini://gem.rest/ This is a link to GemRest -=> /somewhere - -That was a link without text."#; - -fn main() { - std::fs::write( - "examples/convert.html", - germ::convert::from_string(EXAMPLE_GEMTEXT, &germ::convert::Target::HTML), - ) - .expect("could not write to file"); -} diff --git a/crates/germ/examples/markdown.rs b/crates/germ/examples/markdown.rs deleted file mode 100644 index c14cdc5..0000000 --- a/crates/germ/examples/markdown.rs +++ /dev/null @@ -1,57 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -const EXAMPLE_GEMTEXT: &str = r#"```This is alt-text -Here goes the pre-formatted text. - -This continues the pre-formatted text on a new line after a blank line. -``` - -# This is a heading - -This is some text. - -This is more text after a blank line. - -* This is a single list item. -* This is the next list item. - -* This is a new list. -* This is the next item on the new list. - -## This is a sub-heading - -> This is a blockquote. - -### This is a sub-sub-heading. - -=> gemini://gem.rest/ This is a link to GemRest -=> /somewhere - -That was a link without text."#; - -fn main() { - std::fs::write( - "examples/convert.md", - germ::convert::from_string( - EXAMPLE_GEMTEXT, - &germ::convert::Target::Markdown, - ), - ) - .expect("could not write to file"); -} diff --git a/crates/germ/examples/meta.rs b/crates/germ/examples/meta.rs deleted file mode 100644 index a9f4077..0000000 --- a/crates/germ/examples/meta.rs +++ /dev/null @@ -1,24 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -fn main() { - println!( - "{:?}", - germ::meta::Meta::from_string("text/gemini; hi=2; hi2=string=2") - ) -} diff --git a/crates/germ/examples/request.rs b/crates/germ/examples/request.rs deleted file mode 100644 index e33710f..0000000 --- a/crates/germ/examples/request.rs +++ /dev/null @@ -1,24 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -fn main() { - match germ::request::request(&url::Url::parse("gemini://fuwn.me").unwrap()) { - Ok(response) => println!("{:?}", response), - Err(_) => {} - } -} diff --git a/crates/germ/src/ast.rs b/crates/germ/src/ast.rs deleted file mode 100644 index 8c00b52..0000000 --- a/crates/germ/src/ast.rs +++ /dev/null @@ -1,28 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -//! Build AST trees from Gemtext - -mod container; -mod node; - -#[cfg(feature = "macros")] -mod macros; - -pub use container::Ast; -pub use node::Node; diff --git a/crates/germ/src/ast/container.rs b/crates/germ/src/ast/container.rs deleted file mode 100644 index d9e4d18..0000000 --- a/crates/germ/src/ast/container.rs +++ /dev/null @@ -1,290 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -use super::Node; - -/// An AST structure which contains an AST tree -/// -/// # Example -/// -/// ```rust -/// let _ = germ::ast::Ast::from_string(r#"=> gemini://gem.rest/ GemRest"#); -/// ``` -pub struct Ast { - inner: Vec, -} -impl Ast { - /// Build an AST tree from Gemtext. - /// - /// # Example - /// - /// ```rust - /// let _ = germ::ast::Ast::from_string(r#"=> gemini://gem.rest/ GemRest"#); - /// ``` - #[must_use] - pub fn from_string(source: &str) -> Self { - let mut ast = vec![]; - let mut in_preformatted = false; - let mut in_list = false; - let mut lines = source.lines(); - - // Iterate over all lines in the Gemtext `source` - while let Some(line) = lines.next() { - // Evaluate the Gemtext line and append its AST node to the `ast` tree - ast.append(&mut Self::evaluate( - line, - &mut lines, - &mut in_preformatted, - &mut in_list, - )); - } - - Self { - inner: ast - } - } - - #[must_use] - pub fn to_gemtext(&self) -> String { - let mut gemtext = "".to_string(); - - for node in &self.inner { - match node { - Node::Text(text) => gemtext.push_str(&format!("{}\n", text)), - Node::Link { - to, - text, - } => - gemtext.push_str(&format!( - "=> {}{}\n", - to, - text - .clone() - .map_or_else(|| "".to_string(), |text| format!(" {}", text)), - )), - Node::Heading { - level, - text, - } => - gemtext.push_str(&format!( - "{} {}\n", - match level { - 1 => "#", - 2 => "##", - 3 => "###", - _ => "", - }, - text - )), - Node::List(items) => - gemtext.push_str(&format!( - "{}\n", - items - .iter() - .map(|i| format!("* {}", i)) - .collect::>() - .join("\n"), - )), - Node::Blockquote(text) => gemtext.push_str(&format!("> {}\n", text)), - Node::PreformattedText { - alt_text, - text, - } => - gemtext.push_str(&format!( - "```{}\n{}```\n", - alt_text.clone().unwrap_or_else(|| "".to_string()), - text - )), - Node::Whitespace => gemtext.push('\n'), - } - } - - gemtext - } - - /// The actual AST of `Ast` - /// - /// # Example - /// - /// ```rust - /// let _ = - /// germ::ast::Ast::from_string(r#"=> gemini://gem.rest/ GemRest"#).inner(); - /// ``` - #[must_use] - pub const fn inner(&self) -> &Vec { &self.inner } - - #[allow(clippy::too_many_lines)] - fn evaluate( - line: &str, - lines: &mut std::str::Lines<'_>, - in_preformatted: &mut bool, - in_list: &mut bool, - ) -> Vec { - let mut preformatted = String::new(); - let mut alt_text = String::new(); - let mut nodes = vec![]; - let mut line = line; - let mut list_items = vec![]; - - // Enter a not-so-infinite loop as sometimes, we may need to stay in an - // evaluation loop, e.g., multiline contexts: preformatted text, lists, etc. - loop { - // Match the first character of the Gemtext line to understand the line - // type - match line.get(0..1).unwrap_or("") { - "=" => { - // If the Gemtext line starts with an "=" ("=>"), it is a link line, - // so splitting it up should be easy enough. - let line = line.get(2..).unwrap(); - let mut split = line - .split_whitespace() - .map(String::from) - .collect::>() - .into_iter(); - - nodes.push(Node::Link { - to: split.next().expect("no location in link"), - text: { - let rest = split.collect::>().join(" "); - - if rest.is_empty() { - None - } else { - Some(rest) - } - }, - }); - - break; - } - "#" => { - // If the Gemtext line starts with an "#", it is a heading, so let's - // find out how deep it goes. - let level = match line.get(0..3) { - Some(root) => - if root.contains("###") { - 3 - } else if root.contains("##") { - 2 - } else if root.contains('#') { - 1 - } else { - 0 - }, - None => 0, - }; - - nodes.push(Node::Heading { - level, - // Here, we are `get`ing the `&str` starting at the `level`-th - // index, then trimming the start. These operations - // effectively off the line identifier. - text: line.get(level..).unwrap_or("").trim_start().to_string(), - }); - - break; - } - "*" => { - // If the Gemtext line starts with an asterisk, it is a list item, so - // let's enter a list context. - if !*in_list { - *in_list = true; - } - - list_items.push(line.get(1..).unwrap_or("").trim_start().to_string()); - - if let Some(next_line) = lines.next() { - line = next_line; - } else { - break; - } - } - ">" => { - // If the Gemtext line starts with an ">", it is a blockquote, so - // let's just clip off the line identifier. - nodes.push(Node::Blockquote( - line.get(1..).unwrap_or("").trim_start().to_string(), - )); - - break; - } - "`" => { - // If the Gemtext line starts with a backtick, it is a list item, so - // let's enter a preformatted text context. - *in_preformatted = !*in_preformatted; - - if *in_preformatted { - alt_text = line.get(3..).unwrap_or("").to_string(); - line = lines.next().unwrap(); - } else { - nodes.push(Node::PreformattedText { - alt_text: if alt_text.is_empty() { - None - } else { - Some(alt_text) - }, - text: preformatted, - }); - - break; - } - } - "" if !*in_preformatted => { - // If the line has nothing on it, it is a whitespace line, as long as - // we aren't in a preformatted line context. - nodes.push(Node::Whitespace); - - break; - } - // 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 - // preformatted blocks content and increment the line. - preformatted.push_str(&format!("{}\n", line)); - - line = lines.next().unwrap(); - } else { - // If we are in a list item and hit a catchall, that must mean that - // we encountered a line which is not a list line, so - // let's stop adding items to the list context. - if *in_list { - *in_list = false; - - nodes.push(Node::Text(line.to_string())); - - break; - } - - nodes.push(Node::Text(line.to_string())); - - break; - } - } - } - } - - if !list_items.is_empty() { - nodes.reverse(); - nodes.push(Node::List(list_items)); - nodes.reverse(); - } - - nodes - } -} diff --git a/crates/germ/src/ast/macros.rs b/crates/germ/src/ast/macros.rs deleted file mode 100644 index c2e5a85..0000000 --- a/crates/germ/src/ast/macros.rs +++ /dev/null @@ -1,50 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -/// Convert Gemtext an `Ast` -/// -/// # Examples -/// -/// ```rust -/// // Using a value -/// assert_eq!( -/// 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!"), -/// ); -/// -/// /// Using raw Gemtext -/// assert_eq!( -/// germ::gemini_to_ast! { -/// => / A link! -/// => / Another link! -/// } -/// .to_gemtext(), -/// format!("{}\n", "=> / A link!\n=> / Another link!"), -/// ); -/// ``` -#[macro_export] -macro_rules! gemini_to_ast { - ($gemini:expr) => { - germ::ast::Ast::from_string($gemini) - }; - ($($gemini:tt)*) => { - germ::ast::Ast::from_string(germ_macros_impl::gemini_to_tt!($($gemini)*)); - }; -} diff --git a/crates/germ/src/ast/node.rs b/crates/germ/src/ast/node.rs deleted file mode 100644 index 04296ff..0000000 --- a/crates/germ/src/ast/node.rs +++ /dev/null @@ -1,173 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -/// A Gemtext AST node. -/// -/// Each Gemtext line is a `Node`, and some lines can even be grouped together, -/// such as the `Node::List` `Node`! -/// -/// # Gemtext Resources -/// -/// - [Gemtext Documentation](https://gemini.circumlunar.space/docs/gemtext.gmi) -/// - [Gemtext Cheatsheet](https://gemini.circumlunar.space/docs/cheatsheet.gmi). -/// - [Gemini Specification](https://gemini.circumlunar.space/docs/specification.gmi). -#[derive(Debug, PartialEq)] -pub enum Node { - /// A text line - /// - /// # Example - /// - /// ```gemini - /// This is a text line - /// ``` - Text(String), - /// A link line - /// - /// # Examples - /// - /// ```gemini - /// => /this-is-the-to This is the text - /// - /// => gemini://to.somewhere.link - /// ``` - Link { - /// The location that a link line is pointing to - /// - /// # Examples - /// - /// ```gemini - /// => /this-is-the-to This is the text - /// - /// => gemini://to.somewhere.link - /// ``` - to: String, - /// The text a link line *may* have - /// - /// # Examples - /// - /// ```gemini - /// => /this-is-the-to This line has text, unlike the next one. - /// - /// => gemini://to.somewhere.link - /// ``` - text: Option, - }, - /// A heading line - /// - /// # Examples - /// - /// ```gemini - /// # This is a heading - /// - /// ## This is a sub-heading - /// - /// ### This is a sub-sub-heading - /// ``` - Heading { - /// The level of a heading - /// - /// # Examples - /// - /// ```gemini - /// # This is a level 1 heading - /// - /// ## This is a level 2 sub-heading - /// - /// ### This is a level 3 sub-sub-heading - /// ``` - level: usize, - /// The text of a heading - /// - /// # Examples - /// - /// ```gemini - /// # This is the headings text - /// - /// # This is also the headings text - /// ``` - text: String, - }, - /// A collection of sequential list item lines - /// - /// # Examples - /// - /// ```gemini - /// * These are - /// * sequential list - /// * items. - /// ``` - List(Vec), - /// A blockquote line - /// - /// # Examples - /// - /// ```gemini - /// > This is a blockquote line - /// - /// > This is also a blockquote line - /// ``` - Blockquote(String), - /// A preformatted block - /// - /// # Examples - /// - /// Try to ignore the leading backslash in-front of the triple backticks, - /// they are there to not confuse the Markdown engine. - /// - /// ```gemini - /// \```This is the alt-text - /// This is the preformatted block - /// - /// This is the rest of the preformatted block - /// \``` - /// ``` - PreformattedText { - /// A preformatted blocks alt-text - /// - /// # Examples - /// - /// Try to ignore the leading backslash in-front of the triple backticks, - /// they are there to not confuse the Markdown engine. - /// - /// ```gemini - /// \```This is the alt-text - /// This is the preformatted block - /// - /// This is the rest of the preformatted block - /// \``` - /// ``` - alt_text: Option, - /// A preformatted blocks content - /// - /// # Examples - /// - /// Try to ignore the leading backslash in-front of the triple backticks, - /// they are there to not confuse the Markdown engine. - /// - /// ```gemini - /// \```This is the alt-text - /// This is the preformatted blocks content - /// - /// This is the rest of the preformatted blocks content - /// \``` - /// ``` - text: String, - }, - /// A whitespace line, a line which contains nothing but whitespace. - Whitespace, -} diff --git a/crates/germ/src/convert.rs b/crates/germ/src/convert.rs deleted file mode 100644 index 2e323e1..0000000 --- a/crates/germ/src/convert.rs +++ /dev/null @@ -1,72 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -//! Convert Gemtext into many types of markup. - -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 - HTML, - /// Convert Gemtext to Markdown - Markdown, -} - -/// Convert AST'd Gemtext into an alternative markup format. -/// -/// # Example -/// -/// ```rust -/// use germ::convert; -/// -/// let _ = convert::from_ast( -/// &germ::ast::Ast::from_string(r#"=> gemini://gem.rest/ GemRest"#), -/// &convert::Target::HTML, -/// ); -/// ``` -#[must_use] -pub fn from_ast(source: &Ast, target: &Target) -> String { - match target { - Target::Markdown => markdown::convert(source.inner()), - Target::HTML => html::convert(source.inner()), - } -} - -/// Convert raw Gemtext into an alternative markup format. -/// -/// # Example -/// -/// ```rust -/// use germ::convert; -/// -/// let _ = convert::from_string( -/// r#"=> gemini://gem.rest/ GemRest"#, -/// &convert::Target::HTML, -/// ); -/// ``` -#[must_use] -pub fn from_string(source: &str, target: &Target) -> String { - from_ast(&Ast::from_string(source), target) -} diff --git a/crates/germ/src/convert/html.rs b/crates/germ/src/convert/html.rs deleted file mode 100644 index 7b1cafe..0000000 --- a/crates/germ/src/convert/html.rs +++ /dev/null @@ -1,75 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -use crate::ast::Node; - -pub fn convert(source: &[Node]) -> String { - let mut html = String::new(); - - // Since we have an AST tree of the Gemtext, it is very easy to convert from - // this AST tree to an alternative markup format. - for node in source { - match node { - Node::Text(text) => html.push_str(&format!("

{}

", text)), - Node::Link { - to, - text, - } => { - html.push_str(&format!( - "{}
", - to, - text.clone().unwrap_or_else(|| to.clone()) - )); - } - Node::Heading { - level, - text, - } => { - html.push_str(&format!( - "<{}>{}", - match level { - 1 => "h1", - 2 => "h2", - 3 => "h3", - _ => "p", - }, - text - )); - } - Node::List(items) => - html.push_str(&format!( - "
    {}
", - items - .iter() - .map(|i| format!("
  • {}
  • ", i)) - .collect::>() - .join("\n") - )), - Node::Blockquote(text) => - html.push_str(&format!("
    {}
    ", text)), - Node::PreformattedText { - text, .. - } => { - html.push_str(&format!("
    {}
    ", text)); - } - Node::Whitespace => {} - } - } - - html -} diff --git a/crates/germ/src/convert/macros.rs b/crates/germ/src/convert/macros.rs deleted file mode 100644 index ca29050..0000000 --- a/crates/germ/src/convert/macros.rs +++ /dev/null @@ -1,80 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -/// Convert Gemtext into HTML -/// -/// # Examples -/// -/// ```rust -/// // Using a value -/// assert_eq!( -/// germ::gemini_to_html!("=> /to hello !"), -/// "hello !
    ", -/// ); -/// -/// // Using raw Gemtext -/// assert_eq!( -/// germ::gemini_to_html! { => /to hello ! }, -/// "hello !
    ", -/// ); -/// ``` -#[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/src/convert/markdown.rs b/crates/germ/src/convert/markdown.rs deleted file mode 100644 index a38da9f..0000000 --- a/crates/germ/src/convert/markdown.rs +++ /dev/null @@ -1,77 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -use crate::ast::Node; - -pub fn convert(source: &[Node]) -> String { - let mut markdown = String::new(); - - // Since we have an AST tree of the Gemtext, it is very easy to convert from - // this AST tree to an alternative markup format. - for node in source { - match node { - Node::Text(text) => markdown.push_str(&format!("{}\n", text)), - Node::Link { - to, - text, - } => - markdown.push_str(&*text.clone().map_or_else( - || format!("<{}>\n", to), - |text| format!("[{}]({})\n", text, to), - )), - Node::Heading { - level, - text, - } => { - markdown.push_str(&format!( - "{} {}\n", - match level { - 1 => "#", - 2 => "##", - 3 => "###", - _ => "", - }, - text - )); - } - Node::List(items) => - markdown.push_str(&format!( - "{}\n", - items - .iter() - .map(|i| format!("- {}", i)) - .collect::>() - .join("\n"), - )), - Node::Blockquote(text) => markdown.push_str(&format!("> {}\n", text)), - Node::PreformattedText { - alt_text, - text, - } => { - markdown.push_str(&format!( - "```{}\n{}```\n", - alt_text.clone().unwrap_or_else(|| "".to_string()), - text - )); - } - Node::Whitespace => markdown.push('\n'), - } - } - - markdown -} diff --git a/crates/germ/src/lib.rs b/crates/germ/src/lib.rs deleted file mode 100644 index ddab063..0000000 --- a/crates/germ/src/lib.rs +++ /dev/null @@ -1,46 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -#![deny( - warnings, - nonstandard_style, - unused, - future_incompatible, - rust_2018_idioms, - unsafe_code, - clippy::all, - clippy::nursery, - clippy::pedantic -)] -#![doc = include_str!("../../../README.md")] -#![recursion_limit = "128"] - -#[cfg(feature = "ast")] -pub mod ast; - -#[cfg(feature = "convert")] -pub mod convert; - -#[cfg(feature = "request")] -pub mod request; - -#[cfg(feature = "meta")] -pub mod meta; - -#[cfg(feature = "quick")] -pub mod quick; diff --git a/crates/germ/src/meta.rs b/crates/germ/src/meta.rs deleted file mode 100644 index f8a9dfb..0000000 --- a/crates/germ/src/meta.rs +++ /dev/null @@ -1,162 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -use std::collections::HashMap; - -/// Structure-ize a Gemini response's meta section into it's mime type and it's -/// parameters. -#[derive(Debug, Default)] -pub struct Meta { - /// The mime type of a Gemini response - mime: String, - /// The parameters of a Gemini response - parameters: HashMap, -} -impl ToString for Meta { - /// Convert a `Meta` into a `String` - /// - /// # Example - /// ```rust - /// let original_string = "text/gemini; hi=2; hi2=string=2"; - /// - /// assert_eq!( - /// germ::meta::Meta::from_string(original_string).to_string(), - /// original_string - /// ); - /// ``` - fn to_string(&self) -> String { - format!("{}{}", self.mime, { - let mut parameters = self - .parameters - .iter() - .map(|(k, v)| format!("{}={}", *k, v)) - .collect::>(); - - parameters.sort(); - parameters.reverse(); - - if parameters.is_empty() { - "".to_string() - } else { - format!("; {}", parameters.join("; ")) - } - }) - } -} -impl Meta { - /// Create a new `Meta` - /// - /// # Example - /// - /// ```rust - /// let mut meta = germ::meta::Meta::new(); - /// ``` - #[must_use] - pub fn new() -> Self { Self::default() } - - /// Create a `Meta` from a string - /// - /// # Example - /// - /// ```rust - /// assert_eq!( - /// germ::meta::Meta::from_string("text/gemini; hi=2; hi2=string=2").mime(), - /// "text/gemini", - /// ); - /// ``` - #[must_use] - pub fn from_string(meta: &str) -> Self { - let mut metas = meta.split(';'); - let mime = metas.next().unwrap_or("").to_string(); - let mut parameters = HashMap::new(); - - for parameter in metas { - let key_value = parameter - .trim_start() - .split_at(parameter.find('=').unwrap_or(0)); - - parameters.insert( - key_value.0.to_string().replace('=', ""), - key_value.1.to_string(), - ); - } - - Self { - mime, - parameters, - } - } - - /// Obtain non-mutable access to the mime of the `Meta` - /// - /// # Example - /// - /// ```rust - /// assert_eq!( - /// germ::meta::Meta::from_string("text/gemini; hi=2; hi2=string=2").mime(), - /// "text/gemini", - /// ); - /// ``` - #[must_use] - pub fn mime(&self) -> &str { &self.mime } - - /// Obtain mutable access to the mime of the `Meta` - /// - /// # Example - /// - /// ```rust - /// let mut meta = germ::meta::Meta::new(); - /// - /// *meta.mime_mut() = "text/gemini".to_string(); - /// ``` - pub fn mime_mut(&mut self) -> &mut String { &mut self.mime } - - /// Obtain non-mutable access to the parameters of the `Meta` - /// - /// # Example - /// - /// ```rust - /// assert_eq!( - /// germ::meta::Meta::from_string("text/gemini; hi=2; hi2=string=2") - /// .parameters() - /// .get("hi2"), - /// Some(&"string=2".to_string()), - /// ); - /// ``` - #[must_use] - pub const fn parameters(&self) -> &HashMap { - &self.parameters - } - - /// Obtain mutable access to the parameters of the `Meta` - /// - /// # Example - /// - /// ```rust - /// let mut meta = germ::meta::Meta::new(); - /// let mut parameters = std::collections::HashMap::new(); - /// - /// parameters.insert("hi".to_string(), "2".to_string()); - /// parameters.insert("hi2".to_string(), "string=2".to_string()); - /// - /// *meta.parameters_mut() = parameters; - /// ``` - pub fn parameters_mut(&mut self) -> &mut HashMap { - &mut self.parameters - } -} diff --git a/crates/germ/src/quick.rs b/crates/germ/src/quick.rs deleted file mode 100644 index d952b08..0000000 --- a/crates/germ/src/quick.rs +++ /dev/null @@ -1,65 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -pub enum HeadingLevel { - One, - Two, - Three, -} - -#[must_use] -pub fn heading(text: &str, level: &HeadingLevel) -> String { - format!( - "{} {}", - match level { - HeadingLevel::One => "#", - HeadingLevel::Two => "##", - HeadingLevel::Three => "###", - }, - text - ) -} - -#[must_use] -pub fn list_item(text: &str) -> String { format!("* {}", text) } - -#[must_use] -pub fn list_items(items: &[&str]) -> String { - items - .iter() - .map(|item| list_item(item)) - .collect::>() - .join("\n") -} - -#[must_use] -pub fn link(text: &str, location: Option<&str>) -> String { - format!( - "=> {}{}", - text, - location.map_or_else(|| "".to_string(), |l| format!(" {}", l)) - ) -} - -#[must_use] -pub fn block_quote(text: &str) -> String { format!("> {}", text) } - -#[must_use] -pub fn preformatted_text(text: &str, alt_text: Option<&str>) -> String { - format!("```{}\n{}\n```", alt_text.unwrap_or(""), text) -} diff --git a/crates/germ/src/request.rs b/crates/germ/src/request.rs deleted file mode 100644 index 07d3552..0000000 --- a/crates/germ/src/request.rs +++ /dev/null @@ -1,73 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -//! Make Gemini requests and get sane, structured results - -mod response; -mod status; -mod verifier; - -use std::io::{Read, Write}; - -pub use response::Response; -pub use status::Status; -use verifier::GermVerifier; - -/// Make a request to a Gemini server. The `url` **should** be prefixed with a -/// scheme (e.g. "gemini://"). -/// -/// # Example -/// -/// ```rust -/// match germ::request::request(&url::Url::parse("gemini://fuwn.me").unwrap()) { -/// Ok(response) => println!("{:?}", response), -/// Err(_) => {} -/// } -/// ``` -/// -/// # Errors -/// - May error if the URL is invalid -/// - May error if the TLS write fails -/// - May error if the TLS read fails -pub fn request(url: &url::Url) -> anyhow::Result { - let config = rustls::ClientConfig::builder() - .with_safe_defaults() - .with_custom_certificate_verifier(std::sync::Arc::new(GermVerifier::new())) - .with_no_client_auth(); - let mut connection = rustls::ClientConnection::new( - std::sync::Arc::new(config), - url.domain().unwrap_or("").try_into()?, - )?; - let mut stream = std::net::TcpStream::connect(format!( - "{}:{}", - url.domain().unwrap_or(""), - url.port().unwrap_or(1965) - ))?; - let mut tls = rustls::Stream::new(&mut connection, &mut stream); - - tls.write_all(format!("{}\r\n", url).as_bytes())?; - - let mut plain_text = Vec::new(); - - tls.read_to_end(&mut plain_text)?; - - Ok(Response::new( - &plain_text, - tls.conn.negotiated_cipher_suite(), - )) -} diff --git a/crates/germ/src/request/response.rs b/crates/germ/src/request/response.rs deleted file mode 100644 index 5e1f436..0000000 --- a/crates/germ/src/request/response.rs +++ /dev/null @@ -1,71 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -use rustls::SupportedCipherSuite; - -use crate::request::Status; - -#[derive(Debug)] -pub struct Response { - status: Status, - meta: String, - content: Option, - size: usize, - suite: Option, -} -impl Response { - pub(super) fn new(data: &[u8], suite: Option) -> Self { - let string_form = String::from_utf8_lossy(data).to_string(); - let mut content = None; - let header; - - if string_form.ends_with("\r\n") { - header = string_form; - } else { - let mut string_split = string_form.split("\r\n"); - - header = string_split.next().unwrap_or("").to_string(); - content = Some(string_split.collect()); - } - - let header_split = header.split_at(2); - - Self { - status: Status::from(header_split.0.parse::().unwrap_or(0)), - meta: header_split.1.trim_start().to_string(), - content, - size: data.len(), - suite, - } - } - - #[must_use] - pub const fn status(&self) -> &Status { &self.status } - - #[must_use] - pub fn meta(&self) -> &str { &self.meta } - - #[must_use] - pub const fn content(&self) -> &Option { &self.content } - - #[must_use] - pub const fn size(&self) -> &usize { &self.size } - - #[must_use] - pub const fn suite(&self) -> &Option { &self.suite } -} diff --git a/crates/germ/src/request/status.rs b/crates/germ/src/request/status.rs deleted file mode 100644 index f46059a..0000000 --- a/crates/germ/src/request/status.rs +++ /dev/null @@ -1,110 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -use std::{fmt, fmt::Formatter}; - -/// Simple Gemini status reporting -/// -/// # Examples -/// -/// ```rust -/// use germ::request::Status; -/// -/// assert_eq!(Status::from(10), Status::Input); -/// assert_eq!(i32::from(Status::Input), 10); -/// ``` -#[derive(Debug, PartialEq)] -pub enum Status { - Input, - SensitiveInput, - Success, - TemporaryRedirect, - PermanentRedirect, - TemporaryFailure, - ServerUnavailable, - CGIError, - ProxyError, - SlowDown, - PermanentFailure, - NotFound, - Gone, - ProxyRefused, - BadRequest, - ClientCertificateRequired, - CertificateNotAuthorised, - CertificateNotValid, - Unsupported, -} -impl Default for Status { - fn default() -> Self { Self::Success } -} -impl From for i32 { - fn from(n: Status) -> Self { - match n { - Status::Input => 10, - Status::SensitiveInput => 11, - Status::Success => 20, - Status::TemporaryRedirect => 30, - Status::PermanentRedirect => 31, - Status::TemporaryFailure => 40, - Status::ServerUnavailable => 41, - Status::CGIError => 42, - Status::ProxyError => 43, - Status::SlowDown => 44, - Status::PermanentFailure => 50, - Status::NotFound => 51, - Status::Gone => 52, - Status::ProxyRefused => 53, - Status::BadRequest => 59, - Status::ClientCertificateRequired => 60, - Status::CertificateNotAuthorised => 61, - Status::CertificateNotValid => 62, - Status::Unsupported => 0, - } - } -} -impl From for Status { - fn from(n: i32) -> Self { - match n { - 10 => Self::Input, - 11 => Self::SensitiveInput, - 20 => Self::Success, - 30 => Self::TemporaryRedirect, - 31 => Self::PermanentRedirect, - 40 => Self::TemporaryFailure, - 41 => Self::ServerUnavailable, - 42 => Self::CGIError, - 43 => Self::ProxyError, - 44 => Self::SlowDown, - 50 => Self::PermanentFailure, - 51 => Self::NotFound, - 52 => Self::Gone, - 53 => Self::ProxyRefused, - 59 => Self::BadRequest, - 60 => Self::ClientCertificateRequired, - 61 => Self::CertificateNotAuthorised, - 62 => Self::CertificateNotValid, - _ => Self::Unsupported, - } - } -} -impl fmt::Display for Status { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "{:?}", self) - } -} diff --git a/crates/germ/src/request/verifier.rs b/crates/germ/src/request/verifier.rs deleted file mode 100644 index d6511c3..0000000 --- a/crates/germ/src/request/verifier.rs +++ /dev/null @@ -1,39 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -use std::time::SystemTime; - -use rustls::{client, client::ServerCertVerified, Certificate}; - -pub(super) struct GermVerifier; -impl GermVerifier { - pub const fn new() -> Self { Self {} } -} -impl client::ServerCertVerifier for GermVerifier { - fn verify_server_cert( - &self, - _end_entity: &Certificate, - _intermediates: &[Certificate], - _server_name: &client::ServerName, - _scts: &mut dyn Iterator, - _ocsp_response: &[u8], - _now: SystemTime, - ) -> Result { - Ok(ServerCertVerified::assertion()) - } -} diff --git a/crates/germ/tests/ast.rs b/crates/germ/tests/ast.rs deleted file mode 100644 index c91d4b7..0000000 --- a/crates/germ/tests/ast.rs +++ /dev/null @@ -1,131 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -#[cfg(test)] -mod test { - use germ::ast::{Ast, Node}; - - const EXAMPLE_GEMTEXT: &str = r#"```This is alt-text -Here goes the pre-formatted text. - -This continues the pre-formatted text on a new line after a blank line. -``` - -# This is a heading - -This is some text. - -This is more text after a blank line. - -* This is a single list item. -* This is the next list item. - -* This is a new list. -* This is the next item on the new list. - -## This is a sub-heading - -> This is a blockquote. - -### This is a sub-sub-heading. - -=> gemini://gem.rest/ This is a link to GemRest -=> /somewhere - -That was a link without text."#; - - #[test] - fn build_multi_line_list_with_text() { - assert_eq!( - *Ast::from_string("* item1\n* 2\nhi text").inner(), - vec![ - Node::List(vec!["item1".to_string(), "2".to_string()]), - Node::Text("hi text".to_string()), - ], - ); - } - - #[test] - fn build_multi_line_vec() { - assert_eq!( - *Ast::from_string("=> /test hi\nhi there\n> hi").inner(), - vec![ - Node::Link { - to: "/test".to_string(), - text: Some("hi".to_string()), - }, - Node::Text("hi there".to_string()), - Node::Blockquote("hi".to_string()), - ], - ); - } - - #[test] - fn build_single_0th_from_vec() { - assert_eq!( - Ast::from_string("=> /test hi").inner(), - &vec![Node::Link { - to: "/test".to_string(), - text: Some("hi".to_string()), - }], - ); - } - - #[test] - fn build_single_element() { - assert_eq!( - Ast::from_string("=> /test hi").inner().get(0).unwrap(), - &Node::Link { - to: "/test".to_string(), - text: Some("hi".to_string()), - }, - ); - } - - #[test] - fn gemtext_to_ast_then_ast_to_gemtext() { - assert_eq!( - Ast::from_string(EXAMPLE_GEMTEXT).to_gemtext(), - // `to_gemtext` appends a newline to all responses, so let's make sure we - // account for that. - format!("{}\n", EXAMPLE_GEMTEXT), - ); - } - - #[test] - 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 - // account for that. - format!("{}\n", EXAMPLE_GEMTEXT), - ); - } - - #[test] - fn gemtext_to_ast_then_ast_to_gemtext_macro_block() { - assert_eq!( - germ::gemini_to_ast! { - => / A link! - => / Another link! - } - .to_gemtext(), - format!("{}\n", "=> / A link!\n=> / Another link!"), - ); - } -} diff --git a/crates/germ/tests/convert.rs b/crates/germ/tests/convert.rs deleted file mode 100644 index de1baf2..0000000 --- a/crates/germ/tests/convert.rs +++ /dev/null @@ -1,83 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -#[cfg(test)] -mod test { - use germ::{ - convert::{from_string, Target}, - gemini_to_html, - gemini_to_md, - }; - - #[test] - fn convert_from_string_to_html_single_line() { - assert_eq!(from_string("hi", &Target::HTML), "

    hi

    ",); - } - - #[test] - fn convert_from_string_to_html_multi_line() { - assert_eq!( - from_string("hi\n# hi", &Target::HTML), - "

    hi

    hi

    ", - ); - } - - #[test] - fn convert_from_string_to_html_single_link_macro_expression() { - assert_eq!( - gemini_to_html!("=> /to hello !"), - "hello !
    ", - ); - } - - #[test] - fn convert_from_string_to_html_single_link_macro_block() { - assert_eq!( - gemini_to_html! { => /to hello ! }, - "hello !
    ", - ); - } - - #[test] - fn convert_from_string_to_markdown_single_line() { - assert_eq!(from_string("hi", &Target::Markdown), "hi\n",); - } - - #[test] - fn convert_from_string_to_markdown_multi_line() { - assert_eq!(from_string("hi\n# hi", &Target::Markdown), "hi\n# hi\n",); - } - - #[test] - fn convert_from_string_to_markdown_single_link() { - assert_eq!( - from_string("=> /to hello !", &Target::Markdown), - "[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",); - } -} diff --git a/crates/germ/tests/meta.rs b/crates/germ/tests/meta.rs deleted file mode 100644 index 70c8adc..0000000 --- a/crates/germ/tests/meta.rs +++ /dev/null @@ -1,101 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -#[cfg(test)] -mod test { - use germ::meta::Meta; - - #[test] - fn construct_meta_with_mime() { - let mut meta = Meta::new(); - - *meta.mime_mut() = "text/gemini".to_string(); - - assert_eq!(meta.to_string(), "text/gemini"); - } - - #[test] - fn construct_meta_with_mime_and_parameters() { - let mut meta = Meta::new(); - let mut parameters = std::collections::HashMap::new(); - - parameters.insert("hi".to_string(), "2".to_string()); - parameters.insert("hi2".to_string(), "string=2".to_string()); - - *meta.mime_mut() = "text/gemini".to_string(); - *meta.parameters_mut() = parameters; - - assert_eq!(meta.to_string(), "text/gemini; hi=2; hi2=string=2"); - } - - #[test] - fn meta_to_string_without_parameters() { - let original_string = "text/gemini"; - - assert_eq!( - Meta::from_string(original_string).to_string(), - original_string - ); - } - - #[test] - fn meta_to_string_with_parameters() { - let original_string = "text/gemini; hi=2; hi2=string=2"; - - assert_eq!( - Meta::from_string(original_string).to_string(), - original_string - ); - } - - #[test] - fn meta_to_mime_without_parameters() { - let meta = Meta::from_string("text/gemini"); - - assert_eq!(meta.mime(), "text/gemini"); - assert_eq!(meta.parameters().len(), 0); - } - - #[test] - fn meta_to_map_mime() { - assert_eq!( - Meta::from_string("text/gemini; hi=2; hi2=string=2").mime(), - "text/gemini", - ); - } - - #[test] - fn meta_to_map_with_parameters() { - assert_eq!( - Meta::from_string("text/gemini; hi=2; hi2=string=2") - .parameters() - .get("hi2"), - Some(&"string=2".to_string()), - ); - } - - #[test] - fn meta_to_map_length() { - assert_eq!( - Meta::from_string("text/gemini; hi=2; hi2=string=2") - .parameters() - .len(), - 2, - ); - } -} diff --git a/crates/germ/tests/quick.rs b/crates/germ/tests/quick.rs deleted file mode 100644 index 255acd9..0000000 --- a/crates/germ/tests/quick.rs +++ /dev/null @@ -1,71 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -#[cfg(test)] -mod test { - use germ::quick::{self, heading}; - - #[test] - fn all_heading_levels() { - assert_eq!(heading("Soup", &germ::quick::HeadingLevel::One), "# Soup"); - assert_eq!( - heading("Vegetables", &germ::quick::HeadingLevel::Two), - "## Vegetables" - ); - assert_eq!( - heading("Fruits", &germ::quick::HeadingLevel::Three), - "### Fruits" - ); - } - - #[test] - fn list_item() { - assert_eq!(quick::list_item("Soup"), "* Soup"); - } - - #[test] - fn list_items() { - assert_eq!( - quick::list_items(&["Soup", "Vegetables", "Fruits"]), - "* Soup\n* Vegetables\n* Fruits" - ); - } - - #[test] - fn link_variants() { - assert_eq!(quick::link("Soup", None), "=> Soup"); - assert_eq!( - quick::link("Soup", Some("gemini://soup.com")), - "=> Soup gemini://soup.com" - ); - } - - #[test] - fn block_quote() { - assert_eq!(quick::block_quote("Soup"), "> Soup"); - } - - #[test] - fn preformatted_text_variants() { - assert_eq!(quick::preformatted_text("Soup", None), "```\nSoup\n```"); - assert_eq!( - quick::preformatted_text("Vegetables", Some("Fruits")), - "```Fruits\nVegetables\n```" - ); - } -} diff --git a/crates/germ/tests/status.rs b/crates/germ/tests/status.rs deleted file mode 100644 index 51f3f66..0000000 --- a/crates/germ/tests/status.rs +++ /dev/null @@ -1,32 +0,0 @@ -// This file is part of Germ . -// Copyright (C) 2022-2022 Fuwn -// -// 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 . -// -// Copyright (C) 2022-2022 Fuwn -// SPDX-License-Identifier: GPL-3.0-only - -#[cfg(test)] -mod test { - use germ::request::Status; - - #[test] - fn status_from_i32() { - assert_eq!(Status::from(10), Status::Input); - } - - #[test] - fn i32_from_status() { - assert_eq!(i32::from(Status::Input), 10); - } -} diff --git a/germ-macros-impl/Cargo.toml b/germ-macros-impl/Cargo.toml new file mode 100644 index 0000000..8307d93 --- /dev/null +++ b/germ-macros-impl/Cargo.toml @@ -0,0 +1,21 @@ +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[package] +name = "germ-macros-impl" +version = "0.1.1" +authors = ["Fuwn "] +edition = "2021" +description = "Germ Macro Implementations" +documentation = "https://docs.rs/germ" +readme = "../README.md" +homepage = "https://github.com/gemrest/germ" +repository = "https://github.com/gemrest/germ" +license = "GPL-3.0-only" +keywords = ["gemini", "parser", "lexer", "markdown", "converter"] +categories = ["encoding"] + +[lib] +proc-macro = true + +[dependencies] +quote = "1.0.18" # Quasi-quoting diff --git a/germ-macros-impl/src/lib.rs b/germ-macros-impl/src/lib.rs new file mode 100644 index 0000000..30fdd81 --- /dev/null +++ b/germ-macros-impl/src/lib.rs @@ -0,0 +1,58 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +#![deny( + warnings, + nonstandard_style, + unused, + future_incompatible, + rust_2018_idioms, + unsafe_code, + clippy::all, + clippy::nursery, + clippy::pedantic +)] +#![feature(proc_macro_hygiene, proc_macro_span)] +#![recursion_limit = "128"] + +use proc_macro::TokenStream; + +/// Convert Gemtext into a token tree +/// +/// # Panics +/// +/// May panic if the Gemini could not be properly handled, for any reason. +#[proc_macro] +pub fn gemini_to_tt(input: TokenStream) -> TokenStream { + let mut tokens = input.into_iter(); + let mut span = tokens.next().unwrap().span(); + + for token in tokens { + span = span.join(token.span()).unwrap(); + } + + let gemini = span + .source_text() + .unwrap() + .lines() + .map(|l| l.trim_start().to_string()) + .collect::>() + .join("\n"); + + quote::quote!(#gemini).into() +} diff --git a/germ/Cargo.toml b/germ/Cargo.toml new file mode 100644 index 0000000..59e0434 --- /dev/null +++ b/germ/Cargo.toml @@ -0,0 +1,32 @@ +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[package] +name = "germ" +version = "0.3.3" +authors = ["Fuwn "] +edition = "2021" +description = "The Ultimate Gemini Toolkit." +documentation = "https://docs.rs/germ" +readme = "../README.md" +homepage = "https://github.com/gemrest/germ" +repository = "https://github.com/gemrest/germ" +license = "GPL-3.0-only" +keywords = ["gemini", "parser", "lexer", "markdown", "converter"] +categories = ["encoding"] + +[features] +ast = [] +convert = ["ast"] +default = ["ast", "convert", "meta", "request"] +macros = ["ast", "convert", "germ-macros-impl"] +meta = [] +request = ["rustls", "url", "anyhow"] +quick = [] + +[dependencies] +anyhow = { version = "1.0.57", optional = true } # `Result` +germ-macros-impl = { path = "../germ-macros-impl", version = "0.1.0", optional = true } # Germ's Macro Implementations +rustls = { version = "0.20.5", features = [ + "dangerous_configuration" +], optional = true } # TLS +url = { version = "2.2.2", optional = true } # URL Validation diff --git a/germ/examples/ast.rs b/germ/examples/ast.rs new file mode 100644 index 0000000..884bd91 --- /dev/null +++ b/germ/examples/ast.rs @@ -0,0 +1,52 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +const EXAMPLE_GEMTEXT: &str = r#"```This is alt-text +Here goes the pre-formatted text. + +This continues the pre-formatted text on a new line after a blank line. +``` + +# This is a heading + +This is some text. + +This is more text after a blank line. + +* This is a single list item. +* This is the next list item. + +* This is a new list. +* This is the next item on the new list. + +## This is a sub-heading + +> This is a blockquote. + +### This is a sub-sub-heading. + +=> gemini://gem.rest/ This is a link to GemRest +=> /somewhere + +That was a link without text."#; + +fn main() { + for node in germ::ast::Ast::from_string(EXAMPLE_GEMTEXT).inner() { + println!("{:?}", node); + } +} diff --git a/germ/examples/ast_to_gemtext.rs b/germ/examples/ast_to_gemtext.rs new file mode 100644 index 0000000..5ceef21 --- /dev/null +++ b/germ/examples/ast_to_gemtext.rs @@ -0,0 +1,53 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +const EXAMPLE_GEMTEXT: &str = r#"```This is alt-text +Here goes the pre-formatted text. + +This continues the pre-formatted text on a new line after a blank line. +``` + +# This is a heading + +This is some text. + +This is more text after a blank line. + +* This is a single list item. +* This is the next list item. + +* This is a new list. +* This is the next item on the new list. + +## This is a sub-heading + +> This is a blockquote. + +### This is a sub-sub-heading. + +=> gemini://gem.rest/ This is a link to GemRest +=> /somewhere + +That was a link without text."#; + +fn main() { + println!( + "{}", + germ::ast::Ast::from_string(EXAMPLE_GEMTEXT).to_gemtext() + ); +} diff --git a/germ/examples/convert.html b/germ/examples/convert.html new file mode 100644 index 0000000..cdfde66 --- /dev/null +++ b/germ/examples/convert.html @@ -0,0 +1,6 @@ +
    Here goes the pre-formatted text.
    +
    +This continues the pre-formatted text on a new line after a blank line.
    +

    This is a heading

    This is some text.

    This is more text after a blank line.

    • This is a single list item.
    • +
    • This is the next list item.
    • This is a new list.
    • +
    • This is the next item on the new list.

    This is a sub-heading

    This is a blockquote.

    This is a sub-sub-heading.

    This is a link to GemRest
    /somewhere

    That was a link without text.

    \ No newline at end of file diff --git a/germ/examples/convert.md b/germ/examples/convert.md new file mode 100644 index 0000000..ea553ee --- /dev/null +++ b/germ/examples/convert.md @@ -0,0 +1,28 @@ +```This is alt-text +Here goes the pre-formatted text. + +This continues the pre-formatted text on a new line after a blank line. +``` + +# This is a heading + +This is some text. + +This is more text after a blank line. + +- This is a single list item. +- This is the next list item. + +- This is a new list. +- This is the next item on the new list. + +## This is a sub-heading + +> This is a blockquote. + +### This is a sub-sub-heading. + +[This is a link to GemRest](gemini://gem.rest/) + + +That was a link without text. diff --git a/germ/examples/html.rs b/germ/examples/html.rs new file mode 100644 index 0000000..541c01e --- /dev/null +++ b/germ/examples/html.rs @@ -0,0 +1,54 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +const EXAMPLE_GEMTEXT: &str = r#"```This is alt-text +Here goes the pre-formatted text. + +This continues the pre-formatted text on a new line after a blank line. +``` + +# This is a heading + +This is some text. + +This is more text after a blank line. + +* This is a single list item. +* This is the next list item. + +* This is a new list. +* This is the next item on the new list. + +## This is a sub-heading + +> This is a blockquote. + +### This is a sub-sub-heading. + +=> gemini://gem.rest/ This is a link to GemRest +=> /somewhere + +That was a link without text."#; + +fn main() { + std::fs::write( + "examples/convert.html", + germ::convert::from_string(EXAMPLE_GEMTEXT, &germ::convert::Target::HTML), + ) + .expect("could not write to file"); +} diff --git a/germ/examples/markdown.rs b/germ/examples/markdown.rs new file mode 100644 index 0000000..c14cdc5 --- /dev/null +++ b/germ/examples/markdown.rs @@ -0,0 +1,57 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +const EXAMPLE_GEMTEXT: &str = r#"```This is alt-text +Here goes the pre-formatted text. + +This continues the pre-formatted text on a new line after a blank line. +``` + +# This is a heading + +This is some text. + +This is more text after a blank line. + +* This is a single list item. +* This is the next list item. + +* This is a new list. +* This is the next item on the new list. + +## This is a sub-heading + +> This is a blockquote. + +### This is a sub-sub-heading. + +=> gemini://gem.rest/ This is a link to GemRest +=> /somewhere + +That was a link without text."#; + +fn main() { + std::fs::write( + "examples/convert.md", + germ::convert::from_string( + EXAMPLE_GEMTEXT, + &germ::convert::Target::Markdown, + ), + ) + .expect("could not write to file"); +} diff --git a/germ/examples/meta.rs b/germ/examples/meta.rs new file mode 100644 index 0000000..a9f4077 --- /dev/null +++ b/germ/examples/meta.rs @@ -0,0 +1,24 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +fn main() { + println!( + "{:?}", + germ::meta::Meta::from_string("text/gemini; hi=2; hi2=string=2") + ) +} diff --git a/germ/examples/request.rs b/germ/examples/request.rs new file mode 100644 index 0000000..e33710f --- /dev/null +++ b/germ/examples/request.rs @@ -0,0 +1,24 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +fn main() { + match germ::request::request(&url::Url::parse("gemini://fuwn.me").unwrap()) { + Ok(response) => println!("{:?}", response), + Err(_) => {} + } +} diff --git a/germ/src/ast.rs b/germ/src/ast.rs new file mode 100644 index 0000000..8c00b52 --- /dev/null +++ b/germ/src/ast.rs @@ -0,0 +1,28 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +//! Build AST trees from Gemtext + +mod container; +mod node; + +#[cfg(feature = "macros")] +mod macros; + +pub use container::Ast; +pub use node::Node; diff --git a/germ/src/ast/container.rs b/germ/src/ast/container.rs new file mode 100644 index 0000000..d9e4d18 --- /dev/null +++ b/germ/src/ast/container.rs @@ -0,0 +1,290 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +use super::Node; + +/// An AST structure which contains an AST tree +/// +/// # Example +/// +/// ```rust +/// let _ = germ::ast::Ast::from_string(r#"=> gemini://gem.rest/ GemRest"#); +/// ``` +pub struct Ast { + inner: Vec, +} +impl Ast { + /// Build an AST tree from Gemtext. + /// + /// # Example + /// + /// ```rust + /// let _ = germ::ast::Ast::from_string(r#"=> gemini://gem.rest/ GemRest"#); + /// ``` + #[must_use] + pub fn from_string(source: &str) -> Self { + let mut ast = vec![]; + let mut in_preformatted = false; + let mut in_list = false; + let mut lines = source.lines(); + + // Iterate over all lines in the Gemtext `source` + while let Some(line) = lines.next() { + // Evaluate the Gemtext line and append its AST node to the `ast` tree + ast.append(&mut Self::evaluate( + line, + &mut lines, + &mut in_preformatted, + &mut in_list, + )); + } + + Self { + inner: ast + } + } + + #[must_use] + pub fn to_gemtext(&self) -> String { + let mut gemtext = "".to_string(); + + for node in &self.inner { + match node { + Node::Text(text) => gemtext.push_str(&format!("{}\n", text)), + Node::Link { + to, + text, + } => + gemtext.push_str(&format!( + "=> {}{}\n", + to, + text + .clone() + .map_or_else(|| "".to_string(), |text| format!(" {}", text)), + )), + Node::Heading { + level, + text, + } => + gemtext.push_str(&format!( + "{} {}\n", + match level { + 1 => "#", + 2 => "##", + 3 => "###", + _ => "", + }, + text + )), + Node::List(items) => + gemtext.push_str(&format!( + "{}\n", + items + .iter() + .map(|i| format!("* {}", i)) + .collect::>() + .join("\n"), + )), + Node::Blockquote(text) => gemtext.push_str(&format!("> {}\n", text)), + Node::PreformattedText { + alt_text, + text, + } => + gemtext.push_str(&format!( + "```{}\n{}```\n", + alt_text.clone().unwrap_or_else(|| "".to_string()), + text + )), + Node::Whitespace => gemtext.push('\n'), + } + } + + gemtext + } + + /// The actual AST of `Ast` + /// + /// # Example + /// + /// ```rust + /// let _ = + /// germ::ast::Ast::from_string(r#"=> gemini://gem.rest/ GemRest"#).inner(); + /// ``` + #[must_use] + pub const fn inner(&self) -> &Vec { &self.inner } + + #[allow(clippy::too_many_lines)] + fn evaluate( + line: &str, + lines: &mut std::str::Lines<'_>, + in_preformatted: &mut bool, + in_list: &mut bool, + ) -> Vec { + let mut preformatted = String::new(); + let mut alt_text = String::new(); + let mut nodes = vec![]; + let mut line = line; + let mut list_items = vec![]; + + // Enter a not-so-infinite loop as sometimes, we may need to stay in an + // evaluation loop, e.g., multiline contexts: preformatted text, lists, etc. + loop { + // Match the first character of the Gemtext line to understand the line + // type + match line.get(0..1).unwrap_or("") { + "=" => { + // If the Gemtext line starts with an "=" ("=>"), it is a link line, + // so splitting it up should be easy enough. + let line = line.get(2..).unwrap(); + let mut split = line + .split_whitespace() + .map(String::from) + .collect::>() + .into_iter(); + + nodes.push(Node::Link { + to: split.next().expect("no location in link"), + text: { + let rest = split.collect::>().join(" "); + + if rest.is_empty() { + None + } else { + Some(rest) + } + }, + }); + + break; + } + "#" => { + // If the Gemtext line starts with an "#", it is a heading, so let's + // find out how deep it goes. + let level = match line.get(0..3) { + Some(root) => + if root.contains("###") { + 3 + } else if root.contains("##") { + 2 + } else if root.contains('#') { + 1 + } else { + 0 + }, + None => 0, + }; + + nodes.push(Node::Heading { + level, + // Here, we are `get`ing the `&str` starting at the `level`-th + // index, then trimming the start. These operations + // effectively off the line identifier. + text: line.get(level..).unwrap_or("").trim_start().to_string(), + }); + + break; + } + "*" => { + // If the Gemtext line starts with an asterisk, it is a list item, so + // let's enter a list context. + if !*in_list { + *in_list = true; + } + + list_items.push(line.get(1..).unwrap_or("").trim_start().to_string()); + + if let Some(next_line) = lines.next() { + line = next_line; + } else { + break; + } + } + ">" => { + // If the Gemtext line starts with an ">", it is a blockquote, so + // let's just clip off the line identifier. + nodes.push(Node::Blockquote( + line.get(1..).unwrap_or("").trim_start().to_string(), + )); + + break; + } + "`" => { + // If the Gemtext line starts with a backtick, it is a list item, so + // let's enter a preformatted text context. + *in_preformatted = !*in_preformatted; + + if *in_preformatted { + alt_text = line.get(3..).unwrap_or("").to_string(); + line = lines.next().unwrap(); + } else { + nodes.push(Node::PreformattedText { + alt_text: if alt_text.is_empty() { + None + } else { + Some(alt_text) + }, + text: preformatted, + }); + + break; + } + } + "" if !*in_preformatted => { + // If the line has nothing on it, it is a whitespace line, as long as + // we aren't in a preformatted line context. + nodes.push(Node::Whitespace); + + break; + } + // 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 + // preformatted blocks content and increment the line. + preformatted.push_str(&format!("{}\n", line)); + + line = lines.next().unwrap(); + } else { + // If we are in a list item and hit a catchall, that must mean that + // we encountered a line which is not a list line, so + // let's stop adding items to the list context. + if *in_list { + *in_list = false; + + nodes.push(Node::Text(line.to_string())); + + break; + } + + nodes.push(Node::Text(line.to_string())); + + break; + } + } + } + } + + if !list_items.is_empty() { + nodes.reverse(); + nodes.push(Node::List(list_items)); + nodes.reverse(); + } + + nodes + } +} diff --git a/germ/src/ast/macros.rs b/germ/src/ast/macros.rs new file mode 100644 index 0000000..c2e5a85 --- /dev/null +++ b/germ/src/ast/macros.rs @@ -0,0 +1,50 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +/// Convert Gemtext an `Ast` +/// +/// # Examples +/// +/// ```rust +/// // Using a value +/// assert_eq!( +/// 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!"), +/// ); +/// +/// /// Using raw Gemtext +/// assert_eq!( +/// germ::gemini_to_ast! { +/// => / A link! +/// => / Another link! +/// } +/// .to_gemtext(), +/// format!("{}\n", "=> / A link!\n=> / Another link!"), +/// ); +/// ``` +#[macro_export] +macro_rules! gemini_to_ast { + ($gemini:expr) => { + germ::ast::Ast::from_string($gemini) + }; + ($($gemini:tt)*) => { + germ::ast::Ast::from_string(germ_macros_impl::gemini_to_tt!($($gemini)*)); + }; +} diff --git a/germ/src/ast/node.rs b/germ/src/ast/node.rs new file mode 100644 index 0000000..04296ff --- /dev/null +++ b/germ/src/ast/node.rs @@ -0,0 +1,173 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +/// A Gemtext AST node. +/// +/// Each Gemtext line is a `Node`, and some lines can even be grouped together, +/// such as the `Node::List` `Node`! +/// +/// # Gemtext Resources +/// +/// - [Gemtext Documentation](https://gemini.circumlunar.space/docs/gemtext.gmi) +/// - [Gemtext Cheatsheet](https://gemini.circumlunar.space/docs/cheatsheet.gmi). +/// - [Gemini Specification](https://gemini.circumlunar.space/docs/specification.gmi). +#[derive(Debug, PartialEq)] +pub enum Node { + /// A text line + /// + /// # Example + /// + /// ```gemini + /// This is a text line + /// ``` + Text(String), + /// A link line + /// + /// # Examples + /// + /// ```gemini + /// => /this-is-the-to This is the text + /// + /// => gemini://to.somewhere.link + /// ``` + Link { + /// The location that a link line is pointing to + /// + /// # Examples + /// + /// ```gemini + /// => /this-is-the-to This is the text + /// + /// => gemini://to.somewhere.link + /// ``` + to: String, + /// The text a link line *may* have + /// + /// # Examples + /// + /// ```gemini + /// => /this-is-the-to This line has text, unlike the next one. + /// + /// => gemini://to.somewhere.link + /// ``` + text: Option, + }, + /// A heading line + /// + /// # Examples + /// + /// ```gemini + /// # This is a heading + /// + /// ## This is a sub-heading + /// + /// ### This is a sub-sub-heading + /// ``` + Heading { + /// The level of a heading + /// + /// # Examples + /// + /// ```gemini + /// # This is a level 1 heading + /// + /// ## This is a level 2 sub-heading + /// + /// ### This is a level 3 sub-sub-heading + /// ``` + level: usize, + /// The text of a heading + /// + /// # Examples + /// + /// ```gemini + /// # This is the headings text + /// + /// # This is also the headings text + /// ``` + text: String, + }, + /// A collection of sequential list item lines + /// + /// # Examples + /// + /// ```gemini + /// * These are + /// * sequential list + /// * items. + /// ``` + List(Vec), + /// A blockquote line + /// + /// # Examples + /// + /// ```gemini + /// > This is a blockquote line + /// + /// > This is also a blockquote line + /// ``` + Blockquote(String), + /// A preformatted block + /// + /// # Examples + /// + /// Try to ignore the leading backslash in-front of the triple backticks, + /// they are there to not confuse the Markdown engine. + /// + /// ```gemini + /// \```This is the alt-text + /// This is the preformatted block + /// + /// This is the rest of the preformatted block + /// \``` + /// ``` + PreformattedText { + /// A preformatted blocks alt-text + /// + /// # Examples + /// + /// Try to ignore the leading backslash in-front of the triple backticks, + /// they are there to not confuse the Markdown engine. + /// + /// ```gemini + /// \```This is the alt-text + /// This is the preformatted block + /// + /// This is the rest of the preformatted block + /// \``` + /// ``` + alt_text: Option, + /// A preformatted blocks content + /// + /// # Examples + /// + /// Try to ignore the leading backslash in-front of the triple backticks, + /// they are there to not confuse the Markdown engine. + /// + /// ```gemini + /// \```This is the alt-text + /// This is the preformatted blocks content + /// + /// This is the rest of the preformatted blocks content + /// \``` + /// ``` + text: String, + }, + /// A whitespace line, a line which contains nothing but whitespace. + Whitespace, +} diff --git a/germ/src/convert.rs b/germ/src/convert.rs new file mode 100644 index 0000000..2e323e1 --- /dev/null +++ b/germ/src/convert.rs @@ -0,0 +1,72 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +//! Convert Gemtext into many types of markup. + +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 + HTML, + /// Convert Gemtext to Markdown + Markdown, +} + +/// Convert AST'd Gemtext into an alternative markup format. +/// +/// # Example +/// +/// ```rust +/// use germ::convert; +/// +/// let _ = convert::from_ast( +/// &germ::ast::Ast::from_string(r#"=> gemini://gem.rest/ GemRest"#), +/// &convert::Target::HTML, +/// ); +/// ``` +#[must_use] +pub fn from_ast(source: &Ast, target: &Target) -> String { + match target { + Target::Markdown => markdown::convert(source.inner()), + Target::HTML => html::convert(source.inner()), + } +} + +/// Convert raw Gemtext into an alternative markup format. +/// +/// # Example +/// +/// ```rust +/// use germ::convert; +/// +/// let _ = convert::from_string( +/// r#"=> gemini://gem.rest/ GemRest"#, +/// &convert::Target::HTML, +/// ); +/// ``` +#[must_use] +pub fn from_string(source: &str, target: &Target) -> String { + from_ast(&Ast::from_string(source), target) +} diff --git a/germ/src/convert/html.rs b/germ/src/convert/html.rs new file mode 100644 index 0000000..7b1cafe --- /dev/null +++ b/germ/src/convert/html.rs @@ -0,0 +1,75 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +use crate::ast::Node; + +pub fn convert(source: &[Node]) -> String { + let mut html = String::new(); + + // Since we have an AST tree of the Gemtext, it is very easy to convert from + // this AST tree to an alternative markup format. + for node in source { + match node { + Node::Text(text) => html.push_str(&format!("

    {}

    ", text)), + Node::Link { + to, + text, + } => { + html.push_str(&format!( + "{}
    ", + to, + text.clone().unwrap_or_else(|| to.clone()) + )); + } + Node::Heading { + level, + text, + } => { + html.push_str(&format!( + "<{}>{}", + match level { + 1 => "h1", + 2 => "h2", + 3 => "h3", + _ => "p", + }, + text + )); + } + Node::List(items) => + html.push_str(&format!( + "
      {}
    ", + items + .iter() + .map(|i| format!("
  • {}
  • ", i)) + .collect::>() + .join("\n") + )), + Node::Blockquote(text) => + html.push_str(&format!("
    {}
    ", text)), + Node::PreformattedText { + text, .. + } => { + html.push_str(&format!("
    {}
    ", text)); + } + Node::Whitespace => {} + } + } + + html +} diff --git a/germ/src/convert/macros.rs b/germ/src/convert/macros.rs new file mode 100644 index 0000000..ca29050 --- /dev/null +++ b/germ/src/convert/macros.rs @@ -0,0 +1,80 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +/// Convert Gemtext into HTML +/// +/// # Examples +/// +/// ```rust +/// // Using a value +/// assert_eq!( +/// germ::gemini_to_html!("=> /to hello !"), +/// "hello !
    ", +/// ); +/// +/// // Using raw Gemtext +/// assert_eq!( +/// germ::gemini_to_html! { => /to hello ! }, +/// "hello !
    ", +/// ); +/// ``` +#[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/germ/src/convert/markdown.rs b/germ/src/convert/markdown.rs new file mode 100644 index 0000000..a38da9f --- /dev/null +++ b/germ/src/convert/markdown.rs @@ -0,0 +1,77 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +use crate::ast::Node; + +pub fn convert(source: &[Node]) -> String { + let mut markdown = String::new(); + + // Since we have an AST tree of the Gemtext, it is very easy to convert from + // this AST tree to an alternative markup format. + for node in source { + match node { + Node::Text(text) => markdown.push_str(&format!("{}\n", text)), + Node::Link { + to, + text, + } => + markdown.push_str(&*text.clone().map_or_else( + || format!("<{}>\n", to), + |text| format!("[{}]({})\n", text, to), + )), + Node::Heading { + level, + text, + } => { + markdown.push_str(&format!( + "{} {}\n", + match level { + 1 => "#", + 2 => "##", + 3 => "###", + _ => "", + }, + text + )); + } + Node::List(items) => + markdown.push_str(&format!( + "{}\n", + items + .iter() + .map(|i| format!("- {}", i)) + .collect::>() + .join("\n"), + )), + Node::Blockquote(text) => markdown.push_str(&format!("> {}\n", text)), + Node::PreformattedText { + alt_text, + text, + } => { + markdown.push_str(&format!( + "```{}\n{}```\n", + alt_text.clone().unwrap_or_else(|| "".to_string()), + text + )); + } + Node::Whitespace => markdown.push('\n'), + } + } + + markdown +} diff --git a/germ/src/lib.rs b/germ/src/lib.rs new file mode 100644 index 0000000..83bec4c --- /dev/null +++ b/germ/src/lib.rs @@ -0,0 +1,46 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +#![deny( + warnings, + nonstandard_style, + unused, + future_incompatible, + rust_2018_idioms, + unsafe_code, + clippy::all, + clippy::nursery, + clippy::pedantic +)] +#![doc = include_str!("../../README.md")] +#![recursion_limit = "128"] + +#[cfg(feature = "ast")] +pub mod ast; + +#[cfg(feature = "convert")] +pub mod convert; + +#[cfg(feature = "request")] +pub mod request; + +#[cfg(feature = "meta")] +pub mod meta; + +#[cfg(feature = "quick")] +pub mod quick; diff --git a/germ/src/meta.rs b/germ/src/meta.rs new file mode 100644 index 0000000..f8a9dfb --- /dev/null +++ b/germ/src/meta.rs @@ -0,0 +1,162 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +use std::collections::HashMap; + +/// Structure-ize a Gemini response's meta section into it's mime type and it's +/// parameters. +#[derive(Debug, Default)] +pub struct Meta { + /// The mime type of a Gemini response + mime: String, + /// The parameters of a Gemini response + parameters: HashMap, +} +impl ToString for Meta { + /// Convert a `Meta` into a `String` + /// + /// # Example + /// ```rust + /// let original_string = "text/gemini; hi=2; hi2=string=2"; + /// + /// assert_eq!( + /// germ::meta::Meta::from_string(original_string).to_string(), + /// original_string + /// ); + /// ``` + fn to_string(&self) -> String { + format!("{}{}", self.mime, { + let mut parameters = self + .parameters + .iter() + .map(|(k, v)| format!("{}={}", *k, v)) + .collect::>(); + + parameters.sort(); + parameters.reverse(); + + if parameters.is_empty() { + "".to_string() + } else { + format!("; {}", parameters.join("; ")) + } + }) + } +} +impl Meta { + /// Create a new `Meta` + /// + /// # Example + /// + /// ```rust + /// let mut meta = germ::meta::Meta::new(); + /// ``` + #[must_use] + pub fn new() -> Self { Self::default() } + + /// Create a `Meta` from a string + /// + /// # Example + /// + /// ```rust + /// assert_eq!( + /// germ::meta::Meta::from_string("text/gemini; hi=2; hi2=string=2").mime(), + /// "text/gemini", + /// ); + /// ``` + #[must_use] + pub fn from_string(meta: &str) -> Self { + let mut metas = meta.split(';'); + let mime = metas.next().unwrap_or("").to_string(); + let mut parameters = HashMap::new(); + + for parameter in metas { + let key_value = parameter + .trim_start() + .split_at(parameter.find('=').unwrap_or(0)); + + parameters.insert( + key_value.0.to_string().replace('=', ""), + key_value.1.to_string(), + ); + } + + Self { + mime, + parameters, + } + } + + /// Obtain non-mutable access to the mime of the `Meta` + /// + /// # Example + /// + /// ```rust + /// assert_eq!( + /// germ::meta::Meta::from_string("text/gemini; hi=2; hi2=string=2").mime(), + /// "text/gemini", + /// ); + /// ``` + #[must_use] + pub fn mime(&self) -> &str { &self.mime } + + /// Obtain mutable access to the mime of the `Meta` + /// + /// # Example + /// + /// ```rust + /// let mut meta = germ::meta::Meta::new(); + /// + /// *meta.mime_mut() = "text/gemini".to_string(); + /// ``` + pub fn mime_mut(&mut self) -> &mut String { &mut self.mime } + + /// Obtain non-mutable access to the parameters of the `Meta` + /// + /// # Example + /// + /// ```rust + /// assert_eq!( + /// germ::meta::Meta::from_string("text/gemini; hi=2; hi2=string=2") + /// .parameters() + /// .get("hi2"), + /// Some(&"string=2".to_string()), + /// ); + /// ``` + #[must_use] + pub const fn parameters(&self) -> &HashMap { + &self.parameters + } + + /// Obtain mutable access to the parameters of the `Meta` + /// + /// # Example + /// + /// ```rust + /// let mut meta = germ::meta::Meta::new(); + /// let mut parameters = std::collections::HashMap::new(); + /// + /// parameters.insert("hi".to_string(), "2".to_string()); + /// parameters.insert("hi2".to_string(), "string=2".to_string()); + /// + /// *meta.parameters_mut() = parameters; + /// ``` + pub fn parameters_mut(&mut self) -> &mut HashMap { + &mut self.parameters + } +} diff --git a/germ/src/quick.rs b/germ/src/quick.rs new file mode 100644 index 0000000..d952b08 --- /dev/null +++ b/germ/src/quick.rs @@ -0,0 +1,65 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +pub enum HeadingLevel { + One, + Two, + Three, +} + +#[must_use] +pub fn heading(text: &str, level: &HeadingLevel) -> String { + format!( + "{} {}", + match level { + HeadingLevel::One => "#", + HeadingLevel::Two => "##", + HeadingLevel::Three => "###", + }, + text + ) +} + +#[must_use] +pub fn list_item(text: &str) -> String { format!("* {}", text) } + +#[must_use] +pub fn list_items(items: &[&str]) -> String { + items + .iter() + .map(|item| list_item(item)) + .collect::>() + .join("\n") +} + +#[must_use] +pub fn link(text: &str, location: Option<&str>) -> String { + format!( + "=> {}{}", + text, + location.map_or_else(|| "".to_string(), |l| format!(" {}", l)) + ) +} + +#[must_use] +pub fn block_quote(text: &str) -> String { format!("> {}", text) } + +#[must_use] +pub fn preformatted_text(text: &str, alt_text: Option<&str>) -> String { + format!("```{}\n{}\n```", alt_text.unwrap_or(""), text) +} diff --git a/germ/src/request.rs b/germ/src/request.rs new file mode 100644 index 0000000..07d3552 --- /dev/null +++ b/germ/src/request.rs @@ -0,0 +1,73 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +//! Make Gemini requests and get sane, structured results + +mod response; +mod status; +mod verifier; + +use std::io::{Read, Write}; + +pub use response::Response; +pub use status::Status; +use verifier::GermVerifier; + +/// Make a request to a Gemini server. The `url` **should** be prefixed with a +/// scheme (e.g. "gemini://"). +/// +/// # Example +/// +/// ```rust +/// match germ::request::request(&url::Url::parse("gemini://fuwn.me").unwrap()) { +/// Ok(response) => println!("{:?}", response), +/// Err(_) => {} +/// } +/// ``` +/// +/// # Errors +/// - May error if the URL is invalid +/// - May error if the TLS write fails +/// - May error if the TLS read fails +pub fn request(url: &url::Url) -> anyhow::Result { + let config = rustls::ClientConfig::builder() + .with_safe_defaults() + .with_custom_certificate_verifier(std::sync::Arc::new(GermVerifier::new())) + .with_no_client_auth(); + let mut connection = rustls::ClientConnection::new( + std::sync::Arc::new(config), + url.domain().unwrap_or("").try_into()?, + )?; + let mut stream = std::net::TcpStream::connect(format!( + "{}:{}", + url.domain().unwrap_or(""), + url.port().unwrap_or(1965) + ))?; + let mut tls = rustls::Stream::new(&mut connection, &mut stream); + + tls.write_all(format!("{}\r\n", url).as_bytes())?; + + let mut plain_text = Vec::new(); + + tls.read_to_end(&mut plain_text)?; + + Ok(Response::new( + &plain_text, + tls.conn.negotiated_cipher_suite(), + )) +} diff --git a/germ/src/request/response.rs b/germ/src/request/response.rs new file mode 100644 index 0000000..5e1f436 --- /dev/null +++ b/germ/src/request/response.rs @@ -0,0 +1,71 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +use rustls::SupportedCipherSuite; + +use crate::request::Status; + +#[derive(Debug)] +pub struct Response { + status: Status, + meta: String, + content: Option, + size: usize, + suite: Option, +} +impl Response { + pub(super) fn new(data: &[u8], suite: Option) -> Self { + let string_form = String::from_utf8_lossy(data).to_string(); + let mut content = None; + let header; + + if string_form.ends_with("\r\n") { + header = string_form; + } else { + let mut string_split = string_form.split("\r\n"); + + header = string_split.next().unwrap_or("").to_string(); + content = Some(string_split.collect()); + } + + let header_split = header.split_at(2); + + Self { + status: Status::from(header_split.0.parse::().unwrap_or(0)), + meta: header_split.1.trim_start().to_string(), + content, + size: data.len(), + suite, + } + } + + #[must_use] + pub const fn status(&self) -> &Status { &self.status } + + #[must_use] + pub fn meta(&self) -> &str { &self.meta } + + #[must_use] + pub const fn content(&self) -> &Option { &self.content } + + #[must_use] + pub const fn size(&self) -> &usize { &self.size } + + #[must_use] + pub const fn suite(&self) -> &Option { &self.suite } +} diff --git a/germ/src/request/status.rs b/germ/src/request/status.rs new file mode 100644 index 0000000..f46059a --- /dev/null +++ b/germ/src/request/status.rs @@ -0,0 +1,110 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +use std::{fmt, fmt::Formatter}; + +/// Simple Gemini status reporting +/// +/// # Examples +/// +/// ```rust +/// use germ::request::Status; +/// +/// assert_eq!(Status::from(10), Status::Input); +/// assert_eq!(i32::from(Status::Input), 10); +/// ``` +#[derive(Debug, PartialEq)] +pub enum Status { + Input, + SensitiveInput, + Success, + TemporaryRedirect, + PermanentRedirect, + TemporaryFailure, + ServerUnavailable, + CGIError, + ProxyError, + SlowDown, + PermanentFailure, + NotFound, + Gone, + ProxyRefused, + BadRequest, + ClientCertificateRequired, + CertificateNotAuthorised, + CertificateNotValid, + Unsupported, +} +impl Default for Status { + fn default() -> Self { Self::Success } +} +impl From for i32 { + fn from(n: Status) -> Self { + match n { + Status::Input => 10, + Status::SensitiveInput => 11, + Status::Success => 20, + Status::TemporaryRedirect => 30, + Status::PermanentRedirect => 31, + Status::TemporaryFailure => 40, + Status::ServerUnavailable => 41, + Status::CGIError => 42, + Status::ProxyError => 43, + Status::SlowDown => 44, + Status::PermanentFailure => 50, + Status::NotFound => 51, + Status::Gone => 52, + Status::ProxyRefused => 53, + Status::BadRequest => 59, + Status::ClientCertificateRequired => 60, + Status::CertificateNotAuthorised => 61, + Status::CertificateNotValid => 62, + Status::Unsupported => 0, + } + } +} +impl From for Status { + fn from(n: i32) -> Self { + match n { + 10 => Self::Input, + 11 => Self::SensitiveInput, + 20 => Self::Success, + 30 => Self::TemporaryRedirect, + 31 => Self::PermanentRedirect, + 40 => Self::TemporaryFailure, + 41 => Self::ServerUnavailable, + 42 => Self::CGIError, + 43 => Self::ProxyError, + 44 => Self::SlowDown, + 50 => Self::PermanentFailure, + 51 => Self::NotFound, + 52 => Self::Gone, + 53 => Self::ProxyRefused, + 59 => Self::BadRequest, + 60 => Self::ClientCertificateRequired, + 61 => Self::CertificateNotAuthorised, + 62 => Self::CertificateNotValid, + _ => Self::Unsupported, + } + } +} +impl fmt::Display for Status { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{:?}", self) + } +} diff --git a/germ/src/request/verifier.rs b/germ/src/request/verifier.rs new file mode 100644 index 0000000..d6511c3 --- /dev/null +++ b/germ/src/request/verifier.rs @@ -0,0 +1,39 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +use std::time::SystemTime; + +use rustls::{client, client::ServerCertVerified, Certificate}; + +pub(super) struct GermVerifier; +impl GermVerifier { + pub const fn new() -> Self { Self {} } +} +impl client::ServerCertVerifier for GermVerifier { + fn verify_server_cert( + &self, + _end_entity: &Certificate, + _intermediates: &[Certificate], + _server_name: &client::ServerName, + _scts: &mut dyn Iterator, + _ocsp_response: &[u8], + _now: SystemTime, + ) -> Result { + Ok(ServerCertVerified::assertion()) + } +} diff --git a/germ/tests/ast.rs b/germ/tests/ast.rs new file mode 100644 index 0000000..c91d4b7 --- /dev/null +++ b/germ/tests/ast.rs @@ -0,0 +1,131 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +#[cfg(test)] +mod test { + use germ::ast::{Ast, Node}; + + const EXAMPLE_GEMTEXT: &str = r#"```This is alt-text +Here goes the pre-formatted text. + +This continues the pre-formatted text on a new line after a blank line. +``` + +# This is a heading + +This is some text. + +This is more text after a blank line. + +* This is a single list item. +* This is the next list item. + +* This is a new list. +* This is the next item on the new list. + +## This is a sub-heading + +> This is a blockquote. + +### This is a sub-sub-heading. + +=> gemini://gem.rest/ This is a link to GemRest +=> /somewhere + +That was a link without text."#; + + #[test] + fn build_multi_line_list_with_text() { + assert_eq!( + *Ast::from_string("* item1\n* 2\nhi text").inner(), + vec![ + Node::List(vec!["item1".to_string(), "2".to_string()]), + Node::Text("hi text".to_string()), + ], + ); + } + + #[test] + fn build_multi_line_vec() { + assert_eq!( + *Ast::from_string("=> /test hi\nhi there\n> hi").inner(), + vec![ + Node::Link { + to: "/test".to_string(), + text: Some("hi".to_string()), + }, + Node::Text("hi there".to_string()), + Node::Blockquote("hi".to_string()), + ], + ); + } + + #[test] + fn build_single_0th_from_vec() { + assert_eq!( + Ast::from_string("=> /test hi").inner(), + &vec![Node::Link { + to: "/test".to_string(), + text: Some("hi".to_string()), + }], + ); + } + + #[test] + fn build_single_element() { + assert_eq!( + Ast::from_string("=> /test hi").inner().get(0).unwrap(), + &Node::Link { + to: "/test".to_string(), + text: Some("hi".to_string()), + }, + ); + } + + #[test] + fn gemtext_to_ast_then_ast_to_gemtext() { + assert_eq!( + Ast::from_string(EXAMPLE_GEMTEXT).to_gemtext(), + // `to_gemtext` appends a newline to all responses, so let's make sure we + // account for that. + format!("{}\n", EXAMPLE_GEMTEXT), + ); + } + + #[test] + 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 + // account for that. + format!("{}\n", EXAMPLE_GEMTEXT), + ); + } + + #[test] + fn gemtext_to_ast_then_ast_to_gemtext_macro_block() { + assert_eq!( + germ::gemini_to_ast! { + => / A link! + => / Another link! + } + .to_gemtext(), + format!("{}\n", "=> / A link!\n=> / Another link!"), + ); + } +} diff --git a/germ/tests/convert.rs b/germ/tests/convert.rs new file mode 100644 index 0000000..de1baf2 --- /dev/null +++ b/germ/tests/convert.rs @@ -0,0 +1,83 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +#[cfg(test)] +mod test { + use germ::{ + convert::{from_string, Target}, + gemini_to_html, + gemini_to_md, + }; + + #[test] + fn convert_from_string_to_html_single_line() { + assert_eq!(from_string("hi", &Target::HTML), "

    hi

    ",); + } + + #[test] + fn convert_from_string_to_html_multi_line() { + assert_eq!( + from_string("hi\n# hi", &Target::HTML), + "

    hi

    hi

    ", + ); + } + + #[test] + fn convert_from_string_to_html_single_link_macro_expression() { + assert_eq!( + gemini_to_html!("=> /to hello !"), + "hello !
    ", + ); + } + + #[test] + fn convert_from_string_to_html_single_link_macro_block() { + assert_eq!( + gemini_to_html! { => /to hello ! }, + "hello !
    ", + ); + } + + #[test] + fn convert_from_string_to_markdown_single_line() { + assert_eq!(from_string("hi", &Target::Markdown), "hi\n",); + } + + #[test] + fn convert_from_string_to_markdown_multi_line() { + assert_eq!(from_string("hi\n# hi", &Target::Markdown), "hi\n# hi\n",); + } + + #[test] + fn convert_from_string_to_markdown_single_link() { + assert_eq!( + from_string("=> /to hello !", &Target::Markdown), + "[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",); + } +} diff --git a/germ/tests/meta.rs b/germ/tests/meta.rs new file mode 100644 index 0000000..70c8adc --- /dev/null +++ b/germ/tests/meta.rs @@ -0,0 +1,101 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +#[cfg(test)] +mod test { + use germ::meta::Meta; + + #[test] + fn construct_meta_with_mime() { + let mut meta = Meta::new(); + + *meta.mime_mut() = "text/gemini".to_string(); + + assert_eq!(meta.to_string(), "text/gemini"); + } + + #[test] + fn construct_meta_with_mime_and_parameters() { + let mut meta = Meta::new(); + let mut parameters = std::collections::HashMap::new(); + + parameters.insert("hi".to_string(), "2".to_string()); + parameters.insert("hi2".to_string(), "string=2".to_string()); + + *meta.mime_mut() = "text/gemini".to_string(); + *meta.parameters_mut() = parameters; + + assert_eq!(meta.to_string(), "text/gemini; hi=2; hi2=string=2"); + } + + #[test] + fn meta_to_string_without_parameters() { + let original_string = "text/gemini"; + + assert_eq!( + Meta::from_string(original_string).to_string(), + original_string + ); + } + + #[test] + fn meta_to_string_with_parameters() { + let original_string = "text/gemini; hi=2; hi2=string=2"; + + assert_eq!( + Meta::from_string(original_string).to_string(), + original_string + ); + } + + #[test] + fn meta_to_mime_without_parameters() { + let meta = Meta::from_string("text/gemini"); + + assert_eq!(meta.mime(), "text/gemini"); + assert_eq!(meta.parameters().len(), 0); + } + + #[test] + fn meta_to_map_mime() { + assert_eq!( + Meta::from_string("text/gemini; hi=2; hi2=string=2").mime(), + "text/gemini", + ); + } + + #[test] + fn meta_to_map_with_parameters() { + assert_eq!( + Meta::from_string("text/gemini; hi=2; hi2=string=2") + .parameters() + .get("hi2"), + Some(&"string=2".to_string()), + ); + } + + #[test] + fn meta_to_map_length() { + assert_eq!( + Meta::from_string("text/gemini; hi=2; hi2=string=2") + .parameters() + .len(), + 2, + ); + } +} diff --git a/germ/tests/quick.rs b/germ/tests/quick.rs new file mode 100644 index 0000000..255acd9 --- /dev/null +++ b/germ/tests/quick.rs @@ -0,0 +1,71 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +#[cfg(test)] +mod test { + use germ::quick::{self, heading}; + + #[test] + fn all_heading_levels() { + assert_eq!(heading("Soup", &germ::quick::HeadingLevel::One), "# Soup"); + assert_eq!( + heading("Vegetables", &germ::quick::HeadingLevel::Two), + "## Vegetables" + ); + assert_eq!( + heading("Fruits", &germ::quick::HeadingLevel::Three), + "### Fruits" + ); + } + + #[test] + fn list_item() { + assert_eq!(quick::list_item("Soup"), "* Soup"); + } + + #[test] + fn list_items() { + assert_eq!( + quick::list_items(&["Soup", "Vegetables", "Fruits"]), + "* Soup\n* Vegetables\n* Fruits" + ); + } + + #[test] + fn link_variants() { + assert_eq!(quick::link("Soup", None), "=> Soup"); + assert_eq!( + quick::link("Soup", Some("gemini://soup.com")), + "=> Soup gemini://soup.com" + ); + } + + #[test] + fn block_quote() { + assert_eq!(quick::block_quote("Soup"), "> Soup"); + } + + #[test] + fn preformatted_text_variants() { + assert_eq!(quick::preformatted_text("Soup", None), "```\nSoup\n```"); + assert_eq!( + quick::preformatted_text("Vegetables", Some("Fruits")), + "```Fruits\nVegetables\n```" + ); + } +} diff --git a/germ/tests/status.rs b/germ/tests/status.rs new file mode 100644 index 0000000..51f3f66 --- /dev/null +++ b/germ/tests/status.rs @@ -0,0 +1,32 @@ +// This file is part of Germ . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +#[cfg(test)] +mod test { + use germ::request::Status; + + #[test] + fn status_from_i32() { + assert_eq!(Status::from(10), Status::Input); + } + + #[test] + fn i32_from_status() { + assert_eq!(i32::from(Status::Input), 10); + } +} -- cgit v1.2.3