Formatting
This commit is contained in:
parent
360a85b7bf
commit
c09929d2f7
43
src/Page.cpp
43
src/Page.cpp
@ -10,13 +10,54 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <format>
|
||||
#include <algorithm>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
|
11
src/ansi.hpp
Normal file
11
src/ansi.hpp
Normal file
@ -0,0 +1,11 @@
|
||||
|
||||
namespace ansi
|
||||
{
|
||||
|
||||
#include <string>
|
||||
|
||||
constexpr std::string_view BOLD = "\033[1m";
|
||||
constexpr std::string_view ITALIC = "\033[3m";
|
||||
constexpr std::string_view CLEAR = "\033[0m";
|
||||
|
||||
} // namespace ansi
|
Loading…
x
Reference in New Issue
Block a user