From b6ac752e32e6871fbe3226334aabc8e9af7c294b Mon Sep 17 00:00:00 2001 From: Mike Skec Date: Sun, 27 Dec 2020 18:43:19 +1100 Subject: Added page title parsing for Gemini and HTML --- src/browsertab.cpp | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) (limited to 'src/browsertab.cpp') diff --git a/src/browsertab.cpp b/src/browsertab.cpp index 70a1a66..c470d9e 100644 --- a/src/browsertab.cpp +++ b/src/browsertab.cpp @@ -451,10 +451,11 @@ void BrowserTab::on_requestComplete(const QByteArray &ref_data, const QString &m } this->successfully_loaded = true; + this->page_title = ""; + renderPage(data, mime); - QString title = this->current_location.toString(); - emit this->titleChanged(title); + this->updatePageTitle(); this->current_stats.file_size = ref_data.size(); this->current_stats.mime_type = mime; @@ -496,7 +497,8 @@ void BrowserTab::renderPage(const QByteArray &data, const MimeType &mime) data, this->current_location, doc_style, - this->outline); + this->outline, + &this->page_title); } else if (not plaintext_only and mime.is("text","gophermap")) { @@ -512,7 +514,27 @@ void BrowserTab::renderPage(const QByteArray &data, const MimeType &mime) document->setDefaultFont(doc_style.standard_font); document->setDefaultStyleSheet(doc_style.toStyleSheet()); document->setDocumentMargin(doc_style.margin); - document->setHtml(QString::fromUtf8(data)); + QString page_html = QString::fromUtf8(data); + document->setHtml(page_html); + + // Find page title in HTML + // Split so we only look in the + QStringList head = page_html.split("", Qt::KeepEmptyParts, Qt::CaseInsensitive); + if (head[0] != page_html) + { + // Split at first title tag. + QStringList a = head[0].split("", Qt::KeepEmptyParts, Qt::CaseInsensitive); + if (a[0] != head[0]) + { + // Split at second tag. + QStringList b = a[1].split("", Qt::KeepEmptyParts, Qt::CaseInsensitive); + if (b[0] != a[1]) + { + QString title = b[0]; + this->page_title = title; + } + } + } } else if (not plaintext_only and mime.is("text","x-kristall-theme")) { @@ -633,6 +655,19 @@ void BrowserTab::rerenderPage() this->renderPage(this->current_buffer, this->current_mime); } +void BrowserTab::updatePageTitle() +{ + if (page_title.isEmpty()) + { + page_title = this->current_location.toString(); + } + + // TODO: Shorten lengthy titles? + + emit this->titleChanged(this->page_title); +} + + void BrowserTab::on_inputRequired(const QString &query, const bool is_sensitive) { this->network_timeout_timer.stop(); -- cgit v1.2.3