Skip to content

Commit cec6241

Browse files
fahchenclaude
andcommitted
fix(presentation): collapse multi-line colset descrs in mermaid output
`Macro.to_string` returns multi-line output for composite descrs (maps, nested tuples), but mermaid's `%%` only comments a single line. Trailing lines escape the comment block and break the parser, e.g.: Parse error on line 2: ...flowchart TB name: binary(), realm: r ----------------------^ Expecting 'SEMI', 'NEWLINE', 'EOF', ..., got 'NODE_STRING' Collapse whitespace runs to a single space so the colset descr always fits on one line. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent a7db92c commit cec6241

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

lib/coloured_flow/definition/presentation/presentation.ex

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,17 @@ defmodule ColouredFlow.Definition.Presentation do
4949
defp to_mermaid_colour_set(%ColourSet{name: name, type: type}) do
5050
alias ColouredFlow.Definition.ColourSet.Descr
5151

52-
"%% colset #{compose_call(name)} :: #{type |> Descr.to_quoted() |> Macro.to_string()}"
52+
# `Macro.to_string` may return multi-line output for composite descrs
53+
# (maps, nested tuples). Mermaid's `%%` only comments a single line, so
54+
# trailing lines escape the comment and break the parser. Collapse
55+
# whitespace runs to a single space so the colset descr fits on one line.
56+
rendered =
57+
type
58+
|> Descr.to_quoted()
59+
|> Macro.to_string()
60+
|> String.replace(~r/\s+/u, " ")
61+
62+
"%% colset #{compose_call(name)} :: #{rendered}"
5363
end
5464

5565
defp to_mermaid_place(%Place{name: name, colour_set: colour_set}) do

test/coloured_flow/definition/presentation_test.exs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
defmodule ColouredFlow.Definition.PresentationTest do
22
use ExUnit.Case, async: true
33

4+
alias ColouredFlow.Definition.ColourSet
5+
alias ColouredFlow.Definition.ColouredPetriNet
6+
alias ColouredFlow.Definition.Place
47
alias ColouredFlow.Definition.Presentation
58

69
import ColouredFlow.CpnetBuilder
@@ -80,6 +83,33 @@ defmodule ColouredFlow.Definition.PresentationTest do
8083
"""
8184
)
8285
end
86+
87+
test "collapses multi-line composite types into single-line %% comments" do
88+
# `Macro.to_string` returns multi-line output for map descrs. Mermaid's
89+
# `%%` only comments a single line, so without collapsing, the parser
90+
# bails with "Expecting NEWLINE/EOF, got NODE_STRING" on the trailing
91+
# lines that escape the comment.
92+
cpnet = %ColouredPetriNet{
93+
colour_sets: [
94+
%ColourSet{
95+
name: :user,
96+
type: {:map, %{name: {:binary, []}, age: {:integer, []}}}
97+
}
98+
],
99+
places: [%Place{name: "p", colour_set: :user}],
100+
transitions: [],
101+
arcs: [],
102+
variables: [],
103+
constants: [],
104+
functions: []
105+
}
106+
107+
mermaid = Presentation.to_mermaid(cpnet)
108+
[colset_line] = Regex.run(~r/%% colset .+/, mermaid)
109+
110+
assert colset_line =~ "%% colset user() ::"
111+
refute String.contains?(colset_line, "\n")
112+
end
83113
end
84114

85115
defp assert_mermaid(cpnet, expected) do

0 commit comments

Comments
 (0)