diff --git a/Makefile b/Makefile index 8524716d..bd7707db 100644 --- a/Makefile +++ b/Makefile @@ -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\ diff --git a/po/POTFILES.in b/po/POTFILES.in index bb18332d..fd29e902 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -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 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 0ccf39f1..f5281f39 100644 --- a/src/app/browser/main/tab/page/content/text/gemini.cpp +++ b/src/app/browser/main/tab/page/content/text/gemini.cpp @@ -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( // @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", - 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( - "%s", - Glib::Markup::escape_text( - ADDRESS // @TODO to absolute - ), - Glib::Markup::escape_text( - ADDRESS - ), - Glib::Markup::escape_text( - description + * Gtk::make_managed( + GEMTEXT ) ); } \ 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 a342aad5..b908de89 100644 --- a/src/app/browser/main/tab/page/content/text/gemini.hpp +++ b/src/app/browser/main/tab/page/content/text/gemini.hpp @@ -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 -#include #include -#include #include 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( diff --git a/src/app/browser/main/tab/page/content/text/gemini/markup.cpp b/src/app/browser/main/tab/page/content/text/gemini/markup.cpp new file mode 100644 index 00000000..f4e2ebac --- /dev/null +++ b/src/app/browser/main/tab/page/content/text/gemini/markup.cpp @@ -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( + "%s", + Glib::Markup::escape_text( + ADDRESS // @TODO to absolute + ), + Glib::Markup::escape_text( + ADDRESS + ), + Glib::Markup::escape_text( + description + ) + ); +} \ No newline at end of file diff --git a/src/app/browser/main/tab/page/content/text/gemini/markup.hpp b/src/app/browser/main/tab/page/content/text/gemini/markup.hpp new file mode 100644 index 00000000..dfd2fe2a --- /dev/null +++ b/src/app/browser/main/tab/page/content/text/gemini/markup.hpp @@ -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 +#include +#include +#include + +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 \ No newline at end of file diff --git a/src/app/browser/main/tab/page/content/text/plain.cpp b/src/app/browser/main/tab/page/content/text/plain.cpp index 64b9c701..52c67352 100644 --- a/src/app/browser/main/tab/page/content/text/plain.cpp +++ b/src/app/browser/main/tab/page/content/text/plain.cpp @@ -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( false ); - auto label = Gtk::make_managed( // @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( + TEXT + ) ); } \ No newline at end of file diff --git a/src/app/browser/main/tab/page/content/text/plain.hpp b/src/app/browser/main/tab/page/content/text/plain.hpp index c39f7dfc..162eb586 100644 --- a/src/app/browser/main/tab/page/content/text/plain.hpp +++ b/src/app/browser/main/tab/page/content/text/plain.hpp @@ -2,8 +2,6 @@ #define APP_BROWSER_MAIN_TAB_PAGE_CONTENT_TEXT_PLAIN_HPP #include -#include -#include #include namespace app::browser::main::tab::page::content::text diff --git a/src/app/browser/main/tab/page/content/text/plain/markup.cpp b/src/app/browser/main/tab/page/content/text/plain/markup.cpp new file mode 100644 index 00000000..4a00d095 --- /dev/null +++ b/src/app/browser/main/tab/page/content/text/plain/markup.cpp @@ -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 + ); +} \ No newline at end of file diff --git a/src/app/browser/main/tab/page/content/text/plain/markup.hpp b/src/app/browser/main/tab/page/content/text/plain/markup.hpp new file mode 100644 index 00000000..8f93fb5a --- /dev/null +++ b/src/app/browser/main/tab/page/content/text/plain/markup.hpp @@ -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 +#include +#include + +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 \ No newline at end of file