Printers
This guide explains how to create custom printers for BeDoc. Printers are responsible for transforming parsed documentation into specific output formats like Markdown, HTML, or any other format you need.
Printer Structure
Section titled “Printer Structure”A BeDoc printer consists of two main parts:
- Meta Information: Defines the format and file extension. (required)
- Contract: Specifies the printer interface. (required)
- Parser Object: Implements the printer logic. (required)
- Hooks: Supports hook points for custom logic. (optional, external)
A printer action is contained in a JavaScript action file.
Here’s the minimal structure for a BeDoc printer:
Printer
Section titled “Printer”{ // Define meta information meta = { action: "print", // required format: "wikitext", // required documentExtension: ".md" // You can add anything else your // action needs, if you want to. 🤷🏻 },
/** * This is the setup function, called in advance of running the job. * * @param {object} setup An object containing utility objects exposed by BeDoc * @param {object} setup.log A logging utility provided by BeDoc */ setup(setup) { ({ log: this.log } = setup) },
/** * This is the action to print structured object to text. * * @param {object} module Data coming in from the printer * @param {string} module.file The file object representing the file * being currently being processed * @param {object[]} module.moduleContent An array of objects containing * function definitions prepared by the parser. * @returns {Promise<object>} The result of the print operations. */ async run(module) { // Print the content and return a response const {file: {module: moduleName}, moduleContent} = module
// You know what? No. const finalOutput = `${module.toUpperCase()} has to have the worst grammar ever. ` + `I refuse to print it.`
return { status: "success", // Required message: "File printed successfully. 😏", // Optional destFile: `${moduleName}${this.documentExtension}`, // Required destContent: finalOutput, // Required } }}Response Format
Section titled “Response Format”The printer’s run method must return a response object with the following
structure:
Success
Section titled “Success”To convey success and further information about the job, return a success object.
// Success response{ status: "success", destFile: string, // Output filename (e.g., "module.md") content: string, // Formatted documentation content}Warning
Section titled “Warning”To return no content, except a warning, you may return a warning object.
// Warning response{ status: "warning", warning: string, // War}At the time of this writing, an error response is not required. You may
simply throw(), and BeDoc will catch() it and report it.
Core Utilities
Section titled “Core Utilities”The printer has access to certain of BeDoc’s core utilities through
object propagated during the setup() function.
- Logging functions (debug, info, warn, error)
Hook Support
Section titled “Hook Support”Printers automatically support hooks, allowing users to modify the printing process. Hook points include:
start: Before printing beginssection_load: When a documentation section is loadedenter: When entering a sectionexit: When exiting a sectionend: After printing completes
Example Implementation
Section titled “Example Implementation”You can see examples of printer implementations on GitHub.
Best Practices
Section titled “Best Practices”-
Error Handling: Always return appropriate error responses with detailed messages.
-
Async Support: Make your printer async to handle large documents efficiently and support async hooks.
-
Validation: Validate input content structure before processing.
-
Content Structure: Keep your output well-organized and consistent with the format’s conventions.
Testing Your Printer
Section titled “Testing Your Printer”BeDoc provides multiple ways to test your printers:
-
Mock Mode: Test using a local mock environment:
Terminal window bedoc --mock ./mock_dir -l javascript -f myformat -i test/*.js -o test/docs -
Direct File Usage: Test your printer file directly without installing:
Terminal window bedoc --printer ./my-printer.js -l javascript -i test/*.js -o test/docs# Or use the short formbedoc -P ./my-printer.js -l javascript -i test/*.js -o test/docs
These options allow you to rapidly iterate on your printer implementation without needing to package and install it first.
Publishing Your Printer
Section titled “Publishing Your Printer”When ready to publish, package your printer following BeDoc’s naming and structure conventions:
{ "name": "bedoc-myformat-printer", "version": "1.0.0", "type": "module", "description": "MyFormat printer for BeDoc", "bedoc": { "actions": [ "./bedoc-myformat-printer.js" ] }}You can also package multiple printers together:
{ "name": "bedoc-doc-printers", "version": "1.0.0", "type": "module", "description": "Documentation printers for BeDoc", "bedoc": { "actions": [ "./bedoc-markdown-printer.js", "./bedoc-html-printer.js" ] }}This structure allows BeDoc to automatically discover and load your printer when installed.