Do not bundle default system_config (prefer built-in defaults)
[ardour.git] / tools / fmt-luadoc.php
index 683216fe0d3e010201876998626dad9ba993b3c2..b13e38617caf77f2b7c31a75ca67ba958f20d006 100755 (executable)
 # php tools/fmt-luadoc.php > /tmp/luadoc.html
 #
 
+$options = getopt("m");
+if (isset ($options['m'])) {
+       $HTMLOUTPUT = false; ## set to false to output ardour-manual
+} else {
+       $HTMLOUTPUT = true; ## set to false to output ardour-manual
+}
+
 ################################################################################
 ################################################################################
 
 $json = gzdecode (file_get_contents (dirname (__FILE__).'/../doc/luadoc.json.gz'));
 $doc = array ();
+$ardourversion = '';
 foreach (json_decode ($json, true) as $b) {
-       if (!isset ($b['type'])) { continue; }
+       if (!isset ($b['type'])) {
+               if (isset ($b['version'])) { $ardourversion = $b['version']; }
+               continue;
+       }
+       # reserved lua words
+       $b ['lua'] = preg_replace ('/:_end/', ':end', $b ['lua']);
+       $b ['lua'] = preg_replace ('/:_type/', ':type', $b ['lua']);
        $b ['ldec'] = preg_replace ('/ const/', '', preg_replace ('/ const&/', '', $b['decl']));
+       $b ['ldec'] = preg_replace ('/_VampHost::/', '', $b['ldec']);
+       $b ['decl'] = preg_replace ('/_VampHost::/', '', $b['decl']);
        if (isset ($b['ret'])) {
                $b['ret'] = preg_replace ('/ const/', '', preg_replace ('/ const&/', '', $b['ret']));
+               $b['ret'] = preg_replace ('/_VampHost::/', '', $b['ret']);
+       }
+       if (isset ($b['parent'])) {
+               $b ['parent'] = preg_replace ('/_VampHost::/', '', $b['parent']);
        }
        $doc[] = $b;
 }
@@ -77,11 +97,15 @@ function arg2lua ($argtype, $flags = 0) {
        $arg = preg_replace ('/ $/', '', $arg);
 
        # filter out basic types
-       $builtin = array ('float', 'double', 'bool', 'std::string', 'int', 'long', 'unsigned long', 'unsigned int', 'unsigned char', 'char', 'void', 'char*', 'unsigned char*', 'void*');
+       $builtin = array ('float', 'double', 'bool', 'std::string', 'int', 'short', 'long', 'unsigned int', 'unsigned short', 'unsigned long', 'unsigned char', 'char', 'void', 'char*', 'unsigned char*', 'void*');
        if (in_array ($arg, $builtin)) {
                return array ($arg => $flags);
        }
 
+       if ($arg == 'luabridge::LuaRef') {
+               return array ('Lua-Function' => $flags | 4);
+       }
+
        # check Class declarations first
        foreach (array_merge ($classes, $consts) as $b) {
                if ($b['ldec'] == $arg) {
@@ -114,13 +138,22 @@ function stripclass ($classname, $name) {
 function datatype ($decl) {
        # TODO handle spaces in type. Works because
        # we don't yet have templated types (with_space <here >)
-       return substr ($decl, 0, strpos ($decl, ' '));
+       return substr ($decl, 0, strrpos ($decl, ' '));
 }
 
 function luafn2class ($lua) {
        return substr ($lua, 0, strrpos ($lua, ':'));
 }
 
+function luafn2name ($lua) {
+       $fn = strrpos ($lua, ':');
+       if ($fn !== 0 && strlen($lua) > $fn + 1) {
+               return substr ($lua, $fn + 1);
+       }
+       my_die ('invalid class prefix: '. $name);
+}
+
+
 function checkclass ($b) {
        global $classlist;
        if (!isset ($classlist[luafn2class ($b['lua'])])) {
@@ -155,8 +188,12 @@ function canonical_ctor ($b) {
 
 function canonical_decl ($b) {
        $rv = '';
+       $pfx = '';
        # match clang's declatation format
        if (preg_match('/[^(]*\(([^)*]*)\*\)\((.*)\)/', $b['decl'], $matches)) {
+               if (strpos ($b['type'], 'Free Function') !== false) {
+                       $pfx = str_replace (':', '::', luafn2class ($b['lua'])) . '::';
+               }
                $fn = substr ($b['lua'], 1 + strrpos ($b['lua'], ':'));
                $rv = $matches[1] . $fn . '(';
                $arglist = preg_split ('/, */', $matches[2]);
@@ -165,16 +202,19 @@ function canonical_decl ($b) {
                        if (!$first) { $rv .= ', '; }; $first = false;
                        if (empty ($a)) { continue; }
                        $a = preg_replace ('/([^>]) >/', '$1>', $a);
-                       $a = preg_replace ('/^.*::/', '', $a);
+                       $a = preg_replace ('/^Cairo::/', '', $a); // special case cairo enums
                        $a = preg_replace ('/([^ ])&/', '$1 &', $a);
+                       $a = preg_replace ('/std::vector<([^>]*)> const/', 'const std::vector<$1>', $a);
+                       $a = str_replace ('std::vector', 'vector', $a);
                        $a = str_replace ('vector', 'std::vector', $a);
+                       $a = str_replace ('std::string', 'string', $a);
                        $a = str_replace ('string const', 'const string', $a);
                        $a = str_replace ('string', 'std::string', $a);
                        $rv .= $a;
                }
                $rv .= ')';
        }
-       return $rv;
+       return $pfx . $rv;
 }
 
 ################################################################################
@@ -230,7 +270,24 @@ foreach ($doc as $b) {
                $classlist[luafn2class ($b['lua'])]['ctor'][] = array (
                        'name' => luafn2class ($b['lua']),
                        'args' => decl2args ($b['ldec']),
-                       'cand' => canonical_ctor ($b)
+                       'cand' => canonical_ctor ($b),
+                       'nil' => false
+               );
+               break;
+       case "Weak/Shared Pointer NIL Constructor":
+               checkclass ($b);
+               $classlist[luafn2class ($b['lua'])]['ctor'][] = array (
+                       'name' => luafn2class ($b['lua']),
+                       'args' => decl2args ($b['ldec']),
+                       'cand' => canonical_ctor ($b),
+                       'nil' => true
+               );
+               break;
+       case "Property":
+               checkclass ($b);
+               $classlist[luafn2class ($b['lua'])]['props'][] = array (
+                       'name' => $b['lua'],
+                       'ret'  => arg2lua (datatype ($b['ldec']))
                );
                break;
        case "Data Member":
@@ -240,6 +297,26 @@ foreach ($doc as $b) {
                        'ret'  => arg2lua (datatype ($b['ldec']))
                );
                break;
+       case "Static C Function":
+               checkclass ($b);
+               if (strpos ($b['lua'], 'ARDOUR:DataType:') === 0) {
+                       # special case ARDOUR:DataType convenience c'tor
+                       $args = array ();
+                       $ret = array (luafn2class ($b['lua']) => 0);
+                       $canon = 'ARDOUR::LuaAPI::datatype_ctor_'.strtolower (luafn2name ($b['lua'])).'(lua_State*)';
+               } else {
+                       my_die ('unhandled Static C: ' . print_r($b, true));
+               }
+               $classlist[luafn2class ($b['lua'])]['func'][] = array (
+                       'bind' => $b,
+                       'name' => $b['lua'],
+                       'args' => $args,
+                       'ret'  => $ret,
+                       'ref'  => false,
+                       'ext'  => false,
+                       'cand' => $canon
+               );
+               break;
        case "C Function":
                # we required C functions to be in a class namespace
        case "Ext C Function":
@@ -248,9 +325,8 @@ foreach ($doc as $b) {
                $ret = array ('...' => 0);
                $ns = luafn2class ($b['lua']);
                $cls = $classlist[$ns];
-               ## std::Vector std::List types
                if (preg_match ('/.*<([^>]*)[ ]*>/', $cls['ldec'], $templ)) {
-                       // XXX -> move to C-source
+                       # std::vector, std::list types
                        switch (stripclass($ns, $b['lua'])) {
                        case 'add':
                                #$args = array (array ('LuaTable {'.$templ[1].'}' => 0));
@@ -269,6 +345,7 @@ foreach ($doc as $b) {
                                break;
                        }
                } else if (strpos ($cls['type'], ' Array') !== false) {
+                       # catches  C:FloatArray, C:IntArray
                        $templ = preg_replace ('/[&*]*$/', '', $cls['ldec']);
                        switch (stripclass($ns, $b['lua'])) {
                        case 'array':
@@ -297,6 +374,17 @@ foreach ($doc as $b) {
                        'cand' => canonical_decl ($b)
                );
                break;
+       case "Free C Function":
+               $funclist[luafn2class ($b['lua'])][] = array (
+                       'bind' => $b,
+                       'name' => $b['lua'],
+                       'args' => $args,
+                       'ret'  => $ret,
+                       'ref'  => false,
+                       'ext'  => true,
+                       'cand' => str_replace (':', '::', $b['lua']).'(lua_State*)'
+               );
+               break;
        case "Free Function":
        case "Free Function RefReturn":
                $funclist[luafn2class ($b['lua'])][] = array (
@@ -314,7 +402,6 @@ foreach ($doc as $b) {
        case "Weak/Shared Pointer Function":
        case "Weak/Shared Pointer Function RefReturn":
        case "Weak/Shared Null Check":
-       case "Weak/Shared Pointer Cast":
        case "Static Member Function":
                checkclass ($b);
                $classlist[luafn2class ($b['lua'])]['func'][] = array (
@@ -326,6 +413,18 @@ foreach ($doc as $b) {
                        'cand' => canonical_decl ($b)
                );
                break;
+       case "Cast":
+       case "Weak/Shared Pointer Cast":
+               checkclass ($b);
+               $classlist[luafn2class ($b['lua'])]['cast'][] = array (
+                       'bind' => $b,
+                       'name' => $b['lua'],
+                       'args' => decl2args ($b['ldec']),
+                       'ret'  => arg2lua ($b['ret']),
+                       'ref'  => (strpos ($b['type'], "RefReturn") !== false),
+                       'cand' => canonical_decl ($b)
+               );
+               break;
        case "Constant/Enum":
        case "Constant/Enum Member":
                # already handled -> $consts
@@ -345,14 +444,17 @@ foreach ($doc as $b) {
 foreach ($classlist as $ns => $cl) {
        if (strpos ($cl['type'], ' Array') !== false) {
                $classlist[$ns]['arr'] = true;
+               $classlist[$ns]['cdecl'] = $cl['decl'];
                continue;
        }
        foreach ($classes as $c) {
                if ($c['lua'] == $ns) {
                        if (strpos ($c['type'], 'Pointer Class') !== false) {
                                $classlist[$ns]['ptr'] = true;
-                               $classlist[$ns]['decl'] = 'boost::shared_ptr< '.$c['decl']. ' >, boost::weak_ptr< '.$c['decl']. ' >';
+                               $classlist[$ns]['cdecl'] = 'boost::shared_ptr< '.$c['decl']. ' >, boost::weak_ptr< '.$c['decl']. ' >';
                                break;
+                       } else {
+                               $classlist[$ns]['cdecl'] = $c['decl'];
                        }
                }
        }
@@ -387,8 +489,10 @@ ksort ($classlist);
 #### -- split here --  ####
 
 # from here on, only $classlist and $constlist arrays are relevant.
+# we also pull in C++ header annotation from doxygen to $api
+
 
-# read function documentation from doxygen
+# read documentation from doxygen
 $json = gzdecode (file_get_contents (dirname (__FILE__).'/../doc/ardourapi.json.gz'));
 $api = array ();
 foreach (json_decode ($json, true) as $a) {
@@ -398,8 +502,11 @@ foreach (json_decode ($json, true) as $a) {
        $api[$canon] = $a;
 }
 
+# keep track of found/missing doc
 $dox_found = 0;
 $dox_miss = 0;
+
+# retrive a value from $api
 function doxydoc ($canonical_declaration) {
        global $api;
        global $dox_found;
@@ -408,14 +515,18 @@ function doxydoc ($canonical_declaration) {
                $dox_found++;
                return $api[$canonical_declaration]['doc'];
        }
-       elseif (isset ($api['ARDOUR::'.$canonical_declaration])) {
+       // remove template namespace e.g.
+       //  "ARDOUR::Track::bounceable(boost::shared_ptr<ARDOUR::Processor>"
+       //  "ARDOUR::Track::bounceable(boost::shared_ptr<Processor>"
+       $cn = preg_replace ('/<[^>]*::([^>]*)>/', '<$1>', $canonical_declaration);
+       if (isset ($api[$cn])) {
                $dox_found++;
-               return $api['ARDOUR::'.$canonical_declaration]['doc'];
-       }
-       else {
-               $dox_miss++;
-               return '';
+               return $api[$cn]['doc'];
        }
+       #fwrite (STDERR, $canonical_declaration."\n"); # XXX DEBUG
+
+       $dox_miss++;
+       return '';
 }
 
 ################################################################################
@@ -427,22 +538,22 @@ function doxydoc ($canonical_declaration) {
 # Helper functions
 define ('NL', "\n");
 
+# constructors, enums (constants) use a dot.  (e.g. "LuaOSC.Address" -> "LuaOSC.Address" )
 function ctorname ($name) {
        return htmlentities (str_replace (':', '.', $name));
 }
 
+# strip class prefix (e.g "Evoral:MidiEvent:channel"  -> "channel")
 function shortname ($name) {
        return htmlentities (substr ($name, strrpos ($name, ':') + 1));
 }
 
+# retrieve variable name from    array["VARNAME"] => FLAGS
 function varname ($a) {
        return array_keys ($a)[0];
 }
 
-function name_sort_cb ($a, $b) {
-       return strcmp ($a['name'], $b['name']);
-}
-
+# recusively collect class parents (derived classes)
 function traverse_parent ($ns, &$inherited) {
        global $classlist;
        $rv = '';
@@ -451,6 +562,7 @@ function traverse_parent ($ns, &$inherited) {
                asort ($parents);
                foreach ($parents as $p) {
                        if (!empty ($rv)) { $rv .= ', '; }
+                       if ($p == $ns) { continue; }
                        $rv .= typelink ($p);
                        $inherited[$p] = $classlist[$p];
                        traverse_parent ($p, $inherited);
@@ -459,11 +571,11 @@ function traverse_parent ($ns, &$inherited) {
        return $rv;
 }
 
+# create a cross-reference to a type (class or enum)
+# *all* <a> links are generated here, currently anchors on a single page.
 function typelink ($a, $short = false, $argcls = '', $linkcls = '', $suffix = '') {
        global $classlist;
        global $constlist;
-       # all cross-reference links are generated here.
-       # currently anchors on a single page.
        if (isset($classlist[$a]['free'])) {
                return '<a class="'.$linkcls.'" href="#'.htmlentities ($a).'">'.($short ? shortname($a) : ctorname($a)).$suffix.'</a>';
        } else if (in_array ($a, array_keys ($classlist))) {
@@ -475,13 +587,17 @@ function typelink ($a, $short = false, $argcls = '', $linkcls = '', $suffix = ''
        }
 }
 
+# output format function arguments
 function format_args ($args) {
        $rv = '<span class="functionargs"> (';
        $first = true;
        foreach ($args as $a) {
                if (!$first) { $rv .= ', '; }; $first = false;
                $flags = $a[varname ($a)];
-               if ($flags & 2) {
+               if ($flags & 4) {
+                       $rv .= '<span>'.varname ($a).'</span>';
+               }
+               else if ($flags & 2) {
                        $rv .= '<em>LuaTable</em> {'.typelink (varname ($a), true, 'em').'}';
                }
                elseif ($flags & 1) {
@@ -495,6 +611,7 @@ function format_args ($args) {
        return $rv;
 }
 
+# format doxygen documentation for class-definition
 function format_doxyclass ($cl) {
        $rv = '';
        if (isset ($cl['decl'])) {
@@ -506,6 +623,7 @@ function format_doxyclass ($cl) {
        return $rv;
 }
 
+# format doxygen documentation for class-members
 function format_doxydoc ($f) {
        $rv = '';
        if (isset ($f['cand'])) {
@@ -520,19 +638,38 @@ function format_doxydoc ($f) {
        }
        return $rv;
 }
+
+# usort() callback for class-members
+function name_sort_cb ($a, $b) {
+       return strcmp ($a['name'], $b['name']);
+}
+
+# main output function for every class
 function format_class_members ($ns, $cl, &$dups) {
        $rv = '';
+       # print contructor - if any
        if (isset ($cl['ctor'])) {
                usort ($cl['ctor'], 'name_sort_cb');
                $rv.= ' <tr><th colspan="3">Constructor</th></tr>'.NL;
                foreach ($cl['ctor'] as $f) {
-                       $rv.= ' <tr><td class="def">&Copf;</td><td class="decl">';
+                       $rv.= ' <tr>';
+                       if ($f['nil']) {
+                               $rv.= '<td class="def"><abbr title="Nil Pointer Constructor">&alefsym;</abbr></td>';
+                       } else {
+                               $rv.= '<td class="def">&Copf;</td>';
+                       }
+                       $rv.= '<td class="decl">';
                        $rv.= '<span class="functionname">'.ctorname ($f['name']).'</span>';
                        $rv.= format_args ($f['args']);
                        $rv.= '</td><td class="fill"></td></tr>'.NL;
+                       # doxygen documentation (may be empty)
                        $rv.= format_doxydoc($f);
                }
        }
+
+       # strip duplicates (inherited or derived methods)
+       # e.g  AudioTrack -> Track -> Route -> SessionObject -> Stateful
+       # all 5 have "isnil()"
        $nondups = array ();
        if (isset ($cl['func'])) {
                foreach ($cl['func'] as $f) {
@@ -540,30 +677,66 @@ function format_class_members ($ns, $cl, &$dups) {
                        $nondups[] = $f;
                }
        }
+
+       # print methods - if any
        if (count ($nondups) > 0) {
                usort ($nondups, 'name_sort_cb');
                $rv.= ' <tr><th colspan="3">Methods</th></tr>'.NL;
                foreach ($nondups as $f) {
                        $dups[] = stripclass ($ns, $f['name']);
+                       # return value/type
                        $rv.= ' <tr><td class="def">';
                        if ($f['ref'] && isset ($f['ext'])) {
                                # external C functions
                                $rv.= '<em>'.varname ($f['ret']).'</em>';
                        } elseif ($f['ref'] && varname ($f['ret']) == 'void') {
-                               # functions with reference args return args
+                               # void functions with reference args
                                $rv.= '<em>LuaTable</em>(...)';
                        } elseif ($f['ref']) {
+                               # functions with reference args and return value
                                $rv.= '<em>LuaTable</em>('.typelink (varname ($f['ret']), true, 'em').', ...)';
                        } else {
+                               # normal class members
                                $rv.= typelink (varname ($f['ret']), true, 'em');
                        }
+                       # function declaration and arguments
+                       $rv.= '</td><td class="decl">';
+                       $rv.= '<span class="functionname"><abbr title="'.htmlentities($f['bind']['decl']).'">'.stripclass ($ns, $f['name']).'</abbr></span>';
+                       $rv.= format_args ($f['args']);
+                       $rv.= '</td><td class="fill"></td></tr>'.NL;
+                       # doxygen documentation (may be empty)
+                       $rv.= format_doxydoc($f);
+               }
+       }
+       # print cast - if any
+       if (isset ($cl['cast'])) {
+               usort ($cl['cast'], 'name_sort_cb');
+               $rv.= ' <tr><th colspan="3">Cast</th></tr>'.NL;
+               foreach ($cl['cast'] as $f) {
+                       $rv.= ' <tr><td class="def">';
+                       $rv.= typelink (varname ($f['ret']), true, 'em');
+                       # function declaration and arguments
                        $rv.= '</td><td class="decl">';
                        $rv.= '<span class="functionname"><abbr title="'.htmlentities($f['bind']['decl']).'">'.stripclass ($ns, $f['name']).'</abbr></span>';
                        $rv.= format_args ($f['args']);
                        $rv.= '</td><td class="fill"></td></tr>'.NL;
+                       # doxygen documentation (may be empty)
                        $rv.= format_doxydoc($f);
                }
        }
+
+       # print properties - if any
+       if (isset ($cl['props'])) {
+               usort ($cl['props'], 'name_sort_cb');
+               $rv.= ' <tr><th colspan="3">Properties</th></tr>'.NL;
+               foreach ($cl['props'] as $f) {
+                       $rv.= ' <tr><td class="def">'.typelink (array_keys ($f['ret'])[0], false, 'em').'</td><td class="decl">';
+                       $rv.= '<span class="functionname">'.stripclass ($ns, $f['name']).'</span>';
+                       $rv.= '</td><td class="fill"></td></tr>'.NL;
+               }
+       }
+
+       # print data members - if any
        if (isset ($cl['data'])) {
                usort ($cl['data'], 'name_sort_cb');
                $rv.= ' <tr><th colspan="3">Data Members</th></tr>'.NL;
@@ -571,6 +744,8 @@ function format_class_members ($ns, $cl, &$dups) {
                        $rv.= ' <tr><td class="def">'.typelink (array_keys ($f['ret'])[0], false, 'em').'</td><td class="decl">';
                        $rv.= '<span class="functionname">'.stripclass ($ns, $f['name']).'</span>';
                        $rv.= '</td><td class="fill"></td></tr>'.NL;
+                       $f['cand'] = str_replace (':', '::', $f['name']);
+                       $rv.= format_doxydoc($f);
                }
        }
        return $rv;
@@ -580,53 +755,63 @@ function format_class_members ($ns, $cl, &$dups) {
 ################################################################################
 # Start Output
 
+if ($HTMLOUTPUT) {
+
 ?><!DOCTYPE html>
 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
 <head>
 <title>Ardour Lua Bindings</title>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <style type="text/css">
-div.content        { max-width:60em; margin: 1em auto; }
-h1                 { margin:2em 0 0 0; padding:0em; border-bottom: 1px solid black;}
-h2.cls             { margin:2em 0 0 0; padding-left:1em; border: 1px dashed #6666ee;}
-h2.cls abbr        { text-decoration:none; cursor:default;}
-h3.cls             { margin:1em 0 0 0;}
-h2.class           { background-color: #aaee66; }
-h2.enum            { background-color: #aaaaaa; }
-h2.pointerclass    { background-color: #eeaa66; }
-h2.array           { background-color: #66aaee; }
-h2.opaque          { background-color: #6666aa; }
-p.cdecl            { text-align: right; float:right; font-size:90%; margin:0; padding: 0 0 0 1em;}
-ul.classindex      { columns: 2; -webkit-columns: 2; -moz-columns: 2; }
-div.clear          { clear:both; }
-p.classinfo        { margin: .25em 0;}
-div.classdox       { padding: .1em 1em;}
-div.classdox p     { margin: .5em 0 .5em .6em;}
-div.classdox p     { margin: .5em 0 .5em .6em;}
-div.classdox       { padding: .1em 1em;}
-div.classdox p     { margin: .5em 0 .5em .6em;}
-table.classmembers { width: 100%; }
-table.classmembers th      { text-align:left; border-bottom:1px solid black; padding-top:1em; }
-table.classmembers td.def  { text-align:right; padding-right:.5em;  white-space: nowrap;}
-table.classmembers td.decl { text-align:left; padding-left:.5em; white-space: nowrap; }
-table.classmembers td.doc  { text-align:left; padding-left:.6em; line-height: 1.2em; font-size:80%;}
-table.classmembers td.doc div.dox {background-color:#ddd; padding: .1em 1em;}
-table.classmembers td.doc p { margin: .5em 0; }
-table.classmembers td.doc p.para-brief { font-size:120%; }
-table.classmembers td.doc p.para-returns { font-size:120%; }
-table.classmembers td.doc dl { font-size:120%; line-height: 1.3em; }
-table.classmembers td.doc dt { font-style: italic; }
-table.classmembers td.fill { width: 99%;}
-table.classmembers span.em { font-style: italic;}
-span.functionname abbr     { text-decoration:none; cursor:default;}
-div.header         {text-align:center;}
-div.header h1      {margin:0;}
-div.header p       {margin:.25em;}
+div.header         { text-align:center; }
+div.header h2      { margin:0; }
+div.header p       { margin:.25em; text-align:center; }
+div.luafooter      { text-align:center; font-size:80%; color: #888; margin: 2em 0; }
+#luaref            { max-width:60em; margin: 1em auto; }
+
+#luaref h2                 { margin:2em 0 0 0; padding:0em; border-bottom: 1px solid black; }
+#luaref h3.cls             { margin:2em 0 0 0; padding: 0 0 0 1em; border: 1px dashed #6666ee; }
+#luaref h3.cls abbr        { text-decoration:none; cursor:default; }
+#luaref h4.cls             { margin:1em 0 0 0; }
+#luaref h3.class           { background-color: #aaee66; }
+#luaref h3.enum            { background-color: #aaaaaa; }
+#luaref h3.pointerclass    { background-color: #eeaa66; }
+#luaref h3.array           { background-color: #66aaee; }
+#luaref h3.opaque          { background-color: #6666aa; }
+#luaref p                  { text-align: justify; }
+#luaref p.cdecl            { text-align: right; float:right; font-size:90%; margin:0; padding: 0 0 0 1em; }
+#luaref ul.classindex      { columns: 2; -webkit-columns: 2; -moz-columns: 2; }
+#luaref div.clear          { clear:both; }
+#luaref p.classinfo        { margin: .25em 0; }
+#luaref div.code           { width:80%; margin:.5em auto; }
+#luaref div.code div       { width:45%; }
+#luaref div.code pre       { line-height: 1.2em; margin: .25em 0; }
+#luaref div.code samp      { color: green; font-weight: bold; background-color: #eee; }
+#luaref div.classdox       { padding: .1em 1em; }
+#luaref div.classdox p     { margin: .5em 0 .5em .6em; }
+#luaref div.classdox p     { margin: .5em 0 .5em .6em; }
+#luaref div.classdox       { padding: .1em 1em; }
+#luaref div.classdox p     { margin: .5em 0 .5em .6em; }
+#luaref table.classmembers { width: 100%; }
+#luaref table.classmembers th      { text-align:left; border-bottom:1px solid black; padding-top:1em; }
+#luaref table.classmembers td.def  { text-align:right; padding-right:.5em;  white-space: nowrap; }
+#luaref table.classmembers td.decl { text-align:left; padding-left:.5em; white-space: nowrap; }
+#luaref table.classmembers td.doc  { text-align:left; padding-left:.6em; line-height: 1.2em; font-size:80%; }
+#luaref table.classmembers td.doc div.dox {background-color:#eee; padding: .1em 1em; }
+#luaref table.classmembers td.doc p { margin: .5em 0; }
+#luaref table.classmembers td.doc p.para-brief { font-size:120%; }
+#luaref table.classmembers td.doc p.para-returns { font-size:120%; }
+#luaref table.classmembers td.doc dl { font-size:120%; line-height: 1.3em; }
+#luaref table.classmembers td.doc dt { font-style: italic; }
+#luaref table.classmembers td.fill { width: 99%; }
+#luaref table.classmembers span.em { font-style: italic; }
+#luaref span.functionname abbr     { text-decoration:none; cursor:default; }
+#luaref table.classmembers td.def abbr { text-decoration:none; cursor:default; }
 </style>
 </head>
 <body>
 <div class="header">
-<h1>Ardour Lua Bindings</h1>
+<h2>Ardour Lua Bindings</h2>
 <p>
 <a href="#h_classes">Class Documentation</a>
 &nbsp;|&nbsp;
@@ -635,9 +820,33 @@ div.header p       {margin:.25em;}
 <a href="#h_index">Index</a>
 </p>
 </div>
-<div class="content">
 
-<h1 id="h_intro">Overview</h1>
+<!-- #### SNIP #### !-->
+
+<?php
+
+} else {
+
+?>
+
+<p class="warning">
+This documentation is far from complete may be inaccurate and subject to change.
+</p>
+
+<?php
+}
+?>
+
+<div id="luaref">
+
+<?php
+
+################################################################################
+# some general documentation -- should really go elsehere
+
+?>
+
+<h2 id="h_intro">Overview</h2>
 <p>
 The top-level entry point are <?=typelink('ARDOUR:Session')?> and <?=typelink('ArdourUI:Editor')?>.
 Most other Classes are used indirectly starting with a Session function. e.g. Session:get_routes().
@@ -650,7 +859,7 @@ A few classes are dedicated to certain script types, e.g. Lua DSP processors hav
 <p>
 Detailed documentation (parameter names, method description) is not yet available. Please stay tuned.
 </p>
-<h2>Short introduction to Ardour classes</h2>
+<h3>Short introduction to Ardour classes</h3>
 <p>
 Ardour's structure is object oriented. The main object is the Session. A Session contains Audio Tracks, Midi Tracks and Busses.
 Audio and Midi tracks are derived from a more general "Track" Object,  which in turn is derived from a "Route" (aka Bus).
@@ -659,40 +868,135 @@ Tracks contain specifics. For Example a track <em>has-a</em> diskstream (for fil
 </p>
 <p>
 Operations are performed on objects. One gets a reference to an object and then calls a method.
-e.g   obj = Session:route_by_name("Audio")   obj:set_name("Guitar")
+e.g <code>obj = Session:route_by_name("Audio")   obj:set_name("Guitar")</code>.
+</p>
+<p>
+Lua automatically follows C++ class inheritance. e.g one can directly call all SessionObject and Route methods on Track object. However lua does not automatically promote objects. A Route object which just happens to be a Track needs to be explicily cast to a Track. Methods for casts are provided with each class. Note that the cast may fail and return a <em>nil</em> reference.
+</p>
+<p>
+Likewise multiple inheritance is a <a href="http://www.lua.org/pil/16.3.html">non-trivial issue</a> in lua. To avoid performance penalties involved with lookups, explicit casts are required in this case. One example is <?=typelink('ARDOUR:SessionObject')?> which is-a StatefulDestructible which inhertis from both Stateful and Destructible.
 </p>
 <p>
 Object lifetimes are managed by the Session. Most Objects cannot be directly created, but one asks the Session to create or destroy them. This is mainly due to realtime constrains:
 you cannot simply remove a track that is currently processing audio. There are various <em>factory</em> methods for object creation or removal.
 </p>
+<h3>Pass by Reference</h3>
+<p>
+Since lua functions are closures, C++ methods that pass arguments by reference cannot be used as-is.
+All parameters passed to a C++ method which uses references are returned as Lua Table.
+If the C++ method also returns a value it is prefixed. Two parameters are returned: the value and a Lua Table holding the parameters.
+</p>
+
+<div class="code">
+       <div style="float:left;">C++
+
+<pre><code class="cxx">void set_ref (int&amp; var, long&amp; val)
+{
+       printf ("%d %ld\n", var, val);
+       var = 5;
+       val = 7;
+}
+</code></pre>
+
+       </div>
+       <div style="float:right;">Lua
+
+<pre><code class="lua">local var = 0;
+ref = set_ref (var, 2);
+-- output from C++ printf()
+</code><samp class="lua">0 2</samp><code>
+-- var is still 0 here
+print (ref[1], ref[2])
+</code><samp class="lua">5 7</samp></pre>
+
+       </div>
+</div>
+<div class="clear"></div>
+<div class="code">
+       <div style="float:left;">
+
+<pre><code class="cxx">int set_ref2 (int &amp;var, std::string unused)
+{
+       var = 5;
+       return 3;
+}
+</code></pre>
+
+       </div>
+       <div style="float:right;">
+<pre><code class="lua">rv, ref = set_ref2 (0, "hello");
+print (rv, ref[1], ref[2])
+</code><samp class="lua">3 5 hello</samp></pre>
+       </div>
+</div>
+<div class="clear"></div>
+
+<h3>Pointer Classes</h3>
+<p>
+Libardour makes extensive use of reference counted <code>boost::shared_ptr</code> to manage lifetimes.
+The Lua bindings provide a complete abstration of this. There are no pointers in lua.
+For example a <?=typelink('ARDOUR:Route')?> is a pointer in C++, but lua functions operate on it like it was a class instance.
+</p>
+<p>
+<code>shared_ptr</code> are reference counted. Once assigned to a lua variable, the C++ object will be kept and remains valid.
+It is good practice to assign references to lua <code>local</code> variables or reset the variable to <code>nil</code> to drop the ref.
+</p>
+<p>
+All pointer classes have a <code>isnil ()</code> method. This is for two cases:
+Construction may fail. e.g. <code><?=typelink('ARDOUR:LuaAPI')?>.newplugin()</code>
+may not be able to find the given plugin and hence cannot create an object.
+</p>
+<p>
+The second case if for <code>boost::weak_ptr</code>. As opposed to <code>boost::shared_ptr</code> weak-pointers are not reference counted.
+The object may vanish at any time.
+If lua code calls a method on a nil object, the interpreter will raise an exception and the script will not continue.
+This is not unlike <code>a = nil a:test()</code> which results in en error "<em>attempt to index a nil value</em>".
+</p>
+<p>
+From the lua side of things there is no distinction between weak and shared pointers. They behave identically.
+Below they're inidicated in orange and have an arrow to indicate the pointer type.
+Pointer Classes cannot be created in lua scripts. It always requires a call to C++ to create the Object and obtain a reference to it.
+</p>
+
 
 <?php
-echo '<h1 id="h_classes">Class Documentation</h1>'.NL;
 
+#################################
+# Main output function -- Classes
+
+echo '<h2 id="h_classes">Class Documentation</h2>'.NL;
 foreach ($classlist as $ns => $cl) {
        $dups = array ();
        $tbl =  format_class_members ($ns, $cl, $dups);
 
-       # format class title
+       # format class title - depending on type
        if (empty ($tbl)) {
-               echo '<h2 id="'.htmlentities ($ns).'" class="cls opaque"><abbr title="Opaque Object">&empty;</abbr>&nbsp;'.htmlentities ($ns).'</h2>'.NL;
+               # classes with no members (no ctor, no methods, no data)
+               echo '<h3 id="'.htmlentities ($ns).'" class="cls opaque"><abbr title="Opaque Object">&empty;</abbr>&nbsp;'.htmlentities ($ns).'</h3>'.NL;
        }
        else if (isset ($classlist[$ns]['free'])) {
-               echo '<h2 id="'.htmlentities ($ns).'" class="cls freeclass"><abbr title="Namespace">&Nopf;</abbr>&nbsp;'.ctorname($ns).'</h2>'.NL;
+               # free functions (no class)
+               echo '<h3 id="'.htmlentities ($ns).'" class="cls freeclass"><abbr title="Namespace">&Nopf;</abbr>&nbsp;'.ctorname($ns).'</h3>'.NL;
        }
        else if (isset ($classlist[$ns]['arr'])) {
-               echo '<h2 id="'.htmlentities ($ns).'" class="cls array"><abbr title="C Array">&ctdot;</abbr>&nbsp;'.htmlentities ($ns).'</h2>'.NL;
+               # C Arrays
+               echo '<h3 id="'.htmlentities ($ns).'" class="cls array"><abbr title="C Array">&ctdot;</abbr>&nbsp;'.htmlentities ($ns).'</h3>'.NL;
        }
        else if (isset ($classlist[$ns]['ptr'])) {
-               echo '<h2 id="'.htmlentities ($ns).'" class="cls pointerclass"><abbr title="Pointer Class">&Rarr;</abbr>&nbsp;'. htmlentities ($ns).'</h2>'.NL;
-       } else {
-               echo '<h2 id="'.htmlentities ($ns).'" class="cls class"><abbr title="Class">&comp;</abbr>&nbsp;'.htmlentities ($ns).'</h2>'.NL;
+               # Pointer Classes
+               echo '<h3 id="'.htmlentities ($ns).'" class="cls pointerclass"><abbr title="Pointer Class">&Rarr;</abbr>&nbsp;'. htmlentities ($ns).'</h3>'.NL;
        }
-       if (isset ($cl['decl'])) {
-               echo '<p class="cdecl"><em>C&#8225;</em>: '.htmlentities ($cl['decl']).'</p>'.NL;
+       else {
+               # Normal Class
+               echo '<h3 id="'.htmlentities ($ns).'" class="cls class"><abbr title="Class">&comp;</abbr>&nbsp;'.htmlentities ($ns).'</h3>'.NL;
+       }
+
+       # show original C++ declaration
+       if (isset ($cl['cdecl'])) {
+               echo '<p class="cdecl"><em>C&#8225;</em>: '.htmlentities ($cl['cdecl']).'</p>'.NL;
        }
 
-       # print class inheritance
+       # print class inheritance (direct parent *name* only)
        $inherited = array ();
        $isa = traverse_parent ($ns, $inherited);
        if (!empty ($isa)) {
@@ -700,6 +1004,8 @@ foreach ($classlist as $ns => $cl) {
        }
        echo '<div class="clear"></div>'.NL;
 
+
+       # class documentation (if any)
        echo format_doxyclass ($cl);
 
        # member documentation
@@ -711,11 +1017,11 @@ foreach ($classlist as $ns => $cl) {
                echo ' </table>'.NL;
        }
 
-       # traverse parent classes (inherited members)
+       # traverse parent classes (all inherited members)
        foreach ($inherited as $pns => $pcl) {
                $tbl = format_class_members ($pns, $pcl, $dups);
                if (!empty ($tbl)) {
-                       echo '<h3 class="cls">Inherited from '.$pns.'</h3>'.NL;
+                       echo '<h4 class="cls">Inherited from '.$pns.'</h4>'.NL;
                        echo '<table class="classmembers">'.NL;
                        echo $tbl;
                        echo '</table>'.NL;
@@ -723,9 +1029,12 @@ foreach ($classlist as $ns => $cl) {
        }
 }
 
-echo '<h1 id="h_enum">Enum/Constants</h1>'.NL;
+####################
+# Enum and Constants
+
+echo '<h2 id="h_enum">Enum/Constants</h2>'.NL;
 foreach ($constlist as $ns => $cs) {
-       echo '<h2 id="'.ctorname ($ns).'" class="cls enum"><abbr title="Enum">&isin;</abbr>&nbsp;'.ctorname ($ns).'</h2>'.NL;
+       echo '<h3 id="'.ctorname ($ns).'" class="cls enum"><abbr title="Enum">&isin;</abbr>&nbsp;'.ctorname ($ns).'</h3>'.NL;
        echo '<ul class="enum">'.NL;
        foreach ($cs as $c) {
                echo '<li class="const">'.ctorname ($c['lua']).'</li>'.NL;
@@ -733,15 +1042,28 @@ foreach ($constlist as $ns => $cs) {
        echo '</ul>'.NL;
 }
 
-echo '<h1 id="h_index" >Class Index</h1>'.NL;
+######################
+# Index of all classes
+
+echo '<h2 id="h_index" >Class Index</h2>'.NL;
 echo '<ul class="classindex">'.NL;
 foreach ($classlist as $ns => $cl) {
        echo '<li>'.typelink($ns).'</li>'.NL;
 }
 echo '</ul>'.NL;
 
+
+# see how far there is still to go...
 fwrite (STDERR, "Found $dox_found annotations. missing: $dox_miss\n");
+echo '<!-- '.$dox_found.' / '.$dox_miss.' !-->'.NL;
+
 ?>
 </div>
-</body>
-</html>
+<div class="luafooter">Ardour <?=$ardourversion?> &nbsp;-&nbsp; <?=date('r')?></div>
+<?php
+
+if ($HTMLOUTPUT) {
+       echo '<!-- #### SNIP #### !-->'.NL;
+       echo '</body>'.NL;
+       echo '</html>'.NL;
+}