seafoam 0.11 → 0.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Seafoam
4
+ # A writer from graphs to Markdown format, with an embedded Mermaid graph.
5
+ class MarkdownWriter
6
+ def initialize(stream)
7
+ @stream = stream
8
+ end
9
+
10
+ # Write a graph.
11
+ def write_graph(graph)
12
+ @stream.puts "```mermaid"
13
+ mermaid = MermaidWriter.new(@stream)
14
+ mermaid.write_graph(graph)
15
+ @stream.puts "```"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Seafoam
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
11
+ end
12
+
13
+ protected
14
+
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
19
+
20
+ def end_graph
21
+ # Nothing to do
22
+ end
23
+
24
+ def start_subgraph(id)
25
+ @stream.puts " subgraph B#{id}"
26
+ end
27
+
28
+ def end_subgraph
29
+ @stream.puts " end"
30
+ end
31
+
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 = ["{{", "}}"]
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]};"
43
+ end
44
+
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;"
50
+ end
51
+ end
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