summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--plugins/NewStory/src/templates.cpp4
-rw-r--r--plugins/NewStory/src/utils.cpp41
-rw-r--r--plugins/NewStory/src/utils.h1
3 files changed, 46 insertions, 0 deletions
diff --git a/plugins/NewStory/src/templates.cpp b/plugins/NewStory/src/templates.cpp
index a5fe44bd72..e24574c880 100644
--- a/plugins/NewStory/src/templates.cpp
+++ b/plugins/NewStory/src/templates.cpp
@@ -142,6 +142,10 @@ CMStringA ItemData::formatHtml(const wchar_t *pwszStr)
AppendString(str, T2Utf((pwszStr) ? pwszStr : formatString()));
str.Append("</body></html>");
+
+ // url autodetect
+ UrlAutodetect(str);
+
// Netlib_Logf(0, str);
return str;
}
diff --git a/plugins/NewStory/src/utils.cpp b/plugins/NewStory/src/utils.cpp
index 69057fcdd1..f24cd3bd4c 100644
--- a/plugins/NewStory/src/utils.cpp
+++ b/plugins/NewStory/src/utils.cpp
@@ -115,3 +115,44 @@ void RemoveBbcodes(CMStringW &wszText)
idx++;
}
}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+
+static int countNoWhitespace(const char *str)
+{
+ int c;
+ for (c = 0; *str != '\n' && *str != '\r' && *str != '\t' && *str != ' ' && *str != '\0'; str++, c++);
+ return c;
+}
+
+static int DetectUrl(const char *text)
+{
+ int i;
+ for (i = 0; text[i] != '\0'; i++)
+ if (!((text[i] >= '0' && text[i] <= '9') || isalpha(text[i])))
+ break;
+
+ if (i <= 0 || memcmp(text + i, "://", 3))
+ return 0;
+
+ i += countNoWhitespace(text + i);
+ for (; i > 0; i--)
+ if ((text[i - 1] >= '0' && text[i - 1] <= '9') || isalpha(text[i - 1]) || text[i - 1] == '/')
+ break;
+
+ return i;
+}
+
+void UrlAutodetect(CMStringA &str)
+{
+ for (auto *p = str.c_str(); *p; p++)
+ if (int len = DetectUrl(p)) {
+ int pos = p - str.c_str();
+ CMStringA url = str.Mid(pos, len);
+ str.Delete(pos, len);
+
+ CMStringA newText(FORMAT, "<a href =\"%s\">%s</a>", url.c_str(), url.c_str());
+ str.Insert(pos, newText);
+ p = str.c_str() + pos + newText.GetLength();
+ }
+}
diff --git a/plugins/NewStory/src/utils.h b/plugins/NewStory/src/utils.h
index 7dfa5f7d24..35da06c39a 100644
--- a/plugins/NewStory/src/utils.h
+++ b/plugins/NewStory/src/utils.h
@@ -9,4 +9,5 @@ bool NSMenu_Process(int iCommand, NewstoryListData *data);
int GetFontHeight(const LOGFONTA &lf);
+void UrlAutodetect(CMStringA &str);
void RemoveBbcodes(CMStringW &pwszText);