misc: Move cleanupSelectedText to util (#12477)

Instead of duplicating it across ReaderHighlight, languagesupport, and potentially VocabBuilder.

Re: #12469
This commit is contained in:
NiLuJe
2024-09-06 23:06:28 +02:00
committed by GitHub
parent 8f5215abfd
commit f8df76e5c4
3 changed files with 26 additions and 36 deletions

View File

@@ -50,8 +50,23 @@ end
---- @string s the string to be trimmed
---- @treturn string trimmed text
function util.trim(s)
local from = s:match"^%s*()"
return from > #s and "" or s:match(".*%S", from)
local from = s:match"^%s*()"
return from > #s and "" or s:match(".*%S", from)
end
---- Variant tailored for text selection purposes (originally implemented in ReaderHighlight).
---- @string text the text to be trimmed
---- @treturn string trimmed text
function util.cleanupSelectedText(text)
-- Trim spaces and new lines at start and end
text = text:gsub("^[\n%s]*", "")
text = text:gsub("[\n%s]*$", "")
-- Trim spaces around newlines
text = text:gsub("%s*\n%s*", "\n")
-- Trim consecutive spaces (that would probably have collapsed
-- in rendered CreDocuments)
text = text:gsub("%s%s+", " ")
return text
end
--[[