luci-base: introduce luci.dispatcher.lookup()
The lookup function takes multiple, possibly malformed path fragments, splits them on slashes, constructs a temporary path and looks up the result in the dispatch tree. If a matching node has been found, the function will return both the node reference and the canonical url to it. If no corresponding node is found, the function returns nil. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
parent
bc3651ba56
commit
7b04d0bbcf
2 changed files with 27 additions and 1 deletions
|
@ -658,6 +658,23 @@ function node(...)
|
||||||
return c
|
return c
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function lookup(...)
|
||||||
|
local i, path = nil, {}
|
||||||
|
for i = 1, select('#', ...) do
|
||||||
|
local name, arg = nil, tostring(select(i, ...))
|
||||||
|
for name in arg:gmatch("[^/]+") do
|
||||||
|
path[#path+1] = name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for i = #path, 1, -1 do
|
||||||
|
local node = context.treecache[table.concat(path, ".", 1, i)]
|
||||||
|
if node and (i == #path or node.leaf) then
|
||||||
|
return node, build_url(unpack(path))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function _create_node(path)
|
function _create_node(path)
|
||||||
if #path == 0 then
|
if #path == 0 then
|
||||||
return context.tree
|
return context.tree
|
||||||
|
|
|
@ -116,8 +116,8 @@ Create a new dispatching node and define common parameters.
|
||||||
|
|
||||||
---[[
|
---[[
|
||||||
Fetch or create a dispatching node without setting the target module or
|
Fetch or create a dispatching node without setting the target module or
|
||||||
|
|
||||||
enabling the node.
|
enabling the node.
|
||||||
|
|
||||||
@class function
|
@class function
|
||||||
@name get
|
@name get
|
||||||
@param ... Virtual path
|
@param ... Virtual path
|
||||||
|
@ -133,6 +133,15 @@ Fetch or create a new dispatching node.
|
||||||
@return Dispatching tree node
|
@return Dispatching tree node
|
||||||
]]
|
]]
|
||||||
|
|
||||||
|
---[[
|
||||||
|
Lookup node in dispatching tree.
|
||||||
|
|
||||||
|
@class function
|
||||||
|
@name lookup
|
||||||
|
@param ... Virtual path
|
||||||
|
@return Node object, canonical url or nil if the path was not found.
|
||||||
|
]]
|
||||||
|
|
||||||
---[[
|
---[[
|
||||||
Alias the first (lowest order) page automatically
|
Alias the first (lowest order) page automatically
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue