From f45808fc40114be22dfac06f4c9e5a7a9cb63588 Mon Sep 17 00:00:00 2001 From: Artemy Egorov Date: Sat, 3 Aug 2024 21:04:03 +0300 Subject: [PATCH] fix: gemtext link parsing --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/parsers/gemtext.rs | 33 +++++++++++++++++---------------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b6ed03f..f190fc5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -151,7 +151,7 @@ dependencies = [ [[package]] name = "dalet" -version = "1.0.0-pre5" +version = "1.0.0-pre6" dependencies = [ "bincode", "clap", diff --git a/Cargo.toml b/Cargo.toml index 5dc38a3..327453a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dalet" -version = "1.0.0-pre5" +version = "1.0.0-pre6" edition = "2021" authors = ["artegoser"] license = "MIT" diff --git a/src/parsers/gemtext.rs b/src/parsers/gemtext.rs index 7053383..cab8931 100644 --- a/src/parsers/gemtext.rs +++ b/src/parsers/gemtext.rs @@ -1,4 +1,7 @@ -use crate::typed::{Body, Hl, NNBody, Tag}; +use crate::typed::{ + Body, Hl, NNBody, + Tag::{self, *}, +}; #[derive(Debug)] pub enum GemTextParseError { @@ -8,7 +11,7 @@ pub enum GemTextParseError { pub fn parse_gemtext(s: String) -> Result, GemTextParseError> { let mut page: Vec = Vec::new(); let mut preformatted = false; - let mut preformatted_text = String::new(); + let mut preformatted_text: Vec = Vec::new(); let mut list_before = false; let mut list: Vec = Vec::new(); @@ -21,8 +24,7 @@ pub fn parse_gemtext(s: String) -> Result, GemTextParseError> { list_before = false; list.clear(); } else if preformatted && !line.starts_with("```") { - preformatted_text.push_str(&line); - preformatted_text.push('\n'); + preformatted_text.push(line); } else if line.starts_with("=>") { let body = line.split_off(2); let mut body = body.trim().splitn(2, " "); @@ -30,37 +32,36 @@ pub fn parse_gemtext(s: String) -> Result, GemTextParseError> { let url = body.next().ok_or(GemTextParseError::InvalidLink)?.trim(); match body.next() { - Some(label) => page.push(Tag::Link( - Body::Text(label.trim().to_owned()), - url.to_owned(), - )), - None => page.push(Tag::Link(Body::Null, url.to_owned())), + Some(label) => { + page.push(P(vec![Btn(label.trim().into(), url.into()).into()].into())) + } + None => page.push(P(vec![Btn(Body::Null, url.into())].into())), }; } else if line.starts_with("# ") { let body = line.split_off(2); - page.push(Tag::H(body.trim().to_owned(), Hl::One)); + page.push(H(body.trim().into(), Hl::One)); } else if line.starts_with("## ") { let body = line.split_off(3); - page.push(Tag::H(body.trim().to_owned(), Hl::Two)); + page.push(H(body.trim().into(), Hl::Two)); } else if line.starts_with("### ") { let body = line.split_off(4); - page.push(Tag::H(body.trim().to_owned(), Hl::Three)); + page.push(H(body.trim().into(), Hl::Three)); } else if line.starts_with("* ") { list_before = true; let body = line.split_off(2); - list.push(Tag::El(NNBody::Text(body))); + list.push(El(body.into())); } else if line.starts_with("> ") { let body = line.split_off(2); - page.push(Tag::Bq(NNBody::Text(body))); + page.push(Bq(body.into())); } else if line.starts_with("```") { if preformatted { - page.push(Tag::Pre(preformatted_text.clone())); + page.push(Pre(preformatted_text.join("\n"))); preformatted_text.clear(); } preformatted = !preformatted; } else if !line.is_empty() { - page.push(Tag::P(NNBody::Text(line))); + page.push(P(line.into())); } }