Merge remote-tracking branch 'Blu3train/regexReplacerTranslatedTextExtension'
This commit is contained in:
commit
383ea11256
@ -53,6 +53,7 @@ foreach ($language in @{
|
|||||||
"Lua",
|
"Lua",
|
||||||
"Regex Filter",
|
"Regex Filter",
|
||||||
"Regex Replacer",
|
"Regex Replacer",
|
||||||
|
"Regex Replacer Translated Text",
|
||||||
"Remove Repeated Characters",
|
"Remove Repeated Characters",
|
||||||
"Remove Repeated Phrases",
|
"Remove Repeated Phrases",
|
||||||
"Remove Repeated Phrases 2",
|
"Remove Repeated Phrases 2",
|
||||||
|
@ -12,6 +12,7 @@ add_library(Google\ Translate MODULE googletranslate.cpp translatewrapper.cpp ne
|
|||||||
add_library(Lua MODULE lua.cpp extensionimpl.cpp)
|
add_library(Lua MODULE lua.cpp extensionimpl.cpp)
|
||||||
add_library(Regex\ Filter MODULE regexfilter.cpp extensionimpl.cpp)
|
add_library(Regex\ Filter MODULE regexfilter.cpp extensionimpl.cpp)
|
||||||
add_library(Regex\ Replacer MODULE regexreplacer.cpp extensionimpl.cpp)
|
add_library(Regex\ Replacer MODULE regexreplacer.cpp extensionimpl.cpp)
|
||||||
|
add_library(Regex\ Replacer\ Translated\ Text MODULE regexreplacertranslatedtext.cpp extensionimpl.cpp)
|
||||||
add_library(Remove\ Repeated\ Characters MODULE removerepeatchar.cpp extensionimpl.cpp)
|
add_library(Remove\ Repeated\ Characters MODULE removerepeatchar.cpp extensionimpl.cpp)
|
||||||
add_library(Remove\ Repeated\ Phrases MODULE removerepeatphrase.cpp extensionimpl.cpp)
|
add_library(Remove\ Repeated\ Phrases MODULE removerepeatphrase.cpp extensionimpl.cpp)
|
||||||
add_library(Remove\ Repeated\ Phrases\ 2 MODULE removerepeatphrase2.cpp extensionimpl.cpp)
|
add_library(Remove\ Repeated\ Phrases\ 2 MODULE removerepeatphrase2.cpp extensionimpl.cpp)
|
||||||
@ -32,6 +33,7 @@ target_precompile_headers(Google\ Translate REUSE_FROM pch)
|
|||||||
target_precompile_headers(Lua REUSE_FROM pch)
|
target_precompile_headers(Lua REUSE_FROM pch)
|
||||||
target_precompile_headers(Regex\ Filter REUSE_FROM pch)
|
target_precompile_headers(Regex\ Filter REUSE_FROM pch)
|
||||||
target_precompile_headers(Regex\ Replacer REUSE_FROM pch)
|
target_precompile_headers(Regex\ Replacer REUSE_FROM pch)
|
||||||
|
target_precompile_headers(Regex\ Replacer\ Translated\ Text REUSE_FROM pch)
|
||||||
target_precompile_headers(Remove\ Repeated\ Characters REUSE_FROM pch)
|
target_precompile_headers(Remove\ Repeated\ Characters REUSE_FROM pch)
|
||||||
target_precompile_headers(Remove\ Repeated\ Phrases REUSE_FROM pch)
|
target_precompile_headers(Remove\ Repeated\ Phrases REUSE_FROM pch)
|
||||||
target_precompile_headers(Remove\ Repeated\ Phrases\ 2 REUSE_FROM pch)
|
target_precompile_headers(Remove\ Repeated\ Phrases\ 2 REUSE_FROM pch)
|
||||||
|
80
extensions/regexreplacertranslatedtext.cpp
Normal file
80
extensions/regexreplacertranslatedtext.cpp
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
#include "extension.h"
|
||||||
|
#include "module.h"
|
||||||
|
#include "blockmarkup.h"
|
||||||
|
#include <fstream>
|
||||||
|
#include <process.h>
|
||||||
|
|
||||||
|
extern const wchar_t* REGEX_REPLACER_INSTRUCTIONS;
|
||||||
|
|
||||||
|
const char* REPLACE_SAVE_FILE = "SavedRegexReplacementsTranslatedText.txt";
|
||||||
|
|
||||||
|
std::atomic<std::filesystem::file_time_type> replaceFileLastWrite = {};
|
||||||
|
concurrency::reader_writer_lock m;
|
||||||
|
std::vector<std::tuple<std::wregex, std::wstring, std::regex_constants::match_flag_type>> replacements;
|
||||||
|
|
||||||
|
void UpdateReplacements()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (replaceFileLastWrite.exchange(std::filesystem::last_write_time(REPLACE_SAVE_FILE)) == std::filesystem::last_write_time(REPLACE_SAVE_FILE)) return;
|
||||||
|
std::scoped_lock lock(m);
|
||||||
|
replacements.clear();
|
||||||
|
std::ifstream stream(REPLACE_SAVE_FILE, std::ios::binary);
|
||||||
|
BlockMarkupIterator savedFilters(stream, Array<std::wstring_view>{ L"|REGEX|", L"|BECOMES|", L"|MODIFIER|" });
|
||||||
|
while (auto read = savedFilters.Next())
|
||||||
|
{
|
||||||
|
const auto& [regex, replacement, modifier] = read.value();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
replacements.emplace_back(
|
||||||
|
std::wregex(regex, modifier.find(L'i') == std::string::npos ? std::regex::ECMAScript : std::regex::icase),
|
||||||
|
replacement,
|
||||||
|
modifier.find(L'g') == std::string::npos ? std::regex_constants::format_first_only : std::regex_constants::format_default
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch (std::regex_error) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (std::filesystem::filesystem_error) { replaceFileLastWrite.store({}); }
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
|
||||||
|
{
|
||||||
|
switch (ul_reason_for_call)
|
||||||
|
{
|
||||||
|
case DLL_PROCESS_ATTACH:
|
||||||
|
{
|
||||||
|
UpdateReplacements();
|
||||||
|
if (replacements.empty())
|
||||||
|
{
|
||||||
|
auto file = std::ofstream(REPLACE_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));
|
||||||
|
SpawnThread([] { _spawnlp(_P_DETACH, "notepad", "notepad", REPLACE_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) return false;
|
||||||
|
size_t posTranslation = sentence.find(L"\x200b \n");
|
||||||
|
if (posTranslation == std::wstring::npos) return false;
|
||||||
|
posTranslation +=3;
|
||||||
|
|
||||||
|
UpdateReplacements();
|
||||||
|
|
||||||
|
std::wstring sTranslated = sentence.substr(posTranslation, std::wstring::npos);
|
||||||
|
sentence = sentence.substr(0, posTranslation);
|
||||||
|
|
||||||
|
concurrency::reader_writer_lock::scoped_lock_read readLock(m);
|
||||||
|
for (const auto& [regex, replacement, flags] : replacements) sentence += std::regex_replace(sTranslated, regex, replacement, flags);
|
||||||
|
return true;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user