rsyntaxtree 1.0.8 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +77 -0
- data/.solargraph.yml +22 -0
- data/.tags +211 -10
- data/Gemfile +10 -5
- data/README.md +3 -2
- data/Rakefile +3 -1
- data/bin/rsyntaxtree +42 -50
- data/docs/Gemfile +3 -1
- data/docs/_layouts/default.html +1 -1
- data/lib/rsyntaxtree/base_graph.rb +260 -264
- data/lib/rsyntaxtree/element.rb +167 -179
- data/lib/rsyntaxtree/elementlist.rb +105 -124
- data/lib/rsyntaxtree/markup_parser.rb +82 -93
- data/lib/rsyntaxtree/string_parser.rb +221 -237
- data/lib/rsyntaxtree/svg_graph.rb +158 -197
- data/lib/rsyntaxtree/utils.rb +59 -63
- data/lib/rsyntaxtree/version.rb +3 -2
- data/lib/rsyntaxtree.rb +174 -177
- data/rsyntaxtree.gemspec +10 -10
- data/test/markup_parser_test.rb +3 -2
- metadata +23 -21
@@ -1,264 +1,260 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
#
|
8
|
-
#
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
@
|
23
|
-
|
24
|
-
@
|
25
|
-
|
26
|
-
@
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
@
|
32
|
-
|
33
|
-
@
|
34
|
-
|
35
|
-
@
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
return target.width
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
target.
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
parent.children.each do |c|
|
115
|
-
|
116
|
-
end
|
117
|
-
max
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
target.
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
end
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
end
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
return {height: height, width: width}
|
262
|
-
end
|
263
|
-
end
|
264
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#==========================
|
4
|
+
# graph.rb
|
5
|
+
#==========================
|
6
|
+
#
|
7
|
+
# Image utility functions to inspect text font metrics
|
8
|
+
# Copyright (c) 2007-2023 Yoichiro Hasebe <yohasebe@gmail.com>
|
9
|
+
|
10
|
+
require_relative 'utils'
|
11
|
+
|
12
|
+
module RSyntaxTree
|
13
|
+
class BaseGraph
|
14
|
+
def initialize(element_list, params, global)
|
15
|
+
@global = global
|
16
|
+
@element_list = element_list
|
17
|
+
@symmetrize = params[:symmetrize]
|
18
|
+
|
19
|
+
if params[:color]
|
20
|
+
@col_node = "blue"
|
21
|
+
@col_leaf = "green"
|
22
|
+
@col_path = "purple"
|
23
|
+
else
|
24
|
+
@col_node = "black"
|
25
|
+
@col_leaf = "black"
|
26
|
+
@col_path = "black"
|
27
|
+
end
|
28
|
+
|
29
|
+
@col_bg = "none"
|
30
|
+
@col_fg = "black"
|
31
|
+
@col_line = "black"
|
32
|
+
|
33
|
+
@leafstyle = params[:leafstyle]
|
34
|
+
@fontset = params[:fontset]
|
35
|
+
@fontsize = params[:fontsize]
|
36
|
+
end
|
37
|
+
|
38
|
+
def calculate_level
|
39
|
+
@element_list.get_elements.select { |e| e.type == 2 }.each do |e|
|
40
|
+
e.level = @element_list.get_id(e.parent).level + 1
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def calculate_width(id = 1)
|
45
|
+
target = @element_list.get_id(id)
|
46
|
+
if target.children.empty?
|
47
|
+
target.width = target.content_width + @global[:h_gap_between_nodes] * 4
|
48
|
+
|
49
|
+
parent = @element_list.get_id(target.parent)
|
50
|
+
while parent && parent.children.size == 1
|
51
|
+
w = parent.content_width
|
52
|
+
target.width = w + @global[:h_gap_between_nodes] * 4 if w > target.content_width
|
53
|
+
parent = @element_list.get_id(parent.parent)
|
54
|
+
end
|
55
|
+
target.width
|
56
|
+
else
|
57
|
+
return target.width if target.width != 0
|
58
|
+
|
59
|
+
accum_array = []
|
60
|
+
target.children.each do |c|
|
61
|
+
accum_array << calculate_width(c)
|
62
|
+
end
|
63
|
+
accum_width = if @symmetrize
|
64
|
+
accum_array.max * target.children.size
|
65
|
+
else
|
66
|
+
accum_array.sum
|
67
|
+
end
|
68
|
+
|
69
|
+
target.width = [accum_width, target.content_width].max
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def calculate_height(id = 1)
|
74
|
+
target = @element_list.get_id(id)
|
75
|
+
if id == 1
|
76
|
+
target.vertical_indent = 0
|
77
|
+
else
|
78
|
+
parent = @element_list.get_id(target.parent)
|
79
|
+
|
80
|
+
vertical_indent = if !target.triangle &&
|
81
|
+
@leafstyle == "nothing" &&
|
82
|
+
ETYPE_LEAF == target.type &&
|
83
|
+
parent.children.size == 1
|
84
|
+
parent.vertical_indent + parent.content_height
|
85
|
+
else
|
86
|
+
parent.vertical_indent + parent.content_height + @global[:height_connector]
|
87
|
+
end
|
88
|
+
|
89
|
+
target.vertical_indent = vertical_indent
|
90
|
+
end
|
91
|
+
|
92
|
+
if target.children.empty?
|
93
|
+
target.height = target.content_height
|
94
|
+
target.vertical_indent + target.content_height
|
95
|
+
else
|
96
|
+
accum_array = []
|
97
|
+
target.children.each do |c|
|
98
|
+
accum_array << calculate_height(c)
|
99
|
+
end
|
100
|
+
target.height = accum_array.max - target.vertical_indent
|
101
|
+
accum_array.max
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def make_balance(id = 1)
|
106
|
+
target = @element_list.get_id(id)
|
107
|
+
if target.children.empty?
|
108
|
+
parent = @element_list.get_id(target.parent)
|
109
|
+
accum_array = []
|
110
|
+
parent.children.each do |c|
|
111
|
+
accum_array << @element_list.get_id(c).width
|
112
|
+
end
|
113
|
+
max = accum_array.max
|
114
|
+
parent.children.each do |c|
|
115
|
+
@element_list.get_id(c).width = max
|
116
|
+
end
|
117
|
+
max
|
118
|
+
else
|
119
|
+
accum_array = []
|
120
|
+
target.children.each do |c|
|
121
|
+
accum_array << make_balance(c)
|
122
|
+
end
|
123
|
+
accum_width = accum_array.max
|
124
|
+
max = [accum_width, target.content_width].max
|
125
|
+
target.children.each do |c|
|
126
|
+
@element_list.get_id(c).width = max
|
127
|
+
end
|
128
|
+
target.width
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def calculate_indent
|
133
|
+
node_groups = @element_list.get_elements.group_by(&:parent)
|
134
|
+
node_groups.each do |k, v|
|
135
|
+
next if k.zero?
|
136
|
+
|
137
|
+
parent = @element_list.get_id(k)
|
138
|
+
if @symmetrize
|
139
|
+
num_leaves = v.size
|
140
|
+
partition_width = parent.width / num_leaves
|
141
|
+
left_offset = parent.horizontal_indent + parent.content_width / 2.0 - parent.width / 2.0
|
142
|
+
v.each do |e|
|
143
|
+
indent = left_offset + (partition_width - e.content_width) / 2.0
|
144
|
+
e.horizontal_indent = indent
|
145
|
+
left_offset += partition_width
|
146
|
+
end
|
147
|
+
else
|
148
|
+
left_offset = parent.horizontal_indent + parent.content_width / 2.0 - parent.width / 2.0
|
149
|
+
v.each do |e|
|
150
|
+
indent = left_offset + (e.width - e.content_width) / 2.0
|
151
|
+
e.horizontal_indent = indent
|
152
|
+
left_offset += e.width
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def draw_elements
|
159
|
+
@element_list.get_elements.each do |element|
|
160
|
+
draw_element(element)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def draw_connector(id = 1)
|
165
|
+
parent = @element_list.get_id(id)
|
166
|
+
children = parent.children.map { |c| @element_list.get_id(c) }
|
167
|
+
|
168
|
+
if children.size == 1
|
169
|
+
child = children[0]
|
170
|
+
case @leafstyle
|
171
|
+
when "auto"
|
172
|
+
if child.contains_phrase || child.triangle
|
173
|
+
triangle_to_parent(parent, child)
|
174
|
+
else
|
175
|
+
line_to_parent(parent, child)
|
176
|
+
end
|
177
|
+
when "bar"
|
178
|
+
if child.triangle
|
179
|
+
triangle_to_parent(parent, child)
|
180
|
+
else
|
181
|
+
line_to_parent(parent, child)
|
182
|
+
end
|
183
|
+
when "nothing"
|
184
|
+
if child.triangle
|
185
|
+
triangle_to_parent(parent, child)
|
186
|
+
elsif ETYPE_LEAF != child.type
|
187
|
+
line_to_parent(parent, child)
|
188
|
+
elsif ETYPE_LEAF == child.type
|
189
|
+
child.vertical_indent = parent.vertical_indent + parent.content_height + @global[:height_connector] / 2
|
190
|
+
end
|
191
|
+
end
|
192
|
+
else
|
193
|
+
children.each do |child|
|
194
|
+
line_to_parent(parent, child)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
parent.children.each do |c|
|
199
|
+
draw_connector(c)
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
def get_leftmost(id = 1)
|
204
|
+
target = @element_list.get_id(id)
|
205
|
+
target_indent = target.horizontal_indent
|
206
|
+
children_indent = target.children.map { |c| get_leftmost(c) }
|
207
|
+
(children_indent << target_indent).min
|
208
|
+
end
|
209
|
+
|
210
|
+
def get_rightmost(id = 1)
|
211
|
+
target = @element_list.get_id(id)
|
212
|
+
target_right_end = target.horizontal_indent + target.content_width
|
213
|
+
children_right_end = target.children.map { |c| get_rightmost(c) }
|
214
|
+
(children_right_end << target_right_end).max
|
215
|
+
end
|
216
|
+
|
217
|
+
def node_centering
|
218
|
+
node_groups = @element_list.get_elements.group_by(&:parent)
|
219
|
+
node_groups.sort_by { |k, _v| -k }.each do |k, v|
|
220
|
+
next if k.zero?
|
221
|
+
|
222
|
+
parent = @element_list.get_id(k)
|
223
|
+
child_positions = v.map { |child| child.horizontal_indent + child.content_width / 2 }
|
224
|
+
parent.horizontal_indent = child_positions.min + (child_positions.max - child_positions.min - parent.content_width) / 2
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
def parse_list
|
229
|
+
calculate_level
|
230
|
+
calculate_width
|
231
|
+
make_balance if @symmetrize
|
232
|
+
calculate_indent
|
233
|
+
node_centering
|
234
|
+
|
235
|
+
top = @element_list.get_id(1)
|
236
|
+
diff = top.horizontal_indent
|
237
|
+
@element_list.get_elements.each do |e|
|
238
|
+
e.horizontal_indent -= diff
|
239
|
+
end
|
240
|
+
|
241
|
+
offset_l = (top.horizontal_indent - get_leftmost) + @global[:h_gap_between_nodes]
|
242
|
+
# offset_r = top.width / 2 - offset_l - @global[:h_gap_between_nodes:
|
243
|
+
|
244
|
+
@element_list.get_elements.each do |e|
|
245
|
+
e.horizontal_indent += offset_l
|
246
|
+
end
|
247
|
+
|
248
|
+
calculate_height
|
249
|
+
draw_elements
|
250
|
+
draw_connector
|
251
|
+
draw_paths
|
252
|
+
|
253
|
+
width = get_rightmost - get_leftmost + @global[:h_gap_between_nodes]
|
254
|
+
height = @element_list.get_id(1).height
|
255
|
+
height = @height if @height > height
|
256
|
+
|
257
|
+
{ height: height, width: width }
|
258
|
+
end
|
259
|
+
end
|
260
|
+
end
|