psych-comments 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ba5e728c57571dade021e0eddcd6518b9a3aadfb015d084136371ec5fd2b7742
4
+ data.tar.gz: a10c10213b15b1751d3b140a14faae0add32ec1cc4b3c17ffb08da55dd19ef6b
5
+ SHA512:
6
+ metadata.gz: 4f891304d71be4e3e4649a53fb64c2a84a108e41f3d34e7420da2a14ba8660b94e762032dc939f1dba94f0e054ff0538b723d805122ce21e96dfc5d29058a4c7
7
+ data.tar.gz: 2971dd17da1740003f85a6fb9789369677f1ee110b803a71340ad8134c94ea0f09c30606536f38f844de8cc5bf78dcfa71d1fed1d08060ff808dcc8a97715c43
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-08-24
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at ackie.h.gmai@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in psych-comments.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.7"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Masaki Hara
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # `Psych::Comments` -- brings YAML comment handling
2
+
3
+ This gem allows you to manipulate YAML, preserving comment information.
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ # Gemfile
9
+ gem 'psych-comments'
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ```ruby
15
+ require "psych"
16
+ require "psych/comments"
17
+
18
+ ast = Psych::Comments.parse_stream(<<YAML)
19
+ # foo
20
+ - 42
21
+ # bar
22
+ - 12
23
+ YAML
24
+
25
+ ast.children[0].root.children.sort_by! do |node|
26
+ node.value.to_i
27
+ end
28
+
29
+ puts Psych::Comments.emit_yaml(ast)
30
+ ```
31
+
32
+ ## API
33
+
34
+ ### `Psych::Nodes::Node#leading_comments` -> `Array<String>`
35
+
36
+ Returns an array of leading comments. Each comment must start with `#`.
37
+
38
+ Extends [Psych::Nodes::Node](https://docs.ruby-lang.org/en/3.1/Psych/Nodes/Node.html).
39
+
40
+ ### `Psych::Nodes::Node#trailing_comments` -> `Array<String>`
41
+
42
+ Returns an array of leading comments. Each comment must start with `#`.
43
+
44
+ Extends [Psych::Nodes::Node](https://docs.ruby-lang.org/en/3.1/Psych/Nodes/Node.html).
45
+
46
+ ### `Psych::Comments.parse(yaml, filename: nil)`
47
+
48
+ Parse YAML data with comments. Returns [Psych::Nodes::Document](https://docs.ruby-lang.org/en/3.1/Psych/Nodes/Document.html).
49
+
50
+ The interface is equivalent to [Psych.parse](https://docs.ruby-lang.org/en/3.1/Psych.html#method-c-parse).
51
+
52
+ ### `Psych::Comments.parse_file(filename)`
53
+
54
+ Parse YAML data with comments. Returns [Psych::Nodes::Document](https://docs.ruby-lang.org/en/3.1/Psych/Nodes/Document.html).
55
+
56
+ The interface is equivalent to [Psych.parse_file](https://docs.ruby-lang.org/en/3.1/Psych.html#method-c-parse).
57
+
58
+ ### `Psych::Comments.parse_stream(yaml, filename: nil, &block)`
59
+
60
+ Parse YAML stream with comments. Returns [Psych::Nodes::Stream](https://docs.ruby-lang.org/en/3.1/Psych/Nodes/Stream.html).
61
+
62
+ The interface is equivalent to [Psych.parse_stream](https://docs.ruby-lang.org/en/3.1/Psych.html#method-c-parse_stream).
63
+
64
+ ### `Psych::Comments.emit_yaml(node)` -> `String`
65
+
66
+ Serializes the event tree into a string.
67
+
68
+ This method is similar to [`Psych::Nodes::Node#to_yaml`](https://docs.ruby-lang.org/en/3.1/Psych/Nodes/Node.html#method-i-to_yaml),
69
+ except that it takes comments into account.
70
+
71
+ Note that, this is essentially a reimplemention of libyaml's emitter.
72
+ The implementation is incomplete and you may observe an incorrect or inconsistent output
73
+ if you supply an AST containing unusual constructs.
74
+
75
+ ## License
76
+
77
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "psych/comments"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,301 @@
1
+ module Psych
2
+ module Comments
3
+ module NodeUtils
4
+ module_function def stringify_node(node)
5
+ case node
6
+ when Psych::Nodes::Stream
7
+ node.to_yaml
8
+ when Psych::Nodes::Document
9
+ strm = Psych::Nodes::Stream.new
10
+ strm.children << node
11
+ stringify_node(strm)
12
+ else
13
+ doc = Psych::Nodes::Document.new([], [], true)
14
+ doc.children << node
15
+ stringify_node(doc)
16
+ end
17
+ end
18
+
19
+ module_function def stringify_adjust_scalar(node, indent = 0)
20
+ node2 = Psych::Nodes::Scalar.new(node.value, nil, nil, node.plain, node.quoted, node.style)
21
+ if node.tag
22
+ if node.style == Psych::Nodes::Scalar::PLAIN
23
+ node2.plain = true
24
+ else
25
+ node2.quoted = true
26
+ end
27
+ end
28
+
29
+ s = stringify_node(node2).sub(/\n\z/, "")
30
+ if node.style == Psych::Nodes::Scalar::DOUBLE_QUOTED || node.style == Psych::Nodes::Scalar::SINGLE_QUOTED || node.style == Psych::Nodes::Scalar::PLAIN
31
+ s = s.gsub(/\s*\n\s*/, " ")
32
+ else
33
+ s = s.gsub(/\n/, "\n#{INDENT * indent}")
34
+ end
35
+ s.gsub(/\n\s+$/, "\n")
36
+ end
37
+
38
+ module_function def single_line(node)
39
+ case node
40
+ when Psych::Nodes::Scalar, Psych::Nodes::Alias
41
+ node.leading_comments.empty? && node.trailing_comments.empty?
42
+ when Psych::Nodes::Mapping, Psych::Nodes::Sequence
43
+ node.children.empty?
44
+ else
45
+ false
46
+ end
47
+ end
48
+
49
+ module_function def has_bullet(node)
50
+ node.is_a?(Psych::Nodes::Sequence) && !node.children.empty?
51
+ end
52
+
53
+ module_function def has_anchor(node)
54
+ case node
55
+ when Psych::Nodes::Scalar, Psych::Nodes::Mapping, Psych::Nodes::Sequence
56
+ !!node.anchor
57
+ else
58
+ false
59
+ end
60
+ end
61
+ end
62
+ # private_constant :NodeUtils
63
+
64
+ class Emitter
65
+ include NodeUtils
66
+
67
+ INDENT = " "
68
+
69
+ DEFAULT_TAGMAP = {
70
+ '!' => '!',
71
+ '!!' => 'tag:yaml.org,2002:',
72
+ }.freeze
73
+
74
+ attr_reader :out
75
+
76
+ def initialize
77
+ @out = ""
78
+ @state = :init
79
+ @indent = 0
80
+ @flow = false
81
+ @comment_lookahead = []
82
+ @tagmap = DEFAULT_TAGMAP
83
+ end
84
+
85
+ def print(text)
86
+ case @state
87
+ when :word_end
88
+ @out << " "
89
+ when :line_start
90
+ @out << INDENT * @indent
91
+ end
92
+ @state = :in_line
93
+ @out << text
94
+ end
95
+
96
+ def space!
97
+ @state = :word_end
98
+ end
99
+
100
+ def newline!
101
+ return if @state == :init || @state == :line_start || @state == :pseudo_indent
102
+ @out << "\n"
103
+ @state = :line_start
104
+ end
105
+
106
+ def emit(node)
107
+ if node.equal?(@comment_lookahead[0])
108
+ @comment_lookahead.shift
109
+ else
110
+ node.leading_comments.each do |comment|
111
+ print comment
112
+ newline!
113
+ end
114
+ end
115
+ if has_anchor(node)
116
+ print "&#{node.anchor}"
117
+ space!
118
+ end
119
+ if node.tag
120
+ handle, suffix = decompose_tag(node.tag)
121
+ if suffix
122
+ print "#{handle}#{suffix}"
123
+ else
124
+ print "!<#{node.tag}>"
125
+ end
126
+ space!
127
+ end
128
+ case node
129
+ when Psych::Nodes::Scalar, Psych::Nodes::Alias
130
+ if node.is_a?(Psych::Nodes::Alias)
131
+ print "*#{node.anchor}"
132
+ else
133
+ print stringify_adjust_scalar(node, @indent)
134
+ end
135
+ when Psych::Nodes::Mapping
136
+ set_flow(flow?(node)) do
137
+ if @flow
138
+ print "{"
139
+ cont = false
140
+ node.children.each_slice(2) do |(key, value)|
141
+ if cont
142
+ print ","
143
+ space!
144
+ end
145
+ emit(key)
146
+ print ":"
147
+ space!
148
+ emit(value)
149
+ cont = true
150
+ end
151
+ print "}"
152
+ else
153
+ newline!
154
+ node.children.each_slice(2) do |(key, value)|
155
+ emit(key)
156
+ print ":"
157
+ space!
158
+ if single_line(value)
159
+ emit(value)
160
+ elsif has_bullet(value)
161
+ emit(value)
162
+ else
163
+ @indent += 1
164
+ emit(value)
165
+ @indent -= 1
166
+ end
167
+ newline!
168
+ end
169
+ end
170
+ end
171
+ when Psych::Nodes::Sequence
172
+ set_flow(flow?(node)) do
173
+ if @flow
174
+ print "["
175
+ cont = false
176
+ node.children.each do |subnode|
177
+ if cont
178
+ print ","
179
+ space!
180
+ end
181
+ emit(subnode)
182
+ cont = true
183
+ end
184
+ print "]"
185
+ else
186
+ newline!
187
+ node.children.each do |subnode|
188
+ emit_lookahead_comments(subnode) unless @flow
189
+ print "- "
190
+ @state = :pseudo_indent
191
+ indented do
192
+ emit(subnode)
193
+ end
194
+ newline!
195
+ end
196
+ end
197
+ end
198
+ when Psych::Nodes::Document
199
+ node.tag_directives.each do |(handle, prefix)|
200
+ newline!
201
+ print "%TAG #{handle} #{prefix}"
202
+ newline!
203
+ end
204
+ unless node.implicit
205
+ newline!
206
+ print "---"
207
+ newline!
208
+ end
209
+ set_tagmap(node) do
210
+ emit(node.root)
211
+ end
212
+ unless node.implicit_end
213
+ newline!
214
+ print "..."
215
+ newline!
216
+ end
217
+ when Psych::Nodes::Stream
218
+ node.children.each do |subnode|
219
+ emit(subnode)
220
+ end
221
+ else
222
+ raise TypeError, node
223
+ end
224
+ end
225
+
226
+ def emit_lookahead_comments(node)
227
+ node.leading_comments.each do |comment|
228
+ print comment
229
+ newline!
230
+ end
231
+ @comment_lookahead.push(node)
232
+ case node
233
+ when Psych::Nodes::Mapping, Psych::Nodes::Sequence
234
+ emit_lookahead_comments(node.children[0]) unless flow?(node)
235
+ end
236
+ end
237
+
238
+ def indented(&block)
239
+ @indent += 1
240
+ begin
241
+ block.()
242
+ ensure
243
+ @indent -= 1
244
+ end
245
+ end
246
+
247
+ def set_flow(new_flow, &block)
248
+ old_flow, @flow = @flow, new_flow
249
+ begin
250
+ block.()
251
+ ensure
252
+ @flow = old_flow
253
+ end
254
+ end
255
+
256
+ def flow?(node)
257
+ case node
258
+ when Psych::Nodes::Mapping
259
+ @flow || node.style == Psych::Nodes::Mapping::FLOW || node.children.empty?
260
+ when Psych::Nodes::Sequence
261
+ @flow || node.style == Psych::Nodes::Sequence::FLOW || node.children.empty?
262
+ end
263
+ end
264
+
265
+ # @param tag [String]
266
+ def decompose_tag(tag)
267
+ @tagmap.each do |handle, prefix|
268
+ if tag.start_with?(prefix)
269
+ suffix = tag.delete_prefix(prefix)
270
+ if /\A(?:%[0-9a-fA-F]{2}|[-0-9a-z#;\/?:@&=+$_.~*'()])*\z/.match?(suffix)
271
+ return [handle, suffix]
272
+ end
273
+ end
274
+ end
275
+ [nil, nil]
276
+ end
277
+
278
+ # @param node [Psych::Nodes::Document]
279
+ def set_tagmap(node, &block)
280
+ new_tagmap = DEFAULT_TAGMAP.dup
281
+ node.tag_directives.each do |(handle, prefix)|
282
+ new_tagmap[handle] = prefix
283
+ end
284
+ old_tagmap, @tagmap = @tagmap, new_tagmap
285
+ begin
286
+ block.()
287
+ ensure
288
+ @tagmap = old_tagmap
289
+ end
290
+ end
291
+ end
292
+
293
+ private_constant :Emitter
294
+
295
+ def self.emit_yaml(node)
296
+ emitter = Emitter.new
297
+ emitter.emit(node)
298
+ emitter.out
299
+ end
300
+ end
301
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Psych::Nodes::Node
4
+ def leading_comments
5
+ @leading_comments ||= []
6
+ end
7
+
8
+ def trailing_comments
9
+ @trailing_comments ||= []
10
+ end
11
+ end
@@ -0,0 +1,96 @@
1
+ module Psych
2
+ module Comments
3
+ class Analyzer
4
+ def initialize(text)
5
+ @lines = text.lines.to_a
6
+ @last = [0, 0]
7
+ end
8
+
9
+ def sublines(sl, sc, el, ec)
10
+ if el < sl
11
+ ""
12
+ elsif el == sl
13
+ (@lines[sl] || "")[sc...ec]
14
+ else
15
+ (@lines[sl] || "")[sc...] +
16
+ @lines[sl + 1...el].join("") +
17
+ (@lines[el] || "")[...ec]
18
+ end
19
+ end
20
+
21
+ def char_at(l, c)
22
+ (@lines[l] || "")[c]
23
+ end
24
+
25
+ def read_comments(line, column)
26
+ s = sublines(*@last, line, column)
27
+ @last = [line, column]
28
+ s.scan(/#.*?$/)
29
+ end
30
+
31
+ def visit(node)
32
+ case node
33
+ when Psych::Nodes::Scalar, Psych::Nodes::Alias
34
+ node.leading_comments.push(*read_comments(node.start_line, node.start_column))
35
+ @last = [node.end_line, node.end_column]
36
+ when Psych::Nodes::Sequence, Psych::Nodes::Mapping
37
+ has_delim = /[\[{]/.match?(char_at(node.start_line, node.start_column))
38
+ node.leading_comments.push(*read_comments(node.start_line, node.start_column)) if has_delim
39
+ node.children.each do |subnode|
40
+ visit(subnode)
41
+ end
42
+ if has_delim
43
+ target = node.children[-1] || node
44
+ target.trailing_comments.push(*read_comments(node.end_line, node.end_column))
45
+ end
46
+ when Psych::Nodes::Document
47
+ if !node.implicit
48
+ node.leading_comments.push(*read_comments(node.start_line, node.start_column))
49
+ end
50
+ visit(node.root)
51
+ if !node.implicit_end
52
+ node.root.trailing_comments.push(*read_comments(node.end_line, node.end_column))
53
+ end
54
+ when Psych::Nodes::Stream
55
+ node.children.each do |subnode|
56
+ visit(subnode)
57
+ end
58
+ target = node.children[-1] || node
59
+ target.trailing_comments.push(*read_comments(node.end_line, node.end_column))
60
+ else
61
+ raise TypeError
62
+ end
63
+ end
64
+ end
65
+
66
+ private_constant :Analyzer
67
+
68
+ def self.parse(yaml, filename: nil)
69
+ parse_stream(yaml, filename: filename) do |node|
70
+ return node
71
+ end
72
+ false
73
+ end
74
+
75
+ def self.parse_file(filename)
76
+ File.open filename, 'r:bom|utf-8' do |f|
77
+ parse f, filename: filename
78
+ end
79
+ end
80
+
81
+ def self.parse_stream(yaml, filename: nil, &block)
82
+ filename ||= yaml.respond_to?(:path) ? yaml.path : "<unknown>"
83
+ yaml = yaml.read if yaml.respond_to?(:read)
84
+ ast = Psych.parse_stream(yaml, filename: filename)
85
+ Analyzer.new(yaml).visit(ast)
86
+ if block_given?
87
+ ast.children.each do |doc|
88
+ block.(doc)
89
+ end
90
+ nil
91
+ else
92
+ ast
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Psych
4
+ module Comments
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "psych"
4
+ require "psych/comments/version"
5
+ require "psych/comments/node_ext"
6
+ require "psych/comments/parsing"
7
+ require "psych/comments/emitter"
8
+
9
+ module Psych
10
+ module Comments
11
+ class Error < StandardError; end
12
+ # Your code goes here...
13
+ end
14
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/psych/comments/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "psych-comments"
7
+ spec.version = Psych::Comments::VERSION
8
+ spec.authors = ["Masaki Hara"]
9
+ spec.email = ["ackie.h.gmai@gmail.com"]
10
+
11
+ spec.summary = "Comment-aware YAML"
12
+ spec.description = "Brings comment-aware YAML parsing to Psych"
13
+ spec.homepage = "https://github.com/wantedly/psych-comments"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.4.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/wantedly/psych-comments"
19
+ spec.metadata["changelog_uri"] = "https://github.com/wantedly/psych-comments/tree/master/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ # spec.add_dependency "example-gem", "~> 1.0"
34
+
35
+ # For more information and examples about making a new gem, checkout our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: psych-comments
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Masaki Hara
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-08-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Brings comment-aware YAML parsing to Psych
14
+ email:
15
+ - ackie.h.gmai@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - Gemfile
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/setup
30
+ - lib/psych/comments.rb
31
+ - lib/psych/comments/emitter.rb
32
+ - lib/psych/comments/node_ext.rb
33
+ - lib/psych/comments/parsing.rb
34
+ - lib/psych/comments/version.rb
35
+ - psych-comments.gemspec
36
+ homepage: https://github.com/wantedly/psych-comments
37
+ licenses:
38
+ - MIT
39
+ metadata:
40
+ homepage_uri: https://github.com/wantedly/psych-comments
41
+ source_code_uri: https://github.com/wantedly/psych-comments
42
+ changelog_uri: https://github.com/wantedly/psych-comments/tree/master/CHANGELOG.md
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 2.4.0
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubygems_version: 3.3.7
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Comment-aware YAML
62
+ test_files: []