Skip to content

Commit acff56a

Browse files
committed
Implement Phase 2: Bidirectional Conversion & Multi-Format Support
Added comprehensive bidirectional conversion capabilities and support for multiple diagram formats, completing Phase 2 of the roadmap. NEW FEATURES: 1. Mermaid Parser (Bidirectional Conversion): - Parse Mermaid flowchart syntax back to DiagramIR - Support for all node shapes (stadium, rhombus, cylinder, etc.) - Edge parsing with labels and types - Direction and title metadata extraction - Complete roundtrip validation (Mermaid → IR → Mermaid) 2. PlantUML Renderer: - Activity diagram syntax generation - Configurable skinparam styling - Support for all node types - Decision nodes with branches - Topological ordering for clean output 3. D2 Renderer (Terrastruct): - Modern, beautiful diagram syntax - Multiple layout engines (dagre, elk, tala) - Colored node styling by type - Direction conversion from Mermaid - Shape customization (oval, rectangle, diamond, cylinder) 4. Graphviz/DOT Renderer: - Classic graph visualization format - Multiple layout algorithms (dot, neato, fdp, circo, etc.) - Node coloring and styling - Edge type support (dashed, bidirectional) - Direct image rendering support 5. Enhanced Text Converter: - Conditional branches: "If condition: action, else: action" - Parallel flows: "[parallel: A, B, C]" - Loops: "while condition: action" - Better keyword detection and type inference - Sentence-style parsing support COMPONENTS ADDED: Parser Module: - ij/parsers/mermaid.py: Mermaid syntax parser - ij/parsers/__init__.py: Parser module exports Renderers: - ij/renderers/plantuml.py: PlantUML activity diagram renderer - ij/renderers/d2.py: D2 (Terrastruct) renderer - ij/renderers/graphviz.py: Graphviz/DOT renderer with image support - Updated ij/renderers/__init__.py: Export all renderers Converters: - ij/converters/enhanced_text.py: Advanced text parsing with NLP - Updated ij/converters/__init__.py: Export enhanced converter Tests (26 new tests): - tests/test_parsers.py: 7 parser tests including roundtrip validation - tests/test_new_renderers.py: 11 renderer tests for PlantUML, D2, Graphviz - tests/test_enhanced_converter.py: 8 enhanced converter tests Examples: - examples/phase2_features.py: 6 comprehensive examples demonstrating: * Bidirectional conversion workflow * Multi-format rendering * Enhanced text features * Format conversion pipelines * Complex workflows Documentation: - PHASE2.md: Complete Phase 2 feature documentation - Updated README.md: Phase 2 features, updated roadmap, test counts - Updated ij/__init__.py: Export all new classes TEST RESULTS: - 52 total tests (26 Phase 1 + 26 Phase 2) - 100% pass rate - All formats tested and validated - Roundtrip conversion verified CAPABILITIES: Multi-Format Workflow: Text → DiagramIR → Mermaid/PlantUML/D2/Graphviz Mermaid → DiagramIR → Any other format Supported Conversions: - Text → Mermaid (Phase 1) - Text → PlantUML/D2/Graphviz (Phase 2) - Mermaid → DiagramIR (Phase 2) - DiagramIR → Mermaid/PlantUML/D2/Graphviz (Phase 2) Enhanced Text Patterns: - Simple: "A -> B -> C" - Conditional: "If X: do Y, else: do Z" - Parallel: "[parallel: A, B, C]" - Loop: "while X: do Y" Following research recommendations: - Bidirectional editing (Mermaid ↔ IR) - Multiple format support (Mermaid, PlantUML, D2, Graphviz) - Enhanced natural language processing - Text-based DSL for version control - Extensible architecture for future parsers Ready for Phase 3: AI integration, visual editing, collaboration
1 parent aac3411 commit acff56a

File tree

15 files changed

+1976
-18
lines changed

15 files changed

+1976
-18
lines changed

PHASE2.md

Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
# Phase 2 Features - Bidirectional Conversion & Multiple Formats
2+
3+
Phase 2 adds bidirectional conversion capabilities and support for multiple diagram formats: PlantUML, D2, and Graphviz.
4+
5+
## New Features
6+
7+
### 1. Mermaid Parser (Bidirectional Conversion)
8+
9+
Parse Mermaid diagrams back into DiagramIR, enabling true bidirectional editing.
10+
11+
```python
12+
from ij.parsers import MermaidParser
13+
14+
mermaid_text = """
15+
flowchart TD
16+
n1([Start])
17+
n2[Process]
18+
n3([End])
19+
n1 --> n2
20+
n2 --> n3
21+
"""
22+
23+
parser = MermaidParser()
24+
diagram = parser.parse(mermaid_text)
25+
26+
# Now you can manipulate the diagram or convert to other formats
27+
```
28+
29+
**Roundtrip Conversion:**
30+
31+
```python
32+
from ij.renderers import MermaidRenderer
33+
from ij.parsers import MermaidParser
34+
35+
# Mermaid -> DiagramIR -> Mermaid
36+
parser = MermaidParser()
37+
diagram = parser.parse(original_mermaid)
38+
39+
renderer = MermaidRenderer()
40+
regenerated = renderer.render(diagram)
41+
```
42+
43+
### 2. PlantUML Renderer
44+
45+
Export diagrams to PlantUML format, the enterprise standard for comprehensive UML.
46+
47+
```python
48+
from ij.renderers import PlantUMLRenderer
49+
50+
renderer = PlantUMLRenderer(use_skinparam=True)
51+
plantuml_output = renderer.render(diagram)
52+
print(plantuml_output)
53+
```
54+
55+
**Output:**
56+
```plantuml
57+
@startuml
58+
title My Process
59+
60+
skinparam ActivityFontSize 14
61+
skinparam ActivityBorderColor #2C3E50
62+
skinparam ActivityBackgroundColor #ECF0F1
63+
64+
start
65+
:Process step;
66+
stop
67+
@enduml
68+
```
69+
70+
### 3. D2 Renderer
71+
72+
Export to D2 (Terrastruct), a modern diagram language with excellent aesthetics.
73+
74+
```python
75+
from ij.renderers import D2Renderer
76+
77+
renderer = D2Renderer(layout="dagre")
78+
d2_output = renderer.render(diagram)
79+
```
80+
81+
**Output:**
82+
```d2
83+
# My Process
84+
85+
direction: down
86+
87+
n1: "Start" {
88+
shape: oval
89+
style.fill: '#90EE90'
90+
style.stroke: '#228B22'
91+
}
92+
n2: "Process" {
93+
shape: rectangle
94+
}
95+
n1 -> n2
96+
```
97+
98+
**Features:**
99+
- Multiple layout engines (dagre, elk, tala)
100+
- Beautiful default styling
101+
- Direction support
102+
- Shape customization
103+
104+
### 4. Graphviz/DOT Renderer
105+
106+
Export to Graphviz DOT format, the 30-year-old foundation of graph visualization.
107+
108+
```python
109+
from ij.renderers import GraphvizRenderer
110+
111+
renderer = GraphvizRenderer(layout="dot")
112+
dot_output = renderer.render(diagram)
113+
114+
# Or render directly to image
115+
renderer.render_to_image(diagram, "output", format="png")
116+
```
117+
118+
**Output:**
119+
```dot
120+
digraph G {
121+
layout="dot";
122+
rankdir="TB";
123+
124+
n1 [label="Start", shape=oval, style=filled, fillcolor=lightgreen];
125+
n2 [label="Process", shape=box, style=filled, fillcolor=lightgray];
126+
n1 -> n2;
127+
}
128+
```
129+
130+
**Supported layouts:**
131+
- `dot`: Hierarchical layouts
132+
- `neato`: Spring model layouts
133+
- `fdp`: Force-directed placement
134+
- `circo`: Circular layouts
135+
- `twopi`: Radial layouts
136+
137+
### 5. Enhanced Text Converter
138+
139+
More sophisticated natural language processing with support for conditionals, parallel flows, and loops.
140+
141+
#### Conditional Branches
142+
143+
```python
144+
from ij.converters import EnhancedTextConverter
145+
146+
converter = EnhancedTextConverter()
147+
text = "Start -> Check user. If authenticated: Show dashboard, else: Show login"
148+
diagram = converter.convert(text)
149+
```
150+
151+
Creates a decision node with Yes/No branches automatically.
152+
153+
#### Parallel Flows
154+
155+
```python
156+
text = "Start -> [parallel: Send email, Update database, Log event] -> End"
157+
diagram = converter.convert(text)
158+
```
159+
160+
Creates parallel execution paths that converge.
161+
162+
#### Loops
163+
164+
```python
165+
text = "Start while data available: Process item"
166+
diagram = converter.convert(text)
167+
```
168+
169+
Creates a loop with decision point and back edge.
170+
171+
## Multi-Format Workflow
172+
173+
Convert diagrams between any supported format:
174+
175+
```python
176+
from ij.parsers import MermaidParser
177+
from ij.renderers import PlantUMLRenderer, D2Renderer, GraphvizRenderer
178+
179+
# Start with Mermaid
180+
mermaid_source = """
181+
flowchart TD
182+
A[Input] --> B[Process]
183+
B --> C[Output]
184+
"""
185+
186+
# Parse
187+
parser = MermaidParser()
188+
diagram = parser.parse(mermaid_source)
189+
190+
# Export to multiple formats
191+
plantuml = PlantUMLRenderer().render(diagram)
192+
d2 = D2Renderer().render(diagram)
193+
graphviz = GraphvizRenderer().render(diagram)
194+
195+
# Save all formats
196+
renderers = {
197+
"diagram.mmd": MermaidRenderer(),
198+
"diagram.puml": PlantUMLRenderer(),
199+
"diagram.d2": D2Renderer(),
200+
"diagram.dot": GraphvizRenderer(),
201+
}
202+
203+
for filename, renderer in renderers.items():
204+
renderer.render_to_file(diagram, filename)
205+
```
206+
207+
## Format Comparison
208+
209+
| Format | Strengths | Best For | Output |
210+
|--------|-----------|----------|--------|
211+
| **Mermaid** | GitHub native, simple syntax, 84K stars | Documentation, README files | SVG, PNG |
212+
| **PlantUML** | Comprehensive UML, 25+ types, enterprise | Detailed UML diagrams | SVG, PNG |
213+
| **D2** | Modern, beautiful, bidirectional editing | Presentations, architecture | SVG, PNG, PPT |
214+
| **Graphviz** | Foundation, maximum control, 30 years | Complex graphs, custom layouts | Any format |
215+
216+
## Complete Example
217+
218+
```python
219+
from ij import (
220+
SimpleTextConverter,
221+
EnhancedTextConverter,
222+
MermaidParser,
223+
MermaidRenderer,
224+
PlantUMLRenderer,
225+
D2Renderer,
226+
GraphvizRenderer,
227+
)
228+
229+
# Method 1: Simple text conversion
230+
converter = SimpleTextConverter()
231+
diagram = converter.convert("Start -> Process -> End")
232+
233+
# Method 2: Enhanced text with conditionals
234+
enhanced = EnhancedTextConverter()
235+
diagram = enhanced.convert(
236+
"Start -> Check data. If valid: Process, else: Reject"
237+
)
238+
239+
# Method 3: Parse existing Mermaid
240+
parser = MermaidParser()
241+
diagram = parser.parse(existing_mermaid_diagram)
242+
243+
# Render to any format
244+
mermaid = MermaidRenderer().render(diagram)
245+
plantuml = PlantUMLRenderer().render(diagram)
246+
d2 = D2Renderer().render(diagram)
247+
graphviz = GraphvizRenderer().render(diagram)
248+
249+
# Save to files
250+
MermaidRenderer().render_to_file(diagram, "output.mmd")
251+
PlantUMLRenderer().render_to_file(diagram, "output.puml")
252+
D2Renderer().render_to_file(diagram, "output.d2")
253+
GraphvizRenderer().render_to_file(diagram, "output.dot")
254+
```
255+
256+
## Running Examples
257+
258+
```bash
259+
# Phase 2 examples
260+
python examples/phase2_features.py
261+
262+
# Specific examples
263+
python -c "from examples.phase2_features import example1_bidirectional_conversion; example1_bidirectional_conversion()"
264+
```
265+
266+
## Testing
267+
268+
All Phase 2 features are fully tested:
269+
270+
```bash
271+
pytest tests/test_parsers.py -v # Parser tests
272+
pytest tests/test_new_renderers.py -v # New renderer tests
273+
pytest tests/test_enhanced_converter.py -v # Enhanced converter tests
274+
```
275+
276+
**Test Coverage:**
277+
- 52 tests total (26 from Phase 1 + 26 from Phase 2)
278+
- 100% pass rate
279+
- Roundtrip conversion validated
280+
281+
## Next Steps (Phase 3)
282+
283+
- AI/LLM integration for natural language understanding
284+
- Visual editor integration
285+
- Real-time collaboration (CRDT-based)
286+
- Code-to-diagram reverse engineering
287+
- Multiple diagram views from single source
288+
289+
## See Also
290+
291+
- [Main README](README.md) - Overview and Phase 1 features
292+
- [examples/phase2_features.py](examples/phase2_features.py) - Complete examples
293+
- [Research Document](misc/REASEARCH.md) - Background research

0 commit comments

Comments
 (0)