1 -------------------------------------------------------------------------------
2 -- Handlers for several tags
3 -- @release $Id: tags.lua,v 1.8 2007/09/05 12:39:09 tomas Exp $
4 -------------------------------------------------------------------------------
6 local luadoc = require "luadoc"
7 local util = require "luadoc.util"
8 local string = require "string"
9 local table = require "table"
10 local assert, type, tostring = assert, type, tostring
12 module "luadoc.taglet.standard.tags"
14 -------------------------------------------------------------------------------
16 local function author (tag, block, text)
17 block[tag] = block[tag] or {}
19 luadoc.logger:warn("author `name' not defined [["..text.."]]: skipping")
22 table.insert (block[tag], text)
25 -------------------------------------------------------------------------------
26 -- Set the class of a comment block. Classes can be "module", "function",
27 -- "table". The first two classes are automatic, extracted from the source code
29 local function class (tag, block, text)
33 -------------------------------------------------------------------------------
35 local function copyright (tag, block, text)
39 -------------------------------------------------------------------------------
41 local function description (tag, block, text)
45 -------------------------------------------------------------------------------
47 local function field (tag, block, text)
48 if block["class"] ~= "table" then
49 luadoc.logger:warn("documenting `field' for block that is not a `table'")
51 block[tag] = block[tag] or {}
53 local _, _, name, desc = string.find(text, "^([_%w%.]+)%s+(.*)")
54 assert(name, "field name not defined")
56 table.insert(block[tag], name)
57 block[tag][name] = desc
60 -------------------------------------------------------------------------------
61 -- Set the name of the comment block. If the block already has a name, issue
62 -- an error and do not change the previous value
64 local function name (tag, block, text)
65 if block[tag] and block[tag] ~= text then
66 luadoc.logger:error(string.format("block name conflict: `%s' -> `%s'", block[tag], text))
72 -------------------------------------------------------------------------------
73 -- Processes a parameter documentation.
74 -- @param tag String with the name of the tag (it must be "param" always).
75 -- @param block Table with previous information about the block.
76 -- @param text String with the current line beeing processed.
78 local function param (tag, block, text)
79 block[tag] = block[tag] or {}
80 -- TODO: make this pattern more flexible, accepting empty descriptions
81 local _, _, name, desc = string.find(text, "^([_%w%.]+)%s+(.*)")
83 luadoc.logger:warn("parameter `name' not defined [["..text.."]]: skipping")
86 local i = table.foreachi(block[tag], function (i, v)
92 luadoc.logger:warn(string.format("documenting undefined parameter `%s'", name))
93 table.insert(block[tag], name)
95 block[tag][name] = desc
98 -------------------------------------------------------------------------------
100 local function release (tag, block, text)
104 -------------------------------------------------------------------------------
106 local function ret (tag, block, text)
108 if type(block[tag]) == "string" then
109 block[tag] = { block[tag], text }
110 elseif type(block[tag]) == "table" then
111 table.insert(block[tag], text)
117 -------------------------------------------------------------------------------
120 local function see (tag, block, text)
121 -- see is always an array
122 block[tag] = block[tag] or {}
124 -- remove trailing "."
125 text = string.gsub(text, "(.*)%.$", "%1")
127 local s = util.split("%s*,%s*", text)
129 table.foreachi(s, function (_, v)
130 table.insert(block[tag], v)
134 -------------------------------------------------------------------------------
137 local function usage (tag, block, text)
138 if type(block[tag]) == "string" then
139 block[tag] = { block[tag], text }
140 elseif type(block[tag]) == "table" then
141 table.insert(block[tag], text)
147 -------------------------------------------------------------------------------
150 handlers["author"] = author
151 handlers["class"] = class
152 handlers["copyright"] = copyright
153 handlers["description"] = description
154 handlers["field"] = field
155 handlers["name"] = name
156 handlers["param"] = param
157 handlers["release"] = release
158 handlers["return"] = ret
159 handlers["see"] = see
160 handlers["usage"] = usage
162 -------------------------------------------------------------------------------
164 function handle (tag, block, text)
165 if not handlers[tag] then
166 luadoc.logger:error(string.format("undefined handler for tag `%s'", tag))
169 -- assert(handlers[tag], string.format("undefined handler for tag `%s'", tag))
170 return handlers[tag](tag, block, text)