diff --git a/src/document.cpp b/src/document.cpp index 2dc3284..90258d1 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -21,9 +21,12 @@ #include #include #include +#include #include #include +#include +#include #include #include #include @@ -33,8 +36,11 @@ namespace mastorss { using boost::regex; using boost::regex_replace; +using std::transform; +using std::ifstream; using std::list; using std::istringstream; +using std::stringstream; using std::string; using std::move; @@ -175,7 +181,7 @@ void Document::parse_rss(const pt::ptree &tree) { desc = regex_replace(desc, regex{fix}, ""); } - return desc; + return add_hashtags(desc); }(); item.guid = move(guid); item.link = rssitem.get("link"); @@ -232,4 +238,43 @@ string Document::extract_location(const RestClient::HeaderFields &headers) const } return location; } + +string Document::add_hashtags(const string &text) +{ + Json::Value json; + ifstream file{_cfg.get_config_dir() /= "watchwords.json"}; + if (file.good()) + { + stringstream rawjson; + rawjson << file.rdbuf(); + rawjson >> json; + } + else + { + throw FileException{"File Not found:" + + (_cfg.get_config_dir() /= "watchwords.json").string()}; + } + + list watchwords; + const auto &tags_profile = json[_cfg.profile]["tags"]; + const auto &tags_global = json["global"]["tags"]; + transform(tags_profile.begin(), tags_profile.end(), + std::back_inserter(watchwords), + [](const Json::Value &value) { return value.asString(); }); + transform(tags_global.begin(), tags_global.end(), + std::back_inserter(watchwords), + [](const Json::Value &value) { return value.asString(); }); + + string out{text}; + for (const auto &tag : watchwords) + { + regex re_tag("([[:space:][:punct:]]|^)(" + + tag + ")([[:space:][:punct:]]|$)", regex::icase); + out = regex_replace(out, re_tag, "$1#$2$3", boost::format_first_only); + } + + return out; +} + + } // namespace mastorss diff --git a/src/document.hpp b/src/document.hpp index 85f4c35..3682e2c 100644 --- a/src/document.hpp +++ b/src/document.hpp @@ -77,6 +77,7 @@ private: string remove_html(string html) const; [[nodiscard]] string extract_location(const RestClient::HeaderFields &headers) const; + string add_hashtags(const string &text); }; } // namespace mastorss