diff options
| author | Mike Skec <skec@protonmail.ch> | 2020-12-27 18:43:19 +1100 |
|---|---|---|
| committer | Felix Queißner <felix@ib-queissner.de> | 2020-12-27 11:01:48 +0100 |
| commit | b6ac752e32e6871fbe3226334aabc8e9af7c294b (patch) | |
| tree | 59aaaeaaca4abba586dce348d51f05a74ed447a4 /src | |
| parent | 5bfe89f71957910975ae576cf968cf1ec32db49e (diff) | |
| download | kristall-b6ac752e32e6871fbe3226334aabc8e9af7c294b.tar.gz | |
Added page title parsing for Gemini and HTML
Diffstat (limited to 'src')
| -rw-r--r-- | src/browsertab.cpp | 43 | ||||
| -rw-r--r-- | src/browsertab.hpp | 4 | ||||
| -rw-r--r-- | src/renderers/geminirenderer.cpp | 9 | ||||
| -rw-r--r-- | src/renderers/geminirenderer.hpp | 3 |
4 files changed, 53 insertions, 6 deletions
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 <head> + QStringList head = page_html.split("</head>", Qt::KeepEmptyParts, Qt::CaseInsensitive); + if (head[0] != page_html) + { + // Split at first title tag. + QStringList a = head[0].split("<title>", Qt::KeepEmptyParts, Qt::CaseInsensitive); + if (a[0] != head[0]) + { + // Split at second tag. + QStringList b = a[1].split("</title>", 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(); diff --git a/src/browsertab.hpp b/src/browsertab.hpp index 182e397..53f2ef3 100644 --- a/src/browsertab.hpp +++ b/src/browsertab.hpp @@ -77,6 +77,8 @@ public: void rerenderPage(); + void updatePageTitle(); + signals: void titleChanged(QString const & title); void locationChanged(QUrl const & url); @@ -189,6 +191,8 @@ public: QTextCursor current_search_position; bool needs_rerender; + + QString page_title; }; #endif // BROWSERTAB_HPP diff --git a/src/renderers/geminirenderer.cpp b/src/renderers/geminirenderer.cpp index f23a048..5c26e6b 100644 --- a/src/renderers/geminirenderer.cpp +++ b/src/renderers/geminirenderer.cpp @@ -29,7 +29,8 @@ std::unique_ptr<GeminiDocument> GeminiRenderer::render( const QByteArray &input, QUrl const &root_url, DocumentStyle const & themed_style, - DocumentOutlineModel &outline) + DocumentOutlineModel &outline, + QString* const page_title) { TextStyleInstance text_style { themed_style }; @@ -154,6 +155,12 @@ std::unique_ptr<GeminiDocument> GeminiRenderer::render( cursor.insertText(heading + "\n", fmt); outline.appendH1(heading, id); + + // Use first heading as the page's title. + if (page_title != nullptr && page_title->isEmpty()) + { + *page_title = heading; + } } else if (line.startsWith("=>")) { diff --git a/src/renderers/geminirenderer.hpp b/src/renderers/geminirenderer.hpp index 0c07cb9..65fdcf1 100644 --- a/src/renderers/geminirenderer.hpp +++ b/src/renderers/geminirenderer.hpp @@ -32,7 +32,8 @@ struct GeminiRenderer QByteArray const & input, QUrl const & root_url, DocumentStyle const & style, - DocumentOutlineModel & outline + DocumentOutlineModel & outline, + QString* const page_title = nullptr ); }; |
