Skip to content

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

License

Notifications You must be signed in to change notification settings

adrienpoly/psych-comments

This branch is up to date with wantedly/psych-comments:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

f06d5d0 · Aug 25, 2022

History

15 Commits
Aug 24, 2022
Aug 24, 2022
Aug 25, 2022
Aug 25, 2022
Aug 24, 2022
Aug 24, 2022
Aug 24, 2022
Aug 25, 2022
Aug 24, 2022
Aug 24, 2022
Aug 24, 2022
Aug 24, 2022
Aug 24, 2022
Aug 24, 2022

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

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 99.4%
  • Shell 0.6%