From e76328d9bb6d8798ce45810909f09a4e1c476fb3 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Thu, 23 Apr 2020 19:43:56 +0200 Subject: weblog: text formatting (WIP) --- plugins/weblog/weblog.cpp | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'plugins/weblog') diff --git a/plugins/weblog/weblog.cpp b/plugins/weblog/weblog.cpp index 38d6dec..b24573f 100644 --- a/plugins/weblog/weblog.cpp +++ b/plugins/weblog/weblog.cpp @@ -1,6 +1,7 @@ #include "weblog.h" #include +#include #include #include @@ -171,17 +172,23 @@ namespace { return {result.begin(), result.begin() + size}; } - // returns plain text of string (xml elements removed) + // returns plain text of string (html xml elements removed) std::string plainText(const std::string& text) { pt::ptree tree; std::istringstream ss{text}; - //pt::read_xml(ss, tree, pt::xml_parser::no_comments | pt::xml_parser::trim_whitespace); + pt::read_xml(ss, tree, pt::xml_parser::no_comments | pt::xml_parser::trim_whitespace); - //std::cout << "DEBUG: " << tree.get("file") << std::endl; + return tree.get("file"); + } + + std::string verbatimText(std::string text) + { + boost::algorithm::replace_all(text, "<", "<"); + boost::algorithm::replace_all(text, ">", ">"); - return text; + return "
"s + text + "
"; } // returns teaser of article in plain text @@ -194,7 +201,13 @@ namespace { article = "" + article.substr(pos0 + 2) + ""; - article = plainText(article); + auto metaData{getMetaData(path)}; + auto it {metaData.find("Content-Type")}; + + // convert html to plaintext, if tagged as such + // default: text/html + if (it == metaData.end() || it->second == "text/html") + article = plainText(article); size_t pos1 {article.find(".")}; @@ -244,7 +257,13 @@ namespace { if (pos == data.npos) throw std::runtime_error("Error parsing article"); - std::string result { "

"s + metaData.at("Subject") + "

"s + metaData.at("Date") + "

"s + data.substr(pos + 2) + ""s}; + data = data.substr(pos + 2); + + auto it {metaData.find("Content-Type")}; + if (it != metaData.end() && it->second == "text/plain") + data = verbatimText(data); + + std::string result { "

"s + metaData.at("Subject") + "

"s + metaData.at("Date") + "

"s + data + ""s}; return result; } catch (const std::exception& ex) { -- cgit v1.2.3