From f146a6a7bcf0a70937be6c7715be43d7da4b838d Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 28 Dec 2020 23:13:54 +0100 Subject: [PATCH] Added 2 pagination helpers to Utils. --- core/utils.cpp | 166 ++++++++++++++++++++++++++++++++++++++++++++++++- core/utils.h | 16 +++-- 2 files changed, 173 insertions(+), 9 deletions(-) diff --git a/core/utils.cpp b/core/utils.cpp index dc7a953..25165fa 100644 --- a/core/utils.cpp +++ b/core/utils.cpp @@ -1,8 +1,10 @@ #include "utils.h" -#include -#include +#include "html_builder.h" #include +#include +#include +#include void Utils::newline_to_br(std::string *str) { str_replace(str, "\r\n", "
"); @@ -11,7 +13,7 @@ void Utils::newline_to_br(std::string *str) { void Utils::markdown_to_html(std::string *str) { std::shared_ptr config = std::make_shared(); - config->isEmphasizedParserEnabled = true; + config->isEmphasizedParserEnabled = true; config->isHTMLWrappedInParagraph = true; std::shared_ptr parser = std::make_shared(config); @@ -23,6 +25,164 @@ void Utils::markdown_to_html(std::string *str) { (*str) = htmlOutput; } +std::string Utils::get_pagination(const std::string base_url, const uint32_t max, const uint32_t current_index, const uint32_t max_visible_links) { + std::string s = base_url; + if (s.size() > 0 && s[s.size() - 1] != '/') { + s += '/'; + } + + int starti = current_index - max_visible_links / 2; + int toi = current_index + max_visible_links / 2; + + if (starti < 0) { + toi += -starti; + starti = 0; + } + + if (toi > max) { + starti -= toi - max; + toi = max; + + if (starti < 0) { + starti = 0; + } + } + + //int toi = max > max_visible_links ? max_visible_links : max; + + HTMLBuilder b; + + b.ul()->cls("pagination"); + + if (max != 0 && current_index != 0) { + b.li(); + b.a()->href(s + std::to_string(current_index - 1)); + b.w("previous"); + b.ca(); + b.cli(); + } else { + b.li()->cls("disabled"); + b.w("previous"); + b.cli(); + } + + if (starti != toi) { + for (uint32_t i = starti; i < toi; ++i) { + if (i != current_index) { + b.li(); + b.a()->href(s + std::to_string(i + 1)); + b.w(std::to_string(i + 1)); + b.ca(); + b.cli(); + } else { + b.li()->cls("disabled"); + b.w(std::to_string(i + 1)); + b.cli(); + } + } + } else { + b.li()->cls("disabled"); + b.w(std::to_string(1)); + b.cli(); + } + + if (max != 0 && current_index < max - 1) { + b.li(); + b.a()->href(s + std::to_string(current_index + 2)); + b.w("next"); + b.ca(); + b.cli(); + } else { + b.li()->cls("disabled"); + b.w("next"); + b.cli(); + } + + b.cul(); + + return b.result; +} + +std::string Utils::get_pagination_links(const std::string base_url, const std::vector &links, const uint32_t current_index, const uint32_t max_visible_links) { + std::string s = base_url; + if (s.size() > 0 && s[s.size() - 1] != '/') { + s += '/'; + } + + uint32_t max = links.size(); + + int starti = current_index - max_visible_links / 2; + int toi = current_index + max_visible_links / 2; + + if (starti < 0) { + toi += -starti; + starti = 0; + } + + if (toi > max) { + starti -= toi - max; + toi = max; + + if (starti < 0) { + starti = 0; + } + } + + //int toi = max > max_visible_links ? max_visible_links : max; + + HTMLBuilder b; + + b.ul()->cls("pagination"); + + if (max != 0 && current_index != 0) { + b.li(); + b.a()->href(s + links[current_index - 1]); + b.w("previous"); + b.ca(); + b.cli(); + } else { + b.li()->cls("disabled"); + b.w("previous"); + b.cli(); + } + + if (starti != toi) { + for (uint32_t i = starti; i < toi; ++i) { + if (i != current_index) { + b.li(); + b.a()->href(s + links[i]); + b.w(std::to_string(i + 1)); + b.ca(); + b.cli(); + } else { + b.li()->cls("disabled"); + b.w(std::to_string(i + 1)); + b.cli(); + } + } + } else { + b.li()->cls("disabled"); + b.w(std::to_string(1)); + b.cli(); + } + + if (max != 0 && current_index < max - 1) { + b.li(); + b.a()->href(s + links[current_index + 1]); + b.w("next"); + b.ca(); + b.cli(); + } else { + b.li()->cls("disabled"); + b.w("next"); + b.cli(); + } + + b.cul(); + + return b.result; +} + void Utils::str_replace(std::string *str, const std::string &from, const std::string &to) { if (from.empty()) return; diff --git a/core/utils.h b/core/utils.h index bcba648..2971ec2 100644 --- a/core/utils.h +++ b/core/utils.h @@ -2,17 +2,21 @@ #define UTILS_H #include +#include class Utils { - public: - static void newline_to_br(std::string *str); - //htmlspecialchars +public: + static void newline_to_br(std::string *str); + //htmlspecialchars - static void markdown_to_html(std::string *str); + static void markdown_to_html(std::string *str); + + static std::string get_pagination(const std::string base_url, const uint32_t max, const uint32_t current_index, const uint32_t max_visible_links = 10); + static std::string get_pagination_links(const std::string base_url, const std::vector &links, const uint32_t current_index, const uint32_t max_visible_links = 10); + + static void str_replace(std::string *str, const std::string &from, const std::string &to); - static void str_replace(std::string *str, const std::string& from, const std::string& to); protected: }; - #endif \ No newline at end of file