2020-12-26 17:49:59 +01:00
|
|
|
#include "utils.h"
|
|
|
|
|
2021-05-27 16:57:38 +02:00
|
|
|
#include "html/html_builder.h"
|
2020-12-27 03:35:41 +01:00
|
|
|
#include <maddy/parser.h>
|
2020-12-28 23:13:54 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2020-12-27 03:35:41 +01:00
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
void Utils::newline_to_br(String *str) {
|
|
|
|
str->replace("\r\n", "<br>");
|
|
|
|
str->replace("\n", "<br>");
|
2020-12-26 17:49:59 +01:00
|
|
|
}
|
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
void Utils::markdown_to_html(String *str) {
|
2020-12-27 03:35:41 +01:00
|
|
|
std::shared_ptr<maddy::ParserConfig> config = std::make_shared<maddy::ParserConfig>();
|
2021-01-05 19:27:53 +01:00
|
|
|
config->isEmphasizedParserEnabled = false;
|
2020-12-27 03:35:41 +01:00
|
|
|
config->isHTMLWrappedInParagraph = true;
|
|
|
|
|
|
|
|
std::shared_ptr<maddy::Parser> parser = std::make_shared<maddy::Parser>(config);
|
|
|
|
|
|
|
|
std::stringstream ss((*str));
|
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
String htmlOutput = parser->Parse(ss);
|
2020-12-27 03:35:41 +01:00
|
|
|
|
|
|
|
(*str) = htmlOutput;
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
String Utils::get_pagination(const String base_url, const uint32_t max, const uint32_t current_index, const uint32_t max_visible_links) {
|
|
|
|
String s = base_url;
|
2020-12-28 23:13:54 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
String Utils::get_pagination_links(const String base_url, const Vector<String> &links, const uint32_t current_index, const uint32_t max_visible_links) {
|
|
|
|
String s = base_url;
|
2020-12-28 23:13:54 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|