summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/util/html_doclet.lua
blob: 3001d8140abd377ee85483feac619f5016476a74 (plain) (blame)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
-------------------------------------------------------------------------------
-- Doclet that generates HTML output. This doclet generates a set of html files
-- based on a group of templates. The main templates are:
-- <ul>
-- <li>index.lp: index of modules and files;</li>
-- <li>file.lp: documentation for a lua file;</li>
-- <li>module.lp: documentation for a lua module;</li>
-- <li>function.lp: documentation for a lua function. This is a
-- sub-template used by the others.</li>
-- </ul>
--
-- @release $Id: html.lua,v 1.29 2007/12/21 17:50:48 tomas Exp $
-------------------------------------------------------------------------------

local assert, getfenv, ipairs, loadstring, pairs, setfenv, tostring, tonumber, type = assert, getfenv, ipairs, loadstring, pairs, setfenv, tostring, tonumber, type
local io = require"io"
local lfs = require "lfs"
local lp = require "luadoc.lp"
local luadoc = require"luadoc"
local package = package
local string = require"string"
local table = require"table"

--module "luadoc.doclet.html"
module "util.html_doclet"
thismod = getfenv()

-------------------------------------------------------------------------------
-- Looks for a file `name' in given path. Removed from compat-5.1
-- @param path String with the path.
-- @param name String with the name to look for.
-- @return String with the complete path of the file found
--        or nil in case the file is not found.

local function search (path, name)
  for c in string.gfind(path, "[^;]+") do
    c = string.gsub(c, "%?", name)
    local f = io.open(c)
    if f then   -- file exist?
      f:close()
      return c
    end
  end
  return nil    -- file not found
end

-------------------------------------------------------------------------------
-- Include the result of a lp template into the current stream.

function include (template, env)
        -- template_dir is relative to package.path
        local templatepath = options.template_dir .. template

        -- search using package.path (modified to search .lp instead of .lua
        local search_path = string.gsub(package.path, "%.lua", "")
        local templatepath = search(search_path, templatepath)
        assert(templatepath, string.format("template `%s' not found", template))

        env = env or {}
        env.table = table
        env.io = io
        env.lp = lp
        env.ipairs = ipairs
        env.tonumber = tonumber
        env.tostring = tostring
        env.type = type
        env.luadoc = luadoc
        env.options = options

        -- Monkeypatch ourselves over the standard html doclet, so the
        -- templates will work unaltered.
        env.luadoc.doclet = env.luadoc.doclet or {}
        env.luadoc.doclet.html = thismod

        return lp.include(templatepath, env)
end

-------------------------------------------------------------------------------
-- Returns a link to a html file, appending "../" to the link to make it right.
-- @param html Name of the html file to link to
-- @return link to the html file

function link (html, from)
        local h = html
        from = from or ""
        string.gsub(from, "/", function () h = "../" .. h end)
        return h
end

-------------------------------------------------------------------------------
-- Returns the name of the html file to be generated from a module.
-- Files with "lua" or "luadoc" extensions are replaced by "html" extension.
-- @param modulename Name of the module to be processed, may be a .lua file or
-- a .luadoc file.
-- @return name of the generated html file for the module

function module_link (modulename, doc, from)
        -- TODO: replace "." by "/" to create directories?
        -- TODO: how to deal with module names with "/"?
        assert(modulename)
        assert(doc)
        from = from or ""

        if doc.modules[modulename] == nil then
--                logger:error(string.format("unresolved reference to module `%s'", modulename))
                return
        end

        local href = "modules/" .. modulename .. ".html"
        string.gsub(from, "/", function () href = "../" .. href end)
        return href
end

-------------------------------------------------------------------------------
-- Returns the name of the html file to be generated from a lua(doc) file.
-- Files with "lua" or "luadoc" extensions are replaced by "html" extension.
-- @param to Name of the file to be processed, may be a .lua file or
-- a .luadoc file.
-- @param from path of where am I, based on this we append ..'s to the
-- beginning of path
-- @return name of the generated html file

function file_link (to, from)
        assert(to)
        from = from or ""

        local href = to
        href = string.gsub(href, "lua$", "html")
        href = string.gsub(href, "luadoc$", "html")
        href = "files/" .. href
        string.gsub(from, "/", function () href = "../" .. href end)
        return href
end

-------------------------------------------------------------------------------
-- Returns a link to a function or to a table
-- @param fname name of the function or table to link to.
-- @param doc documentation table
-- @param kind String specying the kinf of element to link ("functions" or "tables").

function link_to (fname, doc, module_doc, file_doc, from, kind)
        assert(fname)
        assert(doc)
        from = from or ""
        kind = kind or "functions"

        if file_doc then
                for _, func_name in pairs(file_doc[kind]) do
                        if func_name == fname then
                                return file_link(file_doc.name, from) .. "#" .. fname
                        end
                end
        end

        local _, _, modulename, fname = string.find(fname, "^(.-)[%.%:]?([^%.%:]*)$")
        assert(fname)

        -- if fname does not specify a module, use the module_doc
        if string.len(modulename) == 0 and module_doc then
                modulename = module_doc.name
        end

        local module_doc = doc.modules[modulename]
        if not module_doc then
--                logger:error(string.format("unresolved reference to function `%s': module `%s' not found", fname, modulename))
                return
        end

        for _, func_name in pairs(module_doc[kind]) do
                if func_name == fname then
                        return module_link(modulename, doc, from) .. "#" .. fname
                end
        end

--        logger:error(string.format("unresolved reference to function `%s' of module `%s'", fname, modulename))
end

-------------------------------------------------------------------------------
-- Make a link to a file, module or function

function symbol_link (symbol, doc, module_doc, file_doc, from)
        assert(symbol)
        assert(doc)

        local href =
--                file_link(symbol, from) or
                module_link(symbol, doc, from) or
                link_to(symbol, doc, module_doc, file_doc, from, "functions") or
                link_to(symbol, doc, module_doc, file_doc, from, "tables")

        if not href then
                logger:error(string.format("unresolved reference to symbol `%s'", symbol))
        end

        return href or ""
end

-------------------------------------------------------------------------------
-- Assembly the output filename for an input file.
-- TODO: change the name of this function
function out_file (filename)
        local h = filename
        h = string.gsub(h, "lua$", "html")
        h = string.gsub(h, "luadoc$", "html")
        h = string.gsub(h, "cc$", "html")
        h = "files/" .. h
--        h = options.output_dir .. string.gsub (h, "^.-([%w_]+%.html)$", "%1")
        h = options.output_dir .. h
        return h
end

-------------------------------------------------------------------------------
-- Assembly the output filename for a module.
-- TODO: change the name of this function
function out_module (modulename)
        local h = modulename .. ".html"
        h = "modules/" .. h
        h = options.output_dir .. h
        return h
end

-----------------------------------------------------------------
-- Generate the output.
-- @param doc Table with the structured documentation.

function start (doc)
        -- Generate index file
        if (#doc.files > 0 or #doc.modules > 0) and (not options.noindexpage) then
                local filename = options.output_dir.."index.html"
                logger:info(string.format("generating file `%s'", filename))
                local f = lfs.open(filename, "w")
                assert(f, string.format("could not open `%s' for writing", filename))
                io.output(f)
                include("index.lp", { doc = doc })
                f:close()
        end

        -- Process modules
        if not options.nomodules then
                for _, modulename in ipairs(doc.modules) do
                        local module_doc = doc.modules[modulename]
                        -- assembly the filename
                        local filename = out_module(modulename)
                        logger:info(string.format("generating file `%s'", filename))

                        local f = lfs.open(filename, "w")
                        assert(f, string.format("could not open `%s' for writing", filename))
                        io.output(f)
                        include("module.lp", { doc = doc, module_doc = module_doc })
                        f:close()
                end
        end

        -- Process files
        if not options.nofiles then
                for _, filepath in ipairs(doc.files) do
                        local file_doc = doc.files[filepath]
                        -- assembly the filename
                        local filename = out_file(file_doc.name)
                        logger:info(string.format("generating file `%s'", filename))

                        local f = lfs.open(filename, "w")
                        assert(f, string.format("could not open `%s' for writing", filename))
                        io.output(f)
                        include("file.lp", { doc = doc, file_doc = file_doc} )
                        f:close()
                end
        end

        -- copy extra files
        local f = lfs.open(options.output_dir.."luadoc.css", "w")
        io.output(f)
        include("luadoc.css")
        f:close()
end