Browse Source

draft gemtext links conversion to pango markup

CPP-GTK4
yggverse 2 months ago
parent
commit
bf14e7f173
  1. 92
      src/app/browser/main/tab/page/content/text/gemini.cpp
  2. 4
      src/app/browser/main/tab/page/content/text/gemini.hpp

92
src/app/browser/main/tab/page/content/text/gemini.cpp

@ -13,7 +13,7 @@ Gemini::Gemini( @@ -13,7 +13,7 @@ Gemini::Gemini(
);
auto label = Gtk::make_managed<Gtk::Label>( // @TODO separated file?
to_pango(
to_pango_markup(
GEMTEXT
)
);
@ -40,8 +40,94 @@ Gemini::Gemini( @@ -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(
"<a href=\"%s\" title=\"%s\">%s</a>",
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;
}

4
src/app/browser/main/tab/page/content/text/gemini.hpp

@ -1,10 +1,12 @@ @@ -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 <glibmm/markup.h>
#include <glibmm/regex.h>
#include <glibmm/ustring.h>
#include <gtkmm/label.h>
#include <gtkmm/viewport.h>
#include <sstream>
namespace app::browser::main::tab::page::content::text
{
@ -16,7 +18,7 @@ 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
);
};

Loading…
Cancel
Save