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,37 +1,21 @@
|
|
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/parent'
|
24
12
|
|
25
13
|
module Psychgus
|
26
14
|
class SuperSniffer
|
27
15
|
###
|
28
16
|
# An empty {SuperSniffer} used for speed when you don't need sniffing in {StyledTreeBuilder}.
|
29
|
-
#
|
30
|
-
# @author Jonathan Bradley Whited (@esotericpig)
|
31
|
-
# @since 1.0.0
|
32
17
|
###
|
33
18
|
class Empty < SuperSniffer
|
34
|
-
def initialize(*) end
|
35
19
|
def add_alias(*) end
|
36
20
|
def add_scalar(*) end
|
37
21
|
def end_document(*) end
|
@@ -44,12 +28,12 @@ module Psychgus
|
|
44
28
|
def start_stream(*) end
|
45
29
|
end
|
46
30
|
end
|
47
|
-
|
31
|
+
|
48
32
|
###
|
49
33
|
# This is used in {StyledTreeBuilder} to "sniff" information about the YAML.
|
50
|
-
#
|
34
|
+
#
|
51
35
|
# Then this information can be used in a {Styler} and/or a {Blueberry}.
|
52
|
-
#
|
36
|
+
#
|
53
37
|
# Most information is straightforward:
|
54
38
|
# - {#aliases} # Array of Psych::Nodes::Alias processed so far
|
55
39
|
# - {#documents} # Array of Psych::Nodes::Document processed so far
|
@@ -58,17 +42,17 @@ module Psychgus
|
|
58
42
|
# - {#scalars} # Array of Psych::Nodes::Scalar processed so far
|
59
43
|
# - {#sequences} # Array of Psych::Nodes::Sequence processed so far
|
60
44
|
# - {#streams} # Array of Psych::Nodes::Stream processed so far
|
61
|
-
#
|
45
|
+
#
|
62
46
|
# {#parent} is the current {SuperSniffer::Parent} of the node being processed,
|
63
47
|
# which is an empty Parent for the first node (not nil).
|
64
|
-
#
|
48
|
+
#
|
65
49
|
# {#parents} are all of the (grand){SuperSniffer::Parent}(s) for the current node,
|
66
50
|
# which is an Array that just contains an empty Parent for the first node.
|
67
|
-
#
|
51
|
+
#
|
68
52
|
# A parent is a Mapping or Sequence, or a Key (Scalar) in a Mapping.
|
69
|
-
#
|
53
|
+
#
|
70
54
|
# {#level} and {#position} can be best understood by an example.
|
71
|
-
#
|
55
|
+
#
|
72
56
|
# If you have this YAML:
|
73
57
|
# Burgers:
|
74
58
|
# Classic:
|
@@ -87,10 +71,10 @@ module Psychgus
|
|
87
71
|
# - Mushrooms
|
88
72
|
# - [Lettuce, Onions, Pickles, Tomatoes]
|
89
73
|
# - [[Ketchup,Mustard], [Salt,Pepper]]
|
90
|
-
#
|
74
|
+
#
|
91
75
|
# Then the levels and positions will be as follows:
|
92
76
|
# # (level:position):current_node - <parent:(parent_level:parent_position)>
|
93
|
-
#
|
77
|
+
#
|
94
78
|
# (1:1):Psych::Nodes::Stream - <root:(0:0)>
|
95
79
|
# (1:1):Psych::Nodes::Document - <stream:(1:1)>
|
96
80
|
# (1:1):Psych::Nodes::Mapping - <doc:(1:1)>
|
@@ -137,23 +121,20 @@ module Psychgus
|
|
137
121
|
# (5:2):Psych::Nodes::Sequence - <seq:(4:3)>
|
138
122
|
# (6:1):Salt - <seq:(5:2)>
|
139
123
|
# (6:2):Pepper - <seq:(5:2)>
|
140
|
-
#
|
124
|
+
#
|
141
125
|
# "The Super Sniffer" is the nickname for Gus's nose from the TV show Psych
|
142
126
|
# because he has a very refined sense of smell.
|
143
|
-
#
|
127
|
+
#
|
144
128
|
# @note You should never call the methods that are not readers, like {#add_alias}, {#start_mapping}, etc.
|
145
129
|
# unless you are extending this class (creating a subclass).
|
146
|
-
#
|
147
|
-
# @author Jonathan Bradley Whited (@esotericpig)
|
148
|
-
# @since 1.0.0
|
149
|
-
#
|
130
|
+
#
|
150
131
|
# @see StyledTreeBuilder
|
151
132
|
# @see Styler
|
152
133
|
# @see Blueberry#psychgus_stylers
|
153
134
|
###
|
154
135
|
class SuperSniffer
|
155
|
-
EMPTY = Empty.new
|
156
|
-
|
136
|
+
EMPTY = Empty.new.freeze
|
137
|
+
|
157
138
|
attr_reader :aliases # @return [Array<Psych::Nodes::Alias>] the aliases processed so far
|
158
139
|
attr_reader :documents # @return [Array<Psych::Nodes::Document>] the documents processed so far
|
159
140
|
attr_reader :level # @return [Integer] the current level
|
@@ -165,9 +146,9 @@ module Psychgus
|
|
165
146
|
attr_reader :scalars # @return [Array<Psych::Nodes::Scalar>] the scalars processed so far
|
166
147
|
attr_reader :sequences # @return [Array<Psych::Nodes::Sequence>] the sequences processed so far
|
167
148
|
attr_reader :streams # @return [Array<Psych::Nodes::Stream>] the streams processed so far
|
168
|
-
|
149
|
+
|
169
150
|
# Initialize this class for sniffing.
|
170
|
-
def initialize
|
151
|
+
def initialize
|
171
152
|
@aliases = []
|
172
153
|
@documents = []
|
173
154
|
@level = 0
|
@@ -179,240 +160,240 @@ module Psychgus
|
|
179
160
|
@scalars = []
|
180
161
|
@sequences = []
|
181
162
|
@streams = []
|
182
|
-
|
163
|
+
|
183
164
|
# Do not pass in "top_level: true"
|
184
165
|
start_parent(nil,debug_tag: :root)
|
185
166
|
end
|
186
|
-
|
167
|
+
|
187
168
|
# Add a Psych::Nodes::Alias to this class only (not to the YAML).
|
188
|
-
#
|
169
|
+
#
|
189
170
|
# A {Styler} should probably never call this.
|
190
|
-
#
|
171
|
+
#
|
191
172
|
# @param node [Psych::Nodes::Alias] the alias to add
|
192
|
-
#
|
173
|
+
#
|
193
174
|
# @see add_child
|
194
175
|
def add_alias(node)
|
195
176
|
add_child(node)
|
196
177
|
@aliases.push(node)
|
197
178
|
end
|
198
|
-
|
179
|
+
|
199
180
|
# Add a Psych::Nodes::Scalar to this class only (not to the YAML).
|
200
|
-
#
|
181
|
+
#
|
201
182
|
# A {Styler} should probably never call this.
|
202
|
-
#
|
183
|
+
#
|
203
184
|
# @param node [Psych::Nodes::Scalar] the scalar to add
|
204
|
-
#
|
185
|
+
#
|
205
186
|
# @see add_child
|
206
187
|
def add_scalar(node)
|
207
188
|
add_child(node)
|
208
189
|
@scalars.push(node)
|
209
190
|
end
|
210
|
-
|
191
|
+
|
211
192
|
# End a Psych::Nodes::Document started with {#start_document}.
|
212
|
-
#
|
193
|
+
#
|
213
194
|
# Pops off a parent from {#parents} and sets {#parent} to the last one.
|
214
195
|
# {#level} and {#position} are reset according to the last parent.
|
215
|
-
#
|
196
|
+
#
|
216
197
|
# A {Styler} should probably never call this.
|
217
|
-
def end_document
|
198
|
+
def end_document
|
218
199
|
end_parent(top_level: true)
|
219
200
|
end
|
220
|
-
|
201
|
+
|
221
202
|
# End a Psych::Nodes::Mapping started with {#start_mapping}.
|
222
|
-
#
|
203
|
+
#
|
223
204
|
# Pops off a parent from {#parents} and sets {#parent} to the last one.
|
224
205
|
# {#level} and {#position} are reset according to the last parent.
|
225
|
-
#
|
206
|
+
#
|
226
207
|
# A {Styler} should probably never call this.
|
227
|
-
#
|
208
|
+
#
|
228
209
|
# @see end_parent
|
229
|
-
def end_mapping
|
210
|
+
def end_mapping
|
230
211
|
end_parent(mapping_value: true)
|
231
212
|
end
|
232
|
-
|
213
|
+
|
233
214
|
# End a Psych::Nodes::Sequence started with {#start_sequence}.
|
234
|
-
#
|
215
|
+
#
|
235
216
|
# Pops off a parent from {#parents} and sets {#parent} to the last one.
|
236
217
|
# {#level} and {#position} are reset according to the last parent.
|
237
|
-
#
|
218
|
+
#
|
238
219
|
# A {Styler} should probably never call this.
|
239
|
-
#
|
220
|
+
#
|
240
221
|
# @see end_parent
|
241
|
-
def end_sequence
|
222
|
+
def end_sequence
|
242
223
|
end_parent(mapping_value: true)
|
243
224
|
end
|
244
|
-
|
225
|
+
|
245
226
|
# End a Psych::Nodes::Stream started with {#start_stream}.
|
246
|
-
#
|
227
|
+
#
|
247
228
|
# Pops off a parent from {#parents} and sets {#parent} to the last one.
|
248
229
|
# {#level} and {#position} are reset according to the last parent.
|
249
|
-
#
|
230
|
+
#
|
250
231
|
# A {Styler} should probably never call this.
|
251
|
-
def end_stream
|
232
|
+
def end_stream
|
252
233
|
end_parent(top_level: true)
|
253
234
|
end
|
254
|
-
|
235
|
+
|
255
236
|
# Start a Psych::Nodes::Document.
|
256
|
-
#
|
237
|
+
#
|
257
238
|
# Creates a {SuperSniffer::Parent}, sets {#parent} to it, and adds it to {#parents}.
|
258
239
|
# {#level} and {#position} are incremented/set accordingly.
|
259
|
-
#
|
240
|
+
#
|
260
241
|
# A {Styler} should probably never call this.
|
261
|
-
#
|
242
|
+
#
|
262
243
|
# @param node [Psych::Nodes::Document] the Document to start
|
263
|
-
#
|
244
|
+
#
|
264
245
|
# @see start_parent
|
265
246
|
def start_document(node)
|
266
247
|
start_parent(node,debug_tag: :doc,top_level: true)
|
267
248
|
@documents.push(node)
|
268
249
|
end
|
269
|
-
|
250
|
+
|
270
251
|
# Start a Psych::Nodes::Mapping.
|
271
|
-
#
|
252
|
+
#
|
272
253
|
# Creates a {SuperSniffer::Parent}, sets {#parent} to it, and adds it to {#parents}.
|
273
254
|
# {#level} and {#position} are incremented/set accordingly.
|
274
|
-
#
|
255
|
+
#
|
275
256
|
# A {Styler} should probably never call this.
|
276
|
-
#
|
257
|
+
#
|
277
258
|
# @param node [Psych::Nodes::Mapping] the Mapping to start
|
278
|
-
#
|
259
|
+
#
|
279
260
|
# @see start_parent
|
280
261
|
def start_mapping(node)
|
281
262
|
start_parent(node,debug_tag: :map,child_type: :key)
|
282
263
|
@mappings.push(node)
|
283
264
|
end
|
284
|
-
|
265
|
+
|
285
266
|
# Start a Psych::Nodes::Sequence.
|
286
|
-
#
|
267
|
+
#
|
287
268
|
# Creates a {SuperSniffer::Parent}, sets {#parent} to it, and adds it to {#parents}.
|
288
269
|
# {#level} and {#position} are incremented/set accordingly.
|
289
|
-
#
|
270
|
+
#
|
290
271
|
# A {Styler} should probably never call this.
|
291
|
-
#
|
272
|
+
#
|
292
273
|
# @param node [Psych::Nodes::Sequence] the Sequence to start
|
293
|
-
#
|
274
|
+
#
|
294
275
|
# @see start_parent
|
295
276
|
def start_sequence(node)
|
296
277
|
start_parent(node,debug_tag: :seq)
|
297
278
|
@sequences.push(node)
|
298
279
|
end
|
299
|
-
|
280
|
+
|
300
281
|
# Start a Psych::Nodes::Stream.
|
301
|
-
#
|
282
|
+
#
|
302
283
|
# Creates a {SuperSniffer::Parent}, sets {#parent} to it, and adds it to {#parents}.
|
303
284
|
# {#level} and {#position} are incremented/set accordingly.
|
304
|
-
#
|
285
|
+
#
|
305
286
|
# A {Styler} should probably never call this.
|
306
|
-
#
|
287
|
+
#
|
307
288
|
# @param node [Psych::Nodes::Stream] the Stream to start
|
308
|
-
#
|
289
|
+
#
|
309
290
|
# @see start_parent
|
310
291
|
def start_stream(node)
|
311
292
|
start_parent(node,debug_tag: :stream,top_level: true)
|
312
293
|
@streams.push(node)
|
313
294
|
end
|
314
|
-
|
295
|
+
|
315
296
|
protected
|
316
|
-
|
297
|
+
|
317
298
|
# Add a non-parent node.
|
318
|
-
#
|
299
|
+
#
|
319
300
|
# This will increment {#position} accordingly, and if the child is a Key to a Mapping,
|
320
301
|
# create a fake "{SuperSniffer::Parent}".
|
321
|
-
#
|
302
|
+
#
|
322
303
|
# @param node [Psych::Nodes::Node] the non-parent Node to add
|
323
|
-
#
|
304
|
+
#
|
324
305
|
# @see end_mapping_value
|
325
306
|
def add_child(node)
|
326
|
-
if !@parent.nil?
|
307
|
+
if !@parent.nil?
|
327
308
|
# Fake a "parent" if necessary
|
328
309
|
case @parent.child_type
|
329
310
|
when :key
|
330
311
|
start_mapping_key(node)
|
331
312
|
return
|
332
313
|
when :value
|
333
|
-
end_mapping_value
|
314
|
+
end_mapping_value
|
334
315
|
return
|
335
316
|
else
|
336
317
|
@parent.child_position += 1
|
337
318
|
end
|
338
319
|
end
|
339
|
-
|
320
|
+
|
340
321
|
@position += 1
|
341
|
-
|
322
|
+
|
342
323
|
@nodes.push(node)
|
343
324
|
end
|
344
|
-
|
325
|
+
|
345
326
|
# End a fake "{SuperSniffer::Parent}" that is a Key/Value to a Mapping.
|
346
|
-
#
|
327
|
+
#
|
347
328
|
# @see add_child
|
348
|
-
def end_mapping_value
|
349
|
-
end_parent
|
350
|
-
|
351
|
-
@parent.child_type = :key unless @parent.nil?
|
329
|
+
def end_mapping_value
|
330
|
+
end_parent # Do not pass in "mapping_value: true" and/or "top_level: true"
|
331
|
+
|
332
|
+
@parent.child_type = :key unless @parent.nil?
|
352
333
|
end
|
353
|
-
|
334
|
+
|
354
335
|
# End a {SuperSniffer::Parent}.
|
355
|
-
#
|
336
|
+
#
|
356
337
|
# Pops off a parent from {#parents} and sets {#parent} to the last one.
|
357
338
|
# {#level} and {#position} are reset according to the last parent.
|
358
|
-
#
|
339
|
+
#
|
359
340
|
# @param mapping_value [true,false] true if parent can be the value of a Mapping's key
|
360
341
|
# @param top_level [true,false] true if a top-level parent (i.e., encapsulating the main data)
|
361
342
|
def end_parent(mapping_value: false,top_level: false)
|
362
|
-
@parents.pop
|
343
|
+
@parents.pop
|
363
344
|
@parent = @parents.last
|
364
|
-
|
345
|
+
|
365
346
|
@level = top_level ? 1 : (@level - 1)
|
366
|
-
|
367
|
-
if !@parent.nil?
|
347
|
+
|
348
|
+
if !@parent.nil?
|
368
349
|
@parent.child_position += 1
|
369
350
|
@position = @parent.child_position
|
370
|
-
|
351
|
+
|
371
352
|
# add_child() will not be called again, so end a fake "parent" manually with a fake "value"
|
372
353
|
# - This is necessary for any parents that can be the value of a map's key (e.g., Sequence)
|
373
|
-
end_mapping_value
|
354
|
+
end_mapping_value if mapping_value && !@parent.child_type.nil?
|
374
355
|
end
|
375
356
|
end
|
376
|
-
|
357
|
+
|
377
358
|
# Start a fake "{SuperSniffer::Parent}" that is a Key/Value to a Mapping.
|
378
|
-
#
|
359
|
+
#
|
379
360
|
# Creates a {SuperSniffer::Parent}, sets {#parent} to it, and adds it to {#parents}.
|
380
361
|
# {#level} and {#position} are incremented/set accordingly.
|
381
|
-
#
|
362
|
+
#
|
382
363
|
# @param node [Psych::Nodes::Node] the Node to start
|
383
|
-
#
|
364
|
+
#
|
384
365
|
# @see start_parent
|
385
366
|
def start_mapping_key(node)
|
386
367
|
debug_tag = nil
|
387
|
-
|
368
|
+
|
388
369
|
# Value must be first because Scalar also has an anchor
|
389
370
|
if node.respond_to?(:value)
|
390
371
|
debug_tag = node.value
|
391
372
|
elsif node.respond_to?(:anchor)
|
392
373
|
debug_tag = node.anchor
|
393
374
|
end
|
394
|
-
|
395
|
-
debug_tag = :noface if debug_tag.nil?
|
396
|
-
|
375
|
+
|
376
|
+
debug_tag = :noface if debug_tag.nil?
|
377
|
+
|
397
378
|
start_parent(node,debug_tag: debug_tag,child_type: :value)
|
398
379
|
end
|
399
|
-
|
380
|
+
|
400
381
|
# Start a {SuperSniffer::Parent}.
|
401
|
-
#
|
382
|
+
#
|
402
383
|
# Creates a {SuperSniffer::Parent}, sets {#parent} to it, and adds it to {#parents}.
|
403
384
|
# {#level} and {#position} are incremented/set accordingly.
|
404
|
-
#
|
385
|
+
#
|
405
386
|
# @param node [Psych::Nodes::Node] the parent Node to start
|
406
387
|
# @param top_level [true,false] true if a top-level parent (i.e., encapsulating the main data)
|
407
388
|
# @param extra [Hash] the extra keyword args to pass to {SuperSniffer::Parent#initialize}
|
408
|
-
#
|
389
|
+
#
|
409
390
|
# @see SuperSniffer::Parent#initialize
|
410
391
|
def start_parent(node,top_level: false,**extra)
|
411
392
|
@parent = Parent.new(self,node,**extra)
|
412
|
-
|
393
|
+
|
413
394
|
@parents.push(@parent)
|
414
|
-
@nodes.push(node) unless node.nil?
|
415
|
-
|
395
|
+
@nodes.push(node) unless node.nil?
|
396
|
+
|
416
397
|
if top_level
|
417
398
|
@level = 1
|
418
399
|
@position = @parent.position
|
data/lib/psychgus/version.rb
CHANGED
@@ -1,27 +1,14 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
1
|
# encoding: UTF-8
|
3
2
|
# frozen_string_literal: true
|
4
3
|
|
5
4
|
#--
|
6
5
|
# This file is part of Psychgus.
|
7
|
-
# Copyright (c) 2017
|
8
|
-
#
|
9
|
-
#
|
10
|
-
# it under the terms of the GNU Lesser General Public License as published by
|
11
|
-
# the Free Software Foundation, either version 3 of the License, or
|
12
|
-
# (at your option) any later version.
|
13
|
-
#
|
14
|
-
# Psychgus is distributed in the hope that it will be useful,
|
15
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
-
# GNU Lesser General Public License for more details.
|
18
|
-
#
|
19
|
-
# You should have received a copy of the GNU Lesser General Public License
|
20
|
-
# along with Psychgus. If not, see <http://www.gnu.org/licenses/>.
|
6
|
+
# Copyright (c) 2017 Bradley Whited
|
7
|
+
#
|
8
|
+
# SPDX-License-Identifier: LGPL-3.0-or-later
|
21
9
|
#++
|
22
10
|
|
23
|
-
|
24
11
|
module Psychgus
|
25
|
-
# Version of this gem in "#.#.#" format
|
26
|
-
VERSION = '1.3.
|
12
|
+
# Version of this gem in "#.#.#" format.
|
13
|
+
VERSION = '1.3.5'
|
27
14
|
end
|