build: add modified luadoc for use with LuCI sources
[project/luci.git] / contrib / luadoc / lua / luadoc / taglet / standard.lua
index c02bc98..17a3058 100644 (file)
@@ -4,7 +4,7 @@
 
 local assert, pairs, tostring, type = assert, pairs, tostring, type
 local io = require "io"
-local posix = require "posix"
+local posix = require "nixio.fs"
 local luadoc = require "luadoc"
 local util = require "luadoc.util"
 local tags = require "luadoc.taglet.standard.tags"
@@ -96,6 +96,38 @@ local function check_module (line, currentmodule)
        return currentmodule
 end
 
+-- Patterns for constant recognition
+local constant_patterns = {
+       "^()%s*([A-Z][A-Z0-9_]*)%s*=",
+       "^%s*(local%s)%s*([A-Z][A-Z0-9_]*)%s*=",
+}
+
+-------------------------------------------------------------------------------
+-- Checks if the line contains a constant definition
+-- @param line string with line text
+-- @return constant information or nil if no constant definition found
+
+local function check_constant (line)
+       line = util.trim(line)
+
+       local info = table.foreachi(constant_patterns, function (_, pattern)
+               local r, _, l, id = string.find(line, pattern)
+               if r ~= nil then
+                       return {
+                               name = id,
+                               private = (l == "local"),
+                       }
+               end
+       end)
+
+       -- TODO: remove these assert's?
+       if info ~= nil then
+               assert(info.name, "constant name undefined")
+       end
+
+       return info
+end
+
 -------------------------------------------------------------------------------
 -- Extracts summary information from a description. The first sentence of each
 -- doc comment should be a summary sentence, containing a concise but complete
@@ -172,11 +204,16 @@ local function parse_comment (block, first_line, modulename)
        if code ~= nil then
                local func_info = check_function(code)
                local module_name = check_module(code)
+               local const_info = check_constant(code)
                if func_info then
                        block.class = "function"
                        block.name = func_info.name
                        block.param = func_info.param
                        block.private = func_info.private
+               elseif const_info then
+                       block.class = "constant"
+                       block.name = const_info.name
+                       block.private = const_info.private
                elseif module_name then
                        block.class = "module"
                        block.name = module_name
@@ -403,6 +440,15 @@ function parse_file (filepath, doc, handle, prev_line, prev_block, prev_modname)
                                doc.modules[modulename].tables[t.name] = t
                        end
                end
+
+               -- make constants table
+               doc.modules[modulename].constants = {}
+               for c in class_iterator(blocks, "constant")() do
+                       if c and c.name then
+                               table.insert(doc.modules[modulename].constants, c.name)
+                               doc.modules[modulename].constants[c.name] = c
+                       end
+               end
        end
 
        if filepath then
@@ -459,14 +505,14 @@ end
 -- @return table with documentation
 
 function directory (path, doc)
-       for f in posix.files(path) do
+       for f in posix.dir(path) do
                local fullpath = path .. "/" .. f
                local attr = posix.stat(fullpath)
                assert(attr, string.format("error stating file `%s'", fullpath))
 
-               if attr.type == "regular" then
+               if attr.type == "reg" then
                        doc = file(fullpath, doc)
-               elseif attr.type == "directory" and f ~= "." and f ~= ".." then
+               elseif attr.type == "dir" and f ~= "." and f ~= ".." then
                        doc = directory(fullpath, doc)
                end
        end
@@ -504,9 +550,9 @@ function start (files, doc)
                local attr = posix.stat(path)
                assert(attr, string.format("error stating path `%s'", path))
 
-               if attr.type == "regular" then
+               if attr.type == "reg" then
                        doc = file(path, doc)
-               elseif attr.type == "directory" then
+               elseif attr.type == "dir" then
                        doc = directory(path, doc)
                end
        end)