Browse Source

create separated widget for markup

CPP-GTK4
yggverse 2 months ago
parent
commit
712d1801d8
  1. 2
      Makefile
  2. 2
      po/POTFILES.in
  3. 147
      src/app/browser/main/tab/page/content/text/gemini.cpp
  4. 38
      src/app/browser/main/tab/page/content/text/gemini.hpp
  5. 150
      src/app/browser/main/tab/page/content/text/gemini/markup.cpp
  6. 53
      src/app/browser/main/tab/page/content/text/gemini/markup.hpp
  7. 26
      src/app/browser/main/tab/page/content/text/plain.cpp
  8. 2
      src/app/browser/main/tab/page/content/text/plain.hpp
  9. 28
      src/app/browser/main/tab/page/content/text/plain/markup.cpp
  10. 23
      src/app/browser/main/tab/page/content/text/plain/markup.hpp

2
Makefile

@ -19,7 +19,9 @@ SRCS = src/app.cpp\ @@ -19,7 +19,9 @@ SRCS = src/app.cpp\
src/app/browser/main/tab/page/content.cpp\
src/app/browser/main/tab/page/content/text.cpp\
src/app/browser/main/tab/page/content/text/gemini.cpp\
src/app/browser/main/tab/page/content/text/gemini/markup.cpp\
src/app/browser/main/tab/page/content/text/plain.cpp\
src/app/browser/main/tab/page/content/text/plain/markup.cpp\
src/app/browser/main/tab/page/navigation.cpp\
src/app/browser/main/tab/page/navigation/base.cpp\
src/app/browser/main/tab/page/navigation/bookmark.cpp\

2
po/POTFILES.in

@ -14,7 +14,9 @@ src/app/browser/main/tab/page.cpp @@ -14,7 +14,9 @@ src/app/browser/main/tab/page.cpp
src/app/browser/main/tab/page/content.cpp
src/app/browser/main/tab/page/content/text.cpp
src/app/browser/main/tab/page/content/text/gemini.cpp
src/app/browser/main/tab/page/content/text/gemini/markup.cpp
src/app/browser/main/tab/page/content/text/plain.cpp
src/app/browser/main/tab/page/content/text/plain/markup.cpp
src/app/browser/main/tab/page/navigation.cpp
src/app/browser/main/tab/page/navigation/base.cpp
src/app/browser/main/tab/page/navigation/bookmark.cpp

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

@ -1,161 +1,22 @@ @@ -1,161 +1,22 @@
#include "gemini.hpp"
#include "gemini/markup.hpp"
using namespace app::browser::main::tab::page::content::text;
Gemini::Gemini(
const Glib::ustring & GEMTEXT
) : Gtk::Viewport( // add scrolled window features support
) : Gtk::Viewport( // add scrolled window features to childs
NULL,
NULL
) {
// Init widget
set_scroll_to_focus(
false
);
auto label = Gtk::make_managed<Gtk::Label>( // @TODO separated file?
Markup::make(
GEMTEXT
)
);
// Init widget
label->set_valign(
Gtk::Align::START
);
label->set_wrap(
true
);
label->set_selectable(
true
);
label->set_use_markup(
true
);
// Connect signals
label->signal_activate_link().connect(
[label](const Glib::ustring & URI) -> bool
{
// @TODO follow action
return false;
},
false // after
);
set_child(
* label
);
}
// 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",
* Gtk::make_managed<gemini::Markup>(
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 pango;
std::istringstream stream(
GEMTEXT
);
std::string line;
while (std::getline(stream, line))
{
// Links
Glib::ustring address;
Glib::ustring date;
Glib::ustring alt;
if (Line::Match::link(line, address, date, alt))
{
pango.append(
Markup::Make::link(
address,
date,
alt
)
);
}
else
{
pango.append(
line
);
}
// @TODO other tags..
pango.append(
"\n" // @TODO
);
}
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(
"<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
)
);
}

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

@ -1,51 +1,13 @@ @@ -1,51 +1,13 @@
#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>
namespace app::browser::main::tab::page::content::text
{
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:
Gemini(

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

@ -0,0 +1,150 @@ @@ -0,0 +1,150 @@
#include "markup.hpp"
using namespace app::browser::main::tab::page::content::text::gemini;
Markup::Markup(
const Glib::ustring & GEMTEXT
) {
// Init widget
set_valign(
Gtk::Align::START
);
set_wrap(
true
);
set_selectable(
true
);
set_use_markup(
true
);
set_markup(
Markup::make(
GEMTEXT
)
);
// Connect signals
signal_activate_link().connect(
[this](const Glib::ustring & URI) -> bool
{
// @TODO follow action
return false;
},
false // after
);
}
// Match tools
bool Markup::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 Markup::make(
const Glib::ustring & GEMTEXT
) {
Glib::ustring pango;
std::istringstream stream(
GEMTEXT
);
std::string line;
while (std::getline(stream, line))
{
// Links
Glib::ustring address;
Glib::ustring date;
Glib::ustring alt;
if (Line::Match::link(line, address, date, alt))
{
pango.append(
Markup::Make::link(
address,
date,
alt
)
);
}
else
{
pango.append(
line
);
}
// @TODO other tags..
pango.append(
"\n" // @TODO
);
}
return pango;
}
Glib::ustring 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
)
);
}

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

@ -0,0 +1,53 @@ @@ -0,0 +1,53 @@
#ifndef APP_BROWSER_MAIN_TAB_PAGE_CONTENT_TEXT_GEMINI_MARKUP_HPP
#define APP_BROWSER_MAIN_TAB_PAGE_CONTENT_TEXT_GEMINI_MARKUP_HPP
#include <glibmm/markup.h>
#include <glibmm/regex.h>
#include <glibmm/ustring.h>
#include <gtkmm/label.h>
namespace app::browser::main::tab::page::content::text::gemini
{
class Markup : public Gtk::Label
{
/*
* 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 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:
Markup(
const Glib::ustring & GEMTEXT
);
};
}
#endif // APP_BROWSER_MAIN_TAB_PAGE_CONTENT_TEXT_GEMINI_MARKUP_HPP

26
src/app/browser/main/tab/page/content/text/plain.cpp

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
#include "plain.hpp"
#include "plain/markup.hpp"
using namespace app::browser::main::tab::page::content::text;
@ -12,28 +13,9 @@ Plain::Plain( @@ -12,28 +13,9 @@ Plain::Plain(
false
);
auto label = Gtk::make_managed<Gtk::Label>( // @TODO separated file?
TEXT
);
// Init widget
label->set_valign(
Gtk::Align::START
);
label->set_wrap(
true
);
label->set_selectable(
true
);
label->set_use_markup(
false
);
set_child(
* label
* Gtk::make_managed<plain::Markup>(
TEXT
)
);
}

2
src/app/browser/main/tab/page/content/text/plain.hpp

@ -2,8 +2,6 @@ @@ -2,8 +2,6 @@
#define APP_BROWSER_MAIN_TAB_PAGE_CONTENT_TEXT_PLAIN_HPP
#include <glibmm/ustring.h>
#include <gtkmm/enums.h>
#include <gtkmm/label.h>
#include <gtkmm/viewport.h>
namespace app::browser::main::tab::page::content::text

28
src/app/browser/main/tab/page/content/text/plain/markup.cpp

@ -0,0 +1,28 @@ @@ -0,0 +1,28 @@
#include "markup.hpp"
using namespace app::browser::main::tab::page::content::text::plain;
Markup::Markup(
const Glib::ustring & TEXT
) {
// Init widget
set_valign(
Gtk::Align::START
);
set_wrap(
true
);
set_selectable(
true
);
set_use_markup(
false // @TODO
);
set_text(
TEXT
);
}

23
src/app/browser/main/tab/page/content/text/plain/markup.hpp

@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
#ifndef APP_BROWSER_MAIN_TAB_PAGE_CONTENT_TEXT_PLAIN_MARKUP_HPP
#define APP_BROWSER_MAIN_TAB_PAGE_CONTENT_TEXT_PLAIN_MARKUP_HPP
#include <glibmm/markup.h>
#include <glibmm/ustring.h>
#include <gtkmm/label.h>
namespace app::browser::main::tab::page::content::text::plain
{
class Markup : public Gtk::Label
{
/*
* Gemini class API
*/
public:
Markup(
const Glib::ustring & TEXT
);
};
}
#endif // APP_BROWSER_MAIN_TAB_PAGE_CONTENT_TEXT_PLAIN_MARKUP_HPP
Loading…
Cancel
Save