0.01
No release in over a year
Brings comment-aware YAML parsing to Psych
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies
 Project Readme

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.