extensive readme changes; more config.ld options
This commit is contained in:
parent
fbc283b26c
commit
d2d7d6ac38
2
doc.lua
2
doc.lua
|
@ -324,7 +324,7 @@ function Module:resolve_references(modules)
|
|||
if mod_ref then
|
||||
local name = item_ref and item_ref.name or ''
|
||||
-- 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
|
||||
end
|
||||
item.see:append {mod=mod_ref.name,name=name,label=s}
|
||||
|
|
99
ldoc.lua
99
ldoc.lua
|
@ -16,16 +16,17 @@ app.require_here()
|
|||
local args = lapp [[
|
||||
ldoc, a documentation generator for Lua, vs 0.2 Beta
|
||||
-d,--dir (default docs) output directory
|
||||
--here read parameters from ./config.ld
|
||||
-o,--output (default 'index') output name
|
||||
-v,--verbose verbose
|
||||
-q,--quiet suppress output
|
||||
-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
|
||||
-t,--title (default Reference) page title
|
||||
-f,--format (default plain) formatting - can be markdown or plain
|
||||
-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
|
||||
]]
|
||||
|
@ -442,19 +443,18 @@ if args.module then
|
|||
args.file = fullpath
|
||||
end
|
||||
|
||||
local config_is_read
|
||||
|
||||
if args.file == '.' then
|
||||
-- a special case: 'ldoc .' can get all its parameters from config.ld
|
||||
local dir,err = read_ldoc_config('./'..CONFIG_NAME)
|
||||
if err then quit("no "..CONFIG_NAME.." found here") end
|
||||
config_is_read = true
|
||||
args.file = ldoc.file or '.'
|
||||
if args.file == '.' then
|
||||
args.file = lfs.currentdir()
|
||||
else
|
||||
args.file = path.abspath(args.file)
|
||||
end
|
||||
if args.file == '.' then
|
||||
local err
|
||||
config_dir,err = read_ldoc_config('./'..CONFIG_NAME)
|
||||
if err then quit("no "..CONFIG_NAME.." found here") end
|
||||
config_is_read = true
|
||||
args.file = ldoc.file or '.'
|
||||
if args.file == '.' then
|
||||
args.file = lfs.currentdir()
|
||||
else
|
||||
args.file = path.abspath(args.file)
|
||||
end
|
||||
else
|
||||
args.file = path.abspath(args.file)
|
||||
end
|
||||
|
@ -494,7 +494,7 @@ if path.isdir(args.file) then
|
|||
end)
|
||||
|
||||
-- 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])
|
||||
end
|
||||
|
||||
|
@ -513,11 +513,13 @@ if path.isdir(args.file) then
|
|||
multiple_files = true
|
||||
elseif path.isfile(args.file) then
|
||||
-- a single file may be accompanied by a config.ld in the same dir
|
||||
local config_dir = path.dirname(args.file)
|
||||
if config_dir == '' then config_dir = '.' end
|
||||
local config = path.join(config_dir,CONFIG_NAME)
|
||||
if path.isfile(config) and not config_is_read then
|
||||
read_ldoc_config(config)
|
||||
if not config_dir then
|
||||
config_dir = path.dirname(args.file)
|
||||
if config_dir == '' then config_dir = '.' end
|
||||
local config = path.join(config_dir,CONFIG_NAME)
|
||||
if path.isfile(config) and not config_is_read then
|
||||
read_ldoc_config(config)
|
||||
end
|
||||
end
|
||||
local ext = path.extension(args.file)
|
||||
local ftype = file_types[ext]
|
||||
|
@ -562,23 +564,52 @@ end
|
|||
|
||||
local css, templ = 'ldoc.css','ldoc.ltp'
|
||||
|
||||
-- the style directory for template and stylesheet can be specified
|
||||
-- either by command-line 'style' argument or by 'style' field in
|
||||
-- config.ld. Then it is relative to the location of that file.
|
||||
if ldoc.style then args.style = path.join(config_dir,ldoc.style) end
|
||||
|
||||
-- '!' here means 'use same directory as the ldoc.lua script'
|
||||
if args.style == '!' then
|
||||
args.style = arg[0]:gsub('[^/\\]+$','')
|
||||
local function style_dir (sname)
|
||||
local style = ldoc[sname]
|
||||
local dir
|
||||
if style then
|
||||
if style == true then
|
||||
dir = config_dir
|
||||
elseif type(style) == 'string' and path.isdir(style) then
|
||||
dir = style
|
||||
else
|
||||
quit(tostring(name).." is not a directory")
|
||||
end
|
||||
args[sname] = dir
|
||||
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))
|
||||
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
|
||||
local ok,markup = pcall(require,args.format)
|
||||
|
@ -617,7 +648,7 @@ function generate_output()
|
|||
check_file(args.dir..css, path.join(args.style,css))
|
||||
|
||||
-- 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
|
||||
if not ldoc.single then
|
||||
|
@ -633,7 +664,7 @@ function generate_output()
|
|||
if not out then
|
||||
quit('template failed for '..m.name..': '..err)
|
||||
else
|
||||
writefile(args.dir..kind..'/'..m.name..'.html',out)
|
||||
writefile(args.dir..kind..'/'..m.name..args.ext,out)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
180
readme.md
180
readme.md
|
@ -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:
|
||||
|
||||
--- first function. Some description
|
||||
--- Summary ends with a period.
|
||||
-- Some description, can be over several lines.
|
||||
-- @param p1 first parameter
|
||||
-- @param p2 second parameter
|
||||
-- @return a string value
|
||||
-- @see second_fun
|
||||
function mod1.first_fun(p1,p2)
|
||||
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
|
||||
-- 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
|
||||
-- @p k1 first
|
||||
-- @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"
|
||||
project = "testmod"
|
||||
|
@ -36,9 +63,15 @@ And then used as any other tag:
|
|||
-- @macro first_macro
|
||||
-- @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
|
||||
-- @field A alpha
|
||||
-- @field B beta
|
||||
|
@ -69,6 +102,7 @@ Simularly, function parameter comments can be directly used:
|
|||
...
|
||||
end
|
||||
|
||||
## Supporting Extension modules written in C
|
||||
|
||||
LDoc can process C/C++ files:
|
||||
|
||||
|
@ -81,36 +115,44 @@ LDoc can process C/C++ files:
|
|||
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:
|
||||
|
||||
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
|
||||
-o (default 'index') output name
|
||||
-o,--output (default 'index') output name
|
||||
-v,--verbose verbose
|
||||
-q,--quiet suppress output
|
||||
-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
|
||||
-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(...))
|
||||
-x,--ext (default html) output file extension
|
||||
--dump debug output dump
|
||||
<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/
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
@ -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(...)`)
|
||||
|
||||
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.
|
||||
|
|
Loading…
Reference in New Issue