Cut descriptions at 500 characters.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
tastytea 2019-10-15 15:10:39 +02:00
parent 2b9768a5a9
commit d8102b017e
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 27 additions and 1 deletions

View File

@ -17,10 +17,12 @@
#ifndef REMWHAREAD_URI_HPP
#define REMWHAREAD_URI_HPP
#include <cstdint>
#include <string>
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

View File

@ -29,6 +29,7 @@
#include <cstdint>
#include <exception>
#include <iostream>
#include <iterator>
#include <locale>
#include <sstream>
#include <utility>
@ -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