Class: RailroadDiagrams::NonTerminal
- Inherits:
-
DiagramItem
show all
- Defined in:
- lib/railroad_diagrams/non_terminal.rb,
sig/generated/railroad_diagrams/non_terminal.rbs
Overview
A named grammar rule, optionally linked to its definition.
Instance Attribute Summary
Attributes inherited from DiagramItem
#attrs, #children, #down, #height, #needs_space, #up, #width
Instance Method Summary
collapse
-
#add_background_rect(x, y) ⇒ void
-
#add_connecting_paths(x, y, left_gap, right_gap) ⇒ void
-
#add_text_element(x, y) ⇒ void
-
#child_nodes ⇒ Object
-
#format(x, y, width) ⇒ NonTerminal
-
#initialize(text, legacy_href = Deprecation::UNSET, legacy_title = Deprecation::UNSET, href: Deprecation::UNSET, title: Deprecation::UNSET, cls: '', id: nil, attrs: {}) ⇒ NonTerminal
constructor
rubocop:disable-next Metrics/ParameterLists.
-
#measure(context) ⇒ Object
-
#render_svg(context, x, y, width) ⇒ Object
-
#render_text(context) ⇒ Object
-
#text_diagram ⇒ TextDiagram
-
#to_s ⇒ String
Methods inherited from DiagramItem
#==, #add, #apply_user_attributes, #container_element?, #determine_gaps, #each_node, #hash, inherited, #inspect, #structural_state, #to_str, #walk, #wrap_string, #write_children, #write_opening_tag, #write_standalone, #write_svg
Constructor Details
#initialize(text, legacy_href = Deprecation::UNSET, legacy_title = Deprecation::UNSET, href: Deprecation::UNSET, title: Deprecation::UNSET, cls: '', id: nil, attrs: {}) ⇒ NonTerminal
rubocop:disable-next Metrics/ParameterLists
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/railroad_diagrams/non_terminal.rb', line 15
def initialize(text, legacy_href = Deprecation::UNSET, legacy_title = Deprecation::UNSET,
href: Deprecation::UNSET, title: Deprecation::UNSET, cls: '', id: nil, attrs: {})
super('g', attrs: { 'class' => "non-terminal #{cls}" }, id: id, data_attrs: attrs)
@text = text.to_s
@href = Deprecation.positional_argument('href', legacy_href, href)
@title = Deprecation.positional_argument('title', legacy_title, title)
@cls = cls
@width = (Unicode::DisplayWidth.of(@text) * CHAR_WIDTH) + 20
@up = 11
@down = 11
@needs_space = true
end
|
Instance Method Details
#add_background_rect(x, y) ⇒ void
This method returns an undefined value.
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/railroad_diagrams/non_terminal.rb', line 99
def add_background_rect(x, y)
DiagramItem.new(
'rect',
attrs: {
'x' => x,
'y' => y - 11,
'width' => @width,
'height' => @up + @down
}
).add(self)
end
|
#add_connecting_paths(x, y, left_gap, right_gap) ⇒ void
This method returns an undefined value.
91
92
93
94
|
# File 'lib/railroad_diagrams/non_terminal.rb', line 91
def add_connecting_paths(x, y, left_gap, right_gap)
Path.new(x, y).h(left_gap).add(self)
Path.new(x + left_gap + @width, y).h(right_gap).add(self)
end
|
#add_text_element(x, y) ⇒ void
This method returns an undefined value.
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/railroad_diagrams/non_terminal.rb', line 114
def add_text_element(x, y)
text = DiagramItem.new(
'text',
attrs: { 'x' => x + (@width / 2), 'y' => y + 4 },
text: @text
)
if @href
a = DiagramItem.new('a', attrs: { 'xlink:href' => @href }, text: text).add(self)
text.add(a)
else
text.add(self)
end
DiagramItem.new('title', attrs: {}, text: @title).add(self) if @title
end
|
#child_nodes ⇒ Object
80
81
82
|
# File 'lib/railroad_diagrams/non_terminal.rb', line 80
def child_nodes
[]
end
|
37
38
39
40
41
42
43
|
# File 'lib/railroad_diagrams/non_terminal.rb', line 37
def format(x, y, width)
left_gap, right_gap = determine_gaps(width, @width)
add_connecting_paths(x, y, left_gap, right_gap)
add_background_rect(x + left_gap, y)
add_text_element(x + left_gap, y)
self
end
|
#measure(context) ⇒ Object
50
51
52
53
|
# File 'lib/railroad_diagrams/non_terminal.rb', line 50
def measure(context)
Metrics.new(width: context.text_width(@text, :label) + 20,
up: 11, height: 0, down: 11, needs_space: true)
end
|
#render_svg(context, x, y, width) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/railroad_diagrams/non_terminal.rb', line 55
def render_svg(context, x, y, width)
metrics = context.metrics(self)
left_gap, right_gap = context.gaps(width, metrics.width)
group = Svg::Element.new('g', @attrs.dup)
group << (Svg::Element.new('title') << Svg::TextNode.new(@title)) if @title
group << Svg::Element.new('path', { 'd' => Svg::PathData.new(x, y).h(left_gap) }, self_closing: true)
group << Svg::Element.new('path', { 'd' => Svg::PathData.new(x + left_gap + metrics.width, y).h(right_gap) }, self_closing: true)
rect = { 'x' => x + left_gap, 'y' => y - 11, 'width' => metrics.width, 'height' => 22 }
group << Svg::Element.new('rect', rect)
text = Svg::Element.new('text', { 'x' => x + left_gap + (metrics.width / 2), 'y' => y + 4 })
text << Svg::TextNode.new(@text)
if (attrs = context.link_attrs(@href))
link = Svg::Element.new('a', attrs)
link << text
group << link
else
group << text
end
group
end
|
#render_text(context) ⇒ Object
76
77
78
|
# File 'lib/railroad_diagrams/non_terminal.rb', line 76
def render_text(context)
TextDiagram.rect(@text, parts: context.parts)
end
|
#text_diagram ⇒ TextDiagram
46
47
48
|
# File 'lib/railroad_diagrams/non_terminal.rb', line 46
def text_diagram
TextDiagram.rect(@text)
end
|
#to_s ⇒ String
29
30
31
|
# File 'lib/railroad_diagrams/non_terminal.rb', line 29
def to_s
"NonTerminal(#{@text}, href=#{@href}, title=#{@title}, cls=#{@cls})"
end
|