diff --git a/include/uri.hpp b/include/uri.hpp index 6864345..90cb18b 100644 --- a/include/uri.hpp +++ b/include/uri.hpp @@ -17,10 +17,12 @@ #ifndef REMWHAREAD_URI_HPP #define REMWHAREAD_URI_HPP +#include #include namespace remwharead { +using std::uint16_t; using std::string; /*! @@ -162,6 +164,13 @@ protected: * @since 0.8.5 */ void set_proxy(); + + /*! + * @brief Limits text to N characters, cuts at space. + * + * @since 0.8.5 + */ + string cut_text(const string &text, const uint16_t n_chars) const; }; } // namespace remwharead diff --git a/src/lib/uri.cpp b/src/lib/uri.cpp index 02db23b..bb2cf18 100644 --- a/src/lib/uri.cpp +++ b/src/lib/uri.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -249,7 +250,7 @@ string URI::extract_description(const string &html) re_desc.split(html, matches); if (matches.size() >= 2) { - return remove_newlines(unescape_html(matches[1])); + return remove_newlines(cut_text(unescape_html(matches[1]), 500)); } } @@ -656,4 +657,20 @@ string URI::remove_newlines(string text) return text; } + +string URI::cut_text(const string &text, const uint16_t n_chars) const +{ + if (text.size() > n_chars) + { + constexpr char suffix[] = " […]"; + constexpr auto suffix_len = std::end(suffix) - std::begin(suffix) - 1; + + const size_t pos = text.rfind(' ', n_chars - suffix_len); + + return text.substr(0, pos) + suffix; + } + + return text; +} + } // namespace remwharead