diff --git a/README.md b/README.md index 235e416c..0c7af40b 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ GTK 4 / Libadwaita client written in Rust Make sure your system support: +* Glib 2.56+ * GTK 4.8+ * Libadwaita 1.4+ 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 649ff933..e1cdb0ca 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 @@ -7,15 +7,13 @@ use widget::Widget; use gtk::{ gio::SimpleAction, - glib::{GString, Uri}, + glib::{GString, TimeZone, Uri}, prelude::{TextBufferExt, TextBufferExtManual}, TextBuffer, TextTag, TextTagTable, TextView, WrapMode, }; use std::sync::Arc; -const NEW_LINE: &str = "\n"; - pub struct Reader { title: Option, // css: CssProvider, @@ -68,7 +66,6 @@ impl Reader { tags.add(&link); // Parse lines - let buffer = TextBuffer::new(Some(&tags)); for line in gemtext.lines() { @@ -88,7 +85,7 @@ impl Reader { &[tag], ); - buffer.insert(&mut buffer.end_iter(), NEW_LINE); + buffer.insert(&mut buffer.end_iter(), "\n"); // Set title if empty, on first document header match // this feature wanted to update parent elements like tab title @@ -100,7 +97,7 @@ impl Reader { } // Is link - if let Some(link) = Link::from(line, Some(base)) { + if let Some(link) = Link::from(line, Some(base), Some(&TimeZone::local())) { // Build link alt from optional values let mut alt = Vec::new(); @@ -112,8 +109,11 @@ impl Reader { } // Append date on exist - if let Some(date) = link.date { - alt.push(date.to_string()); + if let Some(timestamp) = link.timestamp { + // https://docs.gtk.org/glib/method.DateTime.format.html + if let Ok(value) = timestamp.format("%Y-%m-%d") { + alt.push(value.to_string()) + } } // Append alt on exist or use URL @@ -123,14 +123,14 @@ impl Reader { }); buffer.insert_with_tags_by_name(&mut buffer.end_iter(), &alt.join(" "), &["link"]); - buffer.insert(&mut buffer.end_iter(), NEW_LINE); + buffer.insert(&mut buffer.end_iter(), "\n"); continue; } // Nothing match, use plain text @TODO buffer.insert(&mut buffer.end_iter(), line); - buffer.insert(&mut buffer.end_iter(), NEW_LINE); + buffer.insert(&mut buffer.end_iter(), "\n"); } // Init widget diff --git a/src/app/browser/window/tab/item/page/content/text/gemini/reader/parser/link.rs b/src/app/browser/window/tab/item/page/content/text/gemini/reader/parser/link.rs index 423b764c..2856fdb2 100644 --- a/src/app/browser/window/tab/item/page/content/text/gemini/reader/parser/link.rs +++ b/src/app/browser/window/tab/item/page/content/text/gemini/reader/parser/link.rs @@ -1,17 +1,19 @@ -use gtk::glib::{GString, Regex, RegexCompileFlags, RegexMatchFlags, Uri, UriFlags}; +use gtk::glib::{ + DateTime, GString, Regex, RegexCompileFlags, RegexMatchFlags, TimeZone, Uri, UriFlags, +}; pub struct Link { - pub alt: Option, - pub date: Option, // @TODO https://docs.gtk.org/glib/struct.Date.html - pub is_external: Option, // on to_base option provided - pub uri: Uri, + pub alt: Option, // [optional] alternative link description + pub is_external: Option, // [optional] external link indication, on base option provided + pub timestamp: Option, // [optional] valid link DateTime object + pub uri: Uri, // [required] valid link URI object } impl Link { - pub fn from(line: &str, to_base: Option<&Uri>) -> Option { + pub fn from(line: &str, base: Option<&Uri>, timezone: Option<&TimeZone>) -> Option { // Define initial values let mut alt = None; - let mut date = None; + let mut timestamp = None; let mut is_external = None; // Begin line parse @@ -26,7 +28,7 @@ impl Link { let unresolved_address = regex.get(1)?; // Convert address to the valid URI - let uri = match to_base { + let uri = match base { // Base conversion requested Some(base_uri) => { // Convert relative address to absolute @@ -64,9 +66,15 @@ impl Link { } }; - // Date - if let Some(value) = regex.get(2) { - date = Some(GString::from(value.as_str())) + // Timestamp + if let Some(date) = regex.get(2) { + // @TODO even possible, but simpler to work with `DateTime` API + // await for new features in `Date` as better in Gemini context + // https://docs.gtk.org/glib/struct.Date.html + timestamp = match DateTime::from_iso8601(&format!("{date}T00:00:00"), timezone) { + Ok(value) => Some(value), + Err(_) => None, + } } // Alt @@ -76,8 +84,8 @@ impl Link { Some(Self { alt, - date, is_external, + timestamp, uri, }) }