From 07586cea71ab3f8624424773ec81795cb8ece892 Mon Sep 17 00:00:00 2001 From: chrox Date: Sun, 1 Feb 2015 17:40:34 +0800 Subject: [PATCH] strip punctuations around word before searching This should fix #1337. --- frontend/apps/reader/modules/readerdictionary.lua | 4 +--- frontend/apps/reader/modules/readersearch.lua | 1 + frontend/util.lua | 11 +++++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 frontend/util.lua diff --git a/frontend/apps/reader/modules/readerdictionary.lua b/frontend/apps/reader/modules/readerdictionary.lua index 4ce6ed0ea..dabf77579 100644 --- a/frontend/apps/reader/modules/readerdictionary.lua +++ b/frontend/apps/reader/modules/readerdictionary.lua @@ -59,9 +59,7 @@ end function ReaderDictionary:stardictLookup(word, box) DEBUG("lookup word:", word, box) if word then - -- strip ASCII punctuation characters around selected word - -- and strip any generic punctuation (U+2000 - U+206F) in the word - word = word:gsub("\226[\128-\131][\128-\191]",''):gsub("^%p+",''):gsub("%p+$",'') + word = require("util").stripePunctuations(word) DEBUG("stripped word:", word) -- escape quotes and other funny characters in word local std_out = io.popen("./sdcv --utf8-input --utf8-output -nj "..("%q"):format(word), "r") diff --git a/frontend/apps/reader/modules/readersearch.lua b/frontend/apps/reader/modules/readersearch.lua index a21aba836..342d525e4 100644 --- a/frontend/apps/reader/modules/readersearch.lua +++ b/frontend/apps/reader/modules/readersearch.lua @@ -29,6 +29,7 @@ function ReaderSearch:addToMainMenu(tab_item_table) end function ReaderSearch:onShowSearchDialog(text) + text = require("util").stripePunctuations(text) local do_search = function(search_func, text, param) return function() local res = search_func(self, text, param) diff --git a/frontend/util.lua b/frontend/util.lua new file mode 100644 index 000000000..ff1fbe4e1 --- /dev/null +++ b/frontend/util.lua @@ -0,0 +1,11 @@ + +local util = {} + +function util.stripePunctuations(word) + if not word then return end + -- strip ASCII punctuation characters around word + -- and strip any generic punctuation (U+2000 - U+206F) in the word + return word:gsub("\226[\128-\131][\128-\191]",''):gsub("^%p+",''):gsub("%p+$",'') +end + +return util