From bf14e7f17343cb9731151edf5e857292d6dd390d Mon Sep 17 00:00:00 2001 From: yggverse Date: Sat, 14 Sep 2024 03:22:03 +0300 Subject: [PATCH] draft gemtext links conversion to pango markup --- .../main/tab/page/content/text/gemini.cpp | 92 ++++++++++++++++++- .../main/tab/page/content/text/gemini.hpp | 4 +- 2 files changed, 92 insertions(+), 4 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 b5547b65..bddaca5a 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( + to_pango_markup( GEMTEXT ) ); @@ -40,8 +40,94 @@ Gemini::Gemini( ); } -Glib::ustring Gemini::to_pango( +Glib::ustring Gemini::to_pango_markup( const Glib::ustring & GEMTEXT ) { - return GEMTEXT; // @TODO + Glib::ustring markup; + + std::istringstream stream( + GEMTEXT + ); + + std::string line; + + 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; + } + + index++; + } + + // Keep original on address not found in line + if (address.empty()) + { + markup.append( + line + ); + } + + // 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 + ) + ) + ); + } + + markup.append( + "\n" // @TODO + ); + } + + // Return original gemtext on failure or pango markup on success + return markup.empty() ? GEMTEXT : markup; } \ 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 3c9551c9..1a74f395 100644 --- a/src/app/browser/main/tab/page/content/text/gemini.hpp +++ b/src/app/browser/main/tab/page/content/text/gemini.hpp @@ -1,10 +1,12 @@ #ifndef APP_BROWSER_MAIN_TAB_PAGE_CONTENT_TEXT_GEMINI_HPP #define APP_BROWSER_MAIN_TAB_PAGE_CONTENT_TEXT_GEMINI_HPP +#include #include #include #include #include +#include namespace app::browser::main::tab::page::content::text { @@ -16,7 +18,7 @@ namespace app::browser::main::tab::page::content::text const Glib::ustring & GEMTEXT ); - static Glib::ustring to_pango( + static Glib::ustring to_pango_markup( const Glib::ustring & GEMTEXT ); };