From 6fd72c2415dd52977fef53db575cf62a85153cdd Mon Sep 17 00:00:00 2001 From: George Hazan Date: Fri, 29 Mar 2024 14:26:31 +0300 Subject: better URL autodetect --- plugins/NewStory/src/history_array.cpp | 2 +- plugins/NewStory/src/templates.cpp | 2 +- plugins/NewStory/src/utils.cpp | 27 ++++++++++++++++++++------- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/plugins/NewStory/src/history_array.cpp b/plugins/NewStory/src/history_array.cpp index a0e2999cc2..04c1b44d64 100644 --- a/plugins/NewStory/src/history_array.cpp +++ b/plugins/NewStory/src/history_array.cpp @@ -394,7 +394,7 @@ void ItemData::load(bool bLoadAlways) auto *pwszLocalName = blob.getLocalName(); if (ProtoGetAvatarFileFormat(pwszLocalName) != PA_FORMAT_UNKNOWN) - buf.AppendFormat(L"[img]%s[/img]", pwszLocalName); + buf.AppendFormat(L"[img]file://%s[/img]", pwszLocalName); else buf.AppendFormat(L"[url]file://%s[/url]", pwszLocalName); } diff --git a/plugins/NewStory/src/templates.cpp b/plugins/NewStory/src/templates.cpp index ef7bba6b9f..405a198fe6 100644 --- a/plugins/NewStory/src/templates.cpp +++ b/plugins/NewStory/src/templates.cpp @@ -118,7 +118,7 @@ static void AppendString(CMStringW &buf, const wchar_t *p) if (auto *p2 = wcsstr(p, L"[/img]")) { CMStringW wszUrl(p, int(p2 - p)); - buf.AppendFormat(L"
", wszUrl.c_str()); + buf.AppendFormat(L"
", wszUrl.c_str()); p = p2 + 5; } } diff --git a/plugins/NewStory/src/utils.cpp b/plugins/NewStory/src/utils.cpp index f227578fd4..bd12aea485 100644 --- a/plugins/NewStory/src/utils.cpp +++ b/plugins/NewStory/src/utils.cpp @@ -145,12 +145,25 @@ static int DetectUrl(const wchar_t *text) void UrlAutodetect(CMStringW &str) { - for (auto *p = str.c_str(); *p; p++) - if (int len = DetectUrl(p)) { - int pos = p - str.c_str(); - CMStringW url = str.Mid(pos, len); - str.Insert(pos + len, L"[/url]"); - str.Insert(pos, L"[url]"); - p = str.c_str() + pos + len + 11; + int level = 0; + + for (auto *p = str.c_str(); *p; p++) { + if (!wcsncmp(p, L"[img]", 5) || !wcsncmp(p, L"[url]", 5)) { + p += 4; + level++; + } + if (!wcsncmp(p, L"[/img]", 6) || !wcsncmp(p, L"[/img]", 6)) { + p += 5; + level--; } + else if (int len = DetectUrl(p)) { + if (level == 0) { + int pos = p - str.c_str(); + CMStringW url = str.Mid(pos, len); + str.Insert(pos + len, L"[/url]"); + str.Insert(pos, L"[url]"); + p = str.c_str() + pos + len + 11; + } + } + } } -- cgit v1.2.3