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
data/lib/rsyntaxtree/element.rb
CHANGED
@@ -1,179 +1,167 @@
|
|
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
|
-
|
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
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
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
|
-
end
|
169
|
-
total_width = content_width if total_width < content_width
|
170
|
-
end
|
171
|
-
@content_width = total_width
|
172
|
-
@content_height = total_height
|
173
|
-
end
|
174
|
-
|
175
|
-
def add_child(child_id)
|
176
|
-
@children << child_id
|
177
|
-
end
|
178
|
-
end
|
179
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#==========================
|
4
|
+
# element.rb
|
5
|
+
#==========================
|
6
|
+
#
|
7
|
+
# Aa class that represents a basic tree element, either node or leaf.
|
8
|
+
# Copyright (c) 2007-2023 Yoichiro Hasebe <yohasebe@gmail.com>
|
9
|
+
|
10
|
+
require_relative "markup_parser"
|
11
|
+
require_relative "utils"
|
12
|
+
|
13
|
+
module RSyntaxTree
|
14
|
+
class Element
|
15
|
+
attr_accessor :id, :parent, :type, :level, :width, :height, :content, :content_width, :content_height, :horizontal_indent, :vertical_indent, :triangle, :enclosure, :children, :font, :fontsize, :contains_phrase, :path
|
16
|
+
|
17
|
+
def initialize(id, parent, content, level, fontset, fontsize, global)
|
18
|
+
@global = global
|
19
|
+
@type = ETYPE_LEAF
|
20
|
+
@id = id # Unique element id
|
21
|
+
@parent = parent # Parent element id
|
22
|
+
@children = [] # Child element ids
|
23
|
+
@type = type # Element type
|
24
|
+
@level = level # Element level in the tree (0=top etc...)
|
25
|
+
@width = 0 # Width of the part of the tree including itself and it governs
|
26
|
+
@content_width = 0 # Width of the content
|
27
|
+
@horizontal_indent = 0 # Drawing offset
|
28
|
+
@vertical_indent = 0 # Drawing offset
|
29
|
+
content = content.strip
|
30
|
+
|
31
|
+
@path = if /.+?\^?((?:\+>?\d+)+)\^?\z/m =~ content
|
32
|
+
$1.sub(/\A\+/, "").split("+")
|
33
|
+
else
|
34
|
+
[]
|
35
|
+
end
|
36
|
+
|
37
|
+
@fontset = fontset
|
38
|
+
@fontsize = fontsize
|
39
|
+
|
40
|
+
parsed = Markup.parse(content)
|
41
|
+
|
42
|
+
if parsed[:status] == :success
|
43
|
+
results = parsed[:results]
|
44
|
+
else
|
45
|
+
error_text = "Error: input text contains an invalid string"
|
46
|
+
error_text += "\n > " + content
|
47
|
+
raise RSTError, error_text
|
48
|
+
end
|
49
|
+
@content = results[:contents]
|
50
|
+
@paths = results[:paths]
|
51
|
+
@enclosure = results[:enclosure]
|
52
|
+
@triangle = results[:triangle]
|
53
|
+
|
54
|
+
@contains_phrase = false
|
55
|
+
setup
|
56
|
+
end
|
57
|
+
|
58
|
+
def setup
|
59
|
+
total_width = 0
|
60
|
+
total_height = 0
|
61
|
+
@content.each do |content|
|
62
|
+
content_width = 0
|
63
|
+
case content[:type]
|
64
|
+
when :border, :bborder
|
65
|
+
height = @global[:single_line_height] / 2
|
66
|
+
content[:height] = height
|
67
|
+
total_height += height
|
68
|
+
when :text
|
69
|
+
row_width = 0
|
70
|
+
elements_height = []
|
71
|
+
content[:elements].each do |e|
|
72
|
+
text = e[:text]
|
73
|
+
e[:text] = text.gsub(" ", WHITESPACE_BLOCK).gsub(">", '>').gsub("<", '<')
|
74
|
+
|
75
|
+
@contains_phrase = true if text.include?(" ")
|
76
|
+
decoration = e[:decoration]
|
77
|
+
fontsize = decoration.include?(:small) ? @fontsize * SUBSCRIPT_CONST : @fontsize
|
78
|
+
fontsize = decoration.include?(:subscript) || decoration.include?(:superscript) ? fontsize * SUBSCRIPT_CONST : fontsize
|
79
|
+
style = decoration.include?(:italic) || decoration.include?(:bolditalic) ? :italic : :normal
|
80
|
+
weight = decoration.include?(:bold) || decoration.include?(:bolditalic) ? :bold : :normal
|
81
|
+
|
82
|
+
# e[:cjk] = false
|
83
|
+
# if e[:decoration].include?(:math)
|
84
|
+
# font = @fontset[:math]
|
85
|
+
# elsif text.contains_cjk?
|
86
|
+
# font = @fontset[:cjk]
|
87
|
+
# e[:cjk] = true
|
88
|
+
# elsif decoration.include? :bolditalic
|
89
|
+
font = if decoration.include? :bolditalic
|
90
|
+
@fontset[:bolditalic]
|
91
|
+
elsif decoration.include? :bold
|
92
|
+
@fontset[:bold]
|
93
|
+
elsif decoration.include? :italic
|
94
|
+
@fontset[:italic]
|
95
|
+
else
|
96
|
+
@fontset[:normal]
|
97
|
+
end
|
98
|
+
|
99
|
+
standard_metrics = FontMetrics.get_metrics('X', @fontset[:normal], fontsize, :normal, :normal)
|
100
|
+
height = standard_metrics.height
|
101
|
+
if /\A[<>]+\z/ =~ text
|
102
|
+
width = standard_metrics.width * text.size / 2
|
103
|
+
elsif text.contains_emoji?
|
104
|
+
segments = text.split_by_emoji
|
105
|
+
width = 0
|
106
|
+
segments.each do |seg|
|
107
|
+
ch = if /\s/ =~ seg[:char]
|
108
|
+
't'
|
109
|
+
else
|
110
|
+
seg[:char]
|
111
|
+
end
|
112
|
+
this_font = if seg[:type] == :emoji
|
113
|
+
@fontset[:emoji]
|
114
|
+
else
|
115
|
+
font
|
116
|
+
end
|
117
|
+
metrics = FontMetrics.get_metrics(ch, this_font, fontsize, style, weight)
|
118
|
+
width += metrics.width
|
119
|
+
end
|
120
|
+
else
|
121
|
+
text.gsub!("\\\\", 'i')
|
122
|
+
text.gsub!("\\", "")
|
123
|
+
text.gsub!(" ", "x")
|
124
|
+
text.gsub!("%", "X")
|
125
|
+
metrics = FontMetrics.get_metrics(text, font, fontsize, style, weight)
|
126
|
+
width = metrics.width
|
127
|
+
end
|
128
|
+
|
129
|
+
if e[:decoration].include?(:box) || e[:decoration].include?(:circle) || e[:decoration].include?(:bar)
|
130
|
+
e[:content_width] = width
|
131
|
+
width += if e[:text].size == 1
|
132
|
+
height - width
|
133
|
+
else
|
134
|
+
@global[:width_half_x]
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
if e[:decoration].include?(:whitespace)
|
139
|
+
width = @global[:width_half_x] / 2 * e[:text].size / 4
|
140
|
+
e[:text] = ""
|
141
|
+
end
|
142
|
+
|
143
|
+
e[:height] = height
|
144
|
+
elements_height << height + @global[:box_vertical_margin] / 2
|
145
|
+
|
146
|
+
e[:width] = width
|
147
|
+
row_width += width
|
148
|
+
end
|
149
|
+
|
150
|
+
total_height += if @enclosure != :none
|
151
|
+
elements_height.max + @global[:height_connector_to_text]
|
152
|
+
else
|
153
|
+
elements_height.max
|
154
|
+
end
|
155
|
+
content_width += row_width
|
156
|
+
end
|
157
|
+
total_width = content_width if total_width < content_width
|
158
|
+
end
|
159
|
+
@content_width = total_width
|
160
|
+
@content_height = total_height
|
161
|
+
end
|
162
|
+
|
163
|
+
def add_child(child_id)
|
164
|
+
@children << child_id
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
@@ -1,124 +1,105 @@
|
|
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
|
-
|
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
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
def set_indent(id, indent)
|
107
|
-
element = get_id(id)
|
108
|
-
if element
|
109
|
-
element.indent = indent
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
def get_level_height
|
114
|
-
maxlevel = 0
|
115
|
-
@elements.each do |element|
|
116
|
-
level = element.level
|
117
|
-
if(level > maxlevel)
|
118
|
-
maxlevel = level
|
119
|
-
end
|
120
|
-
end
|
121
|
-
return maxlevel + 1;
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#==========================
|
4
|
+
# elementlist.rb
|
5
|
+
#==========================
|
6
|
+
#
|
7
|
+
# Contains a list of unordered tree elements with a defined parent
|
8
|
+
# Copyright (c) 2007-2023 Yoichiro Hasebe <yohasebe@gmail.com>
|
9
|
+
|
10
|
+
require_relative "element"
|
11
|
+
|
12
|
+
module RSyntaxTree
|
13
|
+
class ElementList
|
14
|
+
attr_accessor :elements, :iterator
|
15
|
+
|
16
|
+
def initialize
|
17
|
+
@elements = []
|
18
|
+
@iterator = -1 # Iterator index (used for get_first / get_next)
|
19
|
+
end
|
20
|
+
|
21
|
+
def set_hierarchy
|
22
|
+
@elements.each do |e|
|
23
|
+
get_id(e.parent).add_child(e.id) unless e.parent.zero?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def add(element)
|
28
|
+
@elements << element
|
29
|
+
return if element.parent.zero?
|
30
|
+
|
31
|
+
parent = get_id(element.parent)
|
32
|
+
parent.type = ETYPE_NODE
|
33
|
+
end
|
34
|
+
|
35
|
+
def get_first
|
36
|
+
return nil if @elements.length.empty?
|
37
|
+
|
38
|
+
@iterator = 0
|
39
|
+
@elements[@iterator]
|
40
|
+
end
|
41
|
+
|
42
|
+
def get_next
|
43
|
+
@iterator += 1
|
44
|
+
return @elements[@iterator] if @elements[@iterator]
|
45
|
+
|
46
|
+
nil
|
47
|
+
end
|
48
|
+
|
49
|
+
def get_id(id)
|
50
|
+
@elements.each do |element|
|
51
|
+
return element if element.id == id
|
52
|
+
end
|
53
|
+
nil
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_elements
|
57
|
+
@elements
|
58
|
+
end
|
59
|
+
|
60
|
+
def get_child_count(id)
|
61
|
+
get_children(id).length
|
62
|
+
end
|
63
|
+
|
64
|
+
def get_children(id)
|
65
|
+
children = []
|
66
|
+
@elements.each do |element|
|
67
|
+
children << element.id if element.parent == id
|
68
|
+
end
|
69
|
+
children
|
70
|
+
end
|
71
|
+
|
72
|
+
def get_element_width(id)
|
73
|
+
element = get_id(id)
|
74
|
+
return element.width if element
|
75
|
+
|
76
|
+
-1;
|
77
|
+
end
|
78
|
+
|
79
|
+
def set_element_width(id, width)
|
80
|
+
element = get_id(id)
|
81
|
+
element.width = width if element
|
82
|
+
end
|
83
|
+
|
84
|
+
def get_indent(id)
|
85
|
+
element = get_id(id)
|
86
|
+
return element.indent if element
|
87
|
+
|
88
|
+
-1
|
89
|
+
end
|
90
|
+
|
91
|
+
def set_indent(id, indent)
|
92
|
+
element = get_id(id)
|
93
|
+
element.indent = indent if element
|
94
|
+
end
|
95
|
+
|
96
|
+
def get_level_height
|
97
|
+
maxlevel = 0
|
98
|
+
@elements.each do |element|
|
99
|
+
level = element.level
|
100
|
+
maxlevel = level if level > maxlevel
|
101
|
+
end
|
102
|
+
maxlevel + 1
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|