45 |
45 |
46 function data.rfc:_search(string, cb) |
46 function data.rfc:_search(string, cb) |
47 debug("really search for %s", string); |
47 debug("really search for %s", string); |
48 local number = tonumber(string); |
48 local number = tonumber(string); |
49 local link, match, matches; |
49 local link, match, matches; |
|
50 local count = 0 |
50 if number then |
51 if number then |
51 number = ("%04d"):format(number); |
52 number = ("%04d"):format(number); |
52 debug("search for RFC%s", number); |
53 debug("search for RFC%s", number); |
53 link, match = self.data:match("\n(" .. number .. ")%s*([^\n]*)"); |
54 link, match = self.data:match("\n(" .. number .. ")%s*([^\n]*)"); |
|
55 count = 1 |
54 else |
56 else |
55 local pat = string:gsub("[()]", function(s) return "%" .. s end) |
57 local pat = string:gsub("[()]", function(s) return "%" .. s end) |
56 :gsub("[%[]",function(s) return "%" .. s end) |
58 :gsub("[%[]",function(s) return "%" .. s end) |
57 :gsub("%%(%b[])",function(s) return (#s > 2 and "" or "%") .. s end) |
59 :gsub("%%(%b[])",function(s) return (#s > 2 and "" or "%") .. s end) |
58 :gsub("\n+", " "):gsub("\\n", ""); |
60 :gsub("\n+", " "):gsub("\\n", ""); |
59 debug("fulltext search for \"%s\"", pat); |
61 debug("fulltext search for \"%s\"", pat); |
60 --link, match = self.data:match("\n(%d%d%d%d) ([^\n]-"..pat.."[^\n]*)"); |
62 --link, match = self.data:match("\n(%d%d%d%d) ([^\n]-"..pat.."[^\n]*)"); |
61 for l,m in self.data:gmatch("\n(%d%d%d%d) ([^\n]-"..pat.."[^\n]*)") do |
63 for l,m in self.data:gmatch("\n(%d%d%d%d) ([^\n]-"..pat.."[^\n]*)") do |
62 link, match = l, m |
64 link, match = l, m |
|
65 count = count + 1 |
63 -- Note: This allways returns the last result. |
66 -- Note: This allways returns the last result. |
64 -- FIXME Decide on what to do if >1 results. |
67 -- FIXME Decide on what to do if >1 results. |
65 end |
68 end |
66 --[[ |
69 --[[ |
67 matches = {}; |
70 matches = {}; |
92 match = match:gsub("%s*%b()", function(s) |
95 match = match:gsub("%s*%b()", function(s) |
93 local first = s:match("%(([^: ]*)"); return first and remove[first] and "" or s |
96 local first = s:match("%(([^: ]*)"); return first and remove[first] and "" or s |
94 end); |
97 end); |
95 link = self.links:format(link); |
98 link = self.links:format(link); |
96 match = match:gsub("%. ", ".\n", 1); -- Add a newline between title and authors |
99 match = match:gsub("%. ", ".\n", 1); -- Add a newline between title and authors |
|
100 if count > 1 then link = link .. " (" .. count .. " more matches)" end |
97 cb(match .. "\n" .. link); |
101 cb(match .. "\n" .. link); |
98 else |
102 else |
99 cb("Sorry, no match"); |
103 cb("Sorry, no match"); |
100 end |
104 end |
101 end |
105 end |