diff --git a/Cargo.toml b/Cargo.toml index e51eda66..4b3d8947 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ version = "0.11.0" [dependencies.gemtext] package = "ggemtext" -version = "0.2.1" +version = "0.3.0" [dependencies.gtk] package = "gtk4" @@ -35,6 +35,7 @@ version = "0.32.1" [dependencies.openssl] version = "0.10.68" +# development [patch.crates-io] #ggemini = { path = "ggemini" } -#ggemtext = { path = "ggemtext" } +ggemtext = { git = "https://github.com/YGGverse/ggemtext.git" } diff --git a/src/app/browser/window/tab/item/page/content/text/gemini.rs b/src/app/browser/window/tab/item/page/content/text/gemini.rs index 561be006..4e4fd594 100644 --- a/src/app/browser/window/tab/item/page/content/text/gemini.rs +++ b/src/app/browser/window/tab/item/page/content/text/gemini.rs @@ -17,7 +17,7 @@ impl Gemini { // Construct pub fn new(gemtext: &str, base: &Uri, actions: (Rc, Rc)) -> Self { // Init components - let reader = Rc::new(Reader::new(gemtext, base, actions)); + let reader = Rc::new(Reader::new(gemtext, base, actions).unwrap()); // @TODO handle errors let widget = Rc::new(Widget::new(&reader.widget.text_view)); // Result diff --git a/src/app/browser/window/tab/item/page/content/text/gemini/reader.rs b/src/app/browser/window/tab/item/page/content/text/gemini/reader.rs index 26c180c6..664c9419 100644 --- a/src/app/browser/window/tab/item/page/content/text/gemini/reader.rs +++ b/src/app/browser/window/tab/item/page/content/text/gemini/reader.rs @@ -1,6 +1,8 @@ +pub mod error; mod tag; mod widget; +pub use error::Error; use tag::Tag; use widget::Widget; @@ -32,7 +34,11 @@ pub struct Reader { impl Reader { // Construct - pub fn new(gemtext: &str, base: &Uri, actions: (Rc, Rc)) -> Self { + pub fn new( + gemtext: &str, + base: &Uri, + actions: (Rc, Rc), + ) -> Result { // Init default values let mut title = None; @@ -77,36 +83,39 @@ impl Reader { } } Some(ref mut this) => { - Code::multiline_continue_from(this, line); + match Code::multiline_continue_from(this, line) { + Ok(()) => { + // Close tag found: + if this.completed { + // Is alt provided + if let Some(alt) = &this.alt { + // Insert alt value to the main buffer + buffer.insert_with_tags( + &mut buffer.end_iter(), + alt.as_str(), + &[&tag.title.text_tag], + ); + buffer.insert(&mut buffer.end_iter(), "\n"); + } - // Close tag found: - if this.completed { - // Is alt provided - if let Some(alt) = &this.alt { - // Insert alt value to the main buffer - buffer.insert_with_tags( - &mut buffer.end_iter(), - alt.as_str(), - &[&tag.title.text_tag], - ); - buffer.insert(&mut buffer.end_iter(), "\n"); + // Insert multiline code buffer into main buffer + buffer.insert_with_tags( + &mut buffer.end_iter(), + &this.buffer.join("\n"), + &[&tag.code.text_tag], + ); + + buffer.insert(&mut buffer.end_iter(), "\n"); + + // Reset + multiline = None; + } + + // Skip other actions for this line + continue; } - - // Insert multiline code buffer into main buffer - buffer.insert_with_tags( - &mut buffer.end_iter(), - &this.buffer.join("\n"), - &[&tag.code.text_tag], - ); - - buffer.insert(&mut buffer.end_iter(), "\n"); - - // Reset - multiline = None; + Err(e) => return Err(Error::Gemtext(e.to_string())), } - - // Skip other actions for this line - continue; } }; @@ -351,6 +360,6 @@ impl Reader { }); // @TODO may be expensive for CPU, add timeout? // Result - Self { title, widget } + Ok(Self { title, widget }) } } diff --git a/src/app/browser/window/tab/item/page/content/text/gemini/reader/error.rs b/src/app/browser/window/tab/item/page/content/text/gemini/reader/error.rs new file mode 100644 index 00000000..2a9f75cc --- /dev/null +++ b/src/app/browser/window/tab/item/page/content/text/gemini/reader/error.rs @@ -0,0 +1,16 @@ +use std::fmt::{Display, Formatter, Result}; + +#[derive(Debug)] +pub enum Error { + Gemtext(String), +} + +impl Display for Error { + fn fmt(&self, f: &mut Formatter) -> Result { + match self { + Self::Gemtext(e) => { + write!(f, "Gemtext error: {e}") + } + } + } +}