From cae907edff0c068b7a0fc60c9a2cab6015ae5c74 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Wed, 20 Mar 2024 18:09:36 +0300 Subject: url autodetect --- plugins/NewStory/src/templates.cpp | 4 ++++ plugins/NewStory/src/utils.cpp | 41 ++++++++++++++++++++++++++++++++++++++ plugins/NewStory/src/utils.h | 1 + 3 files changed, 46 insertions(+) (limited to 'plugins') 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(""); + + // 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, "%s", 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); -- cgit v1.2.3