1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
--
-- (C) 2017-22 - ntop.org
--
dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "lua_utils"
require "prefs_utils"
local json = require "dkjson"
sendHTTPHeader('application/json')
local max_num_to_find = 5
local res = {results={}}
local results = res.results
local menu_subpages = require "prefs_menu"
local query = _GET["query"] or ""
local function matchesQuery(value, query)
return string.find(string.lower(noHtml(value)), string.lower(query))
end
local function queryResultShorten(result, query, context)
local maxlen = 35
local idx = matchesQuery(result, query)
local left_slice, right_slice = shortenInTheMiddle(result, idx, idx + string.len(query), maxlen)
return context..(
ternary(left_slice ~= 1, "...", "")..
string.sub(result, left_slice, right_slice)..
ternary(right_slice ~= string.len(result), "...", "")
)
end
local function addResult(result, tab, context)
results[#results + 1] = {name=queryResultShorten(noHtml(result), query, context), tab=tab.id}
end
for _, tab in pairs(menu_subpages) do
if isSubpageAvailable(tab) and not (tab.disabled) then
-- Menu match, do not proceed with children
if matchesQuery(tab.label, query) then
addResult(tab.label, tab, "")
else
for _, entry in pairs(tab.entries) do
if entry.hidden ~= true then
-- Entry title match, do not proceed with description
if matchesQuery(entry.title, query) then
-- Decorate with tab label
addResult(entry.title, tab, tab.label .. ": ")
break
--[[elseif matchesQuery(entry.description, query) then
-- Decorate with entry title and tab label
addResult(entry.description, tab, tab.label .. " [" .. entry.title .. "]: ")]]--
end
end
end
end
end
end
print(json.encode(res, nil, 1))
|