Browse Source

add headers markup support

CPP-GTK4
yggverse 2 months ago
parent
commit
8655a95e6e
  1. 94
      src/app/browser/main/tab/page/content/text/gemini/reader.cpp
  2. 12
      src/app/browser/main/tab/page/content/text/gemini/reader.hpp

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

@ -41,6 +41,30 @@ Reader::Reader(
} }
// Match tools // Match tools
bool Reader::Line::Match::header(
const Glib::ustring & GEMTEXT,
int & level,
Glib::ustring & text
) {
auto match = Glib::Regex::split_simple(
R"regex(^(#{1,3})(.*)$)regex",
GEMTEXT
);
int index = 0; for (const Glib::ustring & MATCH : match)
{
switch (index)
{
case 1: level = MATCH.length(); break;
case 2: text = MATCH; break;
}
index++;
}
return level > 0 && !text.empty();
}
bool Reader::Line::Match::link( bool Reader::Line::Match::link(
const Glib::ustring & GEMTEXT, const Glib::ustring & GEMTEXT,
Glib::ustring & address, Glib::ustring & address,
@ -81,7 +105,23 @@ Glib::ustring Reader::make(
while (std::getline(stream, line)) while (std::getline(stream, line))
{ {
// Links // Header
int level;
Glib::ustring text;
if (Line::Match::header(line, level, text))
{
pango.append(
Reader::Make::header(
level,
text
)
);
continue;
}
// Link
Glib::ustring address; Glib::ustring address;
Glib::ustring date; Glib::ustring date;
Glib::ustring alt; Glib::ustring alt;
@ -95,25 +135,61 @@ Glib::ustring Reader::make(
alt alt
) )
); );
}
else continue;
{
pango.append(
line
);
} }
// @TODO other tags.. // @TODO other tags..
pango.append( pango.append(
"\n" // @TODO line.append(
"\n"
) // @TODO
); );
} }
return pango; return pango;
} }
Glib::ustring Reader::Make::header(
const int & LEVEL,
const Glib::ustring & VALUE
) {
switch (LEVEL)
{
case 1:
return Glib::ustring::sprintf(
"<span size=\"xx-large\">%s</span>\n",
Glib::Markup::escape_text(
VALUE
)
);
case 2:
return Glib::ustring::sprintf(
"<span size=\"x-large\">%s</span>\n",
Glib::Markup::escape_text(
VALUE
)
);
case 3:
return Glib::ustring::sprintf(
"<span size=\"large\">%s</span>\n",
Glib::Markup::escape_text(
VALUE
)
);
default:
throw _("Header level not supported"); // @TODO
}
}
Glib::ustring Reader::Make::link( Glib::ustring Reader::Make::link(
const Glib::ustring & ADDRESS, const Glib::ustring & ADDRESS,
const Glib::ustring & DATE, const Glib::ustring & DATE,
@ -136,7 +212,7 @@ Glib::ustring Reader::Make::link(
} }
return Glib::ustring::sprintf( return Glib::ustring::sprintf(
"<a href=\"%s\" title=\"%s\">%s</a>", "<a href=\"%s\" title=\"%s\">%s</a>\n",
Glib::Markup::escape_text( Glib::Markup::escape_text(
ADDRESS // @TODO to absolute ADDRESS // @TODO to absolute
), ),

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

@ -1,6 +1,7 @@
#ifndef APP_BROWSER_MAIN_TAB_PAGE_CONTENT_TEXT_GEMINI_READER_HPP #ifndef APP_BROWSER_MAIN_TAB_PAGE_CONTENT_TEXT_GEMINI_READER_HPP
#define APP_BROWSER_MAIN_TAB_PAGE_CONTENT_TEXT_GEMINI_READER_HPP #define APP_BROWSER_MAIN_TAB_PAGE_CONTENT_TEXT_GEMINI_READER_HPP
#include <glibmm/i18n.h>
#include <glibmm/markup.h> #include <glibmm/markup.h>
#include <glibmm/regex.h> #include <glibmm/regex.h>
#include <glibmm/ustring.h> #include <glibmm/ustring.h>
@ -18,6 +19,12 @@ namespace app::browser::main::tab::page::content::text::gemini
{ {
struct Match struct Match
{ {
static bool header(
const Glib::ustring & GEMTEXT,
int & level,
Glib::ustring & text
);
static bool link( static bool link(
const Glib::ustring & GEMTEXT, const Glib::ustring & GEMTEXT,
Glib::ustring & address, Glib::ustring & address,
@ -29,6 +36,11 @@ namespace app::browser::main::tab::page::content::text::gemini
struct Make struct Make
{ {
static Glib::ustring header(
const int & LEVEL,
const Glib::ustring & VALUE
);
static Glib::ustring link( static Glib::ustring link(
const Glib::ustring & ADDRESS, const Glib::ustring & ADDRESS,
const Glib::ustring & DATE, const Glib::ustring & DATE,

Loading…
Cancel
Save