A tool to automatically fix formatting and alignment issues in ASCII diagrams
Project description
Diagram Fixer
A Python tool designed to automatically fix formatting and alignment issues in ASCII diagrams, particularly those generated by AI tools like Claude. This tool is especially useful in pandoc workflows for cleaning up diagrams before converting to other formats.
Features
- Precision Matrix-Based Detection: Uses sophisticated matrix analysis to identify and fix diagram structural issues
- Box Alignment: Automatically corrects misaligned box borders and inconsistent spacing
- Connection Line Fixing: Repairs broken or misaligned connection lines between diagram elements
- Pandoc Integration: Seamlessly integrates into pandoc preprocessing workflows
- Quiet Operation: Clean, minimal output by default with optional verbose debugging
- Robust Algorithm: Handles complex nested diagrams with stable convergence
Installation
Option 1: Install from PyPI (when available)
pip install markdown-diagram-fixer
Option 2: Install from Source
git clone https://github.com/andrewyager/markdown-diagram-fixer.git
cd markdown-diagram-fixer
pip install .
Option 3: Development Install
git clone https://github.com/andrewyager/markdown-diagram-fixer.git
cd markdown-diagram-fixer
pip install -e .
Requirements
- Python 3.7 or higher
- No external dependencies (uses only Python standard library)
Usage
Command Line
After installation, use the diagram-fixer command:
# Fix a single diagram file
diagram-fixer input_diagram.txt
# Enable verbose output for debugging
diagram-fixer input_diagram.txt --verbose
If running directly from source without installation:
python3 src/precision_diagram_fixer.py input_diagram.txt
Pandoc Integration
The diagram fixer can be used as a pandoc filter to automatically fix diagrams during document processing.
Installation for Pandoc
After installing the package (pip install markdown-diagram-fixer), add the filter to your pandoc workflow:
Option 1: Use in pandoc defaults file (recommended)
Add to your pandoc defaults file (e.g., ~/.pandoc/defaults/pdf.yaml):
filters:
- pandoc-diagram-filter
Option 2: Use direct path to installed binary
If the above doesn't work due to PATH issues, use the full path:
filters:
- /opt/homebrew/bin/pandoc-diagram-filter # On macOS with Homebrew
# or find the path with: which pandoc-diagram-filter
Option 3: Use with individual pandoc commands
# Using pandoc defaults
pandoc -d pdf input.md -o output.pdf
# Or specify filter directly
pandoc --filter pandoc-diagram-filter input.md -o output.pdf
Troubleshooting Pandoc Integration
If the filter doesn't work:
-
Check installation: Verify the console script exists:
which pandoc-diagram-filter -
Test the filter directly:
echo '``` ┌─────┐ │ Box │ └─────┘ ```' | pandoc -f gfm -t json | pandoc-diagram-filter | pandoc -f json -t markdown
-
Use full path if needed: In your pandoc defaults, use the full path from
which pandoc-diagram-filter
See TROUBLESHOOTING.md for detailed troubleshooting help.
Command Line Integration
You can also use the preprocessor directly:
# Preprocess markdown then pass to pandoc
pandoc-diagram-fixer < input.md | pandoc -o output.pdf
How It Works
The precision diagram fixer uses a matrix-based approach:
- Detection: Identifies diagram elements (boxes, connection lines) using Unicode box-drawing characters
- Analysis: Builds a solved/unsolved matrix to track which positions are correctly formatted
- Problem Identification: Finds misaligned borders, broken connections, and spacing issues
- Targeted Fixing: Applies minimal character injections to fix problems without disrupting correctly formatted areas
- Convergence: Iterates until the diagram reaches a stable, correctly formatted state
Supported Diagram Types
- Box and line diagrams using Unicode box-drawing characters (┌┐└┘├┤┬┴┼─│)
- Network topology diagrams
- Flowcharts and organizational charts
- System architecture diagrams
- Any ASCII art using standard box-drawing conventions
Example
Before:
┌─────────┐ ┌──────────┐
│ Source │───│ Target │
│ Node │ │ Node │
└─────────┘ └──────────┘
After: (with proper alignment)
┌─────────┐ ┌──────────┐
│ Source │────│ Target │
│ Node │ │ Node │
└─────────┘ └──────────┘
Files
src/precision_diagram_fixer.py- Main diagram fixing tool with robust matrix-based algorithmsrc/pandoc_preprocessor.py- Pandoc preprocessor for automatic diagram fixing in markdown workflowsexample_diagram.txt- Sample diagram for testing
License
MIT License - see LICENSE file for details.
Copyright (c) 2025 Andrew Yager, Real World Technology Solutions Pty Ltd
Contributing
We welcome contributions! Please see CONTRIBUTING.md for guidelines on:
- Reporting issues and bugs
- Submitting pull requests
- Development setup and testing
- Code style and best practices
Security
If you discover a security vulnerability, please report it privately through GitHub Security Advisories. See SECURITY.md for details.
Project details
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 markdown_diagram_fixer-1.1.1.tar.gz.
File metadata
- Download URL: markdown_diagram_fixer-1.1.1.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a6e1cc75e605f6fc8ea2ce1a655b63b65ba57218aa4d43d1d247b1174e1ba63
|
|
| MD5 |
4098d022b46009d28ccc54ac18d381dd
|
|
| BLAKE2b-256 |
329cd97956fe005baddbc7756af04449ecc707a01db011faeed1cf0d11d447f9
|
Provenance
The following attestation bundles were made for markdown_diagram_fixer-1.1.1.tar.gz:
Publisher:
python-publish.yml on andrewyager/markdown-diagram-fixer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
markdown_diagram_fixer-1.1.1.tar.gz -
Subject digest:
0a6e1cc75e605f6fc8ea2ce1a655b63b65ba57218aa4d43d1d247b1174e1ba63 - Sigstore transparency entry: 367017105
- Sigstore integration time:
-
Permalink:
andrewyager/markdown-diagram-fixer@524acc8a47fe2550ef3a9aec2d9575d54e26d387 -
Branch / Tag:
refs/tags/v1.1.1 - Owner: https://github.com/andrewyager
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@524acc8a47fe2550ef3a9aec2d9575d54e26d387 -
Trigger Event:
release
-
Statement type:
File details
Details for the file markdown_diagram_fixer-1.1.1-py3-none-any.whl.
File metadata
- Download URL: markdown_diagram_fixer-1.1.1-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a0c8d16ef4d92a2172f5b6bbfd0605f6f13df37352982741c509fde2ac986350
|
|
| MD5 |
ccf0159544e4b49ef3a4962ff08e60bf
|
|
| BLAKE2b-256 |
4c9605e2a9d09618ccc01e28e7cdc290ef79944bb457752d8f5936341a0234f6
|
Provenance
The following attestation bundles were made for markdown_diagram_fixer-1.1.1-py3-none-any.whl:
Publisher:
python-publish.yml on andrewyager/markdown-diagram-fixer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
markdown_diagram_fixer-1.1.1-py3-none-any.whl -
Subject digest:
a0c8d16ef4d92a2172f5b6bbfd0605f6f13df37352982741c509fde2ac986350 - Sigstore transparency entry: 367017122
- Sigstore integration time:
-
Permalink:
andrewyager/markdown-diagram-fixer@524acc8a47fe2550ef3a9aec2d9575d54e26d387 -
Branch / Tag:
refs/tags/v1.1.1 - Owner: https://github.com/andrewyager
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@524acc8a47fe2550ef3a9aec2d9575d54e26d387 -
Trigger Event:
release
-
Statement type: