mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
misc: Move cleanupSelectedText to util (#12477)
Instead of duplicating it across ReaderHighlight, languagesupport, and potentially VocabBuilder. Re: #12469
This commit is contained in:
@@ -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
|
||||
|
||||
--[[
|
||||
|
||||
Reference in New Issue
Block a user