Pure Python Mermaid Diagram Converter with robust error handling
Project description
PyMMDC - Pure Python Mermaid Diagram Converter
A robust, local-only Mermaid to PNG converter with comprehensive Markdown processing support. Convert Mermaid diagrams seamlessly without any web requests or external dependencies beyond your local Mermaid CLI installation.
🚀 Key Features
- 🔒 Pure Local Operation: No web requests - exclusively uses local Mermaid CLI
- 🛡️ Robust Error Handling: Comprehensive exception hierarchy with detailed error messages and validation
- 📄 Markdown File Processing: Automatically detects and processes Mermaid code blocks in
.mdfiles - ⚙️ Flexible Configuration: Customizable diagram appearance (themes, sizes, backgrounds)
- 🔄 Batch Processing: Convert multiple files and directories with single commands
- 📝 Metadata Support: Configure diagrams using code block metadata
- 🧹 Resource Management: Proper temporary file cleanup with context manager support
- 📊 Detailed Reporting: Structured results with execution metrics and success rates
📦 Installation
Prerequisites
- Node.js (v14 or higher) - Download here
- Mermaid CLI:
npm install -g @mermaid-js/mermaid-cli
Install PyMMDC
pip install pymmdc
🎯 Quick Start
Command Line Interface
# Convert single Mermaid file
pymmdc diagram.mmd diagram.png
# Process Markdown file with Mermaid blocks
pymmdc document.md --output-dir ./images
# Batch process multiple Markdown files
pymmdc "docs/*.md" --batch --output-dir ./generated_images
# Replace Mermaid blocks with images in output
pymmdc README.md --replace-blocks --output-dir ./assets
# Custom theme and dimensions
pymmdc diagram.mmd --theme dark --width 1600 --height 1200
Python API
from pymmdc import EnhancedMermaidConverter
# Simple conversion
with EnhancedMermaidConverter() as converter:
result = converter.convert_file("diagram.mmd", "output.png")
# Markdown processing
result = converter.convert_file(
"document.md",
process_markdown=True,
output_dir="./images",
replace_blocks=True
)
# Batch processing
results = converter.batch_convert(["file1.md", "file2.mmd"])
📝 Usage Examples
Basic Mermaid Conversion
# Convert .mmd or .mermaid files to PNG
pymmdc flowchart.mmd flowchart.png
pymmdc sequence.mermaid sequence.png
Markdown Processing
Process Markdown files containing Mermaid code blocks:
# My Document
Here's a flowchart:
```mermaid
graph TD
A[Start] --> B{Decision}
B -->|Yes| C[Action 1]
B -->|No| D[Action 2]
```
And a sequence diagram:
```mermaid
sequenceDiagram
Alice->>Bob: Hello Bob!
Bob-->>Alice: Hello Alice!
```
# Process all Mermaid blocks in Markdown
pymmdc document.md --output-dir ./diagrams
# Replace code blocks with images in new Markdown file
pymmdc document.md --replace-blocks --output-dir ./assets
Advanced Configuration
Use metadata in code blocks for per-diagram settings:
```mermaid { "theme": "dark", "width": 1600, "title": "System Architecture" }
graph TB
subgraph Frontend
A[Web App]
B[Mobile App]
end
subgraph Backend
C[API Server]
D[Database]
end
A --> C
B --> C
C --> D
```
⚙️ Configuration Options
Command Line Flags
| Flag | Description | Default |
|---|---|---|
--timeout |
Conversion timeout in seconds | 60 |
--width |
Diagram width in pixels | 1200 |
--height |
Diagram height in pixels | 800 |
--theme |
Diagram theme (default, dark, forest, neutral) |
default |
--bg-color |
Background color | transparent |
--output-dir |
Output directory for images | Current directory |
--replace-blocks |
Replace code blocks with images in Markdown | False |
--batch |
Process multiple files | False |
--verbose |
Enable detailed output | False |
Supported Metadata
Configure diagrams using JSON metadata in code blocks:
{
"theme": "dark",
"width": 1600,
"height": 900,
"title": "My Diagram",
"bgcolor": "white"
}
🔧 Advanced Usage
Batch Processing
# Process all Markdown files in a directory
pymmdc "project/docs/**/*.md" --batch --recursive --output-dir ./generated
# Dry run to see what would be processed
pymmdc "*.md" --batch --dry-run
# Custom output directory and theme
pymmdc "**/*.md" --batch --output-dir ./docs/assets --theme forest
Integration with Documentation Pipelines
# Pre-process documentation before building
pymmdc "docs/**/*.md" --batch --replace-blocks --output-dir docs/assets
# CI/CD integration example
pymmdc README.md --output-dir ./badges || echo "Diagram generation failed"
🐛 Troubleshooting
Common Issues
-
Mermaid CLI not found:
npm install -g @mermaid-js/mermaid-cli
-
Node.js version too old:
# Update Node.js npm install -g n n stable
-
Permission errors:
# Ensure write permissions in output directory chmod +w ./output-directory
Debug Mode
Enable verbose logging for troubleshooting:
pymmdc document.md --verbose --output-dir ./debug
📊 Output Examples
Successful processing provides detailed reports:
Markdown Processing Complete!
File: document.md
Blocks found: 3
Blocks converted: 3
Success rate: 100.0%
Output files:
- document_architecture.png
- document_flowchart.png
- document_sequence.png
🤝 Contributing
We welcome contributions! Please see our contributing guidelines for details.
📄 License
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
See the LICENSE file for more details. See the LICENSE file for details.
🆘 Support
PyMMDC: Making Mermaid diagram conversion reliable, fast, and completely local. 🚀
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pymmdc-0.1.0.tar.gz.
File metadata
- Download URL: pymmdc-0.1.0.tar.gz
- Upload date:
- Size: 36.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a2fb6565f8bb349c581d061335c079629846773270c418338fd4279b734c03d3
|
|
| MD5 |
a4d1fc698cd0f169905c74f03ecc6896
|
|
| BLAKE2b-256 |
15fdf63ce1f9a474c916d80f6b16ff8d56a6767e57c994ee5d84116e299119ff
|
File details
Details for the file pymmdc-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pymmdc-0.1.0-py3-none-any.whl
- Upload date:
- Size: 38.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f2de77ab21e745aaeba5d67a5da707ecbbc44c2a92fc1e4f4244a8052f426e3d
|
|
| MD5 |
1e86d36456abecc5876c79fc05a9c9e4
|
|
| BLAKE2b-256 |
e766213b699121615108cb291cc91c08b9a39eda6ea5bffb322f08c56c916894
|