psychgus 1.3.3 → 1.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.yardopts +4 -0
- data/CHANGELOG.md +22 -7
- data/Gemfile +0 -18
- data/README.md +27 -28
- data/Rakefile +20 -42
- data/lib/psychgus.rb +169 -179
- data/lib/psychgus/blueberry.rb +28 -39
- data/lib/psychgus/ext.rb +6 -17
- data/lib/psychgus/ext/core_ext.rb +17 -28
- data/lib/psychgus/ext/node_ext.rb +13 -24
- data/lib/psychgus/ext/yaml_tree_ext.rb +23 -34
- data/lib/psychgus/stylables.rb +78 -89
- data/lib/psychgus/styled_document_stream.rb +14 -25
- data/lib/psychgus/styled_tree_builder.rb +90 -101
- data/lib/psychgus/styler.rb +33 -44
- data/lib/psychgus/stylers.rb +67 -78
- data/lib/psychgus/super_sniffer.rb +111 -123
- data/lib/psychgus/super_sniffer/parent.rb +42 -51
- data/lib/psychgus/version.rb +4 -16
- data/psychgus.gemspec +39 -43
- data/test/blueberry_test.rb +30 -41
- data/test/psychgus_test.rb +51 -60
- data/test/psychgus_tester.rb +23 -31
- data/test/sniffer_test.rb +18 -32
- data/test/styler_test.rb +20 -31
- data/test/stylers_test.rb +32 -43
- metadata +15 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 91a1bbacfcd8d1a658b07bfad787182c7a789b0d0b2fb3ab51a30b9954572484
|
4
|
+
data.tar.gz: 6afd3dba752a30d76bd694d3d5866765522605757c5826f3c366c51d7a19cc0f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d01c30c222b5da77b34a3c4fd0f97f6d1685fea13a9a4addb4fd91ff178733326aabc57ae72a1e8d4511b11a021210ba4aeb9eec7498b81f23310a7dfa307fd8
|
7
|
+
data.tar.gz: 0d4deeab277e4894124f8fec33f8e1064a5f95c1bd684dd2f09d612427094c3540452608068cd6076080df8545c395d2b9ab55e04437188f164b416bba8568d4
|
data/.yardopts
ADDED
data/CHANGELOG.md
CHANGED
@@ -1,18 +1,32 @@
|
|
1
1
|
# Changelog | Psychgus
|
2
2
|
|
3
|
-
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
4
|
|
5
|
-
|
5
|
+
Format is based on [Keep a Changelog v1.0.0](https://keepachangelog.com/en/1.0.0),
|
6
|
+
and this project adheres to [Semantic Versioning v2.0.0](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [[Unreleased]](https://github.com/esotericpig/psychgus/compare/v1.3.4...HEAD)
|
9
|
+
-
|
6
10
|
|
7
|
-
## [v1.3.3] - 2020-04-25
|
8
11
|
|
12
|
+
## [v1.3.4] - 2021-06-20
|
13
|
+
### Fixed
|
14
|
+
- Fixed test file to use `unsafe_load()` for Psych v4+.
|
15
|
+
|
16
|
+
### Changed
|
17
|
+
- Changed `SuperSniffer::Parent` to use `SimpleDelegator`.
|
18
|
+
- Updated Gems.
|
19
|
+
- Formatted files with RuboCop.
|
20
|
+
|
21
|
+
|
22
|
+
## [v1.3.3] - 2020-04-25
|
9
23
|
### Fixed
|
10
24
|
- SuperSniffer::Parent
|
11
25
|
- Added require of `delegate` for Delegator
|
12
26
|
- Rake test task didn't catch this (must already include it)
|
13
27
|
|
14
|
-
## [v1.3.2] - 2020-04-23
|
15
28
|
|
29
|
+
## [v1.3.2] - 2020-04-23
|
16
30
|
### Changed
|
17
31
|
- SuperSniffer::Parent
|
18
32
|
- Changed to use Delegator to delegate all methods of `node`
|
@@ -22,16 +36,16 @@ Format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|
22
36
|
### Fixed
|
23
37
|
- Fixed some Ruby 2.7 warnings in tests
|
24
38
|
|
25
|
-
## [v1.2.2] - 2020-03-15
|
26
39
|
|
40
|
+
## [v1.2.2] - 2020-03-15
|
27
41
|
### Fixed
|
28
42
|
- Fixed Psych version to be >= 3.0 for `to_yaml/encode_with` warnings
|
29
43
|
- This is mainly for Windows
|
30
44
|
- [Ruby-lang Bug #13115](https://bugs.ruby-lang.org/issues/13115)
|
31
45
|
- [GitHub Psych Commit](https://github.com/ruby/psych/commit/712a65a53f3c15105cd86e8ad3ee3c779050ada4)
|
32
46
|
|
33
|
-
## [v1.2.1] - 2019-12-18
|
34
47
|
|
48
|
+
## [v1.2.1] - 2019-12-18
|
35
49
|
### Added
|
36
50
|
- Use of YardGhurt gem for Rakefile tasks
|
37
51
|
|
@@ -44,8 +58,8 @@ Format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|
44
58
|
### Fixed
|
45
59
|
- Updated gems
|
46
60
|
|
47
|
-
## [v1.2.0] - 2019-07-11
|
48
61
|
|
62
|
+
## [v1.2.0] - 2019-07-11
|
49
63
|
### Added
|
50
64
|
- Commonly-used Stylers and Stylables
|
51
65
|
- Changelog
|
@@ -60,6 +74,7 @@ Format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|
60
74
|
### Fixed
|
61
75
|
- Psychgus.dump_stream() if you only pass in a Hash w/ symbols as keys (options would be set to it, instead of objects)
|
62
76
|
|
77
|
+
|
63
78
|
## [v1.0.0] - 2019-07-03
|
64
79
|
### Added
|
65
80
|
### Changed
|
data/Gemfile
CHANGED
@@ -1,24 +1,6 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
|
-
#--
|
5
|
-
# This file is part of Psychgus.
|
6
|
-
# Copyright (c) 2017-2019 Jonathan Bradley Whited (@esotericpig)
|
7
|
-
#
|
8
|
-
# Psychgus is free software: you can redistribute it and/or modify
|
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/>.
|
20
|
-
#++
|
21
|
-
|
22
4
|
|
23
5
|
source 'https://rubygems.org'
|
24
6
|
|
data/README.md
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
# Psychgus
|
2
2
|
|
3
3
|
[](https://badge.fury.io/rb/psychgus)
|
4
|
-
[](https://travis-ci.org/esotericpig/psychgus)
|
5
4
|
[](https://inch-ci.org/github/esotericpig/psychgus)
|
6
5
|
|
7
6
|
[](https://esotericpig.github.io/docs/psychgus/yardoc/index.html)
|
@@ -118,7 +117,7 @@ require 'psychgus'
|
|
118
117
|
|
119
118
|
class CoffeeStyler
|
120
119
|
include Psychgus::Styler
|
121
|
-
|
120
|
+
|
122
121
|
def style_sequence(sniffer,node)
|
123
122
|
node.style = Psychgus::SEQUENCE_FLOW
|
124
123
|
end
|
@@ -138,12 +137,12 @@ puts coffee.to_yaml(stylers: CoffeeStyler.new)
|
|
138
137
|
|
139
138
|
class Coffee
|
140
139
|
include Psychgus::Blueberry
|
141
|
-
|
140
|
+
|
142
141
|
def initialize
|
143
142
|
@roast = ['Light', 'Medium', 'Dark', 'Extra Dark']
|
144
143
|
@style = ['Cappuccino', 'Espresso', 'Latte', 'Mocha']
|
145
144
|
end
|
146
|
-
|
145
|
+
|
147
146
|
def psychgus_stylers(sniffer)
|
148
147
|
CoffeeStyler.new
|
149
148
|
end
|
@@ -164,20 +163,20 @@ require 'psychgus'
|
|
164
163
|
|
165
164
|
class BurgerStyler
|
166
165
|
include Psychgus::Styler # Mix in methods needed for styling
|
167
|
-
|
166
|
+
|
168
167
|
# Style maps (Psych::Nodes::Mapping)
|
169
168
|
# - Hashes (key/value pairs)
|
170
169
|
# - Example: "Burgers: Classic {}"
|
171
170
|
def style_mapping(sniffer,node)
|
172
171
|
node.style = Psychgus::MAPPING_FLOW if sniffer.level >= 4
|
173
172
|
end
|
174
|
-
|
173
|
+
|
175
174
|
# Style scalars (Psych::Nodes::Scalar)
|
176
175
|
# - Any text (non-alias)
|
177
176
|
def style_scalar(sniffer,node)
|
178
177
|
# Remove colon (change symbols into strings)
|
179
178
|
node.value = node.value.sub(':','')
|
180
|
-
|
179
|
+
|
181
180
|
# Capitalize each word
|
182
181
|
node.value = node.value.split(' ').map do |v|
|
183
182
|
if v.casecmp('BBQ') == 0
|
@@ -186,11 +185,11 @@ class BurgerStyler
|
|
186
185
|
v.capitalize()
|
187
186
|
end
|
188
187
|
end.join(' ')
|
189
|
-
|
188
|
+
|
190
189
|
# Change lettuce to spinach
|
191
190
|
node.value = 'Spinach' if node.value == 'Lettuce'
|
192
191
|
end
|
193
|
-
|
192
|
+
|
194
193
|
# Style sequences (Psych::Nodes::Sequence)
|
195
194
|
# - Arrays
|
196
195
|
# - Example: "[Lettuce, Onions, Pickles, Tomatoes]"
|
@@ -259,50 +258,50 @@ require 'psychgus'
|
|
259
258
|
|
260
259
|
class BurgerStyler
|
261
260
|
include Psychgus::Styler # Mix in methods needed for styling
|
262
|
-
|
261
|
+
|
263
262
|
def initialize(sniffer)
|
264
263
|
@class_level = sniffer.level
|
265
264
|
@class_position = sniffer.position
|
266
265
|
end
|
267
|
-
|
266
|
+
|
268
267
|
# Style all nodes (Psych::Nodes::Node)
|
269
268
|
def style(sniffer,node)
|
270
269
|
# Remove "!ruby/object:..." for Burger classes (not Burgers class)
|
271
270
|
node.tag = nil if node.node_of?(:mapping,:scalar,:sequence)
|
272
|
-
|
271
|
+
|
273
272
|
# This is another way to do the above
|
274
273
|
#node.tag = nil if node.respond_to?(:tag=)
|
275
274
|
end
|
276
|
-
|
275
|
+
|
277
276
|
# Style maps (Psych::Nodes::Mapping)
|
278
277
|
# - Hashes (key/value pairs)
|
279
278
|
# - Example: "Burgers: Classic {}"
|
280
279
|
def style_mapping(sniffer,node)
|
281
280
|
parent = sniffer.parent
|
282
|
-
|
281
|
+
|
283
282
|
if !parent.nil?()
|
284
283
|
# BBQ
|
285
284
|
node.style = Psychgus::MAPPING_FLOW if parent.node_of?(:scalar) &&
|
286
285
|
parent.value.casecmp('BBQ') == 0
|
287
286
|
end
|
288
287
|
end
|
289
|
-
|
288
|
+
|
290
289
|
# Style scalars (Psych::Nodes::Scalar)
|
291
290
|
# - Any text (non-alias)
|
292
291
|
def style_scalar(sniffer,node)
|
293
292
|
parent = sniffer.parent
|
294
|
-
|
293
|
+
|
295
294
|
# Single quote scalars that are not keys to a map
|
296
295
|
# - "child_key?" is the same as "child_type == :key"
|
297
296
|
node.style = Psychgus::SCALAR_SINGLE_QUOTED unless parent.child_key?()
|
298
297
|
end
|
299
|
-
|
298
|
+
|
300
299
|
# Style sequences (Psych::Nodes::Sequence)
|
301
300
|
# - Arrays
|
302
301
|
# - Example: "[Lettuce, Onions, Pickles, Tomatoes]"
|
303
302
|
def style_sequence(sniffer,node)
|
304
303
|
relative_level = (sniffer.level - @class_level) + 1
|
305
|
-
|
304
|
+
|
306
305
|
# "[Ketchup, Mustard]"
|
307
306
|
node.style = Psychgus::SEQUENCE_FLOW if relative_level == 3
|
308
307
|
end
|
@@ -310,23 +309,23 @@ end
|
|
310
309
|
|
311
310
|
class Burger
|
312
311
|
include Psychgus::Blueberry # Mix in methods needed to be stylable
|
313
|
-
|
312
|
+
|
314
313
|
attr_accessor :bun
|
315
314
|
attr_accessor :cheese
|
316
315
|
attr_accessor :sauce
|
317
|
-
|
316
|
+
|
318
317
|
def initialize(sauce,cheese,bun)
|
319
318
|
@bun = bun
|
320
319
|
@cheese = cheese
|
321
320
|
@sauce = sauce
|
322
321
|
end
|
323
|
-
|
322
|
+
|
324
323
|
# Return our styler(s)
|
325
324
|
# - Can be an Array: [MyStyler1.new, MyStyler2.new]
|
326
325
|
def psychgus_stylers(sniffer)
|
327
326
|
return BurgerStyler.new(sniffer)
|
328
327
|
end
|
329
|
-
|
328
|
+
|
330
329
|
# You can still use Psych's encode_with(), no problem
|
331
330
|
def encode_with(coder)
|
332
331
|
coder['Bun'] = @bun
|
@@ -339,23 +338,23 @@ class Burgers
|
|
339
338
|
attr_accessor :burgers
|
340
339
|
attr_accessor :toppings
|
341
340
|
attr_accessor :favorite
|
342
|
-
|
341
|
+
|
343
342
|
def initialize()
|
344
343
|
@burgers = {
|
345
344
|
'Classic' => Burger.new(['Ketchup','Mustard'],'American' ,'Sesame Seed'),
|
346
345
|
'BBQ' => Burger.new('Honey BBQ' ,'Cheddar' ,'Kaiser'),
|
347
346
|
'Fancy' => Burger.new('Spicy Wasabi' ,'Smoked Gouda','Hawaiian')
|
348
347
|
}
|
349
|
-
|
348
|
+
|
350
349
|
@toppings = [
|
351
350
|
'Mushrooms',
|
352
351
|
%w(Lettuce Onions Pickles Tomatoes),
|
353
352
|
[%w(Ketchup Mustard),%w(Salt Pepper)]
|
354
353
|
]
|
355
|
-
|
354
|
+
|
356
355
|
@favorite = @burgers['BBQ'] # Alias
|
357
356
|
end
|
358
|
-
|
357
|
+
|
359
358
|
# You can still use Psych's encode_with(), no problem
|
360
359
|
def encode_with(coder)
|
361
360
|
coder['Burgers'] = @burgers
|
@@ -430,7 +429,7 @@ require 'psychgus'
|
|
430
429
|
|
431
430
|
class MyStyler
|
432
431
|
include Psychgus::Styler
|
433
|
-
|
432
|
+
|
434
433
|
def style_sequence(sniffer,node)
|
435
434
|
node.style = Psychgus::SEQUENCE_FLOW
|
436
435
|
end
|
@@ -605,7 +604,7 @@ Clean & generate pristine doc:
|
|
605
604
|
[GNU LGPL v3+](LICENSE.txt)
|
606
605
|
|
607
606
|
> Psychgus (<https://github.com/esotericpig/psychgus>)
|
608
|
-
> Copyright (c) 2017-
|
607
|
+
> Copyright (c) 2017-2021 Jonathan Bradley Whited
|
609
608
|
>
|
610
609
|
> Psychgus is free software: you can redistribute it and/or modify
|
611
610
|
> it under the terms of the GNU Lesser General Public License as published by
|
data/Rakefile
CHANGED
@@ -1,22 +1,5 @@
|
|
1
1
|
# encoding: UTF-8
|
2
|
-
|
3
|
-
#--
|
4
|
-
# This file is part of Psychgus.
|
5
|
-
# Copyright (c) 2019-2020 Jonathan Bradley Whited (@esotericpig)
|
6
|
-
#
|
7
|
-
# Psychgus is free software: you can redistribute it and/or modify
|
8
|
-
# it under the terms of the GNU Lesser General Public License as published by
|
9
|
-
# the Free Software Foundation, either version 3 of the License, or
|
10
|
-
# (at your option) any later version.
|
11
|
-
#
|
12
|
-
# Psychgus is distributed in the hope that it will be useful,
|
13
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
-
# GNU Lesser General Public License for more details.
|
16
|
-
#
|
17
|
-
# You should have received a copy of the GNU Lesser General Public License
|
18
|
-
# along with Psychgus. If not, see <https://www.gnu.org/licenses/>.
|
19
|
-
#++
|
2
|
+
# frozen_string_literal: true
|
20
3
|
|
21
4
|
|
22
5
|
require 'bundler/gem_tasks'
|
@@ -38,10 +21,11 @@ CLOBBER.include('doc/')
|
|
38
21
|
|
39
22
|
# Execute "rake clobber doc" for pristine docs
|
40
23
|
desc 'Generate documentation (YARDoc)'
|
41
|
-
task :
|
42
|
-
end
|
24
|
+
task doc: %i[yard yard_gfm_fix]
|
43
25
|
|
44
|
-
|
26
|
+
# To test using different Gem versions:
|
27
|
+
# GST=1 bundle update && bundle exec rake test
|
28
|
+
Rake::TestTask.new do |task|
|
45
29
|
task.libs = ['lib','test']
|
46
30
|
task.pattern = File.join('test','**','*_test.rb')
|
47
31
|
task.description += " ('#{task.pattern}')"
|
@@ -53,40 +37,34 @@ end
|
|
53
37
|
desc 'Run all tests (including writing to temp files, etc.)'
|
54
38
|
task :test_all do |task|
|
55
39
|
ENV['PSYCHGUS_TEST'] = 'all'
|
56
|
-
|
40
|
+
|
57
41
|
test_task = Rake::Task[:test]
|
58
|
-
|
59
|
-
test_task.reenable
|
60
|
-
test_task.invoke
|
42
|
+
|
43
|
+
test_task.reenable
|
44
|
+
test_task.invoke
|
61
45
|
end
|
62
46
|
|
63
|
-
YARD::Rake::YardocTask.new
|
47
|
+
YARD::Rake::YardocTask.new do |task|
|
64
48
|
task.files = [File.join('lib','**','*.rb')]
|
65
|
-
|
66
|
-
task.options += ['--files','CHANGELOG.md,LICENSE.txt']
|
67
|
-
task.options += ['--readme','README.md']
|
68
|
-
|
69
|
-
task.options << '--protected' # Show protected methods
|
70
|
-
task.options += ['--template-path',File.join('yard','templates')]
|
71
49
|
task.options += ['--title',"Psychgus v#{Psychgus::VERSION} Doc"]
|
72
50
|
end
|
73
51
|
|
74
|
-
YardGhurt::GFMFixTask.new
|
52
|
+
YardGhurt::GFMFixTask.new do |task|
|
75
53
|
task.description = 'Fix (find & replace) text in the YARD files for GitHub differences'
|
76
|
-
|
54
|
+
|
77
55
|
task.arg_names = [:dev]
|
78
56
|
task.dry_run = false
|
79
57
|
task.fix_code_langs = true
|
80
58
|
task.md_files = ['index.html']
|
81
|
-
|
82
|
-
task.before =
|
59
|
+
|
60
|
+
task.before = proc do |t2,args|
|
83
61
|
# Delete this file as it's never used (index.html is an exact copy)
|
84
|
-
YardGhurt.rm_exist(File.join(
|
85
|
-
|
62
|
+
YardGhurt.rm_exist(File.join(t2.doc_dir,'file.README.html'))
|
63
|
+
|
86
64
|
# Root dir of my GitHub Page for CSS/JS
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
65
|
+
ghp_root = YardGhurt.to_bool(args.dev) ? '../../esotericpig.github.io' : '../../..'
|
66
|
+
|
67
|
+
t2.css_styles << %Q(<link rel="stylesheet" type="text/css" href="#{ghp_root}/css/prism.css" />)
|
68
|
+
t2.js_scripts << %Q(<script src="#{ghp_root}/js/prism.js"></script>)
|
91
69
|
end
|
92
70
|
end
|
data/lib/psychgus.rb
CHANGED
@@ -1,22 +1,11 @@
|
|
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) 2017-
|
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) 2017-2021 Jonathan Bradley Whited
|
7
|
+
#
|
8
|
+
# SPDX-License-Identifier: LGPL-3.0-or-later
|
20
9
|
#++
|
21
10
|
|
22
11
|
|
@@ -42,22 +31,22 @@ require 'psychgus/super_sniffer/parent'
|
|
42
31
|
# Psychgus uses the core standard library {https://github.com/ruby/psych Psych} for working with YAML
|
43
32
|
# and extends it so that developers can easily style the YAML according to their needs.
|
44
33
|
# Thank you to the people that worked and continue to work hard on that project.
|
45
|
-
#
|
34
|
+
#
|
46
35
|
# The name comes from the well-styled character Gus from the TV show Psych.
|
47
|
-
#
|
36
|
+
#
|
48
37
|
# == Create a Styler
|
49
|
-
#
|
38
|
+
#
|
50
39
|
# First, we will create a {Styler}.
|
51
|
-
#
|
40
|
+
#
|
52
41
|
# All you need to do is add +include Psychgus::Styler+ to a class.
|
53
|
-
#
|
42
|
+
#
|
54
43
|
# Here is a complex {Styler} for the examples below:
|
55
44
|
# require 'psychgus'
|
56
|
-
#
|
45
|
+
#
|
57
46
|
# class BurgerStyler
|
58
47
|
# # Mix in methods needed for styling
|
59
48
|
# include Psychgus::Styler
|
60
|
-
#
|
49
|
+
#
|
61
50
|
# def initialize(sniffer=nil)
|
62
51
|
# if sniffer.nil?()
|
63
52
|
# @class_level = 0
|
@@ -68,48 +57,48 @@ require 'psychgus/super_sniffer/parent'
|
|
68
57
|
# @class_position = sniffer.position
|
69
58
|
# end
|
70
59
|
# end
|
71
|
-
#
|
60
|
+
#
|
72
61
|
# # Style all nodes (Psych::Nodes::Node)
|
73
62
|
# def style(sniffer,node)
|
74
63
|
# # Remove "!ruby/object:..." for classes
|
75
64
|
# node.tag = nil if node.node_of?(:mapping,:scalar,:sequence)
|
76
|
-
#
|
65
|
+
#
|
77
66
|
# # This is another way to do the above
|
78
67
|
# #node.tag = nil if node.respond_to?(:tag=)
|
79
68
|
# end
|
80
|
-
#
|
69
|
+
#
|
81
70
|
# # Style aliases (Psych::Nodes::Alias)
|
82
71
|
# def style_alias(sniffer,node)
|
83
72
|
# end
|
84
|
-
#
|
73
|
+
#
|
85
74
|
# # Style maps (Psych::Nodes::Mapping)
|
86
75
|
# # - Hashes (key/value pairs)
|
87
76
|
# # - Example: "Burgers: Classic {}"
|
88
77
|
# def style_mapping(sniffer,node)
|
89
78
|
# parent = sniffer.parent
|
90
|
-
#
|
79
|
+
#
|
91
80
|
# if !parent.nil?()
|
92
81
|
# # BBQ
|
93
82
|
# node.style = Psychgus::MAPPING_FLOW if parent.node_of?(:scalar) &&
|
94
83
|
# parent.value.casecmp('BBQ') == 0
|
95
84
|
# end
|
96
85
|
# end
|
97
|
-
#
|
86
|
+
#
|
98
87
|
# # Style scalars (Psych::Nodes::Scalar)
|
99
88
|
# # - Any text (non-alias)
|
100
89
|
# def style_scalar(sniffer,node)
|
101
90
|
# parent = sniffer.parent
|
102
|
-
#
|
91
|
+
#
|
103
92
|
# # Single quote scalars that are not keys to a map
|
104
93
|
# # - "child_key?" is the same as "child_type == :key"
|
105
94
|
# node.style = Psychgus::SCALAR_SINGLE_QUOTED unless parent.child_key?()
|
106
|
-
#
|
95
|
+
#
|
107
96
|
# # Remove colon (change symbols into strings)
|
108
97
|
# node.value = node.value.sub(':','')
|
109
|
-
#
|
98
|
+
#
|
110
99
|
# # Change lettuce to spinach
|
111
100
|
# node.value = 'Spinach' if node.value.casecmp('Lettuce') == 0
|
112
|
-
#
|
101
|
+
#
|
113
102
|
# # Capitalize each word
|
114
103
|
# node.value = node.value.split(' ').map do |v|
|
115
104
|
# if v.casecmp('BBQ') == 0
|
@@ -119,23 +108,23 @@ require 'psychgus/super_sniffer/parent'
|
|
119
108
|
# end
|
120
109
|
# end.join(' ')
|
121
110
|
# end
|
122
|
-
#
|
111
|
+
#
|
123
112
|
# # Style sequences (Psych::Nodes::Sequence)
|
124
113
|
# # - Arrays
|
125
114
|
# # - Example: "[Lettuce, Onions, Pickles, Tomatoes]"
|
126
115
|
# def style_sequence(sniffer,node)
|
127
116
|
# relative_level = (sniffer.level - @class_level) + 1
|
128
|
-
#
|
117
|
+
#
|
129
118
|
# node.style = Psychgus::SEQUENCE_FLOW if sniffer.level >= 4
|
130
|
-
#
|
119
|
+
#
|
131
120
|
# # Make "[Ketchup, Mustard]" a block for the Class Example
|
132
121
|
# node.style = Psychgus::SEQUENCE_BLOCK if relative_level == 7
|
133
122
|
# end
|
134
123
|
# end
|
135
|
-
#
|
124
|
+
#
|
136
125
|
# @example Hash example
|
137
126
|
# require 'psychgus'
|
138
|
-
#
|
127
|
+
#
|
139
128
|
# burgers = {
|
140
129
|
# :Burgers => {
|
141
130
|
# :Classic => {
|
@@ -161,9 +150,9 @@ require 'psychgus/super_sniffer/parent'
|
|
161
150
|
# ]
|
162
151
|
# }
|
163
152
|
# burgers[:Favorite] = burgers[:Burgers][:BBQ] # Alias
|
164
|
-
#
|
153
|
+
#
|
165
154
|
# puts burgers.to_yaml(indent: 3,stylers: BurgerStyler.new,deref_aliases: true)
|
166
|
-
#
|
155
|
+
#
|
167
156
|
# # Output:
|
168
157
|
# # ---
|
169
158
|
# # Burgers:
|
@@ -184,21 +173,21 @@ require 'psychgus/super_sniffer/parent'
|
|
184
173
|
# # Sauce: 'Honey BBQ'
|
185
174
|
# # Cheese: 'Cheddar'
|
186
175
|
# # Bun: 'Kaiser'
|
187
|
-
#
|
176
|
+
#
|
188
177
|
# @example Class example
|
189
178
|
# require 'psychgus'
|
190
|
-
#
|
179
|
+
#
|
191
180
|
# class Burger
|
192
181
|
# attr_accessor :bun
|
193
182
|
# attr_accessor :cheese
|
194
183
|
# attr_accessor :sauce
|
195
|
-
#
|
184
|
+
#
|
196
185
|
# def initialize(sauce,cheese,bun)
|
197
186
|
# @bun = bun
|
198
187
|
# @cheese = cheese
|
199
188
|
# @sauce = sauce
|
200
189
|
# end
|
201
|
-
#
|
190
|
+
#
|
202
191
|
# # You can still use Psych's encode_with(), no problem
|
203
192
|
# #def encode_with(coder)
|
204
193
|
# # coder['Bun'] = @bun
|
@@ -206,34 +195,34 @@ require 'psychgus/super_sniffer/parent'
|
|
206
195
|
# # coder['Sauce'] = @sauce
|
207
196
|
# #end
|
208
197
|
# end
|
209
|
-
#
|
198
|
+
#
|
210
199
|
# class Burgers
|
211
200
|
# include Psychgus::Blueberry
|
212
|
-
#
|
201
|
+
#
|
213
202
|
# attr_accessor :burgers
|
214
203
|
# attr_accessor :toppings
|
215
204
|
# attr_accessor :favorite
|
216
|
-
#
|
205
|
+
#
|
217
206
|
# def initialize()
|
218
207
|
# @burgers = {
|
219
208
|
# 'Classic' => Burger.new(['Ketchup','Mustard'],'American','Sesame Seed'),
|
220
209
|
# 'BBQ' => Burger.new('Honey BBQ','Cheddar','Kaiser'),
|
221
210
|
# 'Fancy' => Burger.new('Spicy Wasabi','Smoked Gouda','Hawaiian')
|
222
211
|
# }
|
223
|
-
#
|
212
|
+
#
|
224
213
|
# @toppings = [
|
225
214
|
# 'Mushrooms',
|
226
215
|
# %w(Lettuce Onions Pickles Tomatoes),
|
227
216
|
# [%w(Ketchup Mustard),%w(Salt Pepper)]
|
228
217
|
# ]
|
229
|
-
#
|
218
|
+
#
|
230
219
|
# @favorite = @burgers['BBQ'] # Alias
|
231
220
|
# end
|
232
|
-
#
|
221
|
+
#
|
233
222
|
# def psychgus_stylers(sniffer)
|
234
223
|
# return BurgerStyler.new(sniffer)
|
235
224
|
# end
|
236
|
-
#
|
225
|
+
#
|
237
226
|
# # You can still use Psych's encode_with(), no problem
|
238
227
|
# #def encode_with(coder)
|
239
228
|
# # coder['Burgers'] = @burgers
|
@@ -241,10 +230,10 @@ require 'psychgus/super_sniffer/parent'
|
|
241
230
|
# # coder['Favorite'] = @favorite
|
242
231
|
# #end
|
243
232
|
# end
|
244
|
-
#
|
233
|
+
#
|
245
234
|
# burgers = Burgers.new
|
246
235
|
# puts burgers.to_yaml(indent: 3,deref_aliases: true)
|
247
|
-
#
|
236
|
+
#
|
248
237
|
# # Output:
|
249
238
|
# # ---
|
250
239
|
# # Burgers:
|
@@ -267,144 +256,144 @@ require 'psychgus/super_sniffer/parent'
|
|
267
256
|
# # Bun: 'Kaiser'
|
268
257
|
# # Cheese: 'Cheddar'
|
269
258
|
# # Sauce: 'Honey BBQ'
|
270
|
-
#
|
259
|
+
#
|
271
260
|
# @example Emitting / Parsing examples
|
272
261
|
# styler = BurgerStyler.new()
|
273
262
|
# options = {:indentation=>3,:stylers=>styler,:deref_aliases=>true}
|
274
263
|
# yaml = burgers.to_yaml(options)
|
275
|
-
#
|
264
|
+
#
|
276
265
|
# # High-level emitting
|
277
266
|
# Psychgus.dump(burgers,options)
|
278
267
|
# Psychgus.dump_file('burgers.yaml',burgers,options)
|
279
268
|
# burgers.to_yaml(options)
|
280
|
-
#
|
269
|
+
#
|
281
270
|
# # High-level parsing
|
282
271
|
# # - Because to_ruby() will be called, just use Psych:
|
283
272
|
# # - load(), load_file(), load_stream(), safe_load()
|
284
|
-
#
|
273
|
+
#
|
285
274
|
# # Mid-level emitting
|
286
275
|
# stream = Psychgus.parse_stream(yaml,stylers: styler,deref_aliases: true)
|
287
|
-
#
|
276
|
+
#
|
288
277
|
# stream.to_yaml()
|
289
|
-
#
|
278
|
+
#
|
290
279
|
# # Mid-level parsing
|
291
280
|
# Psychgus.parse(yaml,stylers: styler,deref_aliases: true)
|
292
281
|
# Psychgus.parse_file('burgers.yaml',stylers: styler,deref_aliases: true)
|
293
282
|
# Psychgus.parse_stream(yaml,stylers: styler,deref_aliases: true)
|
294
|
-
#
|
283
|
+
#
|
295
284
|
# # Low-level emitting
|
296
285
|
# tree_builder = Psychgus::StyledTreeBuilder.new(styler,deref_aliases: true)
|
297
286
|
# visitor = Psych::Visitors::YAMLTree.create(options,tree_builder)
|
298
|
-
#
|
287
|
+
#
|
299
288
|
# visitor << burgers
|
300
289
|
# visitor.tree.to_yaml
|
301
|
-
#
|
290
|
+
#
|
302
291
|
# # Low-level parsing
|
303
292
|
# parser = Psychgus.parser(stylers: styler,deref_aliases: true)
|
304
|
-
#
|
293
|
+
#
|
305
294
|
# parser.parse(yaml)
|
306
295
|
# parser.handler
|
307
296
|
# parser.handler.root
|
308
|
-
#
|
309
|
-
# @author Jonathan Bradley Whited
|
297
|
+
#
|
298
|
+
# @author Jonathan Bradley Whited
|
310
299
|
# @since 1.0.0
|
311
300
|
###
|
312
301
|
module Psychgus
|
313
302
|
# Include these in the top namespace for convenience (i.e., less typing).
|
314
303
|
include Stylables # @since 1.2.0
|
315
304
|
include Stylers # @since 1.2.0
|
316
|
-
|
317
|
-
NODE_CLASS_ALIASES = {:
|
318
|
-
OPTIONS_ALIASES = {:
|
319
|
-
|
305
|
+
|
306
|
+
NODE_CLASS_ALIASES = {Doc: :Document,Map: :Mapping,Seq: :Sequence}.freeze
|
307
|
+
OPTIONS_ALIASES = {canon: :canonical,indent: :indentation}.freeze
|
308
|
+
|
320
309
|
# Get a Class (constant) from Psych::Nodes.
|
321
|
-
#
|
310
|
+
#
|
322
311
|
# Some +name+s have aliases:
|
323
312
|
# :doc => :document
|
324
313
|
# :map => :mapping
|
325
314
|
# :seq => :sequence
|
326
|
-
#
|
315
|
+
#
|
327
316
|
# @param name [Symbol,String] the name of the class from Psych::Nodes
|
328
|
-
#
|
317
|
+
#
|
329
318
|
# @return [Class] a class from Psych::Nodes
|
330
|
-
#
|
319
|
+
#
|
331
320
|
# @see Psych::Nodes
|
332
321
|
# @see NODE_CLASS_ALIASES
|
333
322
|
def self.node_class(name)
|
334
|
-
name = name.to_sym
|
335
|
-
|
323
|
+
name = name.to_sym.capitalize
|
324
|
+
|
336
325
|
actual_name = NODE_CLASS_ALIASES[name]
|
337
|
-
name = actual_name unless actual_name.nil?
|
338
|
-
|
326
|
+
name = actual_name unless actual_name.nil?
|
327
|
+
|
339
328
|
return Psych::Nodes.const_get(name)
|
340
329
|
end
|
341
|
-
|
330
|
+
|
342
331
|
# Get a constant from a Psych::Nodes class (using {.node_class}).
|
343
|
-
#
|
332
|
+
#
|
344
333
|
# @param class_name [Symbol,String] the name of the class to get using {.node_class}
|
345
334
|
# @param const_name [Symbol,String] the constant to get from the class
|
346
335
|
# @param lenient [true,false] if true, will return 0 if not const_defined?(), else raise an error
|
347
|
-
#
|
336
|
+
#
|
348
337
|
# @return [Integer,Object] the constant value from the class (usually an int)
|
349
|
-
#
|
338
|
+
#
|
350
339
|
# @see .node_class
|
351
340
|
def self.node_const(class_name,const_name,lenient=true)
|
352
341
|
node_class = node_class(class_name)
|
353
|
-
const_name = const_name.to_sym
|
354
|
-
|
342
|
+
const_name = const_name.to_sym.upcase
|
343
|
+
|
355
344
|
return 0 if lenient && !node_class.const_defined?(const_name,true)
|
356
345
|
return node_class.const_get(const_name,true)
|
357
346
|
end
|
358
|
-
|
347
|
+
|
359
348
|
MAPPING_ANY = node_const(:mapping,:any)
|
360
349
|
MAPPING_BLOCK = node_const(:mapping,:block)
|
361
350
|
MAPPING_FLOW = node_const(:mapping,:flow)
|
362
351
|
MAP_ANY = MAPPING_ANY
|
363
352
|
MAP_BLOCK = MAPPING_BLOCK
|
364
353
|
MAP_FLOW = MAPPING_FLOW
|
365
|
-
|
354
|
+
|
366
355
|
SCALAR_ANY = node_const(:scalar,:any)
|
367
356
|
SCALAR_PLAIN = node_const(:scalar,:plain)
|
368
357
|
SCALAR_SINGLE_QUOTED = node_const(:scalar,:single_quoted)
|
369
358
|
SCALAR_DOUBLE_QUOTED = node_const(:scalar,:double_quoted)
|
370
359
|
SCALAR_LITERAL = node_const(:scalar,:literal)
|
371
360
|
SCALAR_FOLDED = node_const(:scalar,:folded)
|
372
|
-
|
361
|
+
|
373
362
|
SEQUENCE_ANY = node_const(:sequence,:any)
|
374
363
|
SEQUENCE_BLOCK = node_const(:sequence,:block)
|
375
364
|
SEQUENCE_FLOW = node_const(:sequence,:flow)
|
376
365
|
SEQ_ANY = SEQUENCE_ANY
|
377
366
|
SEQ_BLOCK = SEQUENCE_BLOCK
|
378
367
|
SEQ_FLOW = SEQUENCE_FLOW
|
379
|
-
|
368
|
+
|
380
369
|
STREAM_ANY = node_const(:stream,:any)
|
381
370
|
STREAM_UTF8 = node_const(:stream,:utf8)
|
382
371
|
STREAM_UTF16LE = node_const(:stream,:utf16le)
|
383
372
|
STREAM_UTF16BE = node_const(:stream,:utf16be)
|
384
|
-
|
373
|
+
|
385
374
|
# Convert +object+ to YAML and dump to +io+.
|
386
|
-
#
|
375
|
+
#
|
387
376
|
# +object+, +io+, and +options+ are used like in Psych.dump so can be a drop-in replacement for Psych.
|
388
|
-
#
|
377
|
+
#
|
389
378
|
# @param object [Object] the Object to convert to YAML and dump
|
390
379
|
# @param io [nil,IO,Hash] the IO to dump the YAML to or the +options+ Hash; if nil, will use StringIO
|
391
380
|
# @param options [Hash] the options (or keyword args) to use; see {.dump_stream}
|
392
|
-
#
|
381
|
+
#
|
393
382
|
# @return [String,Object] the result of converting +object+ to YAML using the params
|
394
|
-
#
|
383
|
+
#
|
395
384
|
# @see .dump_stream
|
396
385
|
# @see Psych.dump_stream
|
397
386
|
def self.dump(object,io=nil,**options)
|
398
387
|
return dump_stream(object,io: io,**options)
|
399
388
|
end
|
400
|
-
|
389
|
+
|
401
390
|
# Convert +objects+ to YAML and dump to a file.
|
402
|
-
#
|
391
|
+
#
|
403
392
|
# @example
|
404
393
|
# Psychgus.dump_file('my_dir/my_file.yaml',my_object1,my_object2,mode: 'w:UTF-16',
|
405
394
|
# stylers: MyStyler.new())
|
406
395
|
# Psychgus.dump_file('my_file.yaml',my_object,stylers: [MyStyler1.new(),MyStyler2.new()])
|
407
|
-
#
|
396
|
+
#
|
408
397
|
# @param filename [String] the name of the file (and path) to dump to
|
409
398
|
# @param objects [Object,Array<Object>] the Object(s) to convert to YAML and dump
|
410
399
|
# @param mode [String,Integer] the IO open mode to use; examples:
|
@@ -415,23 +404,23 @@ module Psychgus
|
|
415
404
|
# @param perm [Integer] the permission bits to use (platform dependent)
|
416
405
|
# @param opt [Hash] Hash of keyword args to pass to +File.open()+
|
417
406
|
# @param options [Hash] the options (or keyword args) to use; see {.dump_stream}
|
418
|
-
#
|
407
|
+
#
|
419
408
|
# @see .dump_stream
|
420
409
|
# @see File.open
|
421
410
|
# @see IO.new
|
422
411
|
# @see https://ruby-doc.org/core/IO.html#method-c-new
|
423
412
|
def self.dump_file(filename,*objects,mode: 'w',perm: nil,opt: nil,**options)
|
424
413
|
opt = Hash(opt)
|
425
|
-
|
414
|
+
|
426
415
|
File.open(filename,mode,perm,**opt) do |file|
|
427
416
|
file.write(dump_stream(*objects,**options))
|
428
417
|
end
|
429
418
|
end
|
430
|
-
|
419
|
+
|
431
420
|
# Convert +objects+ to YAML and dump to +io+.
|
432
|
-
#
|
421
|
+
#
|
433
422
|
# +io+ and +options+ are used like in Psych.dump so can be a drop-in replacement for Psych.
|
434
|
-
#
|
423
|
+
#
|
435
424
|
# @param objects [Object,Array<Object>] the Object(s) to convert to YAML and dump
|
436
425
|
# @param io [nil,IO,Hash] the IO to dump the YAML to or the +options+ Hash; if nil, will use StringIO
|
437
426
|
# @param stylers [nil,Styler,Array<Styler>] the Styler(s) to use when converting to YAML
|
@@ -449,47 +438,48 @@ module Psychgus
|
|
449
438
|
# Write "canonical" YAML form (very verbose, yet strictly formal).
|
450
439
|
# [+:header+] Default: +false+.
|
451
440
|
# Write +%YAML [version]+ at the beginning of document.
|
452
|
-
#
|
441
|
+
#
|
453
442
|
# @return [String,Object] the result of converting +object+ to YAML using the params
|
454
|
-
#
|
443
|
+
#
|
455
444
|
# @see Psych.dump_stream
|
456
445
|
# @see OPTIONS_ALIASES
|
457
446
|
def self.dump_stream(*objects,io: nil,stylers: nil,deref_aliases: false,**options)
|
458
447
|
# If you call this method with only a Hash that uses symbols as keys,
|
459
448
|
# then options will be set to the Hash, instead of objects.
|
460
|
-
#
|
449
|
+
#
|
461
450
|
# For example, the below will be stored in options, not objects:
|
462
451
|
# - dump_stream({:coffee => {:roast => [],:style => []}})
|
463
|
-
#
|
452
|
+
#
|
464
453
|
# This if-statement is guaranteed because dump_stream([]) and dump_stream(nil)
|
465
454
|
# will produce [[]] and [nil], which are not empty.
|
466
|
-
#
|
455
|
+
#
|
467
456
|
# dump_stream() w/o any args is the only problem, but resolved w/ [nil].
|
468
|
-
if objects.empty?
|
469
|
-
objects = options.empty?
|
457
|
+
if objects.empty?
|
458
|
+
objects = options.empty? ? [nil] : [options]
|
470
459
|
options = {}
|
471
460
|
end
|
472
|
-
|
473
|
-
if Hash
|
461
|
+
|
462
|
+
if io.is_a?(Hash)
|
474
463
|
options = io
|
475
464
|
io = nil
|
476
465
|
end
|
477
|
-
|
478
|
-
if !options.empty?
|
466
|
+
|
467
|
+
if !options.empty?
|
479
468
|
OPTIONS_ALIASES.each do |option_alias,actual_option|
|
480
469
|
if options.key?(option_alias) && !options.key?(actual_option)
|
481
470
|
options[actual_option] = options[option_alias]
|
482
471
|
end
|
483
472
|
end
|
484
473
|
end
|
485
|
-
|
474
|
+
|
486
475
|
visitor = Psych::Visitors::YAMLTree.create(options,StyledTreeBuilder.new(*stylers,
|
487
476
|
deref_aliases: deref_aliases))
|
488
|
-
|
489
|
-
if objects.empty?
|
477
|
+
|
478
|
+
if objects.empty?
|
490
479
|
# Else, will throw a cryptic NoMethodError:
|
491
|
-
# - "psych/tree_builder.rb:in `set_end_location':
|
492
|
-
#
|
480
|
+
# - "psych/tree_builder.rb:in `set_end_location':
|
481
|
+
# undefined method `end_line=' for nil:NilClass (NoMethodError)"
|
482
|
+
#
|
493
483
|
# This should never occur because of the if-statement at the top of this method.
|
494
484
|
visitor << nil
|
495
485
|
else
|
@@ -497,17 +487,17 @@ module Psychgus
|
|
497
487
|
visitor << object
|
498
488
|
end
|
499
489
|
end
|
500
|
-
|
490
|
+
|
501
491
|
return visitor.tree.yaml(io,options)
|
502
492
|
end
|
503
|
-
|
493
|
+
|
504
494
|
# Get a visual hierarchy of the levels as a String.
|
505
|
-
#
|
495
|
+
#
|
506
496
|
# This is useful for determining the correct level/position when writing a {Styler}.
|
507
|
-
#
|
497
|
+
#
|
508
498
|
# @example
|
509
499
|
# require 'psychgus'
|
510
|
-
#
|
500
|
+
#
|
511
501
|
# burgers = {
|
512
502
|
# :burgers => {
|
513
503
|
# :classic => {:sauce => %w(Ketchup Mustard),
|
@@ -526,9 +516,9 @@ module Psychgus
|
|
526
516
|
# [%w(Ketchup Mustard), %w(Salt Pepper)]
|
527
517
|
# ]
|
528
518
|
# }
|
529
|
-
#
|
519
|
+
#
|
530
520
|
# puts Psychgus.hierarchy(burgers)
|
531
|
-
#
|
521
|
+
#
|
532
522
|
# # Output:
|
533
523
|
# # ---
|
534
524
|
# # (level:position):current_node - <parent:(parent_level:parent_position)>
|
@@ -579,34 +569,34 @@ module Psychgus
|
|
579
569
|
# # (5:2):Psych::Nodes::Sequence - <seq:(4:3)>
|
580
570
|
# # (6:1):Salt - <seq:(5:2)>
|
581
571
|
# # (6:2):Pepper - <seq:(5:2)>
|
582
|
-
#
|
572
|
+
#
|
583
573
|
# @param objects [Object,Array<Object>] the Object(s) to get a visual hierarchy of
|
584
574
|
# @param kargs [Hash] the keyword args to pass to {Stylers::HierarchyStyler} and to {dump_stream}
|
585
|
-
#
|
575
|
+
#
|
586
576
|
# @return [String] the visual hierarchy of levels
|
587
|
-
#
|
577
|
+
#
|
588
578
|
# @see Stylers::HierarchyStyler
|
589
579
|
# @see dump_stream
|
590
|
-
#
|
580
|
+
#
|
591
581
|
# @since 1.2.0
|
592
582
|
def self.hierarchy(*objects,**kargs)
|
593
583
|
styler = Stylers::HierarchyStyler.new(**kargs)
|
594
|
-
|
584
|
+
|
595
585
|
dump_stream(*objects,stylers: styler,**kargs)
|
596
|
-
|
597
|
-
return styler.to_s
|
586
|
+
|
587
|
+
return styler.to_s
|
598
588
|
end
|
599
|
-
|
589
|
+
|
600
590
|
# Parse +yaml+ into a Psych::Nodes::Document.
|
601
|
-
#
|
591
|
+
#
|
602
592
|
# If you're just going to call to_ruby(), then using this method is unnecessary,
|
603
593
|
# and the styler(s) will do nothing for you.
|
604
|
-
#
|
594
|
+
#
|
605
595
|
# @param yaml [String] the YAML to parse
|
606
596
|
# @param kargs [Hash] the keyword args to use; see {.parse_stream}
|
607
|
-
#
|
597
|
+
#
|
608
598
|
# @return [Psych::Nodes::Document] the parsed Document node
|
609
|
-
#
|
599
|
+
#
|
610
600
|
# @see .parse_stream
|
611
601
|
# @see Psych.parse
|
612
602
|
# @see Psych::Nodes::Document
|
@@ -614,23 +604,23 @@ module Psychgus
|
|
614
604
|
parse_stream(yaml,**kargs) do |node|
|
615
605
|
return node
|
616
606
|
end
|
617
|
-
|
607
|
+
|
618
608
|
return false
|
619
609
|
end
|
620
|
-
|
610
|
+
|
621
611
|
# Parse a YAML file into a Psych::Nodes::Document.
|
622
|
-
#
|
612
|
+
#
|
623
613
|
# If you're just going to call to_ruby(), then using this method is unnecessary,
|
624
614
|
# and the styler(s) will do nothing for you.
|
625
|
-
#
|
615
|
+
#
|
626
616
|
# @param filename [String] the name of the YAML file (and path) to parse
|
627
617
|
# @param fallback [Object] the return value when nothing is parsed
|
628
618
|
# @param mode [String,Integer] the IO open mode to use; example: +'r:BOM|UTF-8'+
|
629
619
|
# @param opt [Hash] Hash of keyword args to pass to +File.open()+
|
630
620
|
# @param kargs [Hash] the keyword args to use; see {.parse_stream}
|
631
|
-
#
|
621
|
+
#
|
632
622
|
# @return [Psych::Nodes::Document] the parsed Document node
|
633
|
-
#
|
623
|
+
#
|
634
624
|
# @see .parse_stream
|
635
625
|
# @see Psych.parse_file
|
636
626
|
# @see Psych::Nodes::Document
|
@@ -638,19 +628,19 @@ module Psychgus
|
|
638
628
|
# @see IO.new
|
639
629
|
def self.parse_file(filename,fallback: false,mode: 'r:BOM|UTF-8',opt: nil,**kargs)
|
640
630
|
opt = Hash(opt)
|
641
|
-
|
631
|
+
|
642
632
|
result = File.open(filename,mode,**opt) do |file|
|
643
633
|
parse(file,filename: filename,**kargs)
|
644
634
|
end
|
645
|
-
|
635
|
+
|
646
636
|
return result || fallback
|
647
637
|
end
|
648
|
-
|
638
|
+
|
649
639
|
# Parse +yaml+ into a Psych::Nodes::Stream for one document or for multiple documents in one YAML.
|
650
|
-
#
|
640
|
+
#
|
651
641
|
# If you're just going to call to_ruby(), then using this method is unnecessary,
|
652
642
|
# and the styler(s) will do nothing for you.
|
653
|
-
#
|
643
|
+
#
|
654
644
|
# @example
|
655
645
|
# burgers = <<EOY
|
656
646
|
# ---
|
@@ -664,9 +654,9 @@ module Psychgus
|
|
664
654
|
# ---
|
665
655
|
# `Invalid`
|
666
656
|
# EOY
|
667
|
-
#
|
657
|
+
#
|
668
658
|
# i = 0
|
669
|
-
#
|
659
|
+
#
|
670
660
|
# begin
|
671
661
|
# Psychgus.parse_stream(burgers,filename: 'burgers.yaml') do |document|
|
672
662
|
# puts "Document ##{i += 1}"
|
@@ -675,52 +665,52 @@ module Psychgus
|
|
675
665
|
# rescue Psych::SyntaxError => err
|
676
666
|
# puts "File: #{err.file}"
|
677
667
|
# end
|
678
|
-
#
|
668
|
+
#
|
679
669
|
# # Output:
|
680
670
|
# # Document #1
|
681
671
|
# # {"Burgers"=>{"Classic"=>{"BBQ"=>{"Sauce"=>"Honey BBQ", "Cheese"=>"Cheddar", "Bun"=>"Kaiser"}}}}
|
682
672
|
# # Document #2
|
683
673
|
# # {"Toppings"=>[["Mushrooms", "Mustard"], ["Salt", "Pepper", "Pickles"]]}
|
684
674
|
# # File: burgers.yaml
|
685
|
-
#
|
675
|
+
#
|
686
676
|
# @param yaml [String] the YAML to parse
|
687
677
|
# @param filename [String] the filename to pass as +file+ to the Error potentially raised
|
688
678
|
# @param stylers [nil,Styler,Array<Styler>] the Styler(s) to use when parsing the YAML
|
689
679
|
# @param deref_aliases [true,false] whether to dereference aliases; output the actual value
|
690
680
|
# instead of the alias
|
691
681
|
# @param block [Proc] an optional block for parsing multiple documents
|
692
|
-
#
|
682
|
+
#
|
693
683
|
# @return [Psych::Nodes::Stream] the parsed Stream node
|
694
|
-
#
|
684
|
+
#
|
695
685
|
# @see StyledDocumentStream
|
696
686
|
# @see Psych.parse_stream
|
697
687
|
# @see Psych::Nodes::Stream
|
698
688
|
# @see Psych::SyntaxError
|
699
689
|
def self.parse_stream(yaml,filename: nil,stylers: nil,deref_aliases: false,**options,&block)
|
700
|
-
if block_given?
|
690
|
+
if block_given?
|
701
691
|
parser = Psych::Parser.new(StyledDocumentStream.new(*stylers,deref_aliases: deref_aliases,**options,
|
702
692
|
&block))
|
703
|
-
|
693
|
+
|
704
694
|
return parser.parse(yaml,filename)
|
705
695
|
else
|
706
696
|
parser = self.parser(stylers: stylers,deref_aliases: deref_aliases,**options)
|
707
697
|
parser.parse(yaml,filename)
|
708
|
-
|
698
|
+
|
709
699
|
return parser.handler.root
|
710
700
|
end
|
711
701
|
end
|
712
|
-
|
702
|
+
|
713
703
|
# Create a new styled Psych::Parser for parsing YAML.
|
714
|
-
#
|
704
|
+
#
|
715
705
|
# @example
|
716
706
|
# class CoffeeStyler
|
717
707
|
# include Psychgus::Styler
|
718
|
-
#
|
708
|
+
#
|
719
709
|
# def style_sequence(sniffer,node)
|
720
710
|
# node.style = Psychgus::SEQUENCE_FLOW
|
721
711
|
# end
|
722
712
|
# end
|
723
|
-
#
|
713
|
+
#
|
724
714
|
# coffee = <<EOY
|
725
715
|
# Coffee:
|
726
716
|
# Roast:
|
@@ -732,43 +722,43 @@ module Psychgus
|
|
732
722
|
# - Latte
|
733
723
|
# - Mocha
|
734
724
|
# EOY
|
735
|
-
#
|
725
|
+
#
|
736
726
|
# parser = Psychgus.parser(stylers: CoffeeStyler.new)
|
737
727
|
# parser.parse(coffee)
|
738
728
|
# puts parser.handler.root.to_yaml
|
739
|
-
#
|
729
|
+
#
|
740
730
|
# # Output:
|
741
731
|
# # Coffee:
|
742
732
|
# # Roast: [Light, Medium, Dark]
|
743
733
|
# # Style: [Cappuccino, Latte, Mocha]
|
744
|
-
#
|
734
|
+
#
|
745
735
|
# @param stylers [nil,Styler,Array<Styler>] the Styler(s) to use when parsing the YAML
|
746
736
|
# @param deref_aliases [true,false] whether to dereference aliases; output the actual value
|
747
737
|
# instead of the alias
|
748
|
-
#
|
738
|
+
#
|
749
739
|
# @return [Psych::Parser] the new styled Parser
|
750
|
-
#
|
740
|
+
#
|
751
741
|
# @see StyledTreeBuilder
|
752
742
|
# @see Psych.parser
|
753
743
|
def self.parser(stylers: nil,deref_aliases: false,**options)
|
754
744
|
return Psych::Parser.new(StyledTreeBuilder.new(*stylers,deref_aliases: deref_aliases,**options))
|
755
745
|
end
|
756
|
-
|
746
|
+
|
757
747
|
###
|
758
748
|
# Unnecessary Methods
|
759
|
-
#
|
749
|
+
#
|
760
750
|
# All of the below methods are not needed, but are defined
|
761
751
|
# so that Psychgus can be a drop-in replacement for Psych.
|
762
|
-
#
|
752
|
+
#
|
763
753
|
# Instead, you should probably use Psych.
|
764
754
|
# This is also the recommended practice in case your version
|
765
755
|
# of Psych defines the method differently.
|
766
|
-
#
|
756
|
+
#
|
767
757
|
# Private methods of Psych are not defined.
|
768
|
-
#
|
758
|
+
#
|
769
759
|
# @note For devs/hacking: because extend is used, do not prefix methods with "self."
|
770
|
-
#
|
771
|
-
# @author Jonathan Bradley Whited
|
760
|
+
#
|
761
|
+
# @author Jonathan Bradley Whited
|
772
762
|
# @since 1.0.0
|
773
763
|
###
|
774
764
|
module PsychDropIn
|
@@ -776,47 +766,47 @@ module Psychgus
|
|
776
766
|
def add_builtin_type(*args,&block)
|
777
767
|
Psych.add_builtin_type(*args,&block)
|
778
768
|
end
|
779
|
-
|
769
|
+
|
780
770
|
# @see Psych.add_domain_type
|
781
771
|
def add_domain_type(*args,&block)
|
782
772
|
Psych.add_domain_type(*args,&block)
|
783
773
|
end
|
784
|
-
|
774
|
+
|
785
775
|
# @see Psych.add_tag
|
786
776
|
def add_tag(*args)
|
787
777
|
Psych.add_tag(*args)
|
788
778
|
end
|
789
|
-
|
779
|
+
|
790
780
|
# @see Psych.load
|
791
781
|
def load(*args,**kargs)
|
792
782
|
Psych.load(*args,**kargs)
|
793
783
|
end
|
794
|
-
|
784
|
+
|
795
785
|
# @see Psych.load_file
|
796
786
|
def load_file(*args,**kargs)
|
797
787
|
Psych.load_file(*args,**kargs)
|
798
788
|
end
|
799
|
-
|
789
|
+
|
800
790
|
# @see Psych.load_stream
|
801
791
|
def load_stream(*args,**kargs)
|
802
792
|
Psych.load_stream(*args,**kargs)
|
803
793
|
end
|
804
|
-
|
794
|
+
|
805
795
|
# @see Psych.remove_type
|
806
796
|
def remove_type(*args)
|
807
797
|
Psych.remove_type(*args)
|
808
798
|
end
|
809
|
-
|
799
|
+
|
810
800
|
# @see Psych.safe_load
|
811
801
|
def safe_load(*args,**kargs)
|
812
802
|
Psych.safe_load(*args,**kargs)
|
813
803
|
end
|
814
|
-
|
804
|
+
|
815
805
|
# @see Psych.to_json
|
816
806
|
def to_json(*args)
|
817
807
|
Psych.to_json(*args)
|
818
808
|
end
|
819
809
|
end
|
820
|
-
|
810
|
+
|
821
811
|
extend PsychDropIn
|
822
812
|
end
|