Regex Replacer Extension
This commit is contained in:
parent
49133974b3
commit
e6ee5ecf3d
@ -52,6 +52,7 @@ foreach ($language in @{
|
||||
"Google Translate",
|
||||
"Lua",
|
||||
"Regex Filter",
|
||||
"Regex Replacer",
|
||||
"Remove Repeated Characters",
|
||||
"Remove Repeated Phrases",
|
||||
"Remove Repeated Phrases 2",
|
||||
|
@ -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)
|
||||
|
53
extensions/regexreplacer.cpp
Normal file
53
extensions/regexreplacer.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
#include "extension.h"
|
||||
#include "module.h"
|
||||
#include "blockmarkup.h"
|
||||
#include <fstream>
|
||||
|
||||
extern const wchar_t* REGEX_REPLACER_INSTRUCTIONS;
|
||||
|
||||
const char* REGEX_REPLACEMENTS_SAVE_FILE = "SavedRegexReplacements.txt";
|
||||
|
||||
std::optional<std::wregex> 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<std::wstring_view>{ L"|REGEX|", L"|BECOMES|" });
|
||||
//std::vector<std::wstring> 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;
|
||||
}
|
12
text.cpp
12
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";
|
||||
|
Loading…
Reference in New Issue
Block a user