5 Collects menu building information from controllers
11 Copyright 2008 Steven Barth <steven@midlink.org>
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at
17 http://www.apache.org/licenses/LICENSE-2.0
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
26 module("ffluci.menu", package.seeall)
29 require("ffluci.util")
30 require("ffluci.template")
32 ctrldir = ffluci.fs.dirname(ffluci.util.__file__()) .. "/controller/"
33 modelpath = ffluci.fs.dirname(ffluci.util.__file__()) .. "/model/menudata.lua"
35 -- Cache menudata into a Luafile instead of recollecting it at every pageload
36 -- Warning: Make sure the menudata cache gets deleted everytime you update
37 -- the menu information of any module or add or remove a module
38 builder_enable_cache = false
41 -- Builds the menudata file
43 local data = collect()
44 ffluci.fs.writefile(modelpath, dump(data, "m"))
49 -- Collect all menu information provided in the controller modules
52 for k,cat in pairs(ffluci.fs.dir(ctrldir)) do
54 for k,con in pairs(ffluci.fs.dir(ctrldir .. "/" .. cat)) do
55 if con:sub(-4) == ".lua" then
56 con = con:sub(1, con:len()-4)
57 local mod = require("ffluci.controller." .. cat .. "." .. con)
58 if mod.menu and mod.menu.descr
59 and mod.menu.entries and mod.menu.order then
61 entry[".descr"] = mod.menu.descr
62 entry[".order"] = mod.menu.order
64 for k,v in pairs(mod.menu.entries) do
68 for k,v in ipairs(m[cat]) do
69 if v[".order"] > entry[".order"] then
74 table.insert(m[cat], i+1, entry)
83 -- Dumps a table into a string of Lua code
84 function dump(tbl, name)
85 local src = name .. "={}\n"
86 for k,v in pairs(tbl) do
87 if type(k) == "string" then
88 k = ffluci.util.escape(k)
89 k = "'" .. ffluci.util.escape(k, "'") .. "'"
91 if type(v) == "string" then
92 v = ffluci.util.escape(v)
93 v = ffluci.util.escape(v, "'")
94 src = src .. name .. "[" .. k .. "]='" .. v .. "'\n"
95 elseif type(v) == "number" then
96 src = src .. name .. "[" .. k .. "]=" .. v .. "\n"
97 elseif type(v) == "table" then
98 src = src .. dump(v, name .. "[" .. k .. "]")
104 -- Returns the menu information
106 if builder_enable_cache then
107 local cachemt = ffluci.fs.mtime(modelpath)
110 if cachemt == nil then
114 local f = loadfile(modelpath)