plaintorich: add option to turn links found inside text into HTML anchors

This commit is contained in:
Jean-Francois Dockes 2018-04-16 10:22:22 +02:00
parent 30e598ab41
commit 2be261e00b
2 changed files with 25 additions and 8 deletions

View File

@ -23,6 +23,7 @@
#include <vector> #include <vector>
#include <map> #include <map>
#include <algorithm> #include <algorithm>
#include <regex>
using std::vector; using std::vector;
using std::list; using std::list;
@ -149,6 +150,16 @@ bool TextSplitPTR::matchGroups()
return true; 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, "<a href=\"$1\">$1</a>");
}
// Fix result text for display inside the gui text window. // Fix result text for display inside the gui text window.
// //
@ -186,7 +197,7 @@ bool PlainToRich::plaintorich(const string& in,
// Rich text output // Rich text output
*olit = header(); *olit = header();
// No term matches. Happens, for example on a snippet selected for // No term matches. Happens, for example on a snippet selected for
// a term match when we are actually looking for a group match // a term match when we are actually looking for a group match
// (the snippet generator does this...). // (the snippet generator does this...).
@ -288,6 +299,9 @@ bool PlainToRich::plaintorich(const string& in,
// chunks cut in the middle of <a></a> for example). // chunks cut in the middle of <a></a> for example).
if (!m_inputhtml && !inrcltag && if (!m_inputhtml && !inrcltag &&
olit->size() > (unsigned int)chunksize) { olit->size() > (unsigned int)chunksize) {
if (m_activatelinks) {
*olit = activate_urls(*olit);
}
out.push_back(string(startChunk())); out.push_back(string(startChunk()));
olit++; olit++;
} }
@ -365,5 +379,8 @@ bool PlainToRich::plaintorich(const string& in,
} }
#endif #endif
LOGDEB2("plaintorich: done " << chron.millis() << " mS\n"); LOGDEB2("plaintorich: done " << chron.millis() << " mS\n");
if (!m_inputhtml && m_activatelinks) {
out.back() = activate_urls(out.back());
}
return ret; return ret;
} }

View File

@ -31,15 +31,14 @@
*/ */
class PlainToRich { class PlainToRich {
public: public:
PlainToRich()
: m_inputhtml(false), m_eolbr(false), m_hdata(0) {
}
virtual ~PlainToRich() {} virtual ~PlainToRich() {}
void set_inputhtml(bool v) { void set_inputhtml(bool v) {
m_inputhtml = v; m_inputhtml = v;
} }
void set_activatelinks(bool v) {
m_activatelinks = v;
}
/** /**
* Transform plain text for highlighting search terms, ie in the * Transform plain text for highlighting search terms, ie in the
@ -89,10 +88,11 @@ public:
} }
protected: protected:
bool m_inputhtml; bool m_inputhtml{false};
// Use <br> to break plain text lines (else caller has used a <pre> tag) // Use <br> to break plain text lines (else caller has used a <pre> tag)
bool m_eolbr; bool m_eolbr{false};
const HighlightData *m_hdata; const HighlightData *m_hdata{0};
bool m_activatelinks{false};
}; };
#endif /* _PLAINTORICH_H_INCLUDED_ */ #endif /* _PLAINTORICH_H_INCLUDED_ */