From 8655a95e6eb758f2d00c8cc53c4f6d94a0a7f42e Mon Sep 17 00:00:00 2001 From: yggverse Date: Sat, 14 Sep 2024 07:41:07 +0300 Subject: [PATCH] add headers markup support --- .../tab/page/content/text/gemini/reader.cpp | 94 +++++++++++++++++-- .../tab/page/content/text/gemini/reader.hpp | 12 +++ 2 files changed, 97 insertions(+), 9 deletions(-) diff --git a/src/app/browser/main/tab/page/content/text/gemini/reader.cpp b/src/app/browser/main/tab/page/content/text/gemini/reader.cpp index f5a5d51..2854369 100644 --- a/src/app/browser/main/tab/page/content/text/gemini/reader.cpp +++ b/src/app/browser/main/tab/page/content/text/gemini/reader.cpp @@ -41,6 +41,30 @@ Reader::Reader( } // 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( const Glib::ustring & GEMTEXT, Glib::ustring & address, @@ -81,7 +105,23 @@ Glib::ustring Reader::make( 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 date; Glib::ustring alt; @@ -95,25 +135,61 @@ Glib::ustring Reader::make( alt ) ); - } - else - { - pango.append( - line - ); + continue; } // @TODO other tags.. pango.append( - "\n" // @TODO + line.append( + "\n" + ) // @TODO ); } return pango; } +Glib::ustring Reader::Make::header( + const int & LEVEL, + const Glib::ustring & VALUE +) { + switch (LEVEL) + { + case 1: + + return Glib::ustring::sprintf( + "%s\n", + Glib::Markup::escape_text( + VALUE + ) + ); + + case 2: + + return Glib::ustring::sprintf( + "%s\n", + Glib::Markup::escape_text( + VALUE + ) + ); + + case 3: + + return Glib::ustring::sprintf( + "%s\n", + Glib::Markup::escape_text( + VALUE + ) + ); + + default: + + throw _("Header level not supported"); // @TODO + } +} + Glib::ustring Reader::Make::link( const Glib::ustring & ADDRESS, const Glib::ustring & DATE, @@ -136,7 +212,7 @@ Glib::ustring Reader::Make::link( } return Glib::ustring::sprintf( - "%s", + "%s\n", Glib::Markup::escape_text( ADDRESS // @TODO to absolute ), diff --git a/src/app/browser/main/tab/page/content/text/gemini/reader.hpp b/src/app/browser/main/tab/page/content/text/gemini/reader.hpp index f9ac076..9ef1f8a 100644 --- a/src/app/browser/main/tab/page/content/text/gemini/reader.hpp +++ b/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 #define APP_BROWSER_MAIN_TAB_PAGE_CONTENT_TEXT_GEMINI_READER_HPP +#include #include #include #include @@ -18,6 +19,12 @@ namespace app::browser::main::tab::page::content::text::gemini { struct Match { + static bool header( + const Glib::ustring & GEMTEXT, + int & level, + Glib::ustring & text + ); + static bool link( const Glib::ustring & GEMTEXT, Glib::ustring & address, @@ -29,6 +36,11 @@ namespace app::browser::main::tab::page::content::text::gemini struct Make { + static Glib::ustring header( + const int & LEVEL, + const Glib::ustring & VALUE + ); + static Glib::ustring link( const Glib::ustring & ADDRESS, const Glib::ustring & DATE,