Architecture
This guide explains the internal architecture of sphinxcontrib-matlabdomain for developers who want to contribute or understand how it works.
Project Overview
sphinxcontrib-matlabdomain is a Sphinx domain extension that enables automatic documentation generation from MATLAB source code, similar to Sphinx’s Python autodoc extension.
Key Features:
Auto-document MATLAB classes, functions, properties, and methods from source files
Integration with Sphinx documentation framework
Support for MATLAB-specific syntax and conventions
Compatible with Napoleon for Google/NumPy docstring formats
Works with both
.mfiles and.mlappapplication files
Project Stats:
Language: Python 3.8+
Main Dependencies: Sphinx (4.0+), tree-sitter (MATLAB parser)
Package:
sphinxcontrib-matlabdomainRepository: https://github.com/sphinx-contrib/matlabdomain
Core Components
The extension consists of six main Python modules:
matlab.py - Main Domain (958 lines)
Purpose: Entry point that registers the MATLAB domain with Sphinx.
Key Responsibilities:
Defines roles (
:class:,:func:,:meth:,:prop:)Registers directives (
.. automodule::,.. autoclass::, etc.)Manages cross-references and linking
Handles domain setup and reference resolution
Main Classes:
MatObject- Base class for all MATLAB object descriptionsMatDomain- The MATLAB domain itselfVarious directive classes for manual documentation
mat_types.py - Type System
Purpose: Represents MATLAB code elements as Python objects.
Classes:
MatObject- Base class for all MATLAB entitiesMatModule- Represents a package or folder of MATLAB codeMatClass- MATLAB class (handles attributes, inheritance)MatFunction- Function file (.mfile)MatMethod- Class methodMatProperty- Class propertyMatScript- MATLAB script fileMatApplication- MATLAB app (.mlappfile)MatEnumeration- MATLAB enumeration classMatModuleAnalyzer- Scans filesystem for MATLAB codeMatException- Exception class
Key Features:
Parses docstrings into description/parameters/returns/examples
Handles MATLAB-specific attributes (class properties, methods)
Manages hierarchical relationships (package/class/method)
File system scanning and module discovery
mat_tree_sitter_parser.py - AST Parser
Purpose: Uses tree-sitter to parse MATLAB syntax trees.
Classes:
MatClassParser- Parses class definitionsMatFunctionParser- Parses function definitionsMatScriptParser- Parses script files
Features:
Extracts function signatures and arguments
Identifies class methods and properties
Parses docstrings and comments
Handles MATLAB syntax specifics (e.g., ellipsis continuation)
Provides fast, accurate parsing using tree-sitter
mat_documenters.py - Documenter Classes
Purpose: Sphinx autodoc-style documenters that extract documentation from MATLAB files.
Classes:
MatlabDocumenter- Base classModuleDocumenter- Handles MATLAB packages/modulesClassDocumenter- Documents MATLAB classesFunctionDocumenter- Documents functionsPropertyDocumenter- Documents class propertiesMethodDocumenter- Documents methodsApplicationDocumenter- Documents MATLAB appsEnumerationDocumenter- Documents enumerations
Each documenter:
Finds MATLAB source files
Parses them using mat_tree_sitter_parser
Generates reStructuredText documentation
Handles cross-references to other entities
mat_directives.py - Autodoc Directives
Purpose: Extends Sphinx’s autodoc directive system.
Provides These Directives:
.. automodule::- Auto-document MATLAB packages.. autoclass::- Auto-document MATLAB classes.. autofunction::- Auto-document functions.. autoproperty::- Auto-document properties.. automethod::- Auto-document methods.. autoenum::- Auto-document enumerations.. autoapp::- Auto-document MATLAB apps
mat_lexer.py - Syntax Highlighting
Purpose: Provides MATLAB code syntax highlighting for Sphinx HTML output.
Features:
Tokenizes MATLAB code
Maps MATLAB syntax to Pygments tokens
Used by
.. code-block:: matlabin documentation
Data Flow
Understanding how documentation is generated:
1. User Setup
User configures conf.py:
extensions = ['sphinxcontrib.matlab']
matlab_src_dir = '/path/to/matlab/src'
2. Sphinx Build Starts
Sphinx reads configuration
Loads extensions including sphinxcontrib.matlab
matlab.py registers the domain, roles, and directives
3. Module Discovery
When matlab_src_dir is set:
MatModuleAnalyzerscans the directory treeFinds all
.mand.mlappfilesBuilds a map of available modules, classes, and functions
4. Directive Processing
When Sphinx encounters .. autofunction:: myfunction:
The appropriate documenter (e.g.,
FunctionDocumenter) is invokedDocumenter locates the source file in
matlab_src_dirFile is read and passed to parser
5. Parsing
mat_tree_sitter_parser.py processes the source:
Creates an Abstract Syntax Tree (AST)
Extracts function signature, parameters, return values
Extracts docstring comments
Identifies code structure
6. Type Creation
Parser output creates MatType objects:
MatFunctionwith signature and docstringProperties filled: name, args, returns, description
Relationships established (e.g., method → class)
7. Documentation Generation
Documenter generates reStructuredText:
Formats function signature
Converts docstring to reST
Adds cross-references for known entities
Returns formatted documentation
8. Cross-Reference Resolution
MatDomain resolves references like :func:`myfunction`:
Looks up entity in domain’s inventory
Creates clickable links in HTML output
Handles short names if
matlab_short_links = True
9. HTML Generation
Sphinx converts reST to HTML
mat_lexer.pyprovides syntax highlighting for code blocksFinal HTML documentation is generated
File System Organization
Source Code Layout
sphinxcontrib/
├── __init__.py # Package initialization
├── matlab.py # Domain, roles, directives
├── mat_types.py # Type system
├── mat_tree_sitter_parser.py # AST parsing
├── mat_documenters.py # Autodoc documenters
├── mat_directives.py # Autodoc directives
├── mat_lexer.py # Syntax highlighting
└── sphinx_matlab_apidoc.py # CLI tool
Test Organization
tests/
├── test_*.py # Test modules
├── roots/ # Sphinx test configurations
│ └── test-*/ # Each test has its own root
│ ├── conf.py
│ ├── index.rst
│ └── test_data/ # MATLAB test files
└── helper.py # Test utilities
Key Algorithms
Module Discovery
MatModuleAnalyzer.find_modules():
Walk directory tree starting from
matlab_src_dirIdentify MATLAB packages (
+directories)Find class folders (
@directories)Catalog all
.mand.mlappfilesBuild hierarchical module structure
Name Resolution
When resolving matlab_short_links:
User references
:class:`MyClass`System searches for matching classes
If multiple matches, uses context to disambiguate
Returns full path or shortest unique path
Creates cross-reference in output
Docstring Parsing
From MATLAB comments to structured documentation:
Extract comment block above function/class
Identify sections (Args, Returns, Example, See also)
Parse parameter descriptions
Convert to docutils node tree
Apply Napoleon transformations if enabled
Design Patterns
The Documenter Pattern
Each MATLAB entity type has a corresponding documenter:
class FunctionDocumenter(MatlabDocumenter):
objtype = 'function'
def parse_definition(self):
# Extract function signature
pass
def generate(self):
# Generate documentation
pass
This follows Sphinx’s autodoc pattern, making it familiar to Sphinx developers.
The Type System
MATLAB entities are represented as Python objects inheriting from MatObject:
class MatFunction(MatObject):
def __init__(self, name, path, docstring):
self.name = name
self.path = path
self.docstring = docstring
self.args = []
self.returns = []
This provides a clean abstraction over MATLAB’s diverse code structures.
Extension Points
For developers extending this project:
Adding New Directive Types
Create a new class in
mat_directives.pyInherit from
ObjectDescriptionImplement
handle_signature()andtransform_content()Register in
setup()function
Adding New MatTypes
Create class in
mat_types.pyinheriting fromMatObjectImplement parsing logic
Create corresponding documenter in
mat_documenters.pyAdd handling to
mat_tree_sitter_parser.py
Custom Parsing
To handle new MATLAB syntax:
Update tree-sitter grammar (external dependency)
Modify parser in
mat_tree_sitter_parser.pyExtract new information into MatType objects
Dependencies
Core Dependencies
Sphinx (≥4.0.0) - Documentation framework
tree-sitter (≥0.21.3, <0.23.0) - Parser infrastructure
tree-sitter-matlab (≥1.0.2, <1.0.5) - MATLAB grammar
docutils - Document utilities (via Sphinx)
Development Dependencies
pytest - Testing framework
pytest-cov - Coverage reporting
ruff - Linting and formatting
pre-commit - Git hooks
tox - Testing across multiple environments
See Also
Contributing - How to contribute to the project
Testing - Testing strategy and how to run tests
Known Issues - Current bugs and limitations
Quick Reference - Command cheat sheet