add gui.escape_markup

This commit is contained in:
MihailRis 2024-12-06 20:25:21 +03:00
parent 16698cc68b
commit 467b4ad043
5 changed files with 56 additions and 3 deletions

View File

@ -50,3 +50,14 @@ gui.clear_markup(
```
Removes markup from text.
```lua
gui.escape_markup(
-- markup language ("md" - Markdown)
language: str,
-- text with markup
text: str
) -> str
```
Escapes markup in text.

View File

@ -47,3 +47,14 @@ gui.clear_markup(
```
Удаляет разметку из текста.
```lua
gui.escape_markup(
-- язык разметки ("md" - Markdown)
language: str,
-- текст с разметкой
text: str
) -> str
```
Экранирует разметку в тексте.

View File

@ -1,7 +1,5 @@
#include "markdown.hpp"
#include <sstream>
#include "graphics/core/Font.hpp"
using namespace markdown;
@ -66,6 +64,7 @@ Result<CharT> process_markdown(
pos++;
continue;
}
pos--;
}
} else if (first == '*') {
if (pos + 1 < source.size() && source[pos+1] == '*') {

View File

@ -2,6 +2,7 @@
#include <string>
#include <memory>
#include <sstream>
struct FontStylesScheme;
@ -19,4 +20,24 @@ namespace markdown {
Result<char> process(std::string_view source, bool eraseMarkdown);
Result<wchar_t> process(std::wstring_view source, bool eraseMarkdown);
template <typename CharT>
inline std::basic_string<CharT> escape(std::string_view source) {
std::basic_stringstream<CharT> ss;
int pos = 0;
while (pos < source.size()) {
CharT first = source[pos];
if (first == '\\' && pos + 1 < source.size()) {
CharT second = source[++pos];
ss << first << second;
pos++;
continue;
} else if (first == '*' || first == '~' || first == '_') {
ss << '\\';
}
ss << first;
pos++;
}
return ss.str();
}
}

View File

@ -738,6 +738,15 @@ static int l_gui_clear_markup(lua::State* L) {
return lua::pushstring(L, text);
}
static int l_gui_escape_markup(lua::State* L) {
auto lang = lua::require_string(L, 1);
std::string text = lua::require_string(L, 2);
if (std::strcmp(lang, "md") == 0) {
text = std::move(markdown::escape<char>(text));
}
return lua::pushstring(L, text);
}
const luaL_Reg guilib[] = {
{"get_viewport", lua::wrap<l_gui_getviewport>},
{"getattr", lua::wrap<l_gui_getattr>},
@ -746,5 +755,7 @@ const luaL_Reg guilib[] = {
{"str", lua::wrap<l_gui_str>},
{"get_locales_info", lua::wrap<l_gui_get_locales_info>},
{"clear_markup", lua::wrap<l_gui_clear_markup>},
{"escape_markup", lua::wrap<l_gui_escape_markup>},
{"__reindex", lua::wrap<l_gui_reindex>},
{NULL, NULL}};
{NULL, NULL}
};