From c09929d2f732054ef0f5060371c13d2728cebdd5 Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Wed, 22 Jan 2025 14:04:12 +0100 Subject: [PATCH] Formatting --- src/Page.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ src/ansi.hpp | 11 +++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/ansi.hpp diff --git a/src/Page.cpp b/src/Page.cpp index ebe23f4..c434283 100644 --- a/src/Page.cpp +++ b/src/Page.cpp @@ -10,13 +10,54 @@ #include #include +#include +#include "ansi.hpp" #include "stringutil.hpp" #include "cpr/cpr.h" #include "libxml/HTMLparser.h" #include "libxml/xpath.h" +bool is_number(std::string_view s){ + return std::all_of(s.begin(), s.end(), [](unsigned char c ){ + return std::isdigit(c); + }); +} + +void italize_numbers(std::string &content, size_t leave_chars = 0) { + size_t end = content.size(); + + // Process backwards, word by word + for (;;) { + size_t space = content.rfind(' ', end - 1); + size_t begin = (space == std::string::npos) ? 0 : space + 1; + size_t word_length = end - begin; + + if (is_number(content.substr(begin, word_length))) { + content.insert(end, ansi::CLEAR); + content.insert(begin, ansi::ITALIC); + } + + if (space == std::string::npos) + break; + if (leave_chars >= space) + break; + + end = space; + } +} + +void pretty_format_page(std::string &content) { + content.insert(0, ansi::BOLD); + size_t line_end = content.find("\n"); + content.insert(line_end, ansi::CLEAR); + + italize_numbers(content, line_end + ansi::CLEAR.size()); +} + + + Page::Page(const uint_fast8_t number): m_number(number), m_subpage(fetchSubpage()) { } @@ -44,6 +85,8 @@ std::string Page::str() const { string_utils::limitConsecutiveWhitespace(ret, MAX_WHITESPACE); string_utils::removeTrailingWhitespace(ret); + + pretty_format_page(ret); return ret; } diff --git a/src/ansi.hpp b/src/ansi.hpp new file mode 100644 index 0000000..bbec41f --- /dev/null +++ b/src/ansi.hpp @@ -0,0 +1,11 @@ + +namespace ansi +{ + +#include + +constexpr std::string_view BOLD = "\033[1m"; +constexpr std::string_view ITALIC = "\033[3m"; +constexpr std::string_view CLEAR = "\033[0m"; + +} // namespace ansi