aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-06-19 09:17:36 +0000
committerFuwn <[email protected]>2024-06-19 09:17:36 +0000
commit6d92ea622bfc98a831a9b94f19896fe0c215f794 (patch)
tree34a8341128580cc190caecb16e9c62e2eb1f72ec
parentfeat(germ): bump version (diff)
downloadgerm-0.4.2.tar.xz
germ-0.4.2.zip
fix(ast): parse preformatted gemtext as preformatted contentv0.4.2
-rw-r--r--Cargo.toml3
-rw-r--r--examples/ast.rs31
-rw-r--r--examples/ast_to_gemtext.rs31
-rw-r--r--examples/html.rs35
-rw-r--r--examples/markdown.rs31
-rw-r--r--src/ast/container.rs12
-rw-r--r--src/lib.rs44
-rw-r--r--tests/ast.rs69
-rw-r--r--tests/node.rs4
9 files changed, 78 insertions, 182 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 3798572..4e2bed9 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -2,7 +2,7 @@
[package]
name = "germ"
-version = "0.4.1"
+version = "0.4.2"
authors = ["Fuwn <[email protected]>"]
edition = "2021"
description = "The Ultimate Gemini Toolkit."
@@ -23,6 +23,7 @@ macros = ["ast", "convert"]
meta = []
request = ["rustls", "url", "anyhow", "tokio", "tokio-rustls"]
quick = []
+example-gemtext = []
[dependencies]
anyhow = { version = "1.0.70", optional = true } # `Result`
diff --git a/examples/ast.rs b/examples/ast.rs
index 5fd0b26..efff88b 100644
--- a/examples/ast.rs
+++ b/examples/ast.rs
@@ -19,38 +19,9 @@
//! This example demonstrates Germ's capabilities for parsing Gemtext into an
//! abstract syntax tree.
-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() {
// Parse `EXAMPLE_GEMTEXT` into an abstract syntax tree
- let ast = germ::ast::Ast::from_string(EXAMPLE_GEMTEXT);
+ let ast = germ::ast::Ast::from_string(germ::EXAMPLE_GEMTEXT);
// Get the nodes of the abstract syntax tree
let ast_nodes = ast.inner();
diff --git a/examples/ast_to_gemtext.rs b/examples/ast_to_gemtext.rs
index d163c8f..3f8f610 100644
--- a/examples/ast_to_gemtext.rs
+++ b/examples/ast_to_gemtext.rs
@@ -19,38 +19,9 @@
//! This example converts Gemtext into an abstract syntax tree and then back
//! into Gemtext, demonstrating both Germ's parsing and generation capabilities.
-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() {
// Parse `EXAMPLE_GEMTEXT` into an abstract syntax tree
- let ast = germ::ast::Ast::from_string(EXAMPLE_GEMTEXT);
+ let ast = germ::ast::Ast::from_string(germ::EXAMPLE_GEMTEXT);
// Convert the abstract syntax tree back to Gemtext
let gemtext = ast.to_gemtext();
diff --git a/examples/html.rs b/examples/html.rs
index 391867f..09b2396 100644
--- a/examples/html.rs
+++ b/examples/html.rs
@@ -19,39 +19,12 @@
//! This example demonstrates Germ's capabilities for converting Gemtext to
//! HTML.
-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() {
// Convert the Gemtext to HTML
- let html =
- germ::convert::from_string(EXAMPLE_GEMTEXT, &germ::convert::Target::HTML);
+ let html = germ::convert::from_string(
+ germ::EXAMPLE_GEMTEXT,
+ &germ::convert::Target::HTML,
+ );
// Write the HTML to a file
std::fs::write("examples/convert.html", html)
diff --git a/examples/markdown.rs b/examples/markdown.rs
index f909894..1bbdd73 100644
--- a/examples/markdown.rs
+++ b/examples/markdown.rs
@@ -19,39 +19,10 @@
//! This example demonstrates Germ's capabilities for converting Gemtext to
//! Markdown.
-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() {
// Convert the Gemtext to Markdown
let html = germ::convert::from_string(
- EXAMPLE_GEMTEXT,
+ germ::EXAMPLE_GEMTEXT,
&germ::convert::Target::Markdown,
);
diff --git a/src/ast/container.rs b/src/ast/container.rs
index 1e6c63f..c8ef58c 100644
--- a/src/ast/container.rs
+++ b/src/ast/container.rs
@@ -190,7 +190,7 @@ impl Ast {
// Match the first character of the Gemtext line to understand the line
// type
match line.get(0..1).unwrap_or("") {
- "=" => {
+ "=" if !*in_preformatted => {
// 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();
@@ -211,7 +211,7 @@ impl Ast {
break;
}
- "#" => {
+ "#" if !*in_preformatted => {
// If the Gemtext line starts with an "#", it is a heading, so let's
// find out how deep it goes.
let level =
@@ -234,7 +234,7 @@ impl Ast {
break;
}
- "*" => {
+ "*" if !*in_preformatted => {
// If the Gemtext line starts with an asterisk, it is a list item, so
// let's enter a list context.
if !*in_list {
@@ -249,7 +249,7 @@ impl Ast {
break;
}
}
- ">" => {
+ ">" if !*in_preformatted => {
// If the Gemtext line starts with an ">", it is a blockquote, so
// let's just clip off the line identifier.
nodes.push(Node::Blockquote(
@@ -259,8 +259,8 @@ impl Ast {
break;
}
"`" => {
- // If the Gemtext line starts with a backtick, it is a list item, so
- // let's enter a preformatted text context.
+ // If the Gemtext line starts with a backtick, it's a preformatted
+ // toggle, so let's enter a preformatted text context.
*in_preformatted = !*in_preformatted;
if *in_preformatted {
diff --git a/src/lib.rs b/src/lib.rs
index b7156a1..aa193f1 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -38,3 +38,47 @@
#[cfg(feature = "meta")] pub mod meta;
#[cfg(feature = "quick")] pub mod quick;
+
+#[cfg(feature = "example-gemtext")]
+pub 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
+
+```This is a preformatted block containing inner Gemtext.
+=> gemini://fuwn.me/ This is a link.
+
+* This is a list item.
+
+> This is a blockquote.
+
+# This is a heading.
+
+## This is a sub-heading.
+
+### This is a sub-sub-heading.
+```
+
+That was a link without text.";
diff --git a/tests/ast.rs b/tests/ast.rs
index f95591a..021fd96 100644
--- a/tests/ast.rs
+++ b/tests/ast.rs
@@ -18,69 +18,34 @@
#[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."#;
+ use germ::{
+ ast::{Ast, Node},
+ EXAMPLE_GEMTEXT,
+ };
#[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()),
- ],
- );
+ 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()),
- ],
- );
+ 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()),
- }],
- );
+ assert_eq!(Ast::from_string("=> /test hi").inner(), &vec![Node::Link {
+ to: "/test".to_string(),
+ text: Some("hi".to_string()),
+ }],);
}
#[test]
diff --git a/tests/node.rs b/tests/node.rs
index ff224a4..0faf8c3 100644
--- a/tests/node.rs
+++ b/tests/node.rs
@@ -4,8 +4,8 @@ mod test {
fn node_to_gemtext() {
assert_eq!(
germ::ast::Node::Link {
- to: "/faq".to_string(),
- text: Some("FAQ".to_string())
+ to: "/faq".to_string(),
+ text: Some("FAQ".to_string()),
}
.to_gemtext(),
"=> /faq FAQ",