-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender_diagrams.py
More file actions
executable file
·114 lines (95 loc) · 3.33 KB
/
Copy pathrender_diagrams.py
File metadata and controls
executable file
·114 lines (95 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
"""
Script to render Mermaid diagrams to PNG and SVG formats.
Requires: npm install -g @mermaid-js/mermaid-cli
"""
import os
import subprocess
import sys
from pathlib import Path
def check_mermaid_cli():
"""Check if mermaid CLI is installed."""
try:
result = subprocess.run(['mmdc', '--version'],
capture_output=True, text=True, check=True)
print(f"Mermaid CLI version: {result.stdout.strip()}")
return True
except (subprocess.CalledProcessError, FileNotFoundError):
print("Error: Mermaid CLI not found.")
print("Please install it with: npm install -g @mermaid-js/mermaid-cli")
return False
def render_diagram(input_file, output_dir):
"""Render a single Mermaid diagram to PNG and SVG."""
input_path = Path(input_file)
base_name = input_path.stem
# Create output directory if it doesn't exist
output_dir = Path(output_dir)
output_dir.mkdir(exist_ok=True)
# Render to SVG
svg_file = output_dir / f"{base_name}.svg"
try:
subprocess.run([
'mmdc',
'-i', str(input_path),
'-o', str(svg_file),
'-t', 'neutral',
'-b', 'white'
], check=True)
print(f"✓ Rendered {input_path.name} to {svg_file}")
except subprocess.CalledProcessError as e:
print(f"✗ Failed to render {input_path.name} to SVG: {e}")
return False
# Render to PNG
png_file = output_dir / f"{base_name}.png"
try:
subprocess.run([
'mmdc',
'-i', str(input_path),
'-o', str(png_file),
'-t', 'neutral',
'-b', 'white',
'-w', '1200',
'-H', '800'
], check=True)
print(f"✓ Rendered {input_path.name} to {png_file}")
except subprocess.CalledProcessError as e:
print(f"✗ Failed to render {input_path.name} to PNG: {e}")
return False
return True
def main():
"""Main function to render all diagrams."""
print("Mermaid Diagram Renderer")
print("=" * 40)
# Check if mermaid CLI is available
if not check_mermaid_cli():
sys.exit(1)
# Define paths
diagrams_dir = Path("diagrams")
output_dir = Path("diagrams/rendered")
if not diagrams_dir.exists():
print(f"Error: Diagrams directory '{diagrams_dir}' not found.")
sys.exit(1)
# Find all .mmd files
mermaid_files = list(diagrams_dir.glob("*.mmd"))
if not mermaid_files:
print("No .mmd files found in diagrams directory.")
sys.exit(1)
print(f"Found {len(mermaid_files)} Mermaid diagram(s) to render:")
for file in mermaid_files:
print(f" - {file.name}")
print("\nRendering diagrams...")
print("-" * 40)
success_count = 0
for mermaid_file in mermaid_files:
if render_diagram(mermaid_file, output_dir):
success_count += 1
print("-" * 40)
print(f"Successfully rendered {success_count}/{len(mermaid_files)} diagrams")
if success_count == len(mermaid_files):
print("✓ All diagrams rendered successfully!")
print(f"Output directory: {output_dir.absolute()}")
else:
print("✗ Some diagrams failed to render.")
sys.exit(1)
if __name__ == "__main__":
main()