fix: gemtext link parsing

This commit is contained in:
Artemy Egorov 2024-08-03 21:04:03 +03:00
parent fac56efca0
commit f45808fc40
3 changed files with 19 additions and 18 deletions

2
Cargo.lock generated
View file

@ -151,7 +151,7 @@ dependencies = [
[[package]] [[package]]
name = "dalet" name = "dalet"
version = "1.0.0-pre5" version = "1.0.0-pre6"
dependencies = [ dependencies = [
"bincode", "bincode",
"clap", "clap",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "dalet" name = "dalet"
version = "1.0.0-pre5" version = "1.0.0-pre6"
edition = "2021" edition = "2021"
authors = ["artegoser"] authors = ["artegoser"]
license = "MIT" license = "MIT"

View file

@ -1,4 +1,7 @@
use crate::typed::{Body, Hl, NNBody, Tag}; use crate::typed::{
Body, Hl, NNBody,
Tag::{self, *},
};
#[derive(Debug)] #[derive(Debug)]
pub enum GemTextParseError { pub enum GemTextParseError {
@ -8,7 +11,7 @@ pub enum GemTextParseError {
pub fn parse_gemtext(s: String) -> Result<Vec<Tag>, GemTextParseError> { pub fn parse_gemtext(s: String) -> Result<Vec<Tag>, GemTextParseError> {
let mut page: Vec<Tag> = Vec::new(); let mut page: Vec<Tag> = Vec::new();
let mut preformatted = false; let mut preformatted = false;
let mut preformatted_text = String::new(); let mut preformatted_text: Vec<String> = Vec::new();
let mut list_before = false; let mut list_before = false;
let mut list: Vec<Tag> = Vec::new(); let mut list: Vec<Tag> = Vec::new();
@ -21,8 +24,7 @@ pub fn parse_gemtext(s: String) -> Result<Vec<Tag>, GemTextParseError> {
list_before = false; list_before = false;
list.clear(); list.clear();
} else if preformatted && !line.starts_with("```") { } else if preformatted && !line.starts_with("```") {
preformatted_text.push_str(&line); preformatted_text.push(line);
preformatted_text.push('\n');
} else if line.starts_with("=>") { } else if line.starts_with("=>") {
let body = line.split_off(2); let body = line.split_off(2);
let mut body = body.trim().splitn(2, " "); let mut body = body.trim().splitn(2, " ");
@ -30,37 +32,36 @@ pub fn parse_gemtext(s: String) -> Result<Vec<Tag>, GemTextParseError> {
let url = body.next().ok_or(GemTextParseError::InvalidLink)?.trim(); let url = body.next().ok_or(GemTextParseError::InvalidLink)?.trim();
match body.next() { match body.next() {
Some(label) => page.push(Tag::Link( Some(label) => {
Body::Text(label.trim().to_owned()), page.push(P(vec![Btn(label.trim().into(), url.into()).into()].into()))
url.to_owned(), }
)), None => page.push(P(vec![Btn(Body::Null, url.into())].into())),
None => page.push(Tag::Link(Body::Null, url.to_owned())),
}; };
} else if line.starts_with("# ") { } else if line.starts_with("# ") {
let body = line.split_off(2); 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("## ") { } else if line.starts_with("## ") {
let body = line.split_off(3); 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("### ") { } else if line.starts_with("### ") {
let body = line.split_off(4); 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("* ") { } else if line.starts_with("* ") {
list_before = true; list_before = true;
let body = line.split_off(2); let body = line.split_off(2);
list.push(Tag::El(NNBody::Text(body))); list.push(El(body.into()));
} else if line.starts_with("> ") { } else if line.starts_with("> ") {
let body = line.split_off(2); let body = line.split_off(2);
page.push(Tag::Bq(NNBody::Text(body))); page.push(Bq(body.into()));
} else if line.starts_with("```") { } else if line.starts_with("```") {
if preformatted { if preformatted {
page.push(Tag::Pre(preformatted_text.clone())); page.push(Pre(preformatted_text.join("\n")));
preformatted_text.clear(); preformatted_text.clear();
} }
preformatted = !preformatted; preformatted = !preformatted;
} else if !line.is_empty() { } else if !line.is_empty() {
page.push(Tag::P(NNBody::Text(line))); page.push(P(line.into()));
} }
} }