styled_yaml 0.0.1.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/styled_yaml.rb +168 -0
  3. metadata +43 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ffece4508e8d9f5ef5926fda64adc0be3d7d5ffa5e08d06f953b43eb47c45844
4
+ data.tar.gz: da3e23d55e606572bf34a815e9bb0fef94f449e8e018b03dc0882f53318d403d
5
+ SHA512:
6
+ metadata.gz: 9bf2ce810a50156020d640eaf215801afbcda609d5153f298dac951867f4f5c443060db95102546bfeb4eb85e4229e859c97d6291801cb55489d867332f9b382
7
+ data.tar.gz: d3167d78d38dba974480e60fd5de26bf028d304c84863246698e88151d000ce999342c6ea1d10ce3ac4610f4d0bdc0e06110c8314d2eaacece116e1bc6090379
@@ -0,0 +1,168 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'psych'
4
+ require 'stringio'
5
+
6
+ # A Psych extension to enable choosing output styles for specific objects.
7
+ #
8
+ # Thanks to Tenderlove for help in <http://stackoverflow.com/q/9640277/11687>.
9
+ #
10
+ # Example:
11
+ #
12
+ # data = {
13
+ # response: { body: StyledYAML.literal(json_string), status: 200 },
14
+ # person: StyledYAML.inline({ 'name' => 'Stevie', 'age' => 12 }),
15
+ # array: StyledYAML.inline(%w[ apples bananas oranges ])
16
+ # }
17
+ #
18
+ # StyledYAML.dump(data, $stdout)
19
+ #
20
+
21
+ module StyledYAML
22
+ # http://www.yaml.org/spec/1.2/spec.html#id2795688
23
+ module LiteralScalar
24
+ def yaml_style
25
+ Psych::Nodes::Scalar::LITERAL
26
+ end
27
+ end
28
+
29
+ # https://yaml.org/spec/1.2/spec.html#id2788097
30
+ module SingleQuotedScalar
31
+ def yaml_style
32
+ Psych::Nodes::Scalar::SINGLE_QUOTED
33
+ end
34
+ end
35
+
36
+ # https://yaml.org/spec/1.2/spec.html#id2787109
37
+ module DoubleQuotedScalar
38
+ def yaml_style
39
+ Psych::Nodes::Scalar::DOUBLE_QUOTED
40
+ end
41
+ end
42
+
43
+ # http://www.yaml.org/spec/1.2/spec.html#id2796251
44
+ module FoldedScalar
45
+ def yaml_style
46
+ Psych::Nodes::Scalar::FOLDED
47
+ end
48
+ end
49
+
50
+ # http://www.yaml.org/spec/1.2/spec.html#id2790832
51
+ module FlowMapping
52
+ def yaml_style
53
+ Psych::Nodes::Mapping::FLOW
54
+ end
55
+ end
56
+
57
+ # http://www.yaml.org/spec/1.2/spec.html#id2790320
58
+ module FlowSequence
59
+ def yaml_style
60
+ Psych::Nodes::Sequence::FLOW
61
+ end
62
+ end
63
+
64
+ # Custom tree builder class to recognize scalars tagged with `yaml_style`
65
+ class TreeBuilder < Psych::TreeBuilder
66
+ attr_writer :next_seq_or_map_style
67
+
68
+ def next_seq_or_map_style(default_style)
69
+ style = @next_seq_or_map_style || default_style
70
+ @next_seq_or_map_style = nil
71
+ style
72
+ end
73
+
74
+ def scalar(value, anchor, tag, plain, quoted, style)
75
+ if value.respond_to?(:yaml_style)
76
+ if style_literal_or_folded? value.yaml_style
77
+ plain = false
78
+ quoted = true
79
+ end
80
+ style = value.yaml_style
81
+ end
82
+ super
83
+ end
84
+
85
+ def style_literal_or_folded?(style)
86
+ [Psych::Nodes::Scalar::LITERAL, Psych::Nodes::Scalar::FOLDED].include?(style)
87
+ end
88
+
89
+ %I[sequence mapping].each do |type|
90
+ class_eval <<-RUBY
91
+ def start_#{type}(anchor, tag, implicit, style)
92
+ style = next_seq_or_map_style(style)
93
+ super
94
+ end
95
+ RUBY
96
+ end
97
+ end
98
+
99
+ # Custom tree class to handle Hashes and Arrays tagged with `yaml_style`.
100
+ class YAMLTree < Psych::Visitors::YAMLTree
101
+ %I[Hash Array Psych_Set Psych_Omap].each do |klass|
102
+ class_eval <<-RUBY
103
+ def visit_#{klass}(o)
104
+ if o.respond_to? :yaml_style
105
+ @emitter.next_seq_or_map_style = o.yaml_style
106
+ end
107
+ super
108
+ end
109
+ RUBY
110
+ end
111
+ end
112
+
113
+ # Tag string to be output using literal style.
114
+ def self.literal(str)
115
+ str.extend(LiteralScalar)
116
+ str
117
+ end
118
+
119
+ # Tag string to be output using folded style.
120
+ def self.folded(str)
121
+ str.extend(FoldedScalar)
122
+ str
123
+ end
124
+
125
+ # Tag string to be output using single quoted style.
126
+ def self.single_quoted(str)
127
+ str.extend(SingleQuotedScalar)
128
+ str
129
+ end
130
+
131
+ # Tag string to be output using double quoted style.
132
+ def self.double_quoted(str)
133
+ str.extend(DoubleQuotedScalar)
134
+ str
135
+ end
136
+
137
+ # Tag Hash or Array to be output all on one line.
138
+ def self.inline(obj)
139
+ case obj
140
+ when Hash
141
+ obj.extend(FlowMapping)
142
+ when Array
143
+ obj.extend(FlowSequence)
144
+ else
145
+ warn "#{self}: unrecognized type to inline (#{obj.class.name})"
146
+ end
147
+ obj
148
+ end
149
+
150
+ # A Psych.dump alternative that uses the custom TreeBuilder
151
+ def self.dump(obj, io = nil, options = {})
152
+ real_io = io || StringIO.new(''.encode('utf-8'))
153
+ visitor = YAMLTree.create(options, TreeBuilder.new)
154
+
155
+ visitor << obj
156
+ ast = visitor.tree
157
+
158
+ begin
159
+ ast.yaml(real_io)
160
+ rescue
161
+ # The `yaml` method was introduced in later versions, so fall back to
162
+ # constructing a visitor
163
+ Psych::Visitors::Emitter.new(real_io).accept(ast)
164
+ end
165
+
166
+ io || real_io.string
167
+ end
168
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: styled_yaml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.rc1
5
+ platform: ruby
6
+ authors:
7
+ - James Ramsay
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-01-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: james@jramsay.com.au
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/styled_yaml.rb
20
+ homepage: http://github.com/jamesramsay/styled-yaml
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">"
36
+ - !ruby/object:Gem::Version
37
+ version: 1.3.1
38
+ requirements: []
39
+ rubygems_version: 3.2.4
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: A Psych extension to enable choosing output styles for specific objects.
43
+ test_files: []