This gem allows you to manipulate YAML, preserving comment information.
# Gemfile
gem 'psych-comments'
require "psych"
require "psych/comments"
ast = Psych::Comments.parse_stream(<<YAML)
# foo
- 42
# bar
- 12
YAML
ast.children[0].root.children.sort_by! do |node|
node.value.to_i
end
puts Psych::Comments.emit_yaml(ast)
Returns an array of leading comments. Each comment must start with #
.
Extends Psych::Nodes::Node.
Returns an array of leading comments. Each comment must start with #
.
Extends Psych::Nodes::Node.
Parse YAML data with comments. Returns Psych::Nodes::Document.
The interface is equivalent to Psych.parse.
Parse YAML data with comments. Returns Psych::Nodes::Document.
The interface is equivalent to Psych.parse_file.
Parse YAML stream with comments. Returns Psych::Nodes::Stream.
The interface is equivalent to Psych.parse_stream.
Serializes the event tree into a string.
This method is similar to Psych::Nodes::Node#to_yaml
,
except that it takes comments into account.
Note that, this is essentially a reimplemention of libyaml's emitter. The implementation is incomplete and you may observe an incorrect or inconsistent output if you supply an AST containing unusual constructs.
The gem is available as open source under the terms of the MIT License.