Menu

Documentation Tools

Relevant source files

Purpose and Scope

This document describes the automated documentation generation system in Basalt2, which extracts structured comments from Lua source files and converts them into markdown reference documentation. The system consists of two main components: the BasaltDoc module for parsing and generating documentation, and generate-docs.lua for orchestrating the generation process across the file system.

For information about the broader build system that invokes these tools, see page 7.1. For type definition generation for IDE support, see page 7.4. For the configuration metadata extraction system, see page 7.2.

Documentation System Overview

The documentation system extracts specially-formatted comments from source files and generates structured markdown documentation. This process is fully automated through the GitHub Actions pipeline and supports both ComputerCraft and standard Lua environments.

Documentation Generation Architecture

Sources: tools/generate-docs.lua1-163

Comment Annotation Format

Documentation is embedded in source files using special LuaDoc-style comments. The parser recognizes two comment types:

  • Triple-dash comments (---): LuaDoc annotations with type information
  • Double-dash comments (--): Descriptive text

Recognized Annotation Types

AnnotationPurposeExample
@classDefine a class and its inheritance---@class Tree : VisualElement
@propertyDocument a property with type and default---@property nodes table {} The tree structure
@paramDocument function parameter---@param node table The node to expand
@returnDocument return value---@return Tree self The Tree instance
@functionMark as a function (inferred from code)function Tree:expandNode(node)
@shortDescriptionBrief one-line description---@shortDescription Expands a node
@usageCode example---@usage tree:expandNode(rootNode)
@eventDocument an event---@event node_select {node} Fired when selected
@fieldDocument a field---@field terminal table The terminal object
@combinedPropertyProperty that combines multiple properties---@combinedProperty size {width, height}
@privateMark next function as private (exclude from docs)---@private
@protectedMark function as overridable---@protected
@splitClassOutput class to separate file---@splitClass
@titleSection title---@title Core Functions

Sources: tools/markdown.lua5-23

Example: Annotated Source File

Sources: src/elements/Tree.lua4-69

BasaltDoc Parsing Architecture

The BasaltDoc module provides parsing and generation capabilities. It processes source files line-by-line, maintaining state across comment blocks to build an abstract syntax tree representation of the documentation.

Comment Parsing State Machine

Parsing Process Flow

Sources: tools/generate-docs.lua131

Markdown Generation Pipeline

The BasaltDoc.generateMarkdown(ast) function transforms the parsed AST into formatted markdown documentation. It processes classes and their members to generate structured output.

Class Documentation Structure

Each class generates a markdown document with the following sections in order:

Property Table Generation

Property tables are generated from @property annotations:

Generated from source annotations:

Sources: src/elements/Tree.lua13-27

Function Documentation Generation

Function blocks include signature, description, parameters, returns, and usage:

 Generated from annotations: ```lua --- Expands a node --- @shortDescription Expands a node to show its children --- @param node table The node to expand --- @return Tree self The Tree instance function Tree:expandNode(node) 

Sources: src/elements/Tree.lua61-69

Documentation Generator Script

The generate-docs.lua script orchestrates the entire documentation generation process. It provides a file system abstraction layer for cross-platform compatibility between ComputerCraft and standard Lua environments.

File System Abstraction Layer

The script detects the runtime environment and provides a unified fileSystem interface:

Standard Lua Shell Command Implementation

For standard Lua, file system operations use shell commands via io.popen:

FunctionShell CommandPurpose
pathExists(path)[ -e 'path' ]Check if path exists
isDirectory(path)[ -d 'path' ]Check if path is directory
makeDirectory(path)mkdir -p 'path'Create directory recursively
listDirectory(dir)ls -1 'dir'List directory contents

Sources: tools/generate-docs.lua9-86

Documentation Generation Process

The main generation loop processes all Lua files in the source directory:

Sources: tools/generate-docs.lua88-163

Key Functions and Variables

SymbolTypePurposeLocation
argVariableCommand-line argumentstools/generate-docs.lua1
SRC_DIRVariableSource directory path (default: "src")tools/generate-docs.lua2
OUT_DIRVariableOutput directory path (default: "build_docs/docs/references")tools/generate-docs.lua3
BasaltDocModuleDocumentation parsing and generationtools/generate-docs.lua7
fileSystemTableUnified file system APItools/generate-docs.lua9-86
getLuaFiles(dir)FunctionRecursively find all .lua filestools/generate-docs.lua94-114

getLuaFiles Implementation

The recursive file scanner:

Sources: tools/generate-docs.lua1-114

File Organization

The generator mirrors the source directory structure in the output:

src/ ├── elements/ │ ├── Tree.lua → build_docs/docs/references/elements/Tree.md │ └── Button.lua → build_docs/docs/references/elements/Button.md ├── plugins/ │ └── animation.lua → build_docs/docs/references/plugins/animation.md └── main.lua → build_docs/docs/references/main.md 

The path transformation logic:

Sources: tools/generate-docs.lua128-129

Split Class Documentation

The @splitClass annotation allows a single source file to generate multiple output files, useful for plugin files that extend multiple classes. When processing a file with split classes, the generator creates separate markdown files for each class extension.

For example, a plugin file that adds methods to both BaseElement and Container can use:

This generates separate documentation files:

  • pluginname_BaseElement.md for BaseElement extensions
  • pluginname_Container.md for Container extensions

Sources: Referenced in annotation system documentation

Error Handling and Validation

The documentation generator includes several validation mechanisms:

Empty Content Detection

The generator validates content at multiple stages:

The generator handles markdown output as either string or table:

Sources: tools/generate-docs.lua124-150

File Access Error Handling

File operations include error checking with informative messages:

Sources: tools/generate-docs.lua119-160

Integration with Build Pipeline

The documentation generator is invoked by the GitHub Actions workflow during the build process. It runs as part of the automated build pipeline alongside configuration generation, type definition generation, and bundling.

GitHub Actions Integration

The workflow invokes the generator with explicit directory arguments:

Build Pipeline Position

The generated documentation is:

  1. Written to build_docs/docs/references/
  2. Committed to the repository
  3. Deployed to GitHub Pages
  4. Made available at the project's documentation site

Sources: Referenced in build system architecture, tools/generate-docs.lua2-3

Usage Examples

Running Locally

Command-Line Arguments

The script accepts two optional arguments:

ArgumentDefaultPurpose
arg[1]"src"Source directory to scan for .lua files
arg[2]"build_docs/docs/references"Output directory for generated markdown

Sources: tools/generate-docs.lua1-3

Directory Structure Mirroring

The generator maintains the source directory structure in the output:

src/elements/Tree.lua → build_docs/docs/references/elements/Tree.md src/elements/Button.lua → build_docs/docs/references/elements/Button.md src/plugins/animation.lua → build_docs/docs/references/plugins/animation.md src/main.lua → build_docs/docs/references/main.md 

Path transformation logic:

Sources: tools/generate-docs.lua128-129

Output Format Examples

Generated Property Table

From property annotations in src/elements/Tree.lua13-31 the generator produces:

Generated Function Documentation

From function annotations in src/elements/Tree.lua61-69 the generator produces:

Generated Event Table

From event definitions in src/elements/Tree.lua33-34:

Sources: src/elements/Tree.lua13-34

Summary

The documentation system provides automated extraction and generation of API documentation from annotated source files. Key components include:

  • markdown.lua: Parser and generator for LuaDoc-style comments
  • generate-docs.lua: Orchestrator with cross-platform file system support
  • Annotation system: Structured comments using @class, @property, @param, @return, etc.
  • Output organization: Mirrors source structure in markdown files
  • Split class support: Multiple output files from single source files
  • Build integration: Automated through GitHub Actions workflow

The system ensures documentation stays synchronized with code by extracting it directly from source files during the build process.

Sources: tools/generate-docs.lua1-163 tools/markdown.lua1-533