Corpus Extensions
Use an extension to rewrite metadata across many symbols at once: backfill briefs from a naming convention, tag symbols by group, mark generated code as "see below" in the output. Extensions run between extraction and rendering, so every generator sees the change.
Languages and addon locations
Extensions are user-supplied scripts dropped under <addons>/extensions/ with the .js or .lua extension. The addon roots come from the addons and addons-supplemental options, the same roots that hold the Handlebars templates. Mr.Docs aggregates scripts across every root and runs them in alphabetical order by full path, with the two languages interleaved.
|
Both scripting languages reach the same
|
Accessing the corpus
A script extends Mr.Docs by defining a function named transform_corpus(corpus). Mr.Docs calls it once per loaded script with a flat read-only view of the corpus. A script that doesn’t define transform_corpus is silently ignored at this step.
-
JavaScript
-
Lua
addons/extensions/noop.jsUnresolved include directive in modules/ROOT/pages/extensions/corpus-extensions.adoc - include::example$snippets/extensions/entry-point/addons/extensions/noop.js[]
addons/extensions/noop.luaUnresolved include directive in modules/ROOT/pages/extensions/corpus-extensions.adoc - include::example$snippets/extensions/entry-point/addons/extensions/noop.lua[]
The corpus object provides functions that expose the symbol graph. The corpus.symbols field is a flat array containing every extracted symbol. Scripts that need queries like "all members of `X`" simply walk the array and filter.
For instance, the following scripts count the symbols of each kind and report the totals at the end of the run:
-
JavaScript
-
Lua
addons/extensions/count_by_kind.jsfunction transform_corpus(corpus) {
var counts = {};
for (var i = 0; i < corpus.symbols.length; ++i) {
var k = corpus.symbols[i].kind;
counts[k] = (counts[k] || 0) + 1;
}
for (var k in counts) {
console.log(k + ": " + counts[k]);
}
}
addons/extensions/count_by_kind.luafunction transform_corpus(corpus)
local counts = {}
for _, sym in ipairs(corpus.symbols) do
counts[sym.kind] = (counts[sym.kind] or 0) + 1
end
for k, v in pairs(counts) do
print(k .. ": " .. v)
end
end
Each entry in corpus.symbols is a proxy for a live Mr.Docs symbol. The fields of each object are at the DOM reference.
When a script knows a symbol’s id and needs to act on that one symbol:
-
corpus.get(id)returns the proxy for it ornullif the id is unknown -
corpus.lookup(name)does a global-namespace name lookup and returns the proxy (ornull)
subclass-tree.cppUnresolved include directive in modules/ROOT/pages/extensions/corpus-extensions.adoc - include::example$snippets/extensions/subclass-tree/subclass-tree.cpp[]
-
JavaScript
-
Lua
addons/extensions/subclass_tree.jsUnresolved include directive in modules/ROOT/pages/extensions/corpus-extensions.adoc - include::example$snippets/extensions/subclass-tree/addons/extensions/subclass_tree.js[]
addons/extensions/subclass_tree.luaUnresolved include directive in modules/ROOT/pages/extensions/corpus-extensions.adoc - include::example$snippets/extensions/subclass-tree/addons/extensions/subclass_tree.lua[]
Running either script against the fixture above prints:
Shape
Circle
Polygon
Quadrilateral
Square
Triangle
Modifying the corpus
Scripts modify the corpus by assigning to fields on a symbol proxy. Each assignment lands directly in the underlying Mr.Docs symbol. The runtime validates each assignment and raises an exception on an invalid value. An uncaught error in an extension aborts the build and includes the script’s path and the error message.
Most extensions read some attribute of a symbol and write back a string, an enumerator, or a small structured value. For instance, a codebase that follows "any function whose name starts with is_<name> is a predicate returning true if its <name> holds" encodes information that the brief could repeat verbatim:
brief-from-name.cppUnresolved include directive in modules/ROOT/pages/extensions/corpus-extensions.adoc - include::example$snippets/extensions/brief-from-name/brief-from-name.cpp[]
-
JavaScript
-
Lua
addons/extensions/brief_from_name.jsUnresolved include directive in modules/ROOT/pages/extensions/corpus-extensions.adoc - include::example$snippets/extensions/brief-from-name/addons/extensions/brief_from_name.js[]
addons/extensions/brief_from_name.luaUnresolved include directive in modules/ROOT/pages/extensions/corpus-extensions.adoc - include::example$snippets/extensions/brief-from-name/addons/extensions/brief_from_name.lua[]
Every is_foo_bar function then ships with "Returns true if foo bar." Authors only have to write a brief when the synthesized one is not the right one.
Unresolved include directive in modules/ROOT/pages/extensions/corpus-extensions.adoc - include::example$snippets/extensions/brief-from-name/brief-from-name.adoc[]
Cross-linking Symbols
When the value being written needs to reference another symbol, the second symbol’s id is what makes the link clickable in the rendered output rather than a plain string.
For instance, consider a project where the parse_X and format_X free functions are symmetric. A reader landing on one almost always wants to see the other. The extension builds a name → id index in one pass, then walks it again to look up each partner:
parse-format-relates.cppUnresolved include directive in modules/ROOT/pages/extensions/corpus-extensions.adoc - include::example$snippets/extensions/parse-format-relates/parse-format-relates.cpp[]
-
JavaScript
-
Lua
addons/extensions/parse_format_relates.jsUnresolved include directive in modules/ROOT/pages/extensions/corpus-extensions.adoc - include::example$snippets/extensions/parse-format-relates/addons/extensions/parse_format_relates.js[]
addons/extensions/parse_format_relates.luaUnresolved include directive in modules/ROOT/pages/extensions/corpus-extensions.adoc - include::example$snippets/extensions/parse-format-relates/addons/extensions/parse_format_relates.lua[]
Unresolved include directive in modules/ROOT/pages/extensions/corpus-extensions.adoc - include::example$snippets/extensions/parse-format-relates/parse-format-relates.adoc[]
The two-pass shape (index, then look up) is the idiom whenever a write needs to refer to a symbol the script hasn’t yet seen during the walk.
|
Notice in this example that |