From 849bc2459bb383af40d169e61ec6371ab1927dc7 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sat, 14 Sep 2024 05:25:50 +0300 Subject: [PATCH] update markup builder api --- .../main/tab/page/content/text/gemini.cpp | 149 ++++++++++-------- .../main/tab/page/content/text/gemini.hpp | 37 ++++- 2 files changed, 117 insertions(+), 69 deletions(-) diff --git a/src/app/browser/main/tab/page/content/text/gemini.cpp b/src/app/browser/main/tab/page/content/text/gemini.cpp index d0587260..392c5f85 100644 --- a/src/app/browser/main/tab/page/content/text/gemini.cpp +++ b/src/app/browser/main/tab/page/content/text/gemini.cpp @@ -13,7 +13,7 @@ Gemini::Gemini( ); auto label = Gtk::make_managed( // @TODO separated file? - to_pango_markup( + Markup::make( GEMTEXT ) ); @@ -51,10 +51,38 @@ Gemini::Gemini( ); } -Glib::ustring Gemini::to_pango_markup( +// Match tools +bool Gemini::Line::Match::link( + const Glib::ustring & GEMTEXT, + Glib::ustring & address, + Glib::ustring & date, + Glib::ustring & alt +) { + auto match = Glib::Regex::split_simple( + R"regex(^=>\s*([^\s]+)(\s(\d{4}-\d{2}-\d{2}))?(\s(.+))?$)regex", + GEMTEXT + ); + + int index = 0; for (const Glib::ustring & MATCH : match) + { + switch (index) + { + case 1: address = MATCH; break; + case 3: date = MATCH; break; + case 5: alt = MATCH; break; + } + + index++; + } + + return !address.empty(); +} + +// Markup tools +Glib::ustring Gemini::Markup::make( const Glib::ustring & GEMTEXT ) { - Glib::ustring markup; + Glib::ustring pango; std::istringstream stream( GEMTEXT @@ -64,81 +92,70 @@ Glib::ustring Gemini::to_pango_markup( while (std::getline(stream, line)) { - // Convert links - auto match = Glib::Regex::split_simple( - R"regex(^=>\s*([^\s]+)(\s(\d{4}-\d{2}-\d{2}))?(\s(.+))?$)regex", - line.c_str() - ); - - Glib::ustring address = ""; - Glib::ustring date = ""; - Glib::ustring alt = ""; - - int index = 0; - - for (const Glib::ustring & VALUE : match) - { - switch (index) - { - case 1: address = VALUE; break; - case 3: date = VALUE; break; - case 5: alt = VALUE; break; - } + // Links + Glib::ustring address; + Glib::ustring date; + Glib::ustring alt; - index++; - } - - // Keep original on address not found in line - if (address.empty()) + if (Line::Match::link(line, address, date, alt)) { - markup.append( - line + pango.append( + Markup::Make::link( + address, + date, + alt + ) ); } - // Make pango link else { - // Crate link name - Glib::ustring name; - - if (!date.empty()) - { - name.append( - date - ); - } - - if (!alt.empty()) - { - name.append( - name.empty() ? alt - : name + " " + alt // append (to date) - ); - } - - // Create pango markup - markup.append( - Glib::ustring::sprintf( - "%s", - Glib::Markup::escape_text( - address // @TODO to absolute - ), - Glib::Markup::escape_text( - address - ), - Glib::Markup::escape_text( - name - ) - ) + pango.append( + GEMTEXT ); } - markup.append( + // @TODO other tags.. + + pango.append( "\n" // @TODO ); } - // Return original gemtext on failure or pango markup on success - return markup.empty() ? GEMTEXT : markup; + return pango; +} + +Glib::ustring Gemini::Markup::Make::link( + const Glib::ustring & ADDRESS, + const Glib::ustring & DATE, + const Glib::ustring & ALT +) { + Glib::ustring description; + + if (!DATE.empty()) + { + description.append( + DATE + ); + } + + if (!ALT.empty()) + { + description.append( + description.empty() ? ALT : description + " " + ALT // append (to date) + ); + } + + return Glib::ustring::sprintf( + "%s", + Glib::Markup::escape_text( + ADDRESS // @TODO to absolute + ), + Glib::Markup::escape_text( + ADDRESS + ), + Glib::Markup::escape_text( + description + ) + ); } \ No newline at end of file diff --git a/src/app/browser/main/tab/page/content/text/gemini.hpp b/src/app/browser/main/tab/page/content/text/gemini.hpp index 1a74f395..e44a3f4f 100644 --- a/src/app/browser/main/tab/page/content/text/gemini.hpp +++ b/src/app/browser/main/tab/page/content/text/gemini.hpp @@ -12,13 +12,44 @@ namespace app::browser::main::tab::page::content::text { class Gemini : public Gtk::Viewport { - public: + /* + * Tools (currently is private) + */ + struct Line + { + struct Match + { + static bool link( + const Glib::ustring & GEMTEXT, + Glib::ustring & address, + Glib::ustring & date, + Glib::ustring & alt + ); + }; + }; - Gemini( + struct Markup + { + struct Make + { + static Glib::ustring link( + const Glib::ustring & ADDRESS, + const Glib::ustring & DATE, + const Glib::ustring & ALT + ); + }; + + static Glib::ustring make( const Glib::ustring & GEMTEXT ); + }; - static Glib::ustring to_pango_markup( + /* + * Gemini class API + */ + public: + + Gemini( const Glib::ustring & GEMTEXT ); };