diff options
author | Roland Reichwein <mail@reichwein.it> | 2020-04-23 19:43:56 +0200 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2020-04-23 19:43:56 +0200 |
commit | e76328d9bb6d8798ce45810909f09a4e1c476fb3 (patch) | |
tree | 1c8816f5872a38bcef06e3ecaea426285c86ed4f /plugins/weblog/weblog.cpp | |
parent | 1560d0efec6876ef8e820c5ec72bea6098f99870 (diff) |
weblog: text formatting (WIP)
Diffstat (limited to 'plugins/weblog/weblog.cpp')
-rw-r--r-- | plugins/weblog/weblog.cpp | 31 |
1 files changed, 25 insertions, 6 deletions
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 <boost/algorithm/string/predicate.hpp> +#include <boost/algorithm/string/replace.hpp> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_parser.hpp> @@ -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<std::string>("file") << std::endl; + return tree.get<std::string>("file"); + } + + std::string verbatimText(std::string text) + { + boost::algorithm::replace_all(text, "<", "<"); + boost::algorithm::replace_all(text, ">", ">"); - return text; + return "<pre>"s + text + "</pre>"; } // returns teaser of article in plain text @@ -194,7 +201,13 @@ namespace { article = "<file>" + article.substr(pos0 + 2) + "</file>"; - 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 { "<!DOCTYPE html><html><head><meta charset=\"utf-8\"/></head><body><h1>"s + metaData.at("Subject") + "</h1>"s + metaData.at("Date") + "<br/><br/>"s + data.substr(pos + 2) + "</body></html>"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 { "<!DOCTYPE html><html><head><meta charset=\"utf-8\"/></head><body><h1>"s + metaData.at("Subject") + "</h1>"s + metaData.at("Date") + "<br/><br/>"s + data + "</body></html>"s}; return result; } catch (const std::exception& ex) { |