#include <boost/shared_ptr.hpp>
#include <openssl/bio.h>
#include <openssl/evp.h>
+#include <pcrecpp.h>
#include <stringfunc.hxx>
return string::npos;
}
-// encoded UTF-8 chars into HTML entities
-string html_entities(std::string str)
+/**
+* @brief Encoded UTF-8 chars into HTML entities.
+*
+* @param[in,out] str the string that will have its special characters replaced
+* by HTML entities.
+* @param expect_amp When true, expect to find ampersand characters and HTML
+* entities, replace any additional special characters.
+* @return std::string same as str.
+*/
+string html_entities(std::string str, bool expect_amp)
{
+ if (expect_amp)
+ {
+ const string amp = "&";
+ const pcrecpp::RE re_amp("^&((#([0-9]{3}|x[0-9a-fA-F]{2}))|[a-zA-Z]{2,6});");
+ string::size_type pos = 0;
+ while ((pos = str.find("&", pos)) != string::npos)
+ {
+ if (!re_amp.PartialMatch(str.substr(pos)))
+ str.replace(pos, 1, amp);
+
+ pos++;
+ }
+ }
+ else
+ {
+ replace_all (str, "&", "&");
+ }
+
// Normal chars
- replace_all (str, "&", "&");
replace_all (str, "<", "<");
replace_all (str, ">", ">");
replace_all (str, "\"", """);
const std::string &input);
std::string strip_html_tags(const std::string &input);
std::string smart_html_entities(const std::string &input);
-std::string html_entities(std::string str);
+std::string html_entities(std::string str, bool expect_amp=false);
std::string html_entities_to_console(std::string str);
typedef std::pair<std::string::size_type, std::string::size_type> CommentZone;