|
1 |
| -# Psych::Comments |
| 1 | +# `Psych::Comments` -- brings YAML comment handling |
2 | 2 |
|
3 |
| -Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/psych/comments`. To experiment with that code, run `bin/console` for an interactive prompt. |
4 |
| - |
5 |
| -TODO: Delete this and the text above, and describe your gem |
| 3 | +This gem allows you to manipulate YAML, preserving comment information. |
6 | 4 |
|
7 | 5 | ## Installation
|
8 | 6 |
|
9 |
| -Add this line to your application's Gemfile: |
10 |
| - |
11 | 7 | ```ruby
|
| 8 | +# Gemfile |
12 | 9 | gem 'psych-comments'
|
13 | 10 | ```
|
14 | 11 |
|
15 |
| -And then execute: |
| 12 | +## Usage |
16 | 13 |
|
17 |
| - $ bundle install |
| 14 | +```ruby |
| 15 | +require "psych" |
| 16 | +require "psych/comments" |
18 | 17 |
|
19 |
| -Or install it yourself as: |
| 18 | +ast = Psych::Comments.parse_stream(<<YAML) |
| 19 | +# foo |
| 20 | +- 42 |
| 21 | +# bar |
| 22 | +- 12 |
| 23 | +YAML |
20 | 24 |
|
21 |
| - $ gem install psych-comments |
| 25 | +ast.children[0].root.children.sort_by! do |node| |
| 26 | + node.value.to_i |
| 27 | +end |
22 | 28 |
|
23 |
| -## Usage |
| 29 | +puts Psych::Comments.emit_yaml(ast) |
| 30 | +``` |
24 | 31 |
|
25 |
| -TODO: Write usage instructions here |
| 32 | +## API |
26 | 33 |
|
27 |
| -## Development |
| 34 | +### `Psych::Nodes::Node#leading_comments` -> `Array<String>` |
28 | 35 |
|
29 |
| -After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. |
| 36 | +Returns an array of leading comments. Each comment must start with `#`. |
30 | 37 |
|
31 |
| -To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org). |
| 38 | +Extends [Psych::Nodes::Node](https://docs.ruby-lang.org/en/3.1/Psych/Nodes/Node.html). |
32 | 39 |
|
33 |
| -## Contributing |
| 40 | +### `Psych::Nodes::Node#trailing_comments` -> `Array<String>` |
34 | 41 |
|
35 |
| -Bug reports and pull requests are welcome on GitHub at https://github.com/qnighy/psych-comments. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/qnighy/psych-comments/blob/master/CODE_OF_CONDUCT.md). |
| 42 | +Returns an array of leading comments. Each comment must start with `#`. |
36 | 43 |
|
37 |
| -## License |
| 44 | +Extends [Psych::Nodes::Node](https://docs.ruby-lang.org/en/3.1/Psych/Nodes/Node.html). |
38 | 45 |
|
39 |
| -The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). |
| 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)` |
40 | 53 |
|
41 |
| -## Code of Conduct |
| 54 | +Parse YAML data with comments. Returns [Psych::Nodes::Document](https://docs.ruby-lang.org/en/3.1/Psych/Nodes/Document.html). |
42 | 55 |
|
43 |
| -Everyone interacting in the Psych::Comments project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/qnighy/psych-comments/blob/master/CODE_OF_CONDUCT.md). |
| 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). |
0 commit comments