extensive readme changes; more config.ld options

This commit is contained in:
steve donovan 2011-06-10 11:44:12 +02:00
parent fbc283b26c
commit d2d7d6ac38
3 changed files with 229 additions and 52 deletions

View File

@ -324,7 +324,7 @@ function Module:resolve_references(modules)
if mod_ref then if mod_ref then
local name = item_ref and item_ref.name or '' local name = item_ref and item_ref.name or ''
-- this is deeply hacky; classes have 'Class ' prepended. -- this is deeply hacky; classes have 'Class ' prepended.
if item_ref.type == 'type' then if item_ref and item_ref.type == 'type' then
name = 'Class_'..name name = 'Class_'..name
end end
item.see:append {mod=mod_ref.name,name=name,label=s} item.see:append {mod=mod_ref.name,name=name,label=s}

View File

@ -16,16 +16,17 @@ app.require_here()
local args = lapp [[ local args = lapp [[
ldoc, a documentation generator for Lua, vs 0.2 Beta ldoc, a documentation generator for Lua, vs 0.2 Beta
-d,--dir (default docs) output directory -d,--dir (default docs) output directory
--here read parameters from ./config.ld
-o,--output (default 'index') output name -o,--output (default 'index') output name
-v,--verbose verbose -v,--verbose verbose
-q,--quiet suppress output -q,--quiet suppress output
-m,--module module docs as text -m,--module module docs as text
-s,--style (default !) directory for templates and style -s,--style (default !) directory for style sheet (ldoc.css)
-l,--template (default !) directory for template (ldoc.ltp)
-p,--project (default ldoc) project name -p,--project (default ldoc) project name
-t,--title (default Reference) page title -t,--title (default Reference) page title
-f,--format (default plain) formatting - can be markdown or plain -f,--format (default plain) formatting - can be markdown or plain
-b,--package (default .) top-level package basename (needed for module(...)) -b,--package (default .) top-level package basename (needed for module(...))
-x,--ext (default html) output file extension
--dump debug output dump --dump debug output dump
<file> (string) source file or directory containing source <file> (string) source file or directory containing source
]] ]]
@ -442,19 +443,18 @@ if args.module then
args.file = fullpath args.file = fullpath
end end
local config_is_read
if args.file == '.' then
-- a special case: 'ldoc .' can get all its parameters from config.ld -- a special case: 'ldoc .' can get all its parameters from config.ld
local dir,err = read_ldoc_config('./'..CONFIG_NAME) if args.file == '.' then
if err then quit("no "..CONFIG_NAME.." found here") end local err
config_is_read = true config_dir,err = read_ldoc_config('./'..CONFIG_NAME)
args.file = ldoc.file or '.' if err then quit("no "..CONFIG_NAME.." found here") end
if args.file == '.' then config_is_read = true
args.file = lfs.currentdir() args.file = ldoc.file or '.'
else if args.file == '.' then
args.file = path.abspath(args.file) args.file = lfs.currentdir()
end else
args.file = path.abspath(args.file)
end
else else
args.file = path.abspath(args.file) args.file = path.abspath(args.file)
end end
@ -494,7 +494,7 @@ if path.isdir(args.file) then
end) end)
-- finding more than one should probably be a warning... -- finding more than one should probably be a warning...
if #config_files > 0 and not config_is_read then if #config_files > 0 and not config_dir then
config_dir = read_ldoc_config(config_files[1]) config_dir = read_ldoc_config(config_files[1])
end end
@ -513,11 +513,13 @@ if path.isdir(args.file) then
multiple_files = true multiple_files = true
elseif path.isfile(args.file) then elseif path.isfile(args.file) then
-- a single file may be accompanied by a config.ld in the same dir -- a single file may be accompanied by a config.ld in the same dir
local config_dir = path.dirname(args.file) if not config_dir then
if config_dir == '' then config_dir = '.' end config_dir = path.dirname(args.file)
local config = path.join(config_dir,CONFIG_NAME) if config_dir == '' then config_dir = '.' end
if path.isfile(config) and not config_is_read then local config = path.join(config_dir,CONFIG_NAME)
read_ldoc_config(config) if path.isfile(config) and not config_is_read then
read_ldoc_config(config)
end
end end
local ext = path.extension(args.file) local ext = path.extension(args.file)
local ftype = file_types[ext] local ftype = file_types[ext]
@ -562,23 +564,52 @@ end
local css, templ = 'ldoc.css','ldoc.ltp' local css, templ = 'ldoc.css','ldoc.ltp'
-- the style directory for template and stylesheet can be specified local function style_dir (sname)
-- either by command-line 'style' argument or by 'style' field in local style = ldoc[sname]
-- config.ld. Then it is relative to the location of that file. local dir
if ldoc.style then args.style = path.join(config_dir,ldoc.style) end if style then
if style == true then
-- '!' here means 'use same directory as the ldoc.lua script' dir = config_dir
if args.style == '!' then elseif type(style) == 'string' and path.isdir(style) then
args.style = arg[0]:gsub('[^/\\]+$','') dir = style
else
quit(tostring(name).." is not a directory")
end
args[sname] = dir
end
end end
local function override (field)
if ldoc[field] then args[field] = ldoc[field] end
end
-- the directories for template and stylesheet can be specified
-- either by command-line '--template','--style' arguments or by 'template and
-- 'style' fields in config.ld.
-- The assumption here is that if these variables are simply true then the directory
-- containing config.ld contains a ldoc.css and a ldoc.ltp respectively. Otherwise
-- they must be a valid subdirectory.
style_dir 'style'
style_dir 'template'
-- can specify format, output, dir and ext in config.ld
override 'format'
override 'output'
override 'dir'
override 'ext'
args.ext = '.'..args.ext
-- '!' here means 'use same directory as ldoc.lua
local ldoc_dir = arg[0]:gsub('[^/\\]+$','')
if args.style == '!' then args.style = ldoc_dir end
if args.template == '!' then args.template = ldoc_dir end
local module_template,err = utils.readfile (path.join(args.style,templ)) local module_template,err = utils.readfile (path.join(args.style,templ))
if not module_template then quit(err) end if not module_template then quit(err) end
-- can specify format, output and dir in config.ld
if ldoc.format then args.format = ldoc.format end
if ldoc.output then args.output = ldoc.output end
if ldoc.dir then args.dir = ldoc.dir end
if args.format ~= 'plain' then if args.format ~= 'plain' then
local ok,markup = pcall(require,args.format) local ok,markup = pcall(require,args.format)
@ -617,7 +648,7 @@ function generate_output()
check_file(args.dir..css, path.join(args.style,css)) check_file(args.dir..css, path.join(args.style,css))
-- write out the module index -- write out the module index
writefile(args.dir..args.output..'.html',out) writefile(args.dir..args.output..args.ext,out)
-- write out the per-module documentation -- write out the per-module documentation
if not ldoc.single then if not ldoc.single then
@ -633,7 +664,7 @@ function generate_output()
if not out then if not out then
quit('template failed for '..m.name..': '..err) quit('template failed for '..m.name..': '..err)
else else
writefile(args.dir..kind..'/'..m.name..'.html',out) writefile(args.dir..kind..'/'..m.name..args.ext,out)
end end
end end
end end

180
readme.md
View File

@ -1,25 +1,52 @@
# LDoc Lua Documentation Tool # LDoc - A Lua Documentation Tool
Copyright (C) 2011 Steve Donovan.
## LDoc as an improved LuaDoc
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: 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:
--- first function. Some description --- Summary ends with a period.
-- Some description, can be over several lines.
-- @param p1 first parameter -- @param p1 first parameter
-- @param p2 second parameter -- @param p2 second parameter
-- @return a string value
-- @see second_fun
function mod1.first_fun(p1,p2) function mod1.first_fun(p1,p2)
end end
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. Tags such as `see` and `usage` are supported, and generally the names of functions and modules can be inferred from the code.
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'. 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.
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.
## Installation
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'.
Unpack the sources somewhere and make an alias to `ldoc.lua` on your path. That is, either an excutable script called 'ldoc' like so:
lua /path/to/ldoc/ldoc.lua $*
Or a batch file called 'ldoc.bat':
@echo off
lua \path\to\ldoc\ldoc.lua %*
## LDoc is Extensible
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:
--- zero function. Two new ldoc features here; item types --- zero function. Two new ldoc features here; item types
-- can be used directly as tags, and aliases for tags -- can be used directly as tags, and aliases for tags
-- can be defined in config.lp. -- can be defined in config.ld.
-- @function zero_fun -- @function zero_fun
-- @p k1 first -- @p k1 first
-- @p k2 second -- @p k2 second
If a file `config.lp` is found in the source, then it will be loaded as Lua data. For example: 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':
title = "testmod docs" title = "testmod docs"
project = "testmod" project = "testmod"
@ -36,9 +63,15 @@ And then used as any other tag:
-- @macro first_macro -- @macro first_macro
-- @see second_function -- @see second_function
LDoc tries to make it more convenient to organize documentation comments. Instead of: This will also create a new module section called 'Macros'.
--- first table ## Inferring more from Code
The qualified name of a function will be inferred from any `function` keyword following the doc comment. LDoc goes further with code analysis, however.
Instead of:
--- first table.
-- @table one -- @table one
-- @field A alpha -- @field A alpha
-- @field B beta -- @field B beta
@ -69,6 +102,7 @@ Simularly, function parameter comments can be directly used:
... ...
end end
## Supporting Extension modules written in C
LDoc can process C/C++ files: LDoc can process C/C++ files:
@ -81,36 +115,44 @@ LDoc can process C/C++ files:
static int l_createtable (lua_State *L) { static int l_createtable (lua_State *L) {
.... ....
Both `/**` and `///` are recognized as starting a comment block. 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.
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'.)
See 'tests/examples/mylib.c' for the full example.
## Basic Usage
The command-line options are: The command-line options are:
ldoc, a Lua documentation generator, vs 0.1 Beta ldoc, a documentation generator for Lua, vs 0.2 Beta
-d,--dir (default docs) output directory -d,--dir (default docs) output directory
-o (default 'index') output name -o,--output (default 'index') output name
-v,--verbose verbose -v,--verbose verbose
-q,--quiet suppress output -q,--quiet suppress output
-m,--module module docs as text -m,--module module docs as text
-s,--style (default !) directory for templates and style -s,--style (default !) directory for style sheet (ldoc.css)
-l,--template (default !) directory for template (ldoc.ltp)
-p,--project (default ldoc) project name -p,--project (default ldoc) project name
-t,--title (default Reference) page title -t,--title (default Reference) page title
-f,--format (default plain) formatting - can be markdown -f,--format (default plain) formatting - can be markdown or plain
-b,--package (default .) top-level package basename (needed for module(...)) -b,--package (default .) top-level package basename (needed for module(...))
-x,--ext (default html) output file extension
--dump debug output dump
<file> (string) source file or directory containing source <file> (string) source file or directory containing source
For example, to process all files in the current directory: For example, to process all files in the 'lua' directory:
$ ldoc . $ ldoc lua
output written to docs/ output written to docs/
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. 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.
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. 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.
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. 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.
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: 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`:
------------ ------------
-- A new-style module. -- A new-style module.
@ -128,5 +170,109 @@ It is common to use an alias for the package name with new-style modules. Here a
(Here the actual module name is deduced from the file name, just like with `module(...)`) (Here the actual module name is deduced from the file name, just like with `module(...)`)
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:
file = "mymod.lua"
title = "mymod documentation"
description = "mymod does some simple but useful things"
`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.
## Processing Single Modules
`--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.
$ ldoc mylib.lua --> results in docs/index.html
$ ldoc --output mylib mylib.lua --> results in docs/mylib.html
$ ldoc --output mylib --dir html mylib.lua --> results in html/mylib.html
## Sections
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.
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.
--- File functions.
-- Useful utilities for opening foobar format files.
-- @section file
--- open a file
...
--- read a file
...
--- Encoding operations.
-- Encoding foobar output in different ways.
-- @section encoding
...
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.
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.
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.
## Dumping and getting Help about a Module
There is an option to simply dump the results of parsing modules. Consider the C example `tests/example/mylib.c':
$ ldoc --dump mylib.c
----
module: mylib A sample C extension.
Demonstrates using ldoc's C/C++ support. Can either use /// or /*** */ etc.
function createtable(narr, nrec)
Create a table with given array and hash slots.
narr initial array slots, default 0
nrec initial hash slots, default 0
function solve(a, b, c)
Solve a quadratic equation.
a coefficient of x^2
b coefficient of x
c constant
return {"first root","second root"}
This is useful to quickly check for problems; here we see that `createable` did not have a return tag.
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:
$ ldoc -m pl.pretty
----
module: pl.pretty Pretty-printing Lua tables.
* read(s) - read a string representation of a Lua table.
* write(tbl, space, not_clever) - Create a string representation of a Lua table.
* dump(t, ...) - Dump a Lua table out to a file or stdout.
You can specify a fully qualified function to get more information:
$ ldoc -m pl.pretty.write
function write(tbl, space, not_clever)
create a string representation of a Lua table.
tbl {table} Table to serialize to a string.
space {string} (optional) The indent to use.
Defaults to two spaces.
not_clever {bool} (optional) Use for plain output, e.g {['key']=1}.
Defaults to false.
## Generating HTML
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 '$(...)'.
<h2>Contents</h2>
<ul>
# for kind,items in module.kinds() do
<li><a href="#$(no_spaces(kind))">$(kind)</a></li>
# end
</ul>
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`.
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.
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.