From d82f923084b2e06b3aac4d5f1d81cd5031a0e8ef Mon Sep 17 00:00:00 2001 From: rambip Date: Tue, 3 Oct 2023 22:10:05 +0200 Subject: [PATCH] fixed strange html conditions --- Cargo.toml | 2 +- src/component.rs | 2 -- src/render.rs | 64 ++++++++++++++++++++++++++++-------------------- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 657fd3f..5f2cad1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "leptos-markdown" -version = "0.5.0" +version = "0.5.1" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/component.rs b/src/component.rs index e323cc7..689d94c 100644 --- a/src/component.rs +++ b/src/component.rs @@ -24,8 +24,6 @@ fn parse_attribute_value(stream: &mut Peekable) loop { match stream.peek() { None => return Err("expected attribute value".into()), - Some(&' ')|Some(&'>')| Some(&'/') - => return Err("please use \" to wrap your attribute".into()), Some(&'"') => break, _ => attribute.push(stream.next().unwrap()) } diff --git a/src/render.rs b/src/render.rs index d40d087..fe5907e 100644 --- a/src/render.rs +++ b/src/render.rs @@ -135,37 +135,13 @@ where I: Iterator, Range)> // when this renderer was created match self.end_tag { Some(t) if t == end => return None, - Some(_) => panic!("wrong closing tag"), + Some(t) => panic!("{t:?} is a wrong closing tag"), None => panic!("didn't expect a closing tag") } }, Text(s) => Ok(render_text(self.context, &s, range)), Code(s) => Ok(render_code(self.context, &s, range)), - Html(s) if self.current_component==None => { - if is_probably_custom_component(&s) { - self.custom_component(&s) - } - else - { - let callback = make_callback(self.context, range); - Ok( - view!{ -
-
- }.into_view() - ) - } - }, - Html(s) => { - let comp = self.current_component.clone().unwrap(); - if s.trim() == format!("", comp) { - logging::log!("exiting at comp={comp}"); - return None - } - else { - HtmlError::err(&format!("the component {comp} is not properly closed")) - } - }, + Html(s) => self.html(&s, range)?, FootnoteReference(_) => HtmlError::err("do not support footnote refs yet"), SoftBreak => Ok(self.next()?), HardBreak => Ok(view!{
}.into_view()), @@ -202,6 +178,40 @@ where I: Iterator, Range)> } } + fn html(&mut self, s: &str, range: Range) + -> Option> { + match (&self.current_component, self.end_tag) { + (None, None) if is_probably_custom_component(s) => { + Some(self.custom_component(s)) + }, + (None, _) => { + let callback = make_callback(self.context, range); + Some(Ok( + view!{ + + + }.into_view() + )) + }, + (Some(x), None) if s.trim()==format!("") => { + // legit end of custom component + return None + }, + (Some(x), None) if is_probably_custom_component(s) => { + Some(HtmlError::err(&format!("the component `{x}` is not properly closed"))) + }, + (Some(x), Some(_)) if s.trim()==format!("") => { + Some(HtmlError::err(&format!("please make sure there is a newline before the end of your component"))) + }, + _ => { + // tries to render html as raw html anyway + Some(Ok(view!{ + + }.into_view())) + }, + } + } + fn custom_component(&mut self, raw_html: &str) -> Result { let description: ComponentCall = raw_html.parse().map_err(|x| HtmlError(x))?; logging::log!("got component call {:?}", description); @@ -218,7 +228,9 @@ where I: Iterator, Range)> end_tag: self.end_tag, current_component: Some(description.name) }; + logging::log!("start collecting view"); let children = sub_renderer.collect_view(); + logging::log!("end collecting view"); Ok( comp(MdComponentProps{ attributes: description.attributes,