add gui.escape_markup
This commit is contained in:
parent
16698cc68b
commit
467b4ad043
@ -50,3 +50,14 @@ gui.clear_markup(
|
|||||||
```
|
```
|
||||||
|
|
||||||
Removes markup from text.
|
Removes markup from text.
|
||||||
|
|
||||||
|
```lua
|
||||||
|
gui.escape_markup(
|
||||||
|
-- markup language ("md" - Markdown)
|
||||||
|
language: str,
|
||||||
|
-- text with markup
|
||||||
|
text: str
|
||||||
|
) -> str
|
||||||
|
```
|
||||||
|
|
||||||
|
Escapes markup in text.
|
||||||
|
|||||||
@ -47,3 +47,14 @@ gui.clear_markup(
|
|||||||
```
|
```
|
||||||
|
|
||||||
Удаляет разметку из текста.
|
Удаляет разметку из текста.
|
||||||
|
|
||||||
|
```lua
|
||||||
|
gui.escape_markup(
|
||||||
|
-- язык разметки ("md" - Markdown)
|
||||||
|
language: str,
|
||||||
|
-- текст с разметкой
|
||||||
|
text: str
|
||||||
|
) -> str
|
||||||
|
```
|
||||||
|
|
||||||
|
Экранирует разметку в тексте.
|
||||||
|
|||||||
@ -1,7 +1,5 @@
|
|||||||
#include "markdown.hpp"
|
#include "markdown.hpp"
|
||||||
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
#include "graphics/core/Font.hpp"
|
#include "graphics/core/Font.hpp"
|
||||||
|
|
||||||
using namespace markdown;
|
using namespace markdown;
|
||||||
@ -66,6 +64,7 @@ Result<CharT> process_markdown(
|
|||||||
pos++;
|
pos++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
pos--;
|
||||||
}
|
}
|
||||||
} else if (first == '*') {
|
} else if (first == '*') {
|
||||||
if (pos + 1 < source.size() && source[pos+1] == '*') {
|
if (pos + 1 < source.size() && source[pos+1] == '*') {
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
struct FontStylesScheme;
|
struct FontStylesScheme;
|
||||||
|
|
||||||
@ -19,4 +20,24 @@ namespace markdown {
|
|||||||
|
|
||||||
Result<char> process(std::string_view source, bool eraseMarkdown);
|
Result<char> process(std::string_view source, bool eraseMarkdown);
|
||||||
Result<wchar_t> process(std::wstring_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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -738,6 +738,15 @@ static int l_gui_clear_markup(lua::State* L) {
|
|||||||
return lua::pushstring(L, text);
|
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[] = {
|
const luaL_Reg guilib[] = {
|
||||||
{"get_viewport", lua::wrap<l_gui_getviewport>},
|
{"get_viewport", lua::wrap<l_gui_getviewport>},
|
||||||
{"getattr", lua::wrap<l_gui_getattr>},
|
{"getattr", lua::wrap<l_gui_getattr>},
|
||||||
@ -746,5 +755,7 @@ const luaL_Reg guilib[] = {
|
|||||||
{"str", lua::wrap<l_gui_str>},
|
{"str", lua::wrap<l_gui_str>},
|
||||||
{"get_locales_info", lua::wrap<l_gui_get_locales_info>},
|
{"get_locales_info", lua::wrap<l_gui_get_locales_info>},
|
||||||
{"clear_markup", lua::wrap<l_gui_clear_markup>},
|
{"clear_markup", lua::wrap<l_gui_clear_markup>},
|
||||||
|
{"escape_markup", lua::wrap<l_gui_escape_markup>},
|
||||||
{"__reindex", lua::wrap<l_gui_reindex>},
|
{"__reindex", lua::wrap<l_gui_reindex>},
|
||||||
{NULL, NULL}};
|
{NULL, NULL}
|
||||||
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user