update markup builder api

This commit is contained in:
yggverse 2024-09-14 05:25:50 +03:00
parent e10b693bbf
commit 849bc2459b
2 changed files with 122 additions and 74 deletions

View File

@ -13,7 +13,7 @@ Gemini::Gemini(
); );
auto label = Gtk::make_managed<Gtk::Label>( // @TODO separated file? auto label = Gtk::make_managed<Gtk::Label>( // @TODO separated file?
to_pango_markup( Markup::make(
GEMTEXT 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 const Glib::ustring & GEMTEXT
) { ) {
Glib::ustring markup; Glib::ustring pango;
std::istringstream stream( std::istringstream stream(
GEMTEXT GEMTEXT
@ -64,81 +92,70 @@ Glib::ustring Gemini::to_pango_markup(
while (std::getline(stream, line)) while (std::getline(stream, line))
{ {
// Convert links // Links
auto match = Glib::Regex::split_simple( Glib::ustring address;
R"regex(^=>\s*([^\s]+)(\s(\d{4}-\d{2}-\d{2}))?(\s(.+))?$)regex", Glib::ustring date;
line.c_str() Glib::ustring alt;
);
Glib::ustring address = ""; if (Line::Match::link(line, address, date, alt))
Glib::ustring date = "";
Glib::ustring alt = "";
int index = 0;
for (const Glib::ustring & VALUE : match)
{ {
switch (index) pango.append(
{ Markup::Make::link(
case 1: address = VALUE; break; address,
case 3: date = VALUE; break; date,
case 5: alt = VALUE; break; alt
}
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( else
{
pango.append(
GEMTEXT
);
}
// @TODO other tags..
pango.append(
"\n" // @TODO "\n" // @TODO
); );
} }
// Return original gemtext on failure or pango markup on success return pango;
return markup.empty() ? GEMTEXT : markup; }
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(
"<a href=\"%s\" title=\"%s\">%s</a>",
Glib::Markup::escape_text(
ADDRESS // @TODO to absolute
),
Glib::Markup::escape_text(
ADDRESS
),
Glib::Markup::escape_text(
description
)
);
} }

View File

@ -12,15 +12,46 @@ namespace app::browser::main::tab::page::content::text
{ {
class Gemini : public Gtk::Viewport class Gemini : public Gtk::Viewport
{ {
/*
* Tools (currently is private)
*/
struct Line
{
struct Match
{
static bool link(
const Glib::ustring & GEMTEXT,
Glib::ustring & address,
Glib::ustring & date,
Glib::ustring & alt
);
};
};
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
);
};
/*
* Gemini class API
*/
public: public:
Gemini( Gemini(
const Glib::ustring & GEMTEXT const Glib::ustring & GEMTEXT
); );
static Glib::ustring to_pango_markup(
const Glib::ustring & GEMTEXT
);
}; };
} }