about summary refs log tree commit diff
diff options
context:
space:
mode:
authorgennyble <gen@nyble.dev>2023-12-27 14:09:58 -0600
committergennyble <gen@nyble.dev>2023-12-27 14:09:58 -0600
commitcd672d033386d4f575f31b49bebb81f20e808d7f (patch)
treecac9cc9b93ffff995be425c65f90c928185e07b6
parent0eefb6b385e78b142f691833c856c1cda01f20a0 (diff)
downloadcutie-cd672d033386d4f575f31b49bebb81f20e808d7f.tar.gz
cutie-cd672d033386d4f575f31b49bebb81f20e808d7f.zip
parses comments
-rw-r--r--src/lib.rs58
1 files changed, 28 insertions, 30 deletions
diff --git a/src/lib.rs b/src/lib.rs
index e0c8c5b..31e8a17 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,3 +1,5 @@
+use core::fmt;
+
 pub struct Html {
 	pub nodes: Vec<Node>,
 }
@@ -10,7 +12,6 @@ impl Html {
 
 		loop {
 			let Consumed { node, remaining } = Self::parse_node(raw);
-
 			nodes.push(node);
 
 			match remaining {
@@ -22,17 +23,15 @@ impl Html {
 
 	fn parse_node(raw: &str) -> Consumed {
 		match Self::is_tag(raw) {
-			Some(_) => match Self::parse_comment(raw) {
-				None => {
-					print!("Node ");
+			Some(_) => {
+				if let Some(cmt) = Self::parse_comment(raw) {
+					cmt
+				} else {
 					Self::parse_tag(raw)
 				}
-				Some(cmt) => cmt,
-			},
+			}
 			None => {
-				print!("Text ");
 				let cons = Self::parse_text(raw);
-				println!("## {:?}", cons.node);
 				cons
 			}
 		}
@@ -48,7 +47,6 @@ impl Html {
 				root_tag.name
 			)
 		} else if root_tag.self_closing {
-			println!("self close return early");
 			return Consumed {
 				node: Node::Tag {
 					self_closing: true,
@@ -72,7 +70,6 @@ impl Html {
 					},
 					remaining,
 				)) if name == root_tag.name => {
-					println!("ret closed - {name}");
 					break Consumed {
 						node: Node::Tag {
 							self_closing: false,
@@ -83,7 +80,6 @@ impl Html {
 					};
 				}
 				_ => {
-					println!("recur. ends on {}", root_tag.name,);
 					let cons = Self::parse_node(rest.unwrap());
 					rest = cons.remaining;
 					children.push(cons.node);
@@ -93,22 +89,11 @@ impl Html {
 	}
 
 	fn parse_comment(raw: &str) -> Option<Consumed> {
-		if raw.starts_with("<!--") {
-			let after_start = &raw[4..];
-			match after_start.find("-->") {
-				None => None,
-				Some(end) => {
-					let comment = &after_start[..end];
-					let rest = after_start.get(end + 3..);
-
-					Some(Consumed {
-						node: Node::Comment {
-							body: comment.into(),
-						},
-						remaining: rest,
-					})
-				}
-			}
+		if let Some(after_start) = raw.strip_prefix("<!--") {
+			after_start.find("-->").map(|end| Consumed {
+				node: Node::Comment(after_start[..end].into()),
+				remaining: after_start.get(end + 3..),
+			})
 		} else {
 			None
 		}
@@ -243,9 +228,7 @@ pub enum Node {
 		name: String,
 		children: Vec<Node>,
 	},
-	Comment {
-		body: String,
-	},
+	Comment(String),
 }
 
 #[macro_export]
@@ -255,6 +238,13 @@ macro_rules! text {
 	};
 }
 
+#[macro_export]
+macro_rules! comment {
+	($text:expr) => {
+		Node::Comment(String::from($text))
+	};
+}
+
 #[cfg(test)]
 mod test {
 	use crate::{Html, Node};
@@ -304,6 +294,14 @@ mod test {
 	}
 
 	#[test]
+	fn parse_node_parses_comment() {
+		let cmt = "<!-- Comment! -->";
+
+		let node = Html::parse_node(cmt);
+		assert_eq!(node.node, comment!(" Comment! "));
+	}
+
+	#[test]
 	fn parse_node_parses_tag() {
 		let basic = "<p>Hello!</p>";