Browse Source

detect reader title on first header tag match

master
yggverse 2 months ago
parent
commit
454feaeba5
  1. 18
      src/browser/main/tab/page/content/text/gemini/reader/mod.rs

18
src/browser/main/tab/page/content/text/gemini/reader/mod.rs

@ -4,11 +4,13 @@ use parser::header::Header;
use parser::plain::Plain; use parser::plain::Plain;
use gtk::{ use gtk::{
glib::GString,
prelude::{StyleContextExt, WidgetExt}, prelude::{StyleContextExt, WidgetExt},
Align, CssProvider, Label, STYLE_PROVIDER_PRIORITY_APPLICATION, Align, CssProvider, Label, STYLE_PROVIDER_PRIORITY_APPLICATION,
}; };
pub struct Reader { pub struct Reader {
title: Option<GString>,
css: CssProvider, css: CssProvider,
widget: Label, widget: Label,
} }
@ -16,13 +18,23 @@ pub struct Reader {
impl Reader { impl Reader {
// Construct // Construct
pub fn new(gemtext: &str) -> Self { pub fn new(gemtext: &str) -> Self {
// Init title
let mut title = None;
// Init markup // Init markup
let mut markup = String::new(); let mut markup = String::new();
for line in gemtext.lines() { for line in gemtext.lines() {
// Is header // Is header
if let Some(header) = Header::from(line) { if let Some(header) = Header::from(line) {
// Format
markup.push_str(header.markup()); markup.push_str(header.markup());
// Set title from first document header tag
if title == None {
title = Some(header.text().clone());
}
continue; continue;
} }
@ -58,10 +70,14 @@ impl Reader {
.add_provider(&css, STYLE_PROVIDER_PRIORITY_APPLICATION); .add_provider(&css, STYLE_PROVIDER_PRIORITY_APPLICATION);
// Result // Result
Self { css, widget } Self { title, css, widget }
} }
// Getters // Getters
pub fn title(&self) -> &Option<GString> {
&self.title
}
pub fn widget(&self) -> &Label { pub fn widget(&self) -> &Label {
&self.widget &self.widget
} }

Loading…
Cancel
Save