mermaid 0.0.5 → 0.1.0

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.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +29 -0
  3. data/README.md +120 -111
  4. data/Rakefile +35 -3
  5. data/lib/mermaid/inline_title.rb +13 -0
  6. data/lib/mermaid/renderer.rb +105 -0
  7. data/lib/mermaid/renderers/class_renderer.rb +56 -0
  8. data/lib/mermaid/renderers/er_renderer.rb +43 -0
  9. data/lib/mermaid/renderers/flowchart_renderer.rb +27 -0
  10. data/lib/mermaid/renderers/gantt_renderer.rb +31 -0
  11. data/lib/mermaid/renderers/gitgraph_renderer.rb +50 -0
  12. data/lib/mermaid/renderers/pie_renderer.rb +34 -0
  13. data/lib/mermaid/renderers/state_renderer.rb +37 -0
  14. data/lib/mermaid/renderers/timeline_renderer.rb +24 -0
  15. data/lib/mermaid/to_mermaid.rb +9 -0
  16. data/lib/mermaid/version.rb +1 -1
  17. data/lib/mermaid.rb +8 -26
  18. data/sig/mermaid.rbs +64 -0
  19. metadata +26 -89
  20. data/lib/mermaid/class_diagram_renderer.rb +0 -45
  21. data/lib/mermaid/element_renderers/edge_renderer.rb +0 -19
  22. data/lib/mermaid/element_renderers/erd_entity_renderer.rb +0 -23
  23. data/lib/mermaid/element_renderers/erd_relationship_renderer.rb +0 -44
  24. data/lib/mermaid/element_renderers/node_renderer.rb +0 -16
  25. data/lib/mermaid/element_renderers/relationship_renderer.rb +0 -24
  26. data/lib/mermaid/element_renderers/slice_renderer.rb +0 -16
  27. data/lib/mermaid/element_renderers/state_renderer.rb +0 -15
  28. data/lib/mermaid/element_renderers/task_renderer.rb +0 -15
  29. data/lib/mermaid/element_renderers/transition_renderer.rb +0 -17
  30. data/lib/mermaid/er_diagram_renderer.rb +0 -24
  31. data/lib/mermaid/flowchart_diagram_renderer.rb +0 -23
  32. data/lib/mermaid/gantt_diagram_renderer.rb +0 -20
  33. data/lib/mermaid/gitgraph_diagram_renderer.rb +0 -113
  34. data/lib/mermaid/pie_diagram_renderer.rb +0 -16
  35. data/lib/mermaid/state_diagram_renderer.rb +0 -42
  36. data/lib/mermaid/timeline_diagram_renderer.rb +0 -29
  37. data/sig/diagrams/base.rbs +0 -8
  38. data/sig/diagrams/class_diagram.rbs +0 -18
  39. data/sig/diagrams/elements/edge.rbs +0 -16
  40. data/sig/diagrams/elements/erd_entity.rbs +0 -11
  41. data/sig/diagrams/elements/erd_relationship.rbs +0 -11
  42. data/sig/diagrams/elements/node.rbs +0 -15
  43. data/sig/diagrams/elements/relationship.rbs +0 -15
  44. data/sig/diagrams/elements/slice.rbs +0 -16
  45. data/sig/diagrams/elements/state.rbs +0 -15
  46. data/sig/diagrams/elements/task.rbs +0 -17
  47. data/sig/diagrams/elements/transition.rbs +0 -16
  48. data/sig/diagrams/er_diagram.rbs +0 -9
  49. data/sig/diagrams/flowchart_diagram.rbs +0 -14
  50. data/sig/diagrams/gantt_diagram.rbs +0 -14
  51. data/sig/diagrams/pie_diagram.rbs +0 -14
  52. data/sig/diagrams/state_diagram.rbs +0 -18
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mermaid
4
- module ElementRenderers
5
- # Module containing Mermaid rendering logic for Diagrams::ElementRenderers::Task
6
- module TaskRenderer
7
- # @!method to_mermaid_fragment
8
- # Generates the Mermaid fragment for a Gantt task.
9
- # @return [String] Mermaid syntax fragment.
10
- def to_mermaid_fragment
11
- "#{label} :#{id}, #{start}, #{duration}"
12
- end
13
- end
14
- end
15
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mermaid
4
- module ElementRenderers
5
- # Module containing Mermaid rendering logic for Diagrams::Elements::Transition
6
- module TransitionRenderer
7
- # @!method to_mermaid_fragment
8
- # Generates the Mermaid fragment for a transition.
9
- # @return [String] Mermaid syntax fragment.
10
- def to_mermaid_fragment
11
- from_node = source_state_id == '*' ? '[*]' : source_state_id
12
- to_node = target_state_id == '*' ? '[*]' : target_state_id
13
- label ? "#{from_node} --> #{to_node} : #{label}" : "#{from_node} --> #{to_node}"
14
- end
15
- end
16
- end
17
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mermaid
4
- # Module containing Mermaid rendering logic for ER Diagrams
5
- module ERDiagramRenderer
6
- # Generates the Mermaid syntax for the ER diagram.
7
- # Prepended to Diagrams::ERDiagram.
8
- # @return [String] The Mermaid syntax string.
9
- def to_mermaid
10
- mermaid_string = +"erDiagram\n"
11
-
12
- # Diagrams::ERDiagram stores entities in a hash, access values
13
- entities.each_value do |entity|
14
- mermaid_string << entity.to_mermaid_fragment
15
- end
16
-
17
- relationships.each do |rel|
18
- mermaid_string << rel.to_mermaid_fragment
19
- end
20
-
21
- mermaid_string.strip
22
- end
23
- end
24
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mermaid
4
- # Module containing Mermaid rendering logic for Diagrams::FlowchartDiagram
5
- module FlowchartDiagramRenderer
6
- # @!method to_mermaid
7
- # Generates the Mermaid syntax for the flowchart diagram.
8
- # @return [String] The Mermaid syntax string.
9
- def to_mermaid
10
- mermaid_string = +"graph TD\n"
11
-
12
- nodes.each do |node|
13
- mermaid_string << " #{node.to_mermaid_fragment}\n" # Ensure newline after node
14
- end
15
-
16
- edges.each do |edge|
17
- mermaid_string << " #{edge.to_mermaid_fragment}\n" # Ensure newline after edge
18
- end
19
-
20
- mermaid_string # Return the full string including final newline
21
- end
22
- end
23
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mermaid
4
- # Module containing Mermaid rendering logic for Diagrams::GanttDiagram
5
- module GanttDiagramRenderer
6
- # @!method to_mermaid
7
- # Generates the Mermaid syntax for the Gantt chart.
8
- # @return [String] The Mermaid syntax string.
9
- def to_mermaid
10
- mermaid_string = +"gantt\n"
11
- mermaid_string << "title #{title}\n" if title && !title.empty?
12
- sections.each do |section|
13
- # Always add section title
14
- mermaid_string << "section #{section.title}\n"
15
- section.tasks&.each { |task| mermaid_string << task.to_mermaid_fragment << "\n" }
16
- end
17
- mermaid_string.strip
18
- end
19
- end
20
- end
@@ -1,113 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mermaid
4
- # Module containing Mermaid rendering logic for Diagrams::GitgraphDiagram
5
- module GitgraphDiagramRenderer
6
- # @!method to_mermaid
7
- # Generates the Mermaid syntax for the gitgraph diagram.
8
- # @return [String] The Mermaid syntax string.
9
- def to_mermaid
10
- mermaid_string = +"gitGraph\n"
11
- processed_commits = Set.new # Track the current branch in Mermaid output (defaults to 'master')
12
-
13
- # Determine initial branch (default to 'main') and add checkout
14
- first_commit_id = commit_order.first
15
- # Default to 'main' if no commits or first commit doesn't specify branch
16
- # Default to 'main' if no commits exist or first commit doesn't specify branch
17
- initial_branch = (first_commit_id ? commits[first_commit_id]&.branch_name : nil) || 'master' # Default to 'master'
18
- mermaid_string << " checkout #{initial_branch}\n"
19
- current_mermaid_branch = initial_branch
20
-
21
- commit_order.each do |commit_id|
22
- commit = commits[commit_id]
23
- next unless commit # Skip if commit data is missing
24
-
25
- # --- Determine Branch Context ---
26
- commit.branch_name
27
-
28
- # --- Handle Branch Creation & Checkout ---
29
- # Determine the branch this commit *should* be on based on its branch_name attribute
30
- target_branch_name = commit.branch_name
31
-
32
- # Check if a new branch *starts* at this commit's parent
33
- parent_commit_id = commit.parent_ids.first
34
- new_branch_starting_here = nil
35
- # Only consider branching if the target is NOT master and IS different from current
36
- if parent_commit_id && target_branch_name != 'master' && target_branch_name != current_mermaid_branch
37
- # Check if a branch object exists that starts at the parent and matches the target name
38
- new_branch_starting_here = branches.values.find do |b|
39
- b.start_commit_id == parent_commit_id && b.name == target_branch_name
40
- end
41
- end
42
-
43
- if new_branch_starting_here
44
- # Ensure we are on the parent branch before creating the new one
45
- parent_commit = commits[parent_commit_id]
46
- parent_branch_name = parent_commit&.branch_name
47
- if parent_branch_name && parent_branch_name != current_mermaid_branch
48
- mermaid_string << " checkout #{parent_branch_name}\n"
49
- current_mermaid_branch = parent_branch_name
50
- end
51
- mermaid_string << " branch #{target_branch_name}\n"
52
- # No checkout here, the next check handles it
53
- end
54
-
55
- # Now, handle checkout if needed for the current commit
56
- if target_branch_name != current_mermaid_branch
57
- # Always checkout if the branch differs from the last known Mermaid branch
58
- mermaid_string << " checkout #{target_branch_name}\n"
59
- current_mermaid_branch = target_branch_name
60
- end
61
-
62
- # --- Handle Merge ---
63
- if commit.type == :MERGE && commit.parent_ids.length == 2 && !processed_commits.include?(commit_id)
64
- # Identify both parents
65
- parent1_id = commit.parent_ids[0]
66
- parent2_id = commit.parent_ids[1]
67
- parent1_commit = commits[parent1_id]
68
- parent2_commit = commits[parent2_id]
69
-
70
- # Find the parent commit whose branch is NOT the current branch
71
- source_commit = if parent1_commit&.branch_name != current_mermaid_branch
72
- parent1_commit
73
- elsif parent2_commit&.branch_name != current_mermaid_branch
74
- parent2_commit
75
- end
76
- source_branch_name = source_commit&.branch_name
77
-
78
- # Ensure source branch is valid and different from target
79
- if source_branch_name && source_branch_name != current_mermaid_branch
80
- mermaid_string << " merge #{source_branch_name}"
81
- mermaid_string << " id: \"#{commit.id}\""
82
- mermaid_string << " tag: \"#{commit.tag}\"" if commit.tag
83
- mermaid_string << "\n"
84
- processed_commits.add(commit_id)
85
- else
86
- # If source branch can't be determined, generate a regular commit as fallback
87
- warn "Could not determine source branch for merge commit #{commit_id}. Generating standard commit."
88
- generate_commit_line(mermaid_string, commit, processed_commits) unless processed_commits.include?(commit_id)
89
- end
90
-
91
- # --- Handle Standard Commit ---
92
- elsif !processed_commits.include?(commit_id)
93
- generate_commit_line(mermaid_string, commit, processed_commits)
94
- end
95
- end
96
-
97
- mermaid_string.strip
98
- end
99
-
100
- private
101
-
102
- # Helper to generate a standard commit line
103
- def generate_commit_line(mermaid_string, commit, processed_commits)
104
- mermaid_string << ' commit'
105
- mermaid_string << " id: \"#{commit.id}\""
106
- mermaid_string << " tag: \"#{commit.tag}\"" if commit.tag
107
- mermaid_string << " type: #{commit.type.to_s.upcase}" unless commit.type == :NORMAL
108
- # Mermaid doesn't directly support messages in the command, they appear on the graph
109
- mermaid_string << "\n"
110
- processed_commits.add(commit.id)
111
- end
112
- end
113
- end
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mermaid
4
- # Module containing Mermaid rendering logic for Diagrams::PieDiagram
5
- module PieDiagramRenderer
6
- # @!method to_mermaid
7
- # Generates the Mermaid syntax for the pie chart.
8
- # @return [String] The Mermaid syntax string.
9
- def to_mermaid
10
- mermaid_string = +"pie\n"
11
- mermaid_string << "title #{@title}\n" if @title && !@title.empty?
12
- slices.each { |slice| mermaid_string << slice.to_mermaid_fragment << "\n" }
13
- mermaid_string.strip
14
- end
15
- end
16
- end
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mermaid
4
- # Module containing Mermaid rendering logic for Diagrams::StateDiagram
5
- module StateDiagramRenderer
6
- # @!method to_mermaid
7
- # Generates the Mermaid syntax for the state diagram.
8
- # @return [String] The Mermaid syntax string.
9
- def to_mermaid
10
- mermaid_string = +"stateDiagram-v2\n" # Use mutable string
11
-
12
- # Define states with labels first
13
- states.each do |state|
14
- mermaid_string << " #{state.to_mermaid_fragment}\n" if state.label
15
- end
16
-
17
- # Add transitions
18
- transitions.each do |transition|
19
- mermaid_string << " #{transition.to_mermaid_fragment}\n"
20
- end
21
-
22
- mermaid_string.strip # Remove potential trailing newline
23
- end
24
-
25
- # Patch validation to allow '*' for start/end states used by Mermaid
26
- private
27
-
28
- def validate_elements!
29
- # Temporarily filter out transitions involving '*' before calling original validation
30
- original_transitions = @transitions
31
- @transitions = original_transitions.reject do |t|
32
- t.source_state_id == '*' || t.target_state_id == '*'
33
- end
34
-
35
- # Call original validation using super
36
- super # Calls Diagrams::StateDiagram#validate_elements!
37
- ensure
38
- # Restore original transitions array regardless of validation outcome
39
- @transitions = original_transitions
40
- end
41
- end
42
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mermaid
4
- # Module containing Mermaid rendering logic for Diagrams::TimelineDiagram
5
- module TimelineDiagramRenderer
6
- # @!method to_mermaid
7
- # Generates the Mermaid syntax for the timeline diagram.
8
- # @return [String] The Mermaid syntax string.
9
- def to_mermaid
10
- mermaid_string = +"timeline\n"
11
- mermaid_string << "title #{@title}\n" if @title && !@title.empty?
12
-
13
- sections.each do |section|
14
- # Don't output the default section title if it's the only one and has no specific title
15
- unless sections.size == 1 && section.title == Diagrams::TimelineDiagram::DEFAULT_SECTION_TITLE
16
- mermaid_string << "section #{section.title}\n"
17
- end
18
-
19
- section.periods.each do |period|
20
- # Format: Period Label : Event 1 : Event 2 ...
21
- event_descriptions = period.events.map(&:description).join(' : ')
22
- mermaid_string << " #{period.label} : #{event_descriptions}\n"
23
- end
24
- end
25
-
26
- mermaid_string.strip
27
- end
28
- end
29
- end
@@ -1,8 +0,0 @@
1
- # RBS signatures for patches applied to the 'diagram' gem's base class
2
- module Diagrams
3
- class Base
4
-
5
- # Generates the Mermaid syntax representation of the diagram.
6
- def to_mermaid: () -> String
7
- end
8
- end
@@ -1,18 +0,0 @@
1
- # RBS signatures for patches applied to the 'diagram' gem's ClassDiagram class
2
- module Diagrams
3
- class ClassDiagram < Base
4
-
5
- # Generates the Mermaid syntax for the class diagram.
6
- def to_mermaid: () -> String
7
-
8
- # Original methods from diagram gem (if known/needed for context)
9
- # attr_reader classes: Array[Elements::ClassEntity]
10
- # attr_reader relationships: Array[Elements::Relationship]
11
- # def initialize: (classes: Array[Elements::ClassEntity], relationships: Array[Elements::Relationship], ?version: untyped) -> void
12
- # ... other methods ...
13
-
14
- # Patched private method (optional to include in RBS)
15
- # private
16
- # def format_class_entity: (Elements::ClassEntity) -> String
17
- end
18
- end
@@ -1,16 +0,0 @@
1
- # RBS signatures for patches applied to the 'diagram' gem's Edge element class
2
- module Diagrams
3
- module Element
4
- class Edge
5
-
6
- # Generates the Mermaid fragment for a flowchart edge.
7
- def to_mermaid_fragment: () -> String
8
-
9
- # Original attributes/methods (if known/needed)
10
- # attr_reader source_id: String
11
- # attr_reader target_id: String
12
- # attr_reader label: String?
13
- # ...
14
- end
15
- end
16
- end
@@ -1,11 +0,0 @@
1
- # Augmenting the class from the 'diagrams' gem
2
- module Diagrams
3
- module Element
4
- class ERDEntity
5
- # Method added by prepending Mermaid::Element::ERDEntityRenderer
6
- # Generates the Mermaid fragment for this entity.
7
- # @return [String] The Mermaid syntax fragment for the entity.
8
- def to_mermaid_fragment: () -> String
9
- end
10
- end
11
- end
@@ -1,11 +0,0 @@
1
- # Augmenting the class from the 'diagrams' gem
2
- module Diagrams
3
- module Element
4
- class ERDRelationship
5
- # Method added by prepending Mermaid::Element::ERDRelationshipRenderer
6
- # Generates the Mermaid fragment for this relationship.
7
- # @return [String] The Mermaid syntax fragment for the relationship.
8
- def to_mermaid_fragment: () -> String
9
- end
10
- end
11
- end
@@ -1,15 +0,0 @@
1
- # RBS signatures for patches applied to the 'diagram' gem's Node element class
2
- module Diagrams
3
- module Element
4
- class Node
5
-
6
- # Generates the Mermaid fragment for a flowchart node.
7
- def to_mermaid_fragment: () -> String
8
-
9
- # Original attributes/methods (if known/needed)
10
- # attr_reader id: String
11
- # attr_reader label: String
12
- # ...
13
- end
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- # RBS signatures for patches applied to the 'diagram' gem's Relationship element class
2
- module Diagrams
3
- module Element
4
- class Relationship
5
- def to_mermaid_fragment: () -> String
6
-
7
- # Original attributes/methods (if known/needed)
8
- # attr_reader source_class_name: String
9
- # attr_reader target_class_name: String
10
- # attr_reader type: String
11
- # attr_reader label: String?
12
- # ...
13
- end
14
- end
15
- end
@@ -1,16 +0,0 @@
1
- # RBS signatures for patches applied to the 'diagram' gem's Slice element class
2
- module Diagrams
3
- module Element
4
- class Slice
5
-
6
- # Generates the Mermaid fragment for a pie chart slice.
7
- def to_mermaid_fragment: () -> String
8
-
9
- # Original attributes/methods (if known/needed)
10
- # attr_reader label: String
11
- # attr_reader value: Float
12
- # attr_reader percentage: Float?
13
- # ...
14
- end
15
- end
16
- end
@@ -1,15 +0,0 @@
1
- # RBS signatures for patches applied to the 'diagram' gem's State element class
2
- module Diagrams
3
- module Element
4
- class State
5
-
6
- # Generates the Mermaid fragment for a state definition.
7
- def to_mermaid_fragment: () -> String
8
-
9
- # Original attributes/methods (if known/needed)
10
- # attr_reader id: String
11
- # attr_reader label: String?
12
- # ...
13
- end
14
- end
15
- end
@@ -1,17 +0,0 @@
1
- # RBS signatures for patches applied to the 'diagram' gem's Task element class
2
- module Diagrams
3
- module Element
4
- class Task
5
-
6
- # Generates the Mermaid fragment for a Gantt task.
7
- def to_mermaid_fragment: () -> String
8
-
9
- # Original attributes/methods (if known/needed)
10
- # attr_reader id: String
11
- # attr_reader name: String
12
- # attr_reader start_date: String
13
- # attr_reader end_date: String
14
- # ...
15
- end
16
- end
17
- end
@@ -1,16 +0,0 @@
1
- # RBS signatures for patches applied to the 'diagram' gem's Transition element class
2
- module Diagrams
3
- module Element
4
- class Transition
5
-
6
- # Generates the Mermaid fragment for a transition.
7
- def to_mermaid_fragment: () -> String
8
-
9
- # Original attributes/methods (if known/needed)
10
- # attr_reader source_state_id: String
11
- # attr_reader target_state_id: String
12
- # attr_reader label: String?
13
- # ...
14
- end
15
- end
16
- end
@@ -1,9 +0,0 @@
1
- # Augmenting the class from the 'diagrams' gem
2
- module Diagrams
3
- class ERDiagram < Base
4
- # Method added by prepending Mermaid::ERDiagramRenderer
5
- # Generates the Mermaid syntax for the ER diagram.
6
- # @return [String] The Mermaid syntax string.
7
- def to_mermaid: () -> String
8
- end
9
- end
@@ -1,14 +0,0 @@
1
- # RBS signatures for patches applied to the 'diagram' gem's FlowchartDiagram class
2
- module Diagrams
3
- class FlowchartDiagram < Base
4
-
5
- # Generates the Mermaid syntax for the flowchart diagram.
6
- def to_mermaid: () -> String
7
-
8
- # Original methods from diagram gem (if known/needed for context)
9
- # attr_reader nodes: Array[Elements::Node]
10
- # attr_reader edges: Array[Elements::Edge]
11
- # def initialize: (nodes: Array[Elements::Node], edges: Array[Elements::Edge], ?version: untyped) -> void
12
- # ... other methods ...
13
- end
14
- end
@@ -1,14 +0,0 @@
1
- # RBS signatures for patches applied to the 'diagram' gem's GanttDiagram class
2
- module Diagrams
3
- class GanttDiagram < Base
4
-
5
- # Generates the Mermaid syntax for the Gantt chart.
6
- def to_mermaid: () -> String
7
-
8
- # Original methods from diagram gem (if known/needed for context)
9
- # attr_reader title: String
10
- # attr_reader tasks: Array[Elements::Task]
11
- # def initialize: (?title: String, ?tasks: Array[Elements::Task], ?version: untyped) -> void
12
- # ... other methods ...
13
- end
14
- end
@@ -1,14 +0,0 @@
1
- # RBS signatures for patches applied to the 'diagram' gem's PieDiagram class
2
- module Diagrams
3
- class PieDiagram < Base
4
-
5
- # Generates the Mermaid syntax for the pie chart.
6
- def to_mermaid: () -> String
7
-
8
- # Original methods from diagram gem (if known/needed for context)
9
- # attr_reader title: String
10
- # attr_reader slices: Array[Elements::Slice]
11
- # def initialize: (?title: String, ?slices: Array[Elements::Slice], ?version: untyped) -> void
12
- # ... other methods ...
13
- end
14
- end
@@ -1,18 +0,0 @@
1
- # RBS signatures for patches applied to the 'diagram' gem's StateDiagram class
2
- module Diagrams
3
- class StateDiagram < Base
4
-
5
- # Generates the Mermaid syntax for the state diagram.
6
- def to_mermaid: () -> String
7
-
8
- # Original methods from diagram gem (if known/needed for context)
9
- # attr_reader states: Array[Elements::State]
10
- # attr_reader transitions: Array[Elements::Transition]
11
- # def initialize: (states: Array[Elements::State], transitions: Array[Elements::Transition], ?version: untyped) -> void
12
- # ... other methods ...
13
-
14
- # Patched private method (optional to include in RBS)
15
- # private
16
- # def validate_elements!: () -> void
17
- end
18
- end