Codebase list lua-ldoc / d2d7d6a
extensive readme changes; more config.ld options steve donovan 12 years ago
3 changed file(s) with 236 addition(s) and 59 deletion(s). Raw diff Collapse all Expand all
323323 if mod_ref then
324324 local name = item_ref and item_ref.name or ''
325325 -- this is deeply hacky; classes have 'Class ' prepended.
326 if item_ref.type == 'type' then
326 if item_ref and item_ref.type == 'type' then
327327 name = 'Class_'..name
328328 end
329329 item.see:append {mod=mod_ref.name,name=name,label=s}
1515 local args = lapp [[
1616 ldoc, a documentation generator for Lua, vs 0.2 Beta
1717 -d,--dir (default docs) output directory
18 --here read parameters from ./config.ld
1918 -o,--output (default 'index') output name
2019 -v,--verbose verbose
2120 -q,--quiet suppress output
2221 -m,--module module docs as text
23 -s,--style (default !) directory for templates and style
22 -s,--style (default !) directory for style sheet (ldoc.css)
23 -l,--template (default !) directory for template (ldoc.ltp)
2424 -p,--project (default ldoc) project name
2525 -t,--title (default Reference) page title
2626 -f,--format (default plain) formatting - can be markdown or plain
2727 -b,--package (default .) top-level package basename (needed for module(...))
28 -x,--ext (default html) output file extension
2829 --dump debug output dump
2930 <file> (string) source file or directory containing source
3031 ]]
441442 args.file = fullpath
442443 end
443444
444 local config_is_read
445
445 -- a special case: 'ldoc .' can get all its parameters from config.ld
446446 if args.file == '.' then
447 -- a special case: 'ldoc .' can get all its parameters from config.ld
448 local dir,err = read_ldoc_config('./'..CONFIG_NAME)
449 if err then quit("no "..CONFIG_NAME.." found here") end
450 config_is_read = true
451 args.file = ldoc.file or '.'
452 if args.file == '.' then
453 args.file = lfs.currentdir()
454 else
455 args.file = path.abspath(args.file)
456 end
447 local err
448 config_dir,err = read_ldoc_config('./'..CONFIG_NAME)
449 if err then quit("no "..CONFIG_NAME.." found here") end
450 config_is_read = true
451 args.file = ldoc.file or '.'
452 if args.file == '.' then
453 args.file = lfs.currentdir()
454 else
455 args.file = path.abspath(args.file)
456 end
457457 else
458458 args.file = path.abspath(args.file)
459459 end
493493 end)
494494
495495 -- finding more than one should probably be a warning...
496 if #config_files > 0 and not config_is_read then
496 if #config_files > 0 and not config_dir then
497497 config_dir = read_ldoc_config(config_files[1])
498498 end
499499
512512 multiple_files = true
513513 elseif path.isfile(args.file) then
514514 -- a single file may be accompanied by a config.ld in the same dir
515 local config_dir = path.dirname(args.file)
516 if config_dir == '' then config_dir = '.' end
517 local config = path.join(config_dir,CONFIG_NAME)
518 if path.isfile(config) and not config_is_read then
519 read_ldoc_config(config)
515 if not config_dir then
516 config_dir = path.dirname(args.file)
517 if config_dir == '' then config_dir = '.' end
518 local config = path.join(config_dir,CONFIG_NAME)
519 if path.isfile(config) and not config_is_read then
520 read_ldoc_config(config)
521 end
520522 end
521523 local ext = path.extension(args.file)
522524 local ftype = file_types[ext]
561563
562564 local css, templ = 'ldoc.css','ldoc.ltp'
563565
564 -- the style directory for template and stylesheet can be specified
565 -- either by command-line 'style' argument or by 'style' field in
566 -- config.ld. Then it is relative to the location of that file.
567 if ldoc.style then args.style = path.join(config_dir,ldoc.style) end
568
569 -- '!' here means 'use same directory as the ldoc.lua script'
570 if args.style == '!' then
571 args.style = arg[0]:gsub('[^/\\]+$','')
572 end
566 local function style_dir (sname)
567 local style = ldoc[sname]
568 local dir
569 if style then
570 if style == true then
571 dir = config_dir
572 elseif type(style) == 'string' and path.isdir(style) then
573 dir = style
574 else
575 quit(tostring(name).." is not a directory")
576 end
577 args[sname] = dir
578 end
579 end
580
581 local function override (field)
582 if ldoc[field] then args[field] = ldoc[field] end
583 end
584
585 -- the directories for template and stylesheet can be specified
586 -- either by command-line '--template','--style' arguments or by 'template and
587 -- 'style' fields in config.ld.
588 -- The assumption here is that if these variables are simply true then the directory
589 -- containing config.ld contains a ldoc.css and a ldoc.ltp respectively. Otherwise
590 -- they must be a valid subdirectory.
591
592 style_dir 'style'
593 style_dir 'template'
594
595 -- can specify format, output, dir and ext in config.ld
596 override 'format'
597 override 'output'
598 override 'dir'
599 override 'ext'
600 args.ext = '.'..args.ext
601
602
603 -- '!' here means 'use same directory as ldoc.lua
604 local ldoc_dir = arg[0]:gsub('[^/\\]+$','')
605 if args.style == '!' then args.style = ldoc_dir end
606 if args.template == '!' then args.template = ldoc_dir end
573607
574608 local module_template,err = utils.readfile (path.join(args.style,templ))
575609 if not module_template then quit(err) end
576610
577 -- can specify format, output and dir in config.ld
578 if ldoc.format then args.format = ldoc.format end
579 if ldoc.output then args.output = ldoc.output end
580 if ldoc.dir then args.dir = ldoc.dir end
611
581612
582613 if args.format ~= 'plain' then
583614 local ok,markup = pcall(require,args.format)
616647 check_file(args.dir..css, path.join(args.style,css))
617648
618649 -- write out the module index
619 writefile(args.dir..args.output..'.html',out)
650 writefile(args.dir..args.output..args.ext,out)
620651
621652 -- write out the per-module documentation
622653 if not ldoc.single then
632663 if not out then
633664 quit('template failed for '..m.name..': '..err)
634665 else
635 writefile(args.dir..kind..'/'..m.name..'.html',out)
666 writefile(args.dir..kind..'/'..m.name..args.ext,out)
636667 end
637668 end
638669 end
0 # LDoc Lua Documentation Tool
0 # LDoc - A Lua Documentation Tool
1
2 Copyright (C) 2011 Steve Donovan.
3
4 ## LDoc as an improved LuaDoc
15
26 LDoc is intended to be compatible with [LuaDoc](http://luadoc.luaforge.net/manual.htm) and thus follows the pattern set by the various *Doc tools:
37
4 --- first function. Some description
8 --- Summary ends with a period.
9 -- Some description, can be over several lines.
510 -- @param p1 first parameter
611 -- @param p2 second parameter
12 -- @return a string value
13 -- @see second_fun
714 function mod1.first_fun(p1,p2)
815 end
916
10 Various tags such as `see` and `usage` are supported, and generally the names of functions and modules can be inferred from the code. The project grew out of the documentation needs of Penlight (and not always getting satisfaction with LuaDoc) and depends on Penlight itself. This allowed me to _not_ write a lot of code.
11
12 LDoc tries to be faithful to LuaDoc, but provides some extensions. Here an alias for 'param' has been defined, and '@function zero_fun' is short for '@class function \ @name zero_fun'.
17 Tags such as `see` and `usage` are supported, and generally the names of functions and modules can be inferred from the code.
18
19 This project grew out of the documentation needs of Penlight (and not always getting satisfaction with LuaDoc) and depends on Penlight itself. This allowed me to _not_ write a lot of code.
20
21 Any claim about 'improvement' needs substantiation. LDoc is designed to give better diagnostics: if a '@see` reference cannot be found, then the line number of the reference is given. LDoc knows about modules which do not use `module()` - this is important since this function has become deprecated in Lua 5.2. And you can avoid having to embed HTML in commments by using Markdown.
22
23 ## Installation
24
25 This should be fairly straightforward; the external dependency is [Penlight](), which in turn needs [LuaFileSystem](). These are already present in Lua for Windows, and Penlight is also available through LuaRocks as 'luarocks install penlight'.
26
27 Unpack the sources somewhere and make an alias to `ldoc.lua` on your path. That is, either an excutable script called 'ldoc' like so:
28
29 lua /path/to/ldoc/ldoc.lua $*
30
31 Or a batch file called 'ldoc.bat':
32
33 @echo off
34 lua \path\to\ldoc\ldoc.lua %*
35
36
37 ## LDoc is Extensible
38
39 LDoc tries to be faithful to LuaDoc, but provides some extensions. '@function zero_fun' is short for the common sequence '@class function \ @name zero_fun'. In general, any type ('function','table',etc) can be used as a tag:
1340
1441 --- zero function. Two new ldoc features here; item types
1542 -- can be used directly as tags, and aliases for tags
16 -- can be defined in config.lp.
43 -- can be defined in config.ld.
1744 -- @function zero_fun
1845 -- @p k1 first
1946 -- @p k2 second
2047
21 If a file `config.lp` is found in the source, then it will be loaded as Lua data. For example:
48 Here an alias for 'param' has been defined. If a file `config.ld` is found in the source, then it will be loaded as Lua data. For example, the configuration for the above module provides a title and defines an alias for 'param':
2249
2350 title = "testmod docs"
2451 project = "testmod"
3562 -- @macro first_macro
3663 -- @see second_function
3764
38 LDoc tries to make it more convenient to organize documentation comments. Instead of:
39
40 --- first table
65 This will also create a new module section called 'Macros'.
66
67 ## Inferring more from Code
68
69 The qualified name of a function will be inferred from any `function` keyword following the doc comment. LDoc goes further with code analysis, however.
70
71 Instead of:
72
73 --- first table.
4174 -- @table one
4275 -- @field A alpha
4376 -- @field B beta
68101 ...
69102 end
70103
104 ## Supporting Extension modules written in C
71105
72106 LDoc can process C/C++ files:
73107
80114 static int l_createtable (lua_State *L) {
81115 ....
82116
83 Both `/**` and `///` are recognized as starting a comment block.
84
117 Both `/**` and `///` are recognized as starting a comment block. Otherwise, the tags are processed in exactly the same way. It is necessary to specify that this is a function with a given name, since this cannot be reliably be inferred from code.
118
119 An unknown extension can be associated with a language using a call like `add_language_extension('lc','c')` in `config.ld`. (Currently the language can only be 'c' or 'lua'.)
120
121 See 'tests/examples/mylib.c' for the full example.
122
123 ## Basic Usage
85124
86125 The command-line options are:
87126
88 ldoc, a Lua documentation generator, vs 0.1 Beta
127 ldoc, a documentation generator for Lua, vs 0.2 Beta
89128 -d,--dir (default docs) output directory
90 -o (default 'index') output name
129 -o,--output (default 'index') output name
91130 -v,--verbose verbose
92131 -q,--quiet suppress output
93132 -m,--module module docs as text
94 -s,--style (default !) directory for templates and style
133 -s,--style (default !) directory for style sheet (ldoc.css)
134 -l,--template (default !) directory for template (ldoc.ltp)
95135 -p,--project (default ldoc) project name
96136 -t,--title (default Reference) page title
97 -f,--format (default plain) formatting - can be markdown
137 -f,--format (default plain) formatting - can be markdown or plain
98138 -b,--package (default .) top-level package basename (needed for module(...))
139 -x,--ext (default html) output file extension
140 --dump debug output dump
99141 <file> (string) source file or directory containing source
100142
101 For example, to process all files in the current directory:
102
103 $ ldoc .
143 For example, to process all files in the 'lua' directory:
144
145 $ ldoc lua
104146 output written to docs/
105147
106 Thereafter the `docs` directory will contain `index.html` which points to individual modules in the `modules` subdirectory. The `--dir` flag can specify where the output is generated, and ensures that the directory exists.
148 Thereafter the `docs` directory will contain `index.html` which points to individual modules in the `modules` subdirectory. The `--dir` flag can specify where the output is generated, and will ensure that the directory exists. The output structure is like LuaDoc: there is an `index.html` and the individual modules are in the `modules` subdirectory.
107149
108150 If your modules use `module(...)` then the module name has to be deduced. If `ldoc` is run from the root of the package, then this deduction does not need any help - e.g. if your package was `foo` then `ldoc foo` will work as expected. If we were actually in the `foo` directory then `ldoc -b .. .` will correctly deduce the module names.
109151
110152 For new-style modules, that don't use `module()`, it is recommended that the module comment has an explicit `@module PACKAGE.NAME`. If it does not, then `ldoc` will still attempt to deduce the module name, but may need help with `--package` as above.
111153
112 It is common to use an alias for the package name with new-style modules. Here an alias is explicitly specified, so that `ldoc` knows that functions qualified with `M` are part of the module:
154 It is common to use an alias for the package name with new-style modules. Here an alias is explicitly specified, so that `ldoc` knows that functions qualified with `M` are part of the module `simple_alias`:
113155
114156 ------------
115157 -- A new-style module.
127169
128170 (Here the actual module name is deduced from the file name, just like with `module(...)`)
129171
130
131
172 A special case is if you simply say 'ldoc .'. Then there _must_ be a `config.ld` file available in the directory, and it can specify the file:
173
174 file = "mymod.lua"
175 title = "mymod documentation"
176 description = "mymod does some simple but useful things"
177
178 `file` can of course point to a directory, just as with the `--file` option. This mode makes it particularly easy for the user to build the documentation, by allowing you to specify everything explicitly in the configuration.
179
180 ## Processing Single Modules
181
182 `--output` can be used to give the output file a different name. This is useful for the special case when a single module file is specified. Here an index would be redundant, so the single HTML file generated contains the module documentation.
183
184 $ ldoc mylib.lua --> results in docs/index.html
185 $ ldoc --output mylib mylib.lua --> results in docs/mylib.html
186 $ ldoc --output mylib --dir html mylib.lua --> results in html/mylib.html
187
188
189 ## Sections
190
191 The default sections used by LDoc are 'Functions', 'Tables' and 'Fields', corresponding to the built-in types 'function', 'table' and 'field'. If `config.ld` contains something like `new_type("macro","Macros")` then this adds a new section 'Macros' which contains items of 'macro' type - 'macro' is registered as a new valid tag name. The default template then presents items under their corresponding section titles, in order of definition.
192
193 New with this release is the idea of _explicit_ sections. The need occurs when a module has a lot of functions that need to be put into logical sections.
194
195 --- File functions.
196 -- Useful utilities for opening foobar format files.
197 -- @section file
198
199 --- open a file
200 ...
201
202 --- read a file
203 ...
204
205 --- Encoding operations.
206 -- Encoding foobar output in different ways.
207 -- @section encoding
208
209 ...
210
211 A section doc-comment has the same structure as a normal doc-comment; the summary is used as the new section title, and the description will be output at the start of the function details for that section.
212
213 In any case, sections appear under 'Contents' on the left-hand side. See the [winapi](http://stevedonovan.github.com/winapi/api.html) documentation for an example of how this looks.
214
215 Arguably a module writer should not write such very long modules, but it is not the job of the documentation tool to limit a programmer.
216
217 ## Dumping and getting Help about a Module
218
219 There is an option to simply dump the results of parsing modules. Consider the C example `tests/example/mylib.c':
220
221 $ ldoc --dump mylib.c
222 ----
223 module: mylib A sample C extension.
224 Demonstrates using ldoc's C/C++ support. Can either use /// or /*** */ etc.
225
226 function createtable(narr, nrec)
227 Create a table with given array and hash slots.
228 narr initial array slots, default 0
229 nrec initial hash slots, default 0
230
231 function solve(a, b, c)
232 Solve a quadratic equation.
233 a coefficient of x^2
234 b coefficient of x
235 c constant
236 return {"first root","second root"}
237
238 This is useful to quickly check for problems; here we see that `createable` did not have a return tag.
239
240 LDoc takes this idea one step further. If used with the `-m` flag it will look up an installed Lua module and parse it. If it has been marked up in LuaDoc-style then you will get a handy summary of the contents:
241
242 $ ldoc -m pl.pretty
243 ----
244 module: pl.pretty Pretty-printing Lua tables.
245 * read(s) - read a string representation of a Lua table.
246 * write(tbl, space, not_clever) - Create a string representation of a Lua table.
247
248 * dump(t, ...) - Dump a Lua table out to a file or stdout.
249
250 You can specify a fully qualified function to get more information:
251
252 $ ldoc -m pl.pretty.write
253
254 function write(tbl, space, not_clever)
255 create a string representation of a Lua table.
256 tbl {table} Table to serialize to a string.
257 space {string} (optional) The indent to use.
258 Defaults to two spaces.
259 not_clever {bool} (optional) Use for plain output, e.g {['key']=1}.
260 Defaults to false.
261
262 ## Generating HTML
263
264 LDoc, like LuaDoc, generates output HTML using a template, in this case `ldoc.ltp`. This is expanded by the powerful but simple preprocessor devised originally by [Rici Lake](http://lua-users.org/wiki/SlightlyLessSimpleLuaPreprocessor) which is now part of Penlight. There are two rules - any line starting with '#' is Lua code, which can also be embedded with '$(...)'.
265
266 <h2>Contents</h2>
267 <ul>
268 # for kind,items in module.kinds() do
269 <li><a href="#$(no_spaces(kind))">$(kind)</a></li>
270 # end
271 </ul>
272
273 This is then styled with `ldoc.css`. Currently the template and stylesheet is very much based on LuaDoc, so the results are equivalent; the main change that the template has been more generalized. The default location (indicated by '!') is the directory of `ldoc.lua`.
274
275 You may customize how you generate your documentation by specifying an alternative style sheet and/or template, which can be deployed with your project. The parameters are `--style` and `--template`, which give the directories where `ldoc.css` and `ldoc.ltp` are to be found. If `config.ld` contains these variables, they are interpreted slightly differently; if they are true, then it means 'use the same directory as config.ld'; otherwise they must be a valid directory relative to the ldoc invocation.
276
277 Of course, there's no reason why LDoc must always generate HTML. `--ext' defines what output extension to use; this can also be set in the configuration file. So it's possible to write a template that converts LDoc output to LaTex, for instance.