diff --git a/src/query/plaintorich.cpp b/src/query/plaintorich.cpp index 7076d344..f612f033 100644 --- a/src/query/plaintorich.cpp +++ b/src/query/plaintorich.cpp @@ -23,6 +23,7 @@ #include #include #include +#include using std::vector; using std::list; @@ -149,6 +150,16 @@ bool TextSplitPTR::matchGroups() return true; } +// Replace HTTP(s) urls in text/plain with proper HTML anchors so that +// they become clickable in the preview. We don't make a lot of effort +// for validating, or catching things which are probably urls but miss +// a scheme (e.g. www.xxx.com/index.html), because complicated. +static const string urlRE = "(https?://[[:alnum:]~_/.%?&=,#@]+)[[:space:]|]"; +static std::regex url_re(urlRE); +static string activate_urls(const string& in) +{ + return std::regex_replace(in, url_re, "$1"); +} // Fix result text for display inside the gui text window. // @@ -186,7 +197,7 @@ bool PlainToRich::plaintorich(const string& in, // Rich text output *olit = header(); - + // No term matches. Happens, for example on a snippet selected for // a term match when we are actually looking for a group match // (the snippet generator does this...). @@ -288,6 +299,9 @@ bool PlainToRich::plaintorich(const string& in, // chunks cut in the middle of for example). if (!m_inputhtml && !inrcltag && olit->size() > (unsigned int)chunksize) { + if (m_activatelinks) { + *olit = activate_urls(*olit); + } out.push_back(string(startChunk())); olit++; } @@ -365,5 +379,8 @@ bool PlainToRich::plaintorich(const string& in, } #endif LOGDEB2("plaintorich: done " << chron.millis() << " mS\n"); + if (!m_inputhtml && m_activatelinks) { + out.back() = activate_urls(out.back()); + } return ret; } diff --git a/src/query/plaintorich.h b/src/query/plaintorich.h index 67a0c42a..e2d9f430 100644 --- a/src/query/plaintorich.h +++ b/src/query/plaintorich.h @@ -31,15 +31,14 @@ */ class PlainToRich { public: - PlainToRich() - : m_inputhtml(false), m_eolbr(false), m_hdata(0) { - } - virtual ~PlainToRich() {} void set_inputhtml(bool v) { m_inputhtml = v; } + void set_activatelinks(bool v) { + m_activatelinks = v; + } /** * Transform plain text for highlighting search terms, ie in the @@ -89,10 +88,11 @@ public: } protected: - bool m_inputhtml; + bool m_inputhtml{false}; // Use
to break plain text lines (else caller has used a
 tag)
-    bool m_eolbr; 
-    const HighlightData *m_hdata;
+    bool m_eolbr{false}; 
+    const HighlightData *m_hdata{0};
+    bool m_activatelinks{false};
 };
 
 #endif /* _PLAINTORICH_H_INCLUDED_ */