update ggemtext api to v0.3.0

This commit is contained in:
yggverse 2024-12-02 16:12:30 +02:00
parent 2a59bebea9
commit cdf6e78408
4 changed files with 58 additions and 32 deletions

View File

@ -21,7 +21,7 @@ version = "0.11.0"
[dependencies.gemtext] [dependencies.gemtext]
package = "ggemtext" package = "ggemtext"
version = "0.2.1" version = "0.3.0"
[dependencies.gtk] [dependencies.gtk]
package = "gtk4" package = "gtk4"
@ -35,6 +35,7 @@ version = "0.32.1"
[dependencies.openssl] [dependencies.openssl]
version = "0.10.68" version = "0.10.68"
# development
[patch.crates-io] [patch.crates-io]
#ggemini = { path = "ggemini" } #ggemini = { path = "ggemini" }
#ggemtext = { path = "ggemtext" } ggemtext = { git = "https://github.com/YGGverse/ggemtext.git" }

View File

@ -17,7 +17,7 @@ impl Gemini {
// Construct // Construct
pub fn new(gemtext: &str, base: &Uri, actions: (Rc<WindowAction>, Rc<TabAction>)) -> Self { pub fn new(gemtext: &str, base: &Uri, actions: (Rc<WindowAction>, Rc<TabAction>)) -> Self {
// Init components // 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)); let widget = Rc::new(Widget::new(&reader.widget.text_view));
// Result // Result

View File

@ -1,6 +1,8 @@
pub mod error;
mod tag; mod tag;
mod widget; mod widget;
pub use error::Error;
use tag::Tag; use tag::Tag;
use widget::Widget; use widget::Widget;
@ -32,7 +34,11 @@ pub struct Reader {
impl Reader { impl Reader {
// Construct // Construct
pub fn new(gemtext: &str, base: &Uri, actions: (Rc<WindowAction>, Rc<TabAction>)) -> Self { pub fn new(
gemtext: &str,
base: &Uri,
actions: (Rc<WindowAction>, Rc<TabAction>),
) -> Result<Self, Error> {
// Init default values // Init default values
let mut title = None; let mut title = None;
@ -77,36 +83,39 @@ impl Reader {
} }
} }
Some(ref mut this) => { 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: // Insert multiline code buffer into main buffer
if this.completed { buffer.insert_with_tags(
// Is alt provided &mut buffer.end_iter(),
if let Some(alt) = &this.alt { &this.buffer.join("\n"),
// Insert alt value to the main buffer &[&tag.code.text_tag],
buffer.insert_with_tags( );
&mut buffer.end_iter(),
alt.as_str(), buffer.insert(&mut buffer.end_iter(), "\n");
&[&tag.title.text_tag],
); // Reset
buffer.insert(&mut buffer.end_iter(), "\n"); multiline = None;
}
// Skip other actions for this line
continue;
} }
Err(e) => return Err(Error::Gemtext(e.to_string())),
// 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;
} }
}; };
@ -351,6 +360,6 @@ impl Reader {
}); // @TODO may be expensive for CPU, add timeout? }); // @TODO may be expensive for CPU, add timeout?
// Result // Result
Self { title, widget } Ok(Self { title, widget })
} }
} }

View File

@ -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}")
}
}
}
}