Cut descriptions at 500 characters.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
2b9768a5a9
commit
d8102b017e
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue