From e6ee5ecf3da7b083322f1f28c20d51841285e1d2 Mon Sep 17 00:00:00 2001 From: Blu3train Date: Mon, 18 Oct 2021 22:57:36 +0200 Subject: [PATCH] Regex Replacer Extension --- deploy.ps1 | 1 + extensions/CMakeLists.txt | 2 ++ extensions/regexreplacer.cpp | 53 ++++++++++++++++++++++++++++++++++++ text.cpp | 12 ++++++++ 4 files changed, 68 insertions(+) create mode 100644 extensions/regexreplacer.cpp diff --git a/deploy.ps1 b/deploy.ps1 index 58403b8..ac13101 100644 --- a/deploy.ps1 +++ b/deploy.ps1 @@ -52,6 +52,7 @@ foreach ($language in @{ "Google Translate", "Lua", "Regex Filter", + "Regex Replacer", "Remove Repeated Characters", "Remove Repeated Phrases", "Remove Repeated Phrases 2", diff --git a/extensions/CMakeLists.txt b/extensions/CMakeLists.txt index 54a9c75..6e2a618 100644 --- a/extensions/CMakeLists.txt +++ b/extensions/CMakeLists.txt @@ -21,6 +21,7 @@ add_library(Remove\ 30\ Repeated\ Sentences MODULE removerepeatsentence.cpp exte add_library(Replacer MODULE replacer.cpp extensionimpl.cpp) add_library(Styler MODULE styler.cpp extensionimpl.cpp) add_library(Thread\ Linker MODULE threadlinker.cpp extensionimpl.cpp) +add_library(Regex\ Replacer MODULE regexreplacer.cpp extensionimpl.cpp) target_precompile_headers(Bing\ Translate REUSE_FROM pch) target_precompile_headers(Copy\ to\ Clipboard REUSE_FROM pch) @@ -40,6 +41,7 @@ target_precompile_headers(Remove\ 30\ Repeated\ Sentences REUSE_FROM pch) target_precompile_headers(Replacer REUSE_FROM pch) target_precompile_headers(Styler REUSE_FROM pch) target_precompile_headers(Thread\ Linker REUSE_FROM pch) +target_precompile_headers(Regex\ Replacer REUSE_FROM pch) target_link_libraries(Bing\ Translate winhttp Qt5::Widgets) target_link_libraries(DeepL\ Translate winhttp Qt5::Widgets) diff --git a/extensions/regexreplacer.cpp b/extensions/regexreplacer.cpp new file mode 100644 index 0000000..6c95e84 --- /dev/null +++ b/extensions/regexreplacer.cpp @@ -0,0 +1,53 @@ +#include "extension.h" +#include "module.h" +#include "blockmarkup.h" +#include + +extern const wchar_t* REGEX_REPLACER_INSTRUCTIONS; + +const char* REGEX_REPLACEMENTS_SAVE_FILE = "SavedRegexReplacements.txt"; + +std::optional regex; +concurrency::reader_writer_lock m; + +BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) +{ + switch (ul_reason_for_call) + { + case DLL_PROCESS_ATTACH: + { + if (!std::ifstream(REGEX_REPLACEMENTS_SAVE_FILE).good()) + { + auto file = std::ofstream(REGEX_REPLACEMENTS_SAVE_FILE, std::ios::binary) << "\xff\xfe"; + for (auto ch : std::wstring_view(REGEX_REPLACER_INSTRUCTIONS)) + file << (ch == L'\n' ? std::string_view("\r\0\n", 4) : std::string_view((char*)&ch, 2)); + _spawnlp(_P_DETACH, "notepad", "notepad", REGEX_REPLACEMENTS_SAVE_FILE, NULL); // show file to user + } + } + break; + case DLL_PROCESS_DETACH: + { + } + break; + } + return TRUE; +} + +bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) +{ + //if (!sentenceInfo["current select"] || sentenceInfo["text number"] == 0 || sentence == L"") return false; + if (!sentenceInfo["current select"] || sentenceInfo["text number"] == 0) return false; + + std::ifstream stream(REGEX_REPLACEMENTS_SAVE_FILE, std::ios::binary); + BlockMarkupIterator savedFilters(stream, Array{ L"|REGEX|", L"|BECOMES|" }); + //std::vector regexes; + concurrency::reader_writer_lock::scoped_lock_read readLock(m); + while (auto read = savedFilters.Next()) { + const auto& [regex, replacement] = read.value(); + if (regex == L"") continue; + try { ::regex = regex; } + catch (std::regex_error) { continue; } + sentence = std::regex_replace(sentence, ::regex.value(), replacement); + } + return true; +} diff --git a/text.cpp b/text.cpp index 07d9d9f..a4f21b9 100644 --- a/text.cpp +++ b/text.cpp @@ -224,6 +224,12 @@ All text in this file outside of a replacement command is ignored. A caret (^) acts as a wildcard that matches any other single character. Whitespace in original_text is ignored, but replacement_text can contain spaces, newlines, etc. This file must be encoded in Unicode (UTF-16 Little Endian).)"; +const wchar_t* REGEX_REPLACER_INSTRUCTIONS = LR"(This file only does anything when the "Regex Replacer" extension is used. +Replacement commands must be formatted like this: +|REGEX|regular_expression|BECOMES|replacement_text|END| +All text in this file outside of a replacement command is ignored. +This file must be encoded in Unicode (UTF-16 Little Endian). +Learn, build, & test Regular Expressions: https://regexr.com/)"; const char* THREAD_LINKER = u8"Thread Linker"; const char* LINK = u8"Link"; const char* UNLINK = u8"Unlink"; @@ -931,6 +937,12 @@ I comandi di rimpiazzo devono essere formattati cosi: Tutto il testo in questo file all'infuori di un comando di rimpiazzo è ignorato. La spaziatura nel testo_originale è ignorato, ma testo_sostituito può contenere spaziature, ritorni a capo, ecc. Questo file deve essere codificato in Unicode (UTF-16 Little Endian).)"; + REGEX_REPLACER_INSTRUCTIONS = LR"(Questo file fa qualcosa solo quando l'estenzione "Regex Replacer" è utilizzata. +I comandi di sostituzione devono essere formattati cosi: +|ORIG|espressione_regolare|BECOMES|testo_sostituito|END| +Tutto il testo in questo file all'infuori di un comando di sostituzione è ignorato. +Questo file deve essere codificato in Unicode (UTF-16 Little Endian). +Apprendere, creare e testare Espressioni Regolari: https://regexr.com/)"; THREAD_LINKER = u8"Collegatore di thread"; LINK = u8"Collegamento"; THREAD_LINK_FROM = u8"Numero di thread da cui collegarsi";