mini_yaml 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0fa30f1798b3119df42e4be52f6ca36619d032cf191d6eb1e50160f71de8bbff
4
+ data.tar.gz: eef3d0553e5e81cb2ff7f69c59dc88e0b1104062f146afd342b32809693295a7
5
+ SHA512:
6
+ metadata.gz: b76adc1db8c66c704ae91384686ea930fa723a4c85c06adc1eaeed324c9987933755989113e817f24c85096b882e72ab39560e797ad310dd0171034109c66097
7
+ data.tar.gz: 70ad69d46711d73f698e7e0ca347fdeb7ebe9ad913cfac9d4e0a14eba552048a06067902a27a7b95656fab4897c5ec7c461c53f9c0d1a92a16b3bcca0de39128
@@ -0,0 +1,16 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.7.3
14
+ bundler-cache: true
15
+ - name: Run the default task
16
+ run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-05-13
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at sam.saffron@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in mini_yaml.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,77 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mini_yaml (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ coderay (1.1.3)
11
+ ffi (1.15.0)
12
+ formatador (0.2.5)
13
+ guard (2.16.2)
14
+ formatador (>= 0.2.4)
15
+ listen (>= 2.7, < 4.0)
16
+ lumberjack (>= 1.0.12, < 2.0)
17
+ nenv (~> 0.1)
18
+ notiffany (~> 0.0)
19
+ pry (>= 0.9.12)
20
+ shellany (~> 0.0)
21
+ thor (>= 0.18.1)
22
+ guard-compat (1.2.1)
23
+ guard-minitest (2.4.6)
24
+ guard-compat (~> 1.2)
25
+ minitest (>= 3.0)
26
+ listen (3.5.1)
27
+ rb-fsevent (~> 0.10, >= 0.10.3)
28
+ rb-inotify (~> 0.9, >= 0.9.10)
29
+ lumberjack (1.2.8)
30
+ method_source (1.0.0)
31
+ minitest (5.14.4)
32
+ nenv (0.3.0)
33
+ notiffany (0.1.3)
34
+ nenv (~> 0.1)
35
+ shellany (~> 0.0)
36
+ parallel (1.20.1)
37
+ parser (3.0.1.1)
38
+ ast (~> 2.4.1)
39
+ pry (0.14.1)
40
+ coderay (~> 1.1)
41
+ method_source (~> 1.0)
42
+ rainbow (3.0.0)
43
+ rake (13.0.3)
44
+ rb-fsevent (0.11.0)
45
+ rb-inotify (0.10.1)
46
+ ffi (~> 1.0)
47
+ regexp_parser (2.1.1)
48
+ rexml (3.2.5)
49
+ rubocop (1.14.0)
50
+ parallel (~> 1.10)
51
+ parser (>= 3.0.0.0)
52
+ rainbow (>= 2.2.2, < 4.0)
53
+ regexp_parser (>= 1.8, < 3.0)
54
+ rexml
55
+ rubocop-ast (>= 1.5.0, < 2.0)
56
+ ruby-progressbar (~> 1.7)
57
+ unicode-display_width (>= 1.4.0, < 3.0)
58
+ rubocop-ast (1.5.0)
59
+ parser (>= 3.0.1.1)
60
+ ruby-progressbar (1.11.0)
61
+ shellany (0.0.1)
62
+ thor (1.1.0)
63
+ unicode-display_width (2.0.0)
64
+
65
+ PLATFORMS
66
+ x86_64-linux
67
+
68
+ DEPENDENCIES
69
+ guard
70
+ guard-minitest
71
+ mini_yaml!
72
+ minitest
73
+ rake
74
+ rubocop
75
+
76
+ BUNDLED WITH
77
+ 2.2.17
data/Guardfile ADDED
@@ -0,0 +1,23 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ guard :minitest do
19
+ # with Minitest::Unit
20
+ watch(%r{^test/(.*)\/(.*)_test\.rb$})
21
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
22
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
23
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Civilized Discourse Construction Kit
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # MiniYaml
2
+
3
+ MiniYaml is a toolkit for amending and formatting YAML files.
4
+
5
+ You can use it to:
6
+
7
+ - Enforce predictable and opinionated formatting for all YAML files
8
+ - Amend YAML files programatically while maintaining comments
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'mini_yaml'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle install
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install mini_yaml
25
+
26
+ ## Usage
27
+
28
+ TODO: Write usage instructions here
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
33
+
34
+ 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).
35
+
36
+ ## Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/SamSaffron/mini_yaml. 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/SamSaffron/mini_yaml/blob/main/CODE_OF_CONDUCT.md).
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
43
+
44
+ ## Code of Conduct
45
+
46
+ Everyone interacting in the MiniYaml project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/SamSaffron/mini_yaml/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "mini_yaml"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/mini_yaml.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "mini_yaml/version"
4
+ require_relative "mini_yaml/linter"
5
+
6
+ module MiniYaml
7
+ class Error < StandardError; end
8
+ end
@@ -0,0 +1,339 @@
1
+ # frozen_string_literal: true
2
+ require 'yaml'
3
+
4
+ module MiniYaml
5
+ class Linter
6
+ def initialize(yaml, paranoid: true)
7
+ @comment_map = nil
8
+ @parsed = parse(yaml)
9
+
10
+ if paranoid
11
+ before, before_e, after, after_e = nil
12
+
13
+ begin
14
+ before = YAML.load(yaml)
15
+ rescue => e
16
+ before_e = e
17
+ end
18
+
19
+ begin
20
+ after = YAML.load(dump)
21
+ rescue => e
22
+ after_e = e
23
+ end
24
+
25
+ if before != after
26
+ STDERR.puts "ERROR YAML mismatch:"
27
+ STDERR.puts
28
+ STDERR.puts "BEFORE TEXT"
29
+ STDERR.puts yaml
30
+ STDERR.puts "AFTER"
31
+ STDERR.puts dump
32
+
33
+ STDERR.puts "BEFORE OBJ"
34
+ STDERR.puts before.inspect
35
+ if before_e
36
+ STDERR.puts before_e
37
+ end
38
+ STDERR.puts "AFTER OBJ"
39
+ STDERR.puts after.inspect
40
+ if after_e
41
+ STDERR.puts after_e
42
+ end
43
+
44
+ raise "Failed to correctly generate YAML"
45
+ end
46
+ end
47
+ end
48
+
49
+ # malliable array/hash structure
50
+ def contents
51
+ @parsed
52
+ end
53
+
54
+ def dump
55
+ buffer = +"---\n"
56
+
57
+ if leading = @comment_map[:leading]
58
+ buffer << leading
59
+ buffer << "\n"
60
+ end
61
+
62
+ _dump(node: @parsed, buffer: buffer, indent: 0, path: "", prev_indent: 0)
63
+
64
+ if trailing = @comment_map[:trailing]
65
+ buffer << trailing
66
+ end
67
+
68
+ buffer
69
+ end
70
+
71
+ protected
72
+
73
+ def to_multiline(val)
74
+ buf = +""
75
+
76
+ val.split("\n").each do |line|
77
+
78
+ if line.match?(/^\s/)
79
+ buf.rstrip!
80
+ buf << "\n"
81
+ buf << line
82
+ buf << "\n"
83
+ else
84
+ col = 0
85
+
86
+ index = 0
87
+ while char = line[index]
88
+ prev_char = line[index - 1]
89
+ next_char = line[index + 1]
90
+
91
+ if col > 80 && char == " " && prev_char.match?(/\S/) && next_char.match?(/\S/)
92
+ buf << "\n"
93
+ col = 0
94
+ else
95
+ buf << char
96
+ end
97
+
98
+ col += 1
99
+
100
+ index += 1
101
+ end
102
+ buf << "\n" if buf[-1] != "\n"
103
+ buf << "\n"
104
+ end
105
+
106
+ end
107
+
108
+ buf.rstrip!
109
+ buf
110
+ end
111
+
112
+ def format_string(val, comment)
113
+ result = +""
114
+
115
+ quote = false
116
+
117
+ if String === val
118
+ if !val.include?("\n") && !(String === scalar_scanner.tokenize(val))
119
+ quote = true
120
+ elsif val.match?(/[\{\}\(\)\/]/)
121
+ quote = true
122
+ end
123
+ end
124
+
125
+ val = val.to_s
126
+
127
+ if val.include?("\n") || val.length > 80
128
+ lines = val.split("\n")
129
+ force_short = lines.map(&:length).max > 80
130
+ strip_trailing_newline = val[-1] != "\n"
131
+
132
+ if force_short
133
+ result << ">"
134
+ val = to_multiline(val)
135
+ else
136
+ result << "|"
137
+ end
138
+
139
+ if strip_trailing_newline
140
+ result << "-"
141
+ end
142
+
143
+ if comment
144
+ result << " ##{comment.gsub(/$#/,"").gsub("\n", " ")}"
145
+ end
146
+ result << "\n"
147
+ result << val
148
+ else
149
+ if val.match?(/['":]/) || quote
150
+ result << val.inspect
151
+ else
152
+ result << val
153
+ end
154
+ if comment
155
+ result << " "
156
+ first = true
157
+
158
+ comment = comment.split("\n").map do |line|
159
+ r = +""
160
+ if !first
161
+ r += " " * result.length
162
+ else
163
+ first = false
164
+ end
165
+ r << line
166
+ end.join("\n")
167
+
168
+ result << comment
169
+ end
170
+ end
171
+
172
+ if result[-1] != "\n"
173
+ result << "\n"
174
+ end
175
+
176
+ result
177
+ end
178
+
179
+ def _dump(node:, buffer:, indent:, path:, prev_indent:)
180
+ case node
181
+ when Array
182
+ if node == []
183
+ buffer << "[]\n"
184
+ else
185
+ node.each do |child|
186
+ buffer << " " * indent if buffer[-1] == "\n"
187
+ buffer << "- "
188
+ _dump(node: child, buffer: buffer, indent: indent + 2, path: path + "-", prev_indent: indent)
189
+ end
190
+ end
191
+ when Hash
192
+ node.each do |key, value|
193
+ buffer << " " * indent if buffer[-1] == "\n"
194
+ buffer << "#{key}:"
195
+ if value == []
196
+ buffer << " []\n"
197
+ elsif Hash === value || Array === value
198
+ buffer << "\n"
199
+ _dump(node: value, buffer: buffer, indent: indent + 2, path: path + ":#{key}", prev_indent: indent)
200
+ else
201
+ buffer << " "
202
+ _dump(node: value, buffer: buffer, indent: indent + key.length + 2, path: path + ":#{key}", prev_indent: indent)
203
+ end
204
+ end
205
+ else
206
+ comment = @comment_map[path + node.to_s]
207
+
208
+ string = format_string(node, comment)
209
+ formatted = string.split("\n")
210
+ buffer << formatted.shift.to_s
211
+ buffer << "\n"
212
+
213
+ formatted.each do |line|
214
+ if line.length > 0
215
+ buffer << " " * (prev_indent + 2)
216
+ buffer << line
217
+ end
218
+ buffer << "\n"
219
+ end
220
+ end
221
+ end
222
+
223
+ def find_comment(lines, node, prev_node)
224
+ start_col = 0
225
+ start_line = 0
226
+ if prev_node
227
+ start_col = prev_node.end_column
228
+ start_line = prev_node.end_line
229
+ end
230
+
231
+ finish_col = lines[-1].length
232
+ finish_line = lines.count
233
+
234
+ if node
235
+ finish_col = node.start_column
236
+ finish_line = node.start_line
237
+ end
238
+
239
+ current_lines = lines[start_line..finish_line]
240
+
241
+ if current_lines.length == 0
242
+ return ""
243
+ end
244
+
245
+ if current_lines.length == 1
246
+ current_lines[0] = current_lines[0][start_col..finish_col]
247
+ else
248
+ current_lines[0] = current_lines[0][start_col..-1]
249
+ current_lines[-1] = current_lines[-1][0...finish_col]
250
+ end
251
+
252
+ current_lines.map do |line|
253
+ if idx = line.index("#")
254
+ line[idx..-1]
255
+ end
256
+ end.compact.join("\n")
257
+ end
258
+
259
+ def parse(yaml)
260
+ parsed = nil
261
+ state = State.new(yaml)
262
+
263
+ YAML.parse_stream(yaml).each do |node|
264
+ if Psych::Nodes::Document === node
265
+ parsed = to_simple(node, state: state, location: '')
266
+ break
267
+ end
268
+ end
269
+
270
+ @comment_map = state.comment_map
271
+
272
+ parsed
273
+ end
274
+
275
+ def scalar_scanner
276
+ @scalar_scanner ||= Psych::ScalarScanner.new(Psych::ClassLoader.new)
277
+ end
278
+
279
+ class State
280
+ attr_accessor :lines, :comment_map, :nodes, :locations
281
+ def initialize(yaml)
282
+ @lines = yaml.split("\n")
283
+ @comment_map = {}
284
+ @nodes = []
285
+ @locations = []
286
+ end
287
+ end
288
+
289
+ def to_simple(node, state:, location:)
290
+ comment = find_comment(state.lines, state.nodes[-1], state.nodes[-2])
291
+ if !comment.empty? && !state.locations[-2].nil?
292
+ state.comment_map[state.locations[-2]] = comment
293
+ elsif state.locations.length == 1 && !comment.empty?
294
+ state.comment_map[:leading] = comment
295
+ end
296
+
297
+ state.nodes << node
298
+ state.locations << location
299
+
300
+ if Psych::Nodes::Sequence === node
301
+ container = node.children.map do |inner|
302
+ to_simple(inner, location: location + "-", state: state)
303
+ end
304
+ return container
305
+ end
306
+
307
+ if Psych::Nodes::Mapping === node
308
+ container = {}
309
+ node.children.each_slice(2) do |key, value|
310
+ k = to_simple(key, state: state, location: location + ":")
311
+ v = to_simple(value, state: state, location: location + ":" + k)
312
+ container[k] = v
313
+ end
314
+ return container
315
+ end
316
+
317
+ if Psych::Nodes::Scalar === node
318
+ val = node.quoted ? node.value : scalar_scanner.tokenize(node.value)
319
+ state.locations[-1] += val.to_s
320
+ return val
321
+ end
322
+
323
+ if Psych::Nodes::Document === node
324
+ if node.children.count > 1
325
+ raise "document with multiple children is not supported"
326
+ end
327
+ val = to_simple(node.children.first, location: location, state: state)
328
+
329
+ comment = find_comment(state.lines, nil, state.nodes[-1])
330
+ if !comment.empty?
331
+ state.comment_map[:trailing] = comment
332
+ end
333
+ return val
334
+ end
335
+
336
+ raise "Unexpected node type #{node}"
337
+ end
338
+ end
339
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MiniYaml
4
+ VERSION = "0.1.0"
5
+ end
data/mini_yaml.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/mini_yaml/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "mini_yaml"
7
+ spec.version = MiniYaml::VERSION
8
+ spec.authors = ["Sam Saffron"]
9
+ spec.email = ["sam.saffron@gmail.com"]
10
+
11
+ spec.summary = "YAML editor and formatter"
12
+ spec.description = "YAML editor that preserves comments and formats YAML using opinionated rules"
13
+ spec.homepage = "https://discourse.org"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/discourse/mini_yaml"
19
+ spec.metadata["changelog_uri"] = "https://github.com/discourse/mini_yaml/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "rake"
31
+ spec.add_development_dependency "minitest"
32
+ spec.add_development_dependency "guard"
33
+ spec.add_development_dependency "guard-minitest"
34
+ spec.add_development_dependency "rubocop"
35
+
36
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mini_yaml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sam Saffron
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-05-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: YAML editor that preserves comments and formats YAML using opinionated
84
+ rules
85
+ email:
86
+ - sam.saffron@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".github/workflows/main.yml"
92
+ - ".gitignore"
93
+ - ".rubocop.yml"
94
+ - CHANGELOG.md
95
+ - CODE_OF_CONDUCT.md
96
+ - Gemfile
97
+ - Gemfile.lock
98
+ - Guardfile
99
+ - LICENSE.txt
100
+ - README.md
101
+ - Rakefile
102
+ - bin/console
103
+ - bin/setup
104
+ - lib/mini_yaml.rb
105
+ - lib/mini_yaml/linter.rb
106
+ - lib/mini_yaml/version.rb
107
+ - mini_yaml.gemspec
108
+ homepage: https://discourse.org
109
+ licenses:
110
+ - MIT
111
+ metadata:
112
+ homepage_uri: https://discourse.org
113
+ source_code_uri: https://github.com/discourse/mini_yaml
114
+ changelog_uri: https://github.com/discourse/mini_yaml/CHANGELOG.md
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: 2.5.0
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubygems_version: 3.1.6
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: YAML editor and formatter
134
+ test_files: []