Skip to content

Brings comment-aware parsing to Psych, Ruby's YAML parser

License

Notifications You must be signed in to change notification settings

wantedly/psych-comments

Repository files navigation

Psych::Comments -- brings YAML comment handling

This gem allows you to manipulate YAML, preserving comment information.

Installation

# Gemfile
gem 'psych-comments'

Usage

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)

API

Psych::Nodes::Node#leading_comments -> Array<String>

Returns an array of leading comments. Each comment must start with #.

Extends Psych::Nodes::Node.

Psych::Nodes::Node#trailing_comments -> Array<String>

Returns an array of leading comments. Each comment must start with #.

Extends Psych::Nodes::Node.

Psych::Comments.parse(yaml, filename: nil)

Parse YAML data with comments. Returns Psych::Nodes::Document.

The interface is equivalent to Psych.parse.

Psych::Comments.parse_file(filename)

Parse YAML data with comments. Returns Psych::Nodes::Document.

The interface is equivalent to Psych.parse_file.

Psych::Comments.parse_stream(yaml, filename: nil, &block)

Parse YAML stream with comments. Returns Psych::Nodes::Stream.

The interface is equivalent to Psych.parse_stream.

Psych::Comments.emit_yaml(node) -> String

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.

License

The gem is available as open source under the terms of the MIT License.

About

Brings comment-aware parsing to Psych, Ruby's YAML parser

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published