psychgus 1.3.3 → 1.3.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +5 -0
- data/CHANGELOG.md +32 -7
- data/Gemfile +15 -18
- data/README.md +43 -47
- data/Rakefile +16 -55
- data/lib/psychgus/blueberry.rb +26 -41
- data/lib/psychgus/ext/core_ext.rb +19 -31
- data/lib/psychgus/ext/node_ext.rb +11 -26
- data/lib/psychgus/ext/yaml_tree_ext.rb +34 -41
- data/lib/psychgus/stylables.rb +76 -91
- data/lib/psychgus/styled_document_stream.rb +12 -27
- data/lib/psychgus/styled_tree_builder.rb +88 -103
- data/lib/psychgus/styler.rb +29 -47
- data/lib/psychgus/stylers.rb +65 -80
- data/lib/psychgus/super_sniffer/parent.rb +38 -56
- data/lib/psychgus/super_sniffer.rb +107 -126
- data/lib/psychgus/version.rb +5 -18
- data/lib/psychgus.rb +166 -186
- data/psychgus.gemspec +37 -51
- data/test/blueberry_test.rb +30 -42
- data/test/psychgus_test.rb +51 -67
- data/test/psychgus_tester.rb +19 -37
- data/test/sniffer_test.rb +18 -33
- data/test/styler_test.rb +20 -32
- data/test/stylers_test.rb +32 -47
- metadata +9 -107
- data/lib/psychgus/ext.rb +0 -32
@@ -1,25 +1,13 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
1
|
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
3
|
|
4
4
|
#--
|
5
5
|
# This file is part of Psychgus.
|
6
|
-
# Copyright (c) 2019
|
7
|
-
#
|
8
|
-
#
|
9
|
-
# it under the terms of the GNU Lesser General Public License as published by
|
10
|
-
# the Free Software Foundation, either version 3 of the License, or
|
11
|
-
# (at your option) any later version.
|
12
|
-
#
|
13
|
-
# Psychgus is distributed in the hope that it will be useful,
|
14
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16
|
-
# GNU Lesser General Public License for more details.
|
17
|
-
#
|
18
|
-
# You should have received a copy of the GNU Lesser General Public License
|
19
|
-
# along with Psychgus. If not, see <http://www.gnu.org/licenses/>.
|
6
|
+
# Copyright (c) 2019 Bradley Whited
|
7
|
+
#
|
8
|
+
# SPDX-License-Identifier: LGPL-3.0-or-later
|
20
9
|
#++
|
21
10
|
|
22
|
-
|
23
11
|
require 'psych'
|
24
12
|
|
25
13
|
require 'psychgus/styler'
|
@@ -28,10 +16,7 @@ require 'psychgus/super_sniffer'
|
|
28
16
|
module Psychgus
|
29
17
|
###
|
30
18
|
# Use this wherever Psych::TreeBuilder would have been used, to enable styling.
|
31
|
-
#
|
32
|
-
# @author Jonathan Bradley Whited (@esotericpig)
|
33
|
-
# @since 1.0.0
|
34
|
-
#
|
19
|
+
#
|
35
20
|
# @see Psychgus.parser Psychgus.parser
|
36
21
|
# @see Psychgus.dump_stream Psychgus.dump_stream
|
37
22
|
# @see Psych::TreeBuilder
|
@@ -40,254 +25,254 @@ module Psychgus
|
|
40
25
|
# @return [true,false] whether to dereference aliases; output the actual value instead of the alias
|
41
26
|
attr_accessor :deref_aliases
|
42
27
|
alias_method :deref_aliases?,:deref_aliases
|
43
|
-
|
28
|
+
|
44
29
|
# @return [SuperSniffer] the {SuperSniffer} being used to sniff the YAML nodes, level, etc.
|
45
30
|
attr_reader :sniffer
|
46
|
-
|
31
|
+
|
47
32
|
# @return [Array<Stylers>] the {Styler}(s) being used to style the YAML nodes
|
48
33
|
attr_reader :stylers
|
49
|
-
|
34
|
+
|
50
35
|
# Initialize this class with {Styler}(s).
|
51
|
-
#
|
36
|
+
#
|
52
37
|
# @param stylers [Styler] {Styler}(s) to use for styling this TreeBuilder
|
53
38
|
# @param deref_aliases [true,false] whether to dereference aliases; output the actual value
|
54
39
|
# instead of the alias
|
55
40
|
def initialize(*stylers,deref_aliases: false,**options)
|
56
41
|
super()
|
57
|
-
|
42
|
+
|
58
43
|
@deref_aliases = deref_aliases
|
59
|
-
@sniffer = SuperSniffer.new
|
44
|
+
@sniffer = SuperSniffer.new
|
60
45
|
@stylers = []
|
61
|
-
|
46
|
+
|
62
47
|
add_styler(*stylers)
|
63
48
|
end
|
64
|
-
|
49
|
+
|
65
50
|
# Add {Styler}(s) onto the end of the data structure.
|
66
|
-
#
|
51
|
+
#
|
67
52
|
# @param stylers [Styler] {Styler}(s) to add
|
68
|
-
#
|
53
|
+
#
|
69
54
|
# @return [self] this class
|
70
55
|
def add_styler(*stylers)
|
71
56
|
@stylers.push(*stylers)
|
72
|
-
|
57
|
+
|
73
58
|
return self
|
74
59
|
end
|
75
|
-
|
60
|
+
|
76
61
|
# Calls super, styler(s), and sniffer.
|
77
|
-
#
|
62
|
+
#
|
78
63
|
# @see Psych::TreeBuilder#alias
|
79
64
|
# @see Styler#style
|
80
65
|
# @see Styler#style_alias
|
81
66
|
# @see SuperSniffer#add_alias
|
82
|
-
#
|
67
|
+
#
|
83
68
|
# @return [Psych::Nodes::Alias] the alias node created
|
84
69
|
def alias(*)
|
85
70
|
node = super
|
86
|
-
|
71
|
+
|
87
72
|
@stylers.each do |styler|
|
88
73
|
styler.style(sniffer,node)
|
89
74
|
styler.style_alias(sniffer,node)
|
90
75
|
end
|
91
|
-
|
76
|
+
|
92
77
|
@sniffer.add_alias(node)
|
93
|
-
|
78
|
+
|
94
79
|
return node
|
95
80
|
end
|
96
|
-
|
81
|
+
|
97
82
|
# Calls super and sniffer.
|
98
|
-
#
|
83
|
+
#
|
99
84
|
# @see Psych::TreeBuilder#end_document
|
100
85
|
# @see SuperSniffer#end_document
|
101
86
|
def end_document(*)
|
102
87
|
result = super
|
103
|
-
|
104
|
-
@sniffer.end_document
|
105
|
-
|
88
|
+
|
89
|
+
@sniffer.end_document
|
90
|
+
|
106
91
|
return result
|
107
92
|
end
|
108
|
-
|
93
|
+
|
109
94
|
# Calls super and sniffer.
|
110
|
-
#
|
95
|
+
#
|
111
96
|
# @see Psych::TreeBuilder#end_mapping
|
112
97
|
# @see SuperSniffer#end_mapping
|
113
98
|
def end_mapping(*)
|
114
99
|
result = super
|
115
|
-
|
116
|
-
@sniffer.end_mapping
|
117
|
-
|
100
|
+
|
101
|
+
@sniffer.end_mapping
|
102
|
+
|
118
103
|
return result
|
119
104
|
end
|
120
|
-
|
105
|
+
|
121
106
|
# Calls super and sniffer.
|
122
|
-
#
|
107
|
+
#
|
123
108
|
# @see Psych::TreeBuilder#end_sequence
|
124
109
|
# @see SuperSniffer#end_sequence
|
125
110
|
def end_sequence(*)
|
126
111
|
result = super
|
127
|
-
|
128
|
-
@sniffer.end_sequence
|
129
|
-
|
112
|
+
|
113
|
+
@sniffer.end_sequence
|
114
|
+
|
130
115
|
return result
|
131
116
|
end
|
132
|
-
|
117
|
+
|
133
118
|
# Calls super and sniffer.
|
134
|
-
#
|
119
|
+
#
|
135
120
|
# @see Psych::TreeBuilder#end_stream
|
136
121
|
# @see SuperSniffer#end_stream
|
137
122
|
def end_stream(*)
|
138
123
|
result = super
|
139
|
-
|
140
|
-
@sniffer.end_stream
|
141
|
-
|
124
|
+
|
125
|
+
@sniffer.end_stream
|
126
|
+
|
142
127
|
return result
|
143
128
|
end
|
144
|
-
|
129
|
+
|
145
130
|
# Insert {Styler}(s) at +index+ into the data structure.
|
146
|
-
#
|
131
|
+
#
|
147
132
|
# @param stylers [Styler] {Styler}(s) to insert
|
148
|
-
#
|
133
|
+
#
|
149
134
|
# @return [self] this class
|
150
135
|
def insert_styler(index,*stylers)
|
151
136
|
@stylers.insert(index,*stylers)
|
152
|
-
|
137
|
+
|
153
138
|
return self
|
154
139
|
end
|
155
|
-
|
140
|
+
|
156
141
|
# Remove the last {Styler}(s) from the data structure.
|
157
|
-
#
|
142
|
+
#
|
158
143
|
# @param count [Integer] the optional amount of tail elements to pop
|
159
|
-
#
|
144
|
+
#
|
160
145
|
# @return [Styler,Array<Styler>,nil] the last {Styler}(s), or if empty or count==0, nil
|
161
146
|
def pop_styler(count=1)
|
162
147
|
return nil if count == 0
|
163
|
-
return @stylers.pop
|
164
|
-
|
148
|
+
return @stylers.pop if count == 1
|
149
|
+
|
165
150
|
return @stylers.pop(count)
|
166
151
|
end
|
167
|
-
|
152
|
+
|
168
153
|
# Remove the {Styler} that matches +styler+ from the data structure.
|
169
|
-
#
|
154
|
+
#
|
170
155
|
# An optional +block+ can return a default value if not found.
|
171
|
-
#
|
156
|
+
#
|
172
157
|
# @param styler [Styler] the {Styler} to find and remove
|
173
158
|
# @param block [Proc] an optional block to call when +styler+ is not found
|
174
|
-
#
|
159
|
+
#
|
175
160
|
# @return [Styler,nil] the last {Styler}, or if not found, nil or the result of +block+
|
176
161
|
def remove_styler(styler,&block)
|
177
162
|
return @stylers.delete(styler,&block)
|
178
163
|
end
|
179
|
-
|
164
|
+
|
180
165
|
# Remove the {Styler} at +index+ from the data structure.
|
181
|
-
#
|
166
|
+
#
|
182
167
|
# @param index [Integer] the index of the {Styler} to remove
|
183
|
-
#
|
168
|
+
#
|
184
169
|
# @return [Styler,nil] the {Styler} removed or nil if empty
|
185
170
|
def remove_styler_at(index)
|
186
171
|
return @stylers.delete_at(index)
|
187
172
|
end
|
188
|
-
|
173
|
+
|
189
174
|
# Calls super, styler(s), and sniffer.
|
190
|
-
#
|
175
|
+
#
|
191
176
|
# @see Psych::TreeBuilder#scalar
|
192
177
|
# @see Styler#style
|
193
178
|
# @see Styler#style_scalar
|
194
179
|
# @see SuperSniffer#add_scalar
|
195
|
-
#
|
180
|
+
#
|
196
181
|
# @return [Psych::Nodes::Scalar] the scalar node created
|
197
182
|
def scalar(*)
|
198
183
|
node = super
|
199
|
-
|
184
|
+
|
200
185
|
@stylers.each do |styler|
|
201
186
|
styler.style(sniffer,node)
|
202
187
|
styler.style_scalar(sniffer,node)
|
203
188
|
end
|
204
|
-
|
189
|
+
|
205
190
|
@sniffer.add_scalar(node)
|
206
|
-
|
191
|
+
|
207
192
|
return node
|
208
193
|
end
|
209
|
-
|
194
|
+
|
210
195
|
# Calls super, styler(s), and sniffer.
|
211
|
-
#
|
196
|
+
#
|
212
197
|
# @see Psych::TreeBuilder#start_document
|
213
198
|
# @see Styler#style
|
214
199
|
# @see Styler#style_document
|
215
200
|
# @see SuperSniffer#start_document
|
216
|
-
#
|
201
|
+
#
|
217
202
|
# @return [Psych::Nodes::Document] the document node created
|
218
203
|
def start_document(*)
|
219
204
|
node = super
|
220
|
-
|
205
|
+
|
221
206
|
@stylers.each do |styler|
|
222
207
|
styler.style(sniffer,node)
|
223
208
|
styler.style_document(sniffer,node)
|
224
209
|
end
|
225
|
-
|
210
|
+
|
226
211
|
@sniffer.start_document(node)
|
227
|
-
|
212
|
+
|
228
213
|
return node
|
229
214
|
end
|
230
|
-
|
215
|
+
|
231
216
|
# Calls super, styler(s), and sniffer.
|
232
|
-
#
|
217
|
+
#
|
233
218
|
# @see Psych::TreeBuilder#start_mapping
|
234
219
|
# @see Styler#style
|
235
220
|
# @see Styler#style_mapping
|
236
221
|
# @see SuperSniffer#start_mapping
|
237
|
-
#
|
222
|
+
#
|
238
223
|
# @return [Psych::Nodes::Mapping] the mapping node created
|
239
224
|
def start_mapping(*)
|
240
225
|
node = super
|
241
|
-
|
226
|
+
|
242
227
|
@stylers.each do |styler|
|
243
228
|
styler.style(sniffer,node)
|
244
229
|
styler.style_mapping(sniffer,node)
|
245
230
|
end
|
246
|
-
|
231
|
+
|
247
232
|
@sniffer.start_mapping(node)
|
248
|
-
|
233
|
+
|
249
234
|
return node
|
250
235
|
end
|
251
|
-
|
236
|
+
|
252
237
|
# Calls super, styler(s), and sniffer.
|
253
|
-
#
|
238
|
+
#
|
254
239
|
# @see Psych::TreeBuilder#start_sequence
|
255
240
|
# @see Styler#style
|
256
241
|
# @see Styler#style_sequence
|
257
242
|
# @see SuperSniffer#start_sequence
|
258
|
-
#
|
243
|
+
#
|
259
244
|
# @return [Psych::Nodes::Sequence] the sequence node created
|
260
245
|
def start_sequence(*)
|
261
246
|
node = super
|
262
|
-
|
247
|
+
|
263
248
|
@stylers.each do |styler|
|
264
249
|
styler.style(sniffer,node)
|
265
250
|
styler.style_sequence(sniffer,node)
|
266
251
|
end
|
267
|
-
|
252
|
+
|
268
253
|
@sniffer.start_sequence(node)
|
269
|
-
|
254
|
+
|
270
255
|
return node
|
271
256
|
end
|
272
|
-
|
257
|
+
|
273
258
|
# Calls super, styler(s), and sniffer.
|
274
|
-
#
|
259
|
+
#
|
275
260
|
# @see Psych::TreeBuilder#start_stream
|
276
261
|
# @see Styler#style
|
277
262
|
# @see Styler#style_stream
|
278
263
|
# @see SuperSniffer#start_stream
|
279
|
-
#
|
264
|
+
#
|
280
265
|
# @return [Psych::Nodes::Stream] the stream node created
|
281
266
|
def start_stream(*)
|
282
267
|
node = super
|
283
|
-
|
268
|
+
|
284
269
|
@stylers.each do |styler|
|
285
270
|
styler.style(sniffer,node)
|
286
271
|
styler.style_stream(sniffer,node)
|
287
272
|
end
|
288
|
-
|
273
|
+
|
289
274
|
@sniffer.start_stream(node)
|
290
|
-
|
275
|
+
|
291
276
|
return node
|
292
277
|
end
|
293
278
|
end
|
data/lib/psychgus/styler.rb
CHANGED
@@ -1,72 +1,54 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
1
|
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
3
|
|
4
4
|
#--
|
5
5
|
# This file is part of Psychgus.
|
6
|
-
# Copyright (c) 2019
|
7
|
-
#
|
8
|
-
#
|
9
|
-
# it under the terms of the GNU Lesser General Public License as published by
|
10
|
-
# the Free Software Foundation, either version 3 of the License, or
|
11
|
-
# (at your option) any later version.
|
12
|
-
#
|
13
|
-
# Psychgus is distributed in the hope that it will be useful,
|
14
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16
|
-
# GNU Lesser General Public License for more details.
|
17
|
-
#
|
18
|
-
# You should have received a copy of the GNU Lesser General Public License
|
19
|
-
# along with Psychgus. If not, see <http://www.gnu.org/licenses/>.
|
6
|
+
# Copyright (c) 2019 Bradley Whited
|
7
|
+
#
|
8
|
+
# SPDX-License-Identifier: LGPL-3.0-or-later
|
20
9
|
#++
|
21
10
|
|
22
|
-
|
23
11
|
require 'psychgus/super_sniffer'
|
24
12
|
|
25
13
|
module Psychgus
|
26
14
|
module Styler
|
27
15
|
###
|
28
16
|
# An empty {Styler} as a class.
|
29
|
-
#
|
30
|
-
# @author Jonathan Bradley Whited (@esotericpig)
|
31
|
-
# @since 1.0.0
|
32
17
|
###
|
33
18
|
class Empty
|
34
19
|
include Styler
|
35
20
|
end
|
36
21
|
end
|
37
|
-
|
22
|
+
|
38
23
|
###
|
39
24
|
# Mix in (include) this module to make a class/module/etc. a styler for YAML.
|
40
|
-
#
|
25
|
+
#
|
41
26
|
# Although it's unnecessary (because of Duck Typing), it's the recommended practice in case a new method is
|
42
27
|
# added in the future, and also so you don't have to define methods that you don't use.
|
43
|
-
#
|
28
|
+
#
|
44
29
|
# You can either use this as is (see example) or inside of a class (see {Blueberry}).
|
45
|
-
#
|
30
|
+
#
|
46
31
|
# @example
|
47
32
|
# class MyStyler
|
48
33
|
# include Psychgus::Styler
|
49
|
-
#
|
34
|
+
#
|
50
35
|
# def style_sequence(sniffer,node)
|
51
36
|
# node.style = Psychgus::SEQUENCE_FLOW if sniffer.level == 3
|
52
37
|
# end
|
53
38
|
# end
|
54
|
-
#
|
39
|
+
#
|
55
40
|
# hash = {'Coffee'=>{
|
56
41
|
# 'Roast'=>['Light','Medium','Dark','Extra Dark'],
|
57
42
|
# 'Style'=>['Cappuccino','Espresso','Latte','Mocha']
|
58
43
|
# }}
|
59
44
|
# puts hash.to_yaml(stylers: MyStyler.new())
|
60
|
-
#
|
45
|
+
#
|
61
46
|
# # Output:
|
62
47
|
# # ---
|
63
48
|
# # Coffee:
|
64
49
|
# # Roast: [Light, Medium, Dark, Extra Dark]
|
65
50
|
# # Style: [Cappuccino, Espresso, Latte, Mocha]
|
66
|
-
#
|
67
|
-
# @author Jonathan Bradley Whited (@esotericpig)
|
68
|
-
# @since 1.0.0
|
69
|
-
#
|
51
|
+
#
|
70
52
|
# @see Psychgus
|
71
53
|
# @see Ext::ObjectExt#to_yaml
|
72
54
|
# @see Blueberry
|
@@ -75,51 +57,51 @@ module Psychgus
|
|
75
57
|
# @see Ext::YAMLTreeExt#accept
|
76
58
|
###
|
77
59
|
module Styler
|
78
|
-
EMPTY = Empty.new
|
79
|
-
|
60
|
+
EMPTY = Empty.new.freeze
|
61
|
+
|
80
62
|
# Style a node of any type.
|
81
|
-
#
|
63
|
+
#
|
82
64
|
# You can use {Ext::NodeExt#node_of?} to determine its type:
|
83
65
|
# puts node.value if node.node_of?(:scalar)
|
84
|
-
#
|
66
|
+
#
|
85
67
|
# @param sniffer [SuperSniffer] passed in from {StyledTreeBuilder}
|
86
68
|
# @param node [Psych::Nodes::Node] passed in from {StyledTreeBuilder}
|
87
|
-
#
|
69
|
+
#
|
88
70
|
# @see Ext::NodeExt#node_of?
|
89
71
|
def style(sniffer,node) end
|
90
|
-
|
72
|
+
|
91
73
|
# Style a node guaranteed to be of type Psych::Nodes::Alias, to avoid if statements.
|
92
|
-
#
|
74
|
+
#
|
93
75
|
# @param sniffer [SuperSniffer] passed in from {StyledTreeBuilder}
|
94
76
|
# @param node [Psych::Nodes::Alias] of type Alias passed in from {StyledTreeBuilder}
|
95
77
|
def style_alias(sniffer,node) end
|
96
|
-
|
78
|
+
|
97
79
|
# Style a node guaranteed to be of type Psych::Nodes::Document, to avoid if statements.
|
98
|
-
#
|
80
|
+
#
|
99
81
|
# @param sniffer [SuperSniffer] passed in from {StyledTreeBuilder}
|
100
82
|
# @param node [Psych::Nodes::Document] of type Document passed in from {StyledTreeBuilder}
|
101
83
|
def style_document(sniffer,node) end
|
102
|
-
|
84
|
+
|
103
85
|
# Style a node guaranteed to be of type Psych::Nodes::Mapping, to avoid if statements.
|
104
|
-
#
|
86
|
+
#
|
105
87
|
# @param sniffer [SuperSniffer] passed in from {StyledTreeBuilder}
|
106
88
|
# @param node [Psych::Nodes::Mapping] of type Mapping passed in from {StyledTreeBuilder}
|
107
89
|
def style_mapping(sniffer,node) end
|
108
|
-
|
90
|
+
|
109
91
|
# Style a node guaranteed to be of type Psych::Nodes::Scalar, to avoid if statements.
|
110
|
-
#
|
92
|
+
#
|
111
93
|
# @param sniffer [SuperSniffer] passed in from {StyledTreeBuilder}
|
112
94
|
# @param node [Psych::Nodes::Scalar] of type Scalar passed in from {StyledTreeBuilder}
|
113
95
|
def style_scalar(sniffer,node) end
|
114
|
-
|
96
|
+
|
115
97
|
# Style a node guaranteed to be of type Psych::Nodes::Sequence, to avoid if statements.
|
116
|
-
#
|
98
|
+
#
|
117
99
|
# @param sniffer [SuperSniffer] passed in from {StyledTreeBuilder}
|
118
100
|
# @param node [Psych::Nodes::Sequence] of type Sequence passed in from {StyledTreeBuilder}
|
119
101
|
def style_sequence(sniffer,node) end
|
120
|
-
|
102
|
+
|
121
103
|
# Style a node guaranteed to be of type Psych::Nodes::Stream, to avoid if statements.
|
122
|
-
#
|
104
|
+
#
|
123
105
|
# @param sniffer [SuperSniffer] passed in from {StyledTreeBuilder}
|
124
106
|
# @param node [Psych::Nodes::Stream] of type Stream passed in from {StyledTreeBuilder}
|
125
107
|
def style_stream(sniffer,node) end
|