|
| 1 | +using DiagramForge.Abstractions; |
| 2 | +using DiagramForge.Models; |
| 3 | + |
| 4 | +namespace DiagramForge.Parsers.Conceptual; |
| 5 | + |
| 6 | +public sealed partial class ConceptualDslParser |
| 7 | +{ |
| 8 | + private static void ParseTreeDiagram(string[] lines, IDiagramSemanticModelBuilder builder) |
| 9 | + { |
| 10 | + // ── Parse optional style: section ───────────────────────────────────── |
| 11 | + string? stylePreset = null; |
| 12 | + int styleLine = FindSectionLine(lines, "style"); |
| 13 | + if (styleLine >= 0) |
| 14 | + { |
| 15 | + var trimmed = lines[styleLine].Trim(); |
| 16 | + var colonPos = trimmed.IndexOf(':', StringComparison.Ordinal); |
| 17 | + if (colonPos >= 0) |
| 18 | + { |
| 19 | + var value = trimmed[(colonPos + 1)..].Trim(); |
| 20 | + if (!string.IsNullOrEmpty(value)) |
| 21 | + stylePreset = value.ToLowerInvariant(); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + // ── Parse optional colors: section ──────────────────────────────────── |
| 26 | + var colorMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| 27 | + int colorsLine = FindSectionLine(lines, "colors"); |
| 28 | + if (colorsLine >= 0) |
| 29 | + { |
| 30 | + for (int i = colorsLine + 1; i < lines.Length; i++) |
| 31 | + { |
| 32 | + var line = lines[i]; |
| 33 | + var trimmed = line.Trim(); |
| 34 | + if (string.IsNullOrEmpty(trimmed)) |
| 35 | + continue; |
| 36 | + |
| 37 | + // Stop at the next top-level section key |
| 38 | + if (GetIndent(line) == 0 && trimmed.EndsWith(':') && !trimmed.StartsWith('-')) |
| 39 | + break; |
| 40 | + |
| 41 | + // Expect "name: \"#hex\"" or "name: #hex" |
| 42 | + var sep = trimmed.IndexOf(':', StringComparison.Ordinal); |
| 43 | + if (sep > 0) |
| 44 | + { |
| 45 | + var key = trimmed[..sep].Trim(); |
| 46 | + var val = trimmed[(sep + 1)..].Trim().Trim('"'); |
| 47 | + if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(val)) |
| 48 | + colorMap[key] = val; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // ── Parse required tree: section ────────────────────────────────────── |
| 54 | + int treeLine = FindSectionLine(lines, "tree"); |
| 55 | + if (treeLine < 0) |
| 56 | + throw new DiagramParseException("Missing required section 'tree:' in tree diagram."); |
| 57 | + |
| 58 | + int baseIndent = -1; |
| 59 | + var stack = new Stack<(int indent, string nodeId)>(); |
| 60 | + int nodeCounter = 0; |
| 61 | + bool isOrgChart = string.Equals(stylePreset, "orgchart", StringComparison.OrdinalIgnoreCase); |
| 62 | + |
| 63 | + for (int i = treeLine + 1; i < lines.Length; i++) |
| 64 | + { |
| 65 | + var line = lines[i]; |
| 66 | + var trimmed = line.Trim(); |
| 67 | + if (string.IsNullOrEmpty(trimmed)) |
| 68 | + continue; |
| 69 | + |
| 70 | + // Stop at next top-level section key |
| 71 | + if (GetIndent(line) == 0 && trimmed.EndsWith(':') && !trimmed.StartsWith('-')) |
| 72 | + break; |
| 73 | + |
| 74 | + int indent = GetIndent(line); |
| 75 | + if (baseIndent < 0) |
| 76 | + baseIndent = indent; |
| 77 | + |
| 78 | + // Normalize indent relative to base |
| 79 | + int relIndent = indent - baseIndent; |
| 80 | + |
| 81 | + // Parse optional [color-group] tag |
| 82 | + string label = trimmed; |
| 83 | + string? colorGroup = null; |
| 84 | + int bracketStart = trimmed.LastIndexOf('['); |
| 85 | + int bracketEnd = trimmed.LastIndexOf(']'); |
| 86 | + if (bracketStart >= 0 && bracketEnd > bracketStart) |
| 87 | + { |
| 88 | + colorGroup = trimmed[(bracketStart + 1)..bracketEnd].Trim(); |
| 89 | + label = trimmed[..bracketStart].Trim(); |
| 90 | + } |
| 91 | + |
| 92 | + if (string.IsNullOrEmpty(label)) |
| 93 | + continue; |
| 94 | + |
| 95 | + var nodeId = $"node_{nodeCounter++}"; |
| 96 | + var node = new Node(nodeId, label); |
| 97 | + |
| 98 | + // Apply fill color from color map |
| 99 | + if (colorGroup is not null && colorMap.TryGetValue(colorGroup, out var color)) |
| 100 | + node.FillColor = color; |
| 101 | + |
| 102 | + // Store tree metadata – depth equals the current ancestor count |
| 103 | + // (i.e. the stack size after popping), which is independent of indent width. |
| 104 | + node.Metadata["tree:depth"] = stack.Count; |
| 105 | + if (isOrgChart) |
| 106 | + node.Metadata["tree:orgchart"] = true; |
| 107 | + |
| 108 | + builder.AddNode(node); |
| 109 | + |
| 110 | + // Pop stack until we find a parent with strictly smaller indentation |
| 111 | + while (stack.Count > 0 && stack.Peek().indent >= relIndent) |
| 112 | + stack.Pop(); |
| 113 | + |
| 114 | + if (stack.Count > 0) |
| 115 | + { |
| 116 | + var edge = new Edge(stack.Peek().nodeId, nodeId) |
| 117 | + { |
| 118 | + Routing = EdgeRouting.Orthogonal, |
| 119 | + ArrowHead = ArrowHeadStyle.None, |
| 120 | + }; |
| 121 | + edge.Metadata["tree:edge"] = true; |
| 122 | + builder.AddEdge(edge); |
| 123 | + } |
| 124 | + |
| 125 | + stack.Push((relIndent, nodeId)); |
| 126 | + } |
| 127 | + |
| 128 | + if (nodeCounter == 0) |
| 129 | + throw new DiagramParseException("Section 'tree' contains no items."); |
| 130 | + |
| 131 | + builder.WithLayoutHints(new LayoutHints { Direction = LayoutDirection.TopToBottom }); |
| 132 | + } |
| 133 | +} |
0 commit comments