psychgus 1.0.0
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 +7 -0
- data/Gemfile +25 -0
- data/LICENSE.txt +165 -0
- data/README.md +507 -0
- data/Rakefile +158 -0
- data/lib/psychgus/blueberry.rb +110 -0
- data/lib/psychgus/ext/core_ext.rb +77 -0
- data/lib/psychgus/ext/node_ext.rb +68 -0
- data/lib/psychgus/ext/yaml_tree_ext.rb +101 -0
- data/lib/psychgus/ext.rb +32 -0
- data/lib/psychgus/styled_document_stream.rb +66 -0
- data/lib/psychgus/styled_tree_builder.rb +294 -0
- data/lib/psychgus/styler.rb +127 -0
- data/lib/psychgus/super_sniffer/parent.rb +218 -0
- data/lib/psychgus/super_sniffer.rb +420 -0
- data/lib/psychgus/version.rb +27 -0
- data/lib/psychgus.rb +693 -0
- data/psychgus.gemspec +121 -0
- data/test/blueberry_test.rb +141 -0
- data/test/psychgus_test.rb +142 -0
- data/test/psychgus_tester.rb +90 -0
- data/test/sniffer_test.rb +287 -0
- data/test/styler_test.rb +85 -0
- data/yard/templates/default/layout/html/footer.erb +5 -0
- metadata +182 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d8fc77e8bff6b4c3b09e3b4b895c3f5b8e5b10d008a1fd3fd96b94061e96b403
|
4
|
+
data.tar.gz: a9f79466520817c04a017f06d27fad23b3a0a2043aa21d6efb377cab1b3d9bcf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 43ed14aaab0bac8b8f9e168ec9e085925ec17942ddbcc3356e9782c70bbc2cd4dd3ccf2c7c6f70fab5fbe424c02a710a16cb7956a059f0f0e3a6f83226eb270d
|
7
|
+
data.tar.gz: e1716f1e701afdb87d8086d5c2553b04356478ef6356586b7db25180f19fa4b0c5c38d32800be2d2794f4e72cf58205e6c63a1326c9d4a3a6c864b207f3b48ec
|
data/Gemfile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
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
|
+
|
23
|
+
source 'https://rubygems.org'
|
24
|
+
|
25
|
+
gemspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
data/README.md
ADDED
@@ -0,0 +1,507 @@
|
|
1
|
+
# Psychgus
|
2
|
+
|
3
|
+
Psychgus uses the core standard library [Psych](https://github.com/ruby/psych) for working with [YAML](https://yaml.org) and extends it so that developers can easily style the YAML according to their needs.
|
4
|
+
|
5
|
+
Thank you to the people that worked and continue to work hard on the Psych project.
|
6
|
+
|
7
|
+
The Psychgus name comes from the well-styled character Gus from the TV show Psych.
|
8
|
+
|
9
|
+
## Contents
|
10
|
+
|
11
|
+
- [Setup](#setup)
|
12
|
+
- [Using](#using)
|
13
|
+
- [Simple Example](#simple-example)
|
14
|
+
- [Hash Example](#hash-example)
|
15
|
+
- [Class Example](#class-example)
|
16
|
+
- [Advanced Usage](#advanced-usage)
|
17
|
+
- [Hacking](#hacking)
|
18
|
+
- [Testing](#testing)
|
19
|
+
- [Generating Doc](#generating-doc)
|
20
|
+
- [License](#license)
|
21
|
+
|
22
|
+
## [Setup](#contents)
|
23
|
+
|
24
|
+
Pick your poison...
|
25
|
+
|
26
|
+
- With the RubyGems CLI package manager:
|
27
|
+
- `$ gem install psychgus`
|
28
|
+
- In your *Gemspec* (*<project>.gemspec*):
|
29
|
+
- `spec.add_runtime_dependency 'psychgus','~> x.x.x'`
|
30
|
+
- Or, if you only need Psychgus in development (e.g., tests, rake, documentation):
|
31
|
+
- `spec.add_development_dependency 'psychgus','~> x.x.x'`
|
32
|
+
- In your *Gemfile*:
|
33
|
+
- `gem 'psychgus','~> x.x.x'`
|
34
|
+
- Or, with GitHub:
|
35
|
+
- `gem 'psychgus',:git=>'https://github.com/esotericpig/psychgus.git'`
|
36
|
+
- Manually:
|
37
|
+
- `$ git clone 'https://github.com/esotericpig/psychgus.git'`
|
38
|
+
- `$ cd psychgus`
|
39
|
+
- `$ bundle install`
|
40
|
+
- `$ bundle exec rake install:local`
|
41
|
+
|
42
|
+
## [Using](#contents)
|
43
|
+
|
44
|
+
Documentation (YARDoc) is available on my [GitHub Page](https://esotericpig.github.io/docs/psychgus/yardoc/index.html) and RubyDoc.info ([[/gems]](https://www.rubydoc.info/gems/psychgus) or [[/github]](https://www.rubydoc.info/github/esotericpig/psychgus/master)).
|
45
|
+
|
46
|
+
To begin styling, create a class and mix in (include) `Psychgus::Styler`. Then pass it in as a keyword arg (`stylers: MyStyler.new` or `stylers: [MyStyler1.new,MyStyler2.new]`) into one of the Psychgus methods.
|
47
|
+
|
48
|
+
For classes, you can optionally include `Psychgus::Blueberry` and return the styler(s) for the class by defining the `psychgus_stylers(sniffer)` method.
|
49
|
+
|
50
|
+
### [Simple Example](#contents)
|
51
|
+
|
52
|
+
```Ruby
|
53
|
+
require 'psychgus'
|
54
|
+
|
55
|
+
class CoffeeStyler
|
56
|
+
include Psychgus::Styler
|
57
|
+
|
58
|
+
def style_sequence(sniffer,node)
|
59
|
+
node.style = Psychgus::SEQUENCE_FLOW
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
coffee = {
|
64
|
+
'Roast'=>['Light', 'Medium', 'Dark', 'Extra Dark'],
|
65
|
+
'Style'=>['Cappuccino', 'Espresso', 'Latte', 'Mocha']
|
66
|
+
}
|
67
|
+
|
68
|
+
puts coffee.to_yaml(stylers: CoffeeStyler.new)
|
69
|
+
|
70
|
+
# Output:
|
71
|
+
# ---
|
72
|
+
# Roast: [Light, Medium, Dark, Extra Dark]
|
73
|
+
# Style: [Cappuccino, Espresso, Latte, Mocha]
|
74
|
+
|
75
|
+
class Coffee
|
76
|
+
include Psychgus::Blueberry
|
77
|
+
|
78
|
+
def initialize
|
79
|
+
@roast = ['Light', 'Medium', 'Dark', 'Extra Dark']
|
80
|
+
@style = ['Cappuccino', 'Espresso', 'Latte', 'Mocha']
|
81
|
+
end
|
82
|
+
|
83
|
+
def psychgus_stylers(sniffer)
|
84
|
+
CoffeeStyler.new
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
puts Coffee.new.to_yaml
|
89
|
+
|
90
|
+
# Output:
|
91
|
+
# --- !ruby/object:Coffee
|
92
|
+
# roast: [Light, Medium, Dark, Extra Dark]
|
93
|
+
# style: [Cappuccino, Espresso, Latte, Mocha]
|
94
|
+
```
|
95
|
+
|
96
|
+
### [Hash Example](#contents)
|
97
|
+
|
98
|
+
```Ruby
|
99
|
+
require 'psychgus'
|
100
|
+
|
101
|
+
class BurgerStyler
|
102
|
+
include Psychgus::Styler # Mix in methods needed for styling
|
103
|
+
|
104
|
+
# Style maps (Psych::Nodes::Mapping)
|
105
|
+
# - Hashes (key/value pairs)
|
106
|
+
# - Example: "Burgers: Classic {}"
|
107
|
+
def style_mapping(sniffer,node)
|
108
|
+
node.style = Psychgus::MAPPING_FLOW if sniffer.level >= 4
|
109
|
+
end
|
110
|
+
|
111
|
+
# Style scalars (Psych::Nodes::Scalar)
|
112
|
+
# - Any text (non-alias)
|
113
|
+
def style_scalar(sniffer,node)
|
114
|
+
# Remove colon (change symbols into strings)
|
115
|
+
node.value = node.value.sub(':','')
|
116
|
+
|
117
|
+
# Capitalize each word
|
118
|
+
node.value = node.value.split(' ').map do |v|
|
119
|
+
if v.casecmp('BBQ') == 0
|
120
|
+
v.upcase()
|
121
|
+
else
|
122
|
+
v.capitalize()
|
123
|
+
end
|
124
|
+
end.join(' ')
|
125
|
+
|
126
|
+
# Change lettuce to spinach
|
127
|
+
node.value = 'Spinach' if node.value == 'Lettuce'
|
128
|
+
end
|
129
|
+
|
130
|
+
# Style sequences (Psych::Nodes::Sequence)
|
131
|
+
# - Arrays
|
132
|
+
# - Example: "[Lettuce, Onions, Pickles, Tomatoes]"
|
133
|
+
def style_sequence(sniffer,node)
|
134
|
+
node.style = Psychgus::SEQUENCE_FLOW if sniffer.level >= 4
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
burgers = {
|
139
|
+
:burgers => {
|
140
|
+
:classic => {:sauce => %w(Ketchup Mustard),
|
141
|
+
:cheese => 'American',
|
142
|
+
:bun => 'Sesame Seed'},
|
143
|
+
:bbq => {:sauce => 'Honey BBQ',
|
144
|
+
:cheese => 'Cheddar',
|
145
|
+
:bun => 'Kaiser'},
|
146
|
+
:fancy => {:sauce => 'Spicy Wasabi',
|
147
|
+
:cheese => 'Smoked Gouda',
|
148
|
+
:bun => 'Hawaiian'}
|
149
|
+
},
|
150
|
+
:toppings => [
|
151
|
+
'Mushrooms',
|
152
|
+
%w(Lettuce Onions Pickles Tomatoes),
|
153
|
+
[%w(Ketchup Mustard), %w(Salt Pepper)]
|
154
|
+
]
|
155
|
+
}
|
156
|
+
burgers[:favorite] = burgers[:burgers][:bbq] # Alias
|
157
|
+
|
158
|
+
puts burgers.to_yaml(indent: 3,stylers: BurgerStyler.new)
|
159
|
+
|
160
|
+
# Output:
|
161
|
+
# ---
|
162
|
+
# Burgers:
|
163
|
+
# Classic: {Sauce: [Ketchup, Mustard], Cheese: American, Bun: Sesame Seed}
|
164
|
+
# BBQ: &1 {Sauce: Honey BBQ, Cheese: Cheddar, Bun: Kaiser}
|
165
|
+
# Fancy: {Sauce: Spicy Wasabi, Cheese: Smoked Gouda, Bun: Hawaiian}
|
166
|
+
# Toppings:
|
167
|
+
# - Mushrooms
|
168
|
+
# - [Spinach, Onions, Pickles, Tomatoes]
|
169
|
+
# - [[Ketchup, Mustard], [Salt, Pepper]]
|
170
|
+
# Favorite: *1
|
171
|
+
|
172
|
+
# Or pass in a Hash. Can also dereference aliases.
|
173
|
+
puts burgers.to_yaml({:indent => 3,:stylers => BurgerStyler.new,:deref_aliases => true})
|
174
|
+
|
175
|
+
# Output:
|
176
|
+
# ---
|
177
|
+
# Burgers:
|
178
|
+
# Classic: {Sauce: [Ketchup, Mustard], Cheese: American, Bun: Sesame Seed}
|
179
|
+
# BBQ: {Sauce: Honey BBQ, Cheese: Cheddar, Bun: Kaiser}
|
180
|
+
# Fancy: {Sauce: Spicy Wasabi, Cheese: Smoked Gouda, Bun: Hawaiian}
|
181
|
+
# Toppings:
|
182
|
+
# - Mushrooms
|
183
|
+
# - [Spinach, Onions, Pickles, Tomatoes]
|
184
|
+
# - [[Ketchup, Mustard], [Salt, Pepper]]
|
185
|
+
# Favorite:
|
186
|
+
# Sauce: Honey BBQ
|
187
|
+
# Cheese: Cheddar
|
188
|
+
# Bun: Kaiser
|
189
|
+
```
|
190
|
+
|
191
|
+
### [Class Example](#contents)
|
192
|
+
|
193
|
+
```Ruby
|
194
|
+
require 'psychgus'
|
195
|
+
|
196
|
+
class BurgerStyler
|
197
|
+
include Psychgus::Styler # Mix in methods needed for styling
|
198
|
+
|
199
|
+
def initialize(sniffer)
|
200
|
+
@class_level = sniffer.level
|
201
|
+
@class_position = sniffer.position
|
202
|
+
end
|
203
|
+
|
204
|
+
# Style all nodes (Psych::Nodes::Node)
|
205
|
+
def style(sniffer,node)
|
206
|
+
# Remove "!ruby/object:..." for Burger classes (not Burgers class)
|
207
|
+
node.tag = nil if node.node_of?(:mapping,:scalar,:sequence)
|
208
|
+
|
209
|
+
# This is another way to do the above
|
210
|
+
#node.tag = nil if node.respond_to?(:tag=)
|
211
|
+
end
|
212
|
+
|
213
|
+
# Style maps (Psych::Nodes::Mapping)
|
214
|
+
# - Hashes (key/value pairs)
|
215
|
+
# - Example: "Burgers: Classic {}"
|
216
|
+
def style_mapping(sniffer,node)
|
217
|
+
parent = sniffer.parent
|
218
|
+
|
219
|
+
if !parent.nil?()
|
220
|
+
# BBQ
|
221
|
+
node.style = Psychgus::MAPPING_FLOW if parent.node_of?(:scalar) &&
|
222
|
+
parent.value.casecmp('BBQ') == 0
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
# Style scalars (Psych::Nodes::Scalar)
|
227
|
+
# - Any text (non-alias)
|
228
|
+
def style_scalar(sniffer,node)
|
229
|
+
parent = sniffer.parent
|
230
|
+
|
231
|
+
# Single quote scalars that are not keys to a map
|
232
|
+
node.style = Psychgus::SCALAR_SINGLE_QUOTED if !parent.nil?() &&
|
233
|
+
parent.child_type != :key
|
234
|
+
end
|
235
|
+
|
236
|
+
# Style sequences (Psych::Nodes::Sequence)
|
237
|
+
# - Arrays
|
238
|
+
# - Example: "[Lettuce, Onions, Pickles, Tomatoes]"
|
239
|
+
def style_sequence(sniffer,node)
|
240
|
+
relative_level = (sniffer.level - @class_level) + 1
|
241
|
+
|
242
|
+
# "[Ketchup, Mustard]"
|
243
|
+
node.style = Psychgus::SEQUENCE_FLOW if relative_level == 3
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
class Burger
|
248
|
+
include Psychgus::Blueberry # Mix in methods needed to be stylable
|
249
|
+
|
250
|
+
attr_accessor :bun
|
251
|
+
attr_accessor :cheese
|
252
|
+
attr_accessor :sauce
|
253
|
+
|
254
|
+
def initialize(sauce,cheese,bun)
|
255
|
+
@bun = bun
|
256
|
+
@cheese = cheese
|
257
|
+
@sauce = sauce
|
258
|
+
end
|
259
|
+
|
260
|
+
# Return our styler(s)
|
261
|
+
# - Can be an Array: [MyStyler1.new, MyStyler2.new]
|
262
|
+
def psychgus_stylers(sniffer)
|
263
|
+
return BurgerStyler.new(sniffer)
|
264
|
+
end
|
265
|
+
|
266
|
+
# You can still use Psych's encode_with(), no problem
|
267
|
+
def encode_with(coder)
|
268
|
+
coder['Bun'] = @bun
|
269
|
+
coder['Cheese'] = @cheese
|
270
|
+
coder['Sauce'] = @sauce
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
class Burgers
|
275
|
+
attr_accessor :burgers
|
276
|
+
attr_accessor :toppings
|
277
|
+
attr_accessor :favorite
|
278
|
+
|
279
|
+
def initialize()
|
280
|
+
@burgers = {
|
281
|
+
'Classic' => Burger.new(['Ketchup','Mustard'],'American' ,'Sesame Seed'),
|
282
|
+
'BBQ' => Burger.new('Honey BBQ' ,'Cheddar' ,'Kaiser'),
|
283
|
+
'Fancy' => Burger.new('Spicy Wasabi' ,'Smoked Gouda','Hawaiian')
|
284
|
+
}
|
285
|
+
|
286
|
+
@toppings = [
|
287
|
+
'Mushrooms',
|
288
|
+
%w(Lettuce Onions Pickles Tomatoes),
|
289
|
+
[%w(Ketchup Mustard),%w(Salt Pepper)]
|
290
|
+
]
|
291
|
+
|
292
|
+
@favorite = @burgers['BBQ'] # Alias
|
293
|
+
end
|
294
|
+
|
295
|
+
# You can still use Psych's encode_with(), no problem
|
296
|
+
def encode_with(coder)
|
297
|
+
coder['Burgers'] = @burgers
|
298
|
+
coder['Toppings'] = @toppings
|
299
|
+
coder['Favorite'] = @favorite
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
burgers = Burgers.new
|
304
|
+
|
305
|
+
puts burgers.to_yaml(indent: 3)
|
306
|
+
|
307
|
+
# Output:
|
308
|
+
# --- !ruby/object:Burgers
|
309
|
+
# Burgers:
|
310
|
+
# Classic:
|
311
|
+
# Bun: 'Sesame Seed'
|
312
|
+
# Cheese: 'American'
|
313
|
+
# Sauce: ['Ketchup', 'Mustard']
|
314
|
+
# BBQ: &1 {Bun: 'Kaiser', Cheese: 'Cheddar', Sauce: 'Honey BBQ'}
|
315
|
+
# Fancy:
|
316
|
+
# Bun: 'Hawaiian'
|
317
|
+
# Cheese: 'Smoked Gouda'
|
318
|
+
# Sauce: 'Spicy Wasabi'
|
319
|
+
# Toppings:
|
320
|
+
# - Mushrooms
|
321
|
+
# - - Lettuce
|
322
|
+
# - Onions
|
323
|
+
# - Pickles
|
324
|
+
# - Tomatoes
|
325
|
+
# - - - Ketchup
|
326
|
+
# - Mustard
|
327
|
+
# - - Salt
|
328
|
+
# - Pepper
|
329
|
+
# Favorite: *1
|
330
|
+
|
331
|
+
# Or pass in a Hash. Can also dereference aliases.
|
332
|
+
puts burgers.to_yaml({:indent => 3,:deref_aliases => true})
|
333
|
+
|
334
|
+
# Output:
|
335
|
+
# --- !ruby/object:Burgers
|
336
|
+
# Burgers:
|
337
|
+
# Classic:
|
338
|
+
# Bun: 'Sesame Seed'
|
339
|
+
# Cheese: 'American'
|
340
|
+
# Sauce: ['Ketchup', 'Mustard']
|
341
|
+
# BBQ: {Bun: 'Kaiser', Cheese: 'Cheddar', Sauce: 'Honey BBQ'}
|
342
|
+
# Fancy:
|
343
|
+
# Bun: 'Hawaiian'
|
344
|
+
# Cheese: 'Smoked Gouda'
|
345
|
+
# Sauce: 'Spicy Wasabi'
|
346
|
+
# Toppings:
|
347
|
+
# - Mushrooms
|
348
|
+
# - - Lettuce
|
349
|
+
# - Onions
|
350
|
+
# - Pickles
|
351
|
+
# - Tomatoes
|
352
|
+
# - - - Ketchup
|
353
|
+
# - Mustard
|
354
|
+
# - - Salt
|
355
|
+
# - Pepper
|
356
|
+
# Favorite:
|
357
|
+
# Bun: 'Kaiser'
|
358
|
+
# Cheese: 'Cheddar'
|
359
|
+
# Sauce: 'Honey BBQ'
|
360
|
+
```
|
361
|
+
|
362
|
+
### [Advanced Usage](#contents)
|
363
|
+
|
364
|
+
```Ruby
|
365
|
+
require 'psychgus'
|
366
|
+
|
367
|
+
class MyStyler
|
368
|
+
include Psychgus::Styler
|
369
|
+
|
370
|
+
def style_sequence(sniffer,node)
|
371
|
+
node.style = Psychgus::SEQUENCE_FLOW
|
372
|
+
end
|
373
|
+
end
|
374
|
+
|
375
|
+
coffee = {
|
376
|
+
'Coffee' => {
|
377
|
+
'Roast'=>['Light', 'Medium', 'Dark', 'Extra Dark'],
|
378
|
+
'Style'=>['Cappuccino', 'Espresso', 'Latte', 'Mocha']
|
379
|
+
}
|
380
|
+
}
|
381
|
+
eggs = {
|
382
|
+
'Eggs' => {
|
383
|
+
'Color'=>['Brown', 'White', 'Blue', 'Olive'],
|
384
|
+
'Style'=>['Fried', 'Scrambled', 'Omelette', 'Poached']
|
385
|
+
}
|
386
|
+
}
|
387
|
+
|
388
|
+
filename = 'coffee-and-eggs.yaml'
|
389
|
+
styler = MyStyler.new
|
390
|
+
options = {:indentation=>3, :stylers=>styler, :deref_aliases=>true}
|
391
|
+
|
392
|
+
coffee_yaml = coffee.to_yaml(options)
|
393
|
+
coffee_and_eggs_yaml = Psychgus.dump_stream(coffee,eggs,options)
|
394
|
+
|
395
|
+
|
396
|
+
# High-level emitting
|
397
|
+
puts Psychgus.dump(coffee,options)
|
398
|
+
puts
|
399
|
+
|
400
|
+
Psychgus.dump_file(filename,coffee,eggs,options)
|
401
|
+
puts File.readlines(filename)
|
402
|
+
puts
|
403
|
+
|
404
|
+
puts Psychgus.dump_stream(coffee,eggs,options)
|
405
|
+
puts
|
406
|
+
|
407
|
+
puts coffee.to_yaml(options)
|
408
|
+
puts
|
409
|
+
|
410
|
+
# High-level parsing
|
411
|
+
# - Because to_ruby() will be called, just use Psych:
|
412
|
+
# - load(), load_file(), load_stream(), safe_load()
|
413
|
+
|
414
|
+
# Mid-level emitting
|
415
|
+
stream = Psychgus.parse_stream(coffee_and_eggs_yaml,options)
|
416
|
+
|
417
|
+
puts stream.to_yaml()
|
418
|
+
puts
|
419
|
+
|
420
|
+
# Mid-level parsing
|
421
|
+
puts Psychgus.parse(coffee_yaml,options).to_ruby
|
422
|
+
puts
|
423
|
+
|
424
|
+
puts Psychgus.parse_file(filename,options).to_ruby
|
425
|
+
puts
|
426
|
+
|
427
|
+
i = 0
|
428
|
+
Psychgus.parse_stream(coffee_and_eggs_yaml,options) do |doc|
|
429
|
+
puts "Doc ##{i += 1}:"
|
430
|
+
puts " #{doc.to_ruby}"
|
431
|
+
end
|
432
|
+
puts
|
433
|
+
|
434
|
+
# Low-level emitting
|
435
|
+
tree_builder = Psychgus::StyledTreeBuilder.new(styler,options)
|
436
|
+
visitor = Psych::Visitors::YAMLTree.create(options,tree_builder)
|
437
|
+
|
438
|
+
visitor << coffee
|
439
|
+
visitor << eggs
|
440
|
+
|
441
|
+
puts visitor.tree.to_yaml
|
442
|
+
puts
|
443
|
+
|
444
|
+
# Low-level parsing
|
445
|
+
parser = Psychgus.parser(options)
|
446
|
+
|
447
|
+
parser.parse(coffee_yaml)
|
448
|
+
|
449
|
+
puts parser.handler.root.to_ruby
|
450
|
+
puts
|
451
|
+
```
|
452
|
+
|
453
|
+
## [Hacking](#contents)
|
454
|
+
|
455
|
+
```
|
456
|
+
$ git clone 'https://github.com/esotericpig/psychgus.git'
|
457
|
+
$ bundle install
|
458
|
+
$ bundle exec rake -T
|
459
|
+
```
|
460
|
+
|
461
|
+
### [Testing](#contents)
|
462
|
+
|
463
|
+
Run tests, excluding tests that create temp files:
|
464
|
+
|
465
|
+
`$ bundle exec rake test`
|
466
|
+
|
467
|
+
Run all tests:
|
468
|
+
|
469
|
+
`$ bundle exec rake test_all`
|
470
|
+
|
471
|
+
### [Generating Doc](#contents)
|
472
|
+
|
473
|
+
Generate basic doc:
|
474
|
+
|
475
|
+
`$ bundle exec rake yard`
|
476
|
+
|
477
|
+
Fix GitHub-specific differences:
|
478
|
+
|
479
|
+
`$ bundle exec rake yard_fix`
|
480
|
+
|
481
|
+
Clean doc & run all of the above:
|
482
|
+
|
483
|
+
`$ bundle exec rake yard_fresh`
|
484
|
+
|
485
|
+
Deploy doc to my GitHub Page (not useful for others):
|
486
|
+
|
487
|
+
`$ bundle exec rake ghp_doc`
|
488
|
+
|
489
|
+
## [License](#contents)
|
490
|
+
|
491
|
+
[GNU LGPL v3+](LICENSE.txt)
|
492
|
+
|
493
|
+
> Psychgus (<https://github.com/esotericpig/psychgus>)
|
494
|
+
> Copyright (c) 2017-2019 Jonathan Bradley Whited (@esotericpig)
|
495
|
+
>
|
496
|
+
> Psychgus is free software: you can redistribute it and/or modify
|
497
|
+
> it under the terms of the GNU Lesser General Public License as published by
|
498
|
+
> the Free Software Foundation, either version 3 of the License, or
|
499
|
+
> (at your option) any later version.
|
500
|
+
>
|
501
|
+
> Psychgus is distributed in the hope that it will be useful,
|
502
|
+
> but WITHOUT ANY WARRANTY; without even the implied warranty of
|
503
|
+
> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
504
|
+
> GNU Lesser General Public License for more details.
|
505
|
+
>
|
506
|
+
> You should have received a copy of the GNU Lesser General Public License
|
507
|
+
> along with Psychgus. If not, see <http://www.gnu.org/licenses/>.
|