seafoam 0.13 → 0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,6 @@
1
- require 'json'
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
2
4
 
3
5
  module Seafoam
4
6
  # Write files in a JSON format.
@@ -30,26 +32,28 @@ module Seafoam
30
32
  name: name,
31
33
  props: graph.props,
32
34
  nodes: nodes,
33
- edges: edges
35
+ edges: edges,
34
36
  }
35
37
 
36
38
  @out.puts JSON.pretty_generate(prepare_json(object))
37
39
  end
38
40
 
39
- def self.prepare_json(object)
40
- case object
41
- when Float
42
- if object.nan?
43
- '[NaN]'
41
+ class << self
42
+ def prepare_json(object)
43
+ case object
44
+ when Float
45
+ if object.nan?
46
+ "[NaN]"
47
+ else
48
+ object
49
+ end
50
+ when Array
51
+ object.map { |o| prepare_json(o) }
52
+ when Hash
53
+ object.transform_values { |v| prepare_json(v) }
44
54
  else
45
55
  object
46
56
  end
47
- when Array
48
- object.map { |o| prepare_json(o) }
49
- when Hash
50
- object.transform_values { |v| prepare_json(v) }
51
- else
52
- object
53
57
  end
54
58
  end
55
59
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Seafoam
2
4
  # A writer from graphs to Markdown format, with an embedded Mermaid graph.
3
5
  class MarkdownWriter
@@ -7,10 +9,10 @@ module Seafoam
7
9
 
8
10
  # Write a graph.
9
11
  def write_graph(graph)
10
- @stream.puts '```mermaid'
12
+ @stream.puts "```mermaid"
11
13
  mermaid = MermaidWriter.new(@stream)
12
- mermaid.write_graph graph
13
- @stream.puts '```'
14
+ mermaid.write_graph(graph)
15
+ @stream.puts "```"
14
16
  end
15
17
  end
16
18
  end
@@ -1,40 +1,52 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Seafoam
2
- # A writer from graphs to the Mermaid format.
3
- class MermaidWriter
4
- def initialize(stream)
5
- @stream = stream
4
+ # A writer from graphs to the Mermaid format. Re-uses the Graphviz writer,
5
+ # just adapting for the syntax. Attributes are cherry-picked, and more
6
+ # things are left to defaults.
7
+ class MermaidWriter < GraphvizWriter
8
+ def initialize(*args)
9
+ super(*args)
10
+ @link_style_counter = 0
6
11
  end
7
12
 
8
- # Write a graph.
9
- def write_graph(graph)
10
- @stream.puts 'flowchart TD'
11
- write_nodes graph
12
- write_edges graph
13
- end
13
+ protected
14
14
 
15
- private
15
+ def start_graph(_attrs)
16
+ # Ignore bgcolor, as I can't figure out how to do it in Mermaid
17
+ @stream.puts "flowchart TD"
18
+ end
16
19
 
17
- # Write node declarations.
18
- def write_nodes(graph)
19
- graph.nodes.each_value do |node|
20
- write_node node
21
- end
20
+ def end_graph
21
+ # Nothing to do
22
22
  end
23
23
 
24
- def write_node(node)
25
- @stream.puts " node#{node.id}[#{node.props[:label].inspect}]"
24
+ def start_subgraph(id)
25
+ @stream.puts " subgraph B#{id}"
26
26
  end
27
27
 
28
- # Write edge declarations.
28
+ def end_subgraph
29
+ @stream.puts " end"
30
+ end
29
31
 
30
- def write_edges(graph)
31
- graph.edges.each do |edge|
32
- write_edge edge
32
+ def output_node(indent, id, attrs)
33
+ case attrs[:shape]
34
+ when "rectangle"
35
+ shape = ["(", ")"]
36
+ when "oval"
37
+ shape = ["([", "])"]
38
+ when "diamond"
39
+ shape = ["{{", "}}"]
33
40
  end
41
+ @stream.puts "#{indent}#{id}#{shape[0]}#{attrs[:label].inspect}#{shape[1]}"
42
+ @stream.puts "#{indent}style #{id} fill:#{attrs[:fillcolor]},stroke:#{attrs[:color]},color:#{attrs[:fontcolor]};"
34
43
  end
35
44
 
36
- def write_edge(edge)
37
- @stream.puts " node#{edge.from.id} --> node#{edge.to.id}"
45
+ def output_edge(from, to, attrs)
46
+ @stream.puts " #{from} --> #{to}"
47
+ link_counter = (@link_style_counter += 1) - 1
48
+ penwidth = attrs[:penwidth]
49
+ @stream.puts " linkStyle #{link_counter} stroke:#{attrs[:color]},stroke-width:#{penwidth}px;"
38
50
  end
39
51
  end
40
52
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Seafoam
2
4
  module Passes
3
5
  # The fallback pass always applies, and adds some basic properties.
@@ -5,21 +7,23 @@ module Seafoam
5
7
  # can emit a graph with 'label' properties and we can do something useful
6
8
  # with it.
7
9
  class FallbackPass < Pass
8
- def self.applies?(_graph)
9
- true
10
+ class << self
11
+ def applies?(_graph)
12
+ true
13
+ end
10
14
  end
11
15
 
12
16
  def apply(graph)
13
17
  graph.nodes.each_value do |node|
14
- if node.props[:label].nil? && node.props['label']
15
- node.props[:label] = node.props['label']
18
+ if node.props[:label].nil? && node.props["label"]
19
+ node.props[:label] = node.props["label"]
16
20
  end
17
21
 
18
- node.props[:kind] ||= 'other'
22
+ node.props[:kind] ||= "other"
19
23
  end
20
24
 
21
25
  graph.edges.each do |edge|
22
- edge.props[:kind] ||= 'other'
26
+ edge.props[:kind] ||= "other"
23
27
  end
24
28
  end
25
29
  end