aboutsummaryrefslogtreecommitdiffstats
path: root/spellcheck.lua
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fl.fischer@fau.de>2020-08-28 16:45:06 +0200
committerFlorian Fischer <florian.fl.fischer@fau.de>2020-08-28 16:45:06 +0200
commit4b08e0a08ca2b3d776186e71a743eb4a7fd2652c (patch)
treef0765e397c0cd961643b0d47c799df3c64079839 /spellcheck.lua
parent8af2f0f18350078ae8d70a62c95e161b2d43a0af (diff)
downloadvis-spellcheck-4b08e0a08ca2b3d776186e71a743eb4a7fd2652c.tar.gz
escape magic characters in typos
If the typo itself contains magic pattern characters we can't reliable find it in the text.
Diffstat (limited to 'spellcheck.lua')
-rw-r--r--spellcheck.lua27
1 files changed, 26 insertions, 1 deletions
diff --git a/spellcheck.lua b/spellcheck.lua
index 6a20172..31b3387 100644
--- a/spellcheck.lua
+++ b/spellcheck.lua
@@ -81,6 +81,31 @@ local ignored = {}
local function typo_iter(text, typos, ignored)
local index = 1
local unfiltered_iterator, iter_state = typos:gmatch("(.-)\n")
+
+-- see https://stackoverflow.com/questions/6705872/how-to-escape-a-variable-in-lua
+ local escape_lua_pattern
+ do
+ local matches =
+ {
+ ["^"] = "%^";
+ ["$"] = "%$";
+ ["("] = "%(";
+ [")"] = "%)";
+ ["%"] = "%%";
+ ["."] = "%.";
+ ["["] = "%[";
+ ["]"] = "%]";
+ ["*"] = "%*";
+ ["+"] = "%+";
+ ["-"] = "%-";
+ ["?"] = "%?";
+ }
+
+ escape_lua_pattern = function(s)
+ return (s:gsub(".", matches))
+ end
+ end
+
return function(foo, bar)
repeat
typo = unfiltered_iterator(iter_state)
@@ -90,7 +115,7 @@ local function typo_iter(text, typos, ignored)
-- to prevent typos from being found in correct words before them
-- ("stuff stuf", "broken ok", ...)
-- we match typos only when they are enclosed in non-letter characters.
- local start, finish = text:find("[%A]" .. typo .. "[%A]", index)
+ local start, finish = text:find("[%A]" .. escape_lua_pattern(typo) .. "[%A]", index)
-- typo was not found by our pattern this means it must be either
-- the first or last word in the text
if not start then