nested_string_parser 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e0fd6dba43658f034613dd768a23e82fee6303aa
4
+ data.tar.gz: f3a0577e9190dedf881b3ea4808be5c982185baa
5
+ SHA512:
6
+ metadata.gz: adf100b54e17ca969da8360aa70cbc0072ddd934a5315c17f2dfffeb8481cd5b6f4bcf9d178c47c41f3ab35e660c3d986e0a57ee8c3f193b1ffe85c4dea64899
7
+ data.tar.gz: 46954bed6b8b365c8c10a6b462ae345ac0b89e293e60f8675efc75015072aeb658039f1136245037da1587e7233a3fc94afed96e4c4d7f67f524965a3b741d0a
@@ -0,0 +1,30 @@
1
+ *.a
2
+ *.bundle
3
+ *.gem
4
+ *.o
5
+ *.rbc
6
+ *.so
7
+ .#*
8
+ .bundle
9
+ .config
10
+ .yardoc
11
+ /.yardoc
12
+ /Gemfile.lock
13
+ /_yardoc/
14
+ /coverage/
15
+ /doc/
16
+ /pkg/
17
+ Gemfile.lock
18
+ InstalledFiles
19
+ _yardoc
20
+ coverage
21
+ doc/
22
+ lib/bundler/man
23
+ mkmf.log
24
+ pkg
25
+ rdoc
26
+ spec/reports
27
+ test/tmp
28
+ test/version_tmp
29
+ tmp
30
+ spec/examples.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nested_string_parser.gemspec
4
+ gemspec
@@ -0,0 +1,37 @@
1
+ # NestedStringParser
2
+
3
+ `NestedStringParser` takes a multi-line string, containing indented lines, and returns a `Node` object for each line, such that:
4
+
5
+ * `node.parent` returns the node for the most recent less-indented previous line
6
+ * `node.children` returns the nodes for all following more-indented lines, either up to end-of-string, or up to the next equally-indented or less-indented line
7
+ * `node.value` returns the stripped text of the line
8
+ * `node.indent` returns the length of the blank string for this line in the source string
9
+
10
+ See specs for some examples.
11
+
12
+ `NestedStringParser` is not terribly fussy about indentation ; any string that is indented more than a previous string will be assigned the previous string as parent. Use `format` to normalise indentation if your input is sloppy. Specs provide an example of sloppy indentation.
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'nested_string_parser'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install nested_string_parser
29
+
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it ( https://github.com/[my-github-username]/nested_string_parser/fork )
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,20 @@
1
+ require "nested_string_parser/version"
2
+
3
+ module NestedStringParser
4
+ class Node
5
+ attr_accessor :nesting, :value, :parent, :children
6
+
7
+ def initialize s=nil ; @nesting, @value, @children = (s ? s.gsub(/^( *)[^ ].*/, '\1').length : -1), (s && s.strip), [] ; end
8
+ def add_child? child ; (child.nesting > self.nesting) ? add_child(child) : parent.add_child?(child) ; end
9
+ def add_child child ; @children << child ; child.parent = self ; child ; end
10
+ def nb? str ; str && str.strip != "" ; end
11
+ def accept str=nil, *ss ; nb?(str) ? add_child?(Node.new(str)).accept(*ss) : (ss.size > 0 ? self.accept(*ss) : self) ; end
12
+ def root ; parent ? parent.root : self ; end
13
+ def format indent ; "#{indent}#{value}\n#{children.map { |c| c.format "#{indent} " }.join}" ; end
14
+ end
15
+
16
+ def self.parse str ; NestedStringParser::Node.new.accept(*str.split(/\n/)).root ; end
17
+ module StringExtension ; def parse_nested_string ; NestedStringParser.parse self ; end ; end
18
+
19
+ def self.patch ; ::String.send :include, StringExtension ; end
20
+ end
@@ -0,0 +1,3 @@
1
+ module NestedStringParser
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2017 Conan Dalton conan@conandalton.net
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,23 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'nested_string_parser/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "nested_string_parser"
7
+ spec.version = NestedStringParser::VERSION
8
+ spec.authors = ["Conan Dalton"]
9
+ spec.email = ["conan@conandalton.net"]
10
+ spec.summary = %q{Parse multi-line string with indentation, return a structure where nesting is based on line indentation}
11
+ spec.description = %q{Parse multi-line string with indentation, return a structure where nesting is based on line indentation}
12
+ spec.homepage = "http://github.com/conanite/nested_string_parser"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency 'rspec', '~> 3.1'
23
+ end
@@ -0,0 +1,132 @@
1
+ require 'spec_helper'
2
+ require "nested_string_parser"
3
+
4
+ RSpec::describe NestedStringParser do
5
+ before(:all) { NestedStringParser.patch }
6
+ it "parses nothing at all" do
7
+ node = "".parse_nested_string
8
+ expect(node.value ).to eq nil
9
+ expect(node.nesting ).to eq(-1)
10
+ expect(node.children).to eq []
11
+ end
12
+
13
+ it "parses a single line" do
14
+ node = "foo bar hello world".parse_nested_string
15
+ expect(node.value ).to eq nil
16
+ expect(node.nesting ).to eq(-1)
17
+ expect(node.children.size).to eq 1
18
+
19
+ child = node.children.first
20
+ expect(child.value ).to eq "foo bar hello world"
21
+ expect(child.nesting ).to eq 0
22
+ expect(child.children).to eq []
23
+ end
24
+
25
+ it "parses two root lines" do
26
+ node = "foo bar\nhello world".parse_nested_string
27
+ expect(node.value ).to eq nil
28
+ expect(node.nesting ).to eq(-1)
29
+ expect(node.children.size).to eq 2
30
+
31
+ child = node.children[0]
32
+ expect(child.value ).to eq "foo bar"
33
+ expect(child.nesting ).to eq 0
34
+ expect(child.children).to eq []
35
+
36
+ child = node.children[1]
37
+ expect(child.value ).to eq "hello world"
38
+ expect(child.nesting ).to eq 0
39
+ expect(child.children).to eq []
40
+ end
41
+
42
+ CANONICAL_QUARKS = <<STRING
43
+ quarks
44
+ G1
45
+ up
46
+ down
47
+ G2
48
+ strange
49
+ charmed
50
+ G3
51
+ top
52
+ bottom
53
+ leptons
54
+ G1
55
+ electron
56
+ electron neutrino
57
+ G2
58
+ muon
59
+ muon neutrino
60
+ G3
61
+ tau
62
+ tau neutrino
63
+ STRING
64
+
65
+ it "parses a whole hierarchy" do
66
+ node = CANONICAL_QUARKS.parse_nested_string
67
+ expect(node.value ).to eq nil
68
+ expect(node.nesting ).to eq(-1)
69
+ expect(node.children.map(&:value)).to eq %w{ quarks leptons }
70
+
71
+ quarks = node.children[0]
72
+ expect(quarks.nesting ).to eq 0
73
+ expect(quarks.children.map(&:value) ).to eq %w{ G1 G2 G3 }
74
+ expect(quarks.children.map(&:nesting)).to eq [2,2,2]
75
+ expect(quarks.children.map { |q| q.children.map(&:nesting) }).to eq [[4,4], [4,4], [4,4]]
76
+ expect(quarks.children.map { |q| q.children.map(&:value) }).to eq [%w{up down}, %w{strange charmed}, %w{top bottom}]
77
+
78
+ leptons = node.children[1]
79
+ expect(leptons.nesting ).to eq 0
80
+ expect(leptons.children.map(&:value) ).to eq %w{ G1 G2 G3 }
81
+ expect(leptons.children.map(&:nesting)).to eq [2,2,2]
82
+ expect(leptons.children.map { |q| q.children.map(&:nesting) }).to eq [[4,4], [4,4], [4,4]]
83
+ expect(leptons.children.map { |q| q.children.map(&:value) }).to eq [%w{electron electron\ neutrino}, %w{muon muon\ neutrino}, %w{tau tau\ neutrino}]
84
+
85
+ expect(node.children.map { |c| c.format "" }.join.strip).to eq CANONICAL_QUARKS.strip
86
+ end
87
+
88
+ it "parses a sloppy indentation and ignores blank lines" do
89
+ string = <<STRING
90
+ quarks
91
+ G1
92
+ up
93
+ down
94
+
95
+ G2
96
+ strange
97
+ charmed
98
+ G3
99
+ top
100
+ bottom
101
+ leptons
102
+ G1
103
+ electron
104
+
105
+ electron neutrino
106
+ G2
107
+ muon
108
+ muon neutrino
109
+ G3
110
+ tau
111
+
112
+ tau neutrino
113
+
114
+
115
+ STRING
116
+ node = string.parse_nested_string
117
+ expect(node.value ).to eq nil
118
+ expect(node.nesting ).to eq(-1)
119
+ expect(node.children.map(&:value)).to eq %w{ quarks leptons }
120
+
121
+ quarks = node.children[0]
122
+ expect(quarks.children.map(&:value) ).to eq %w{ G1 G2 G3 }
123
+ expect(quarks.children.map { |q| q.children.map(&:value) }).to eq [%w{up down}, %w{strange charmed}, %w{top bottom}]
124
+
125
+ leptons = node.children[1]
126
+ expect(leptons.children.map(&:value) ).to eq %w{ G1 G2 G3 }
127
+ expect(leptons.children.map { |q| q.children.map(&:value) }).to eq [%w{electron electron\ neutrino}, %w{muon muon\ neutrino}, %w{tau tau\ neutrino}]
128
+
129
+ # #format, however, ignores original indentation
130
+ expect(node.children.map { |c| c.format "" }.join.strip).to eq CANONICAL_QUARKS.strip
131
+ end
132
+ end
@@ -0,0 +1,96 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
16
+ RSpec.configure do |config|
17
+ # rspec-expectations config goes here. You can use an alternate
18
+ # assertion/expectation library such as wrong or the stdlib/minitest
19
+ # assertions if you prefer.
20
+ config.expect_with :rspec do |expectations|
21
+ # This option will default to `true` in RSpec 4. It makes the `description`
22
+ # and `failure_message` of custom matchers include text for helper methods
23
+ # defined using `chain`, e.g.:
24
+ # be_bigger_than(2).and_smaller_than(4).description
25
+ # # => "be bigger than 2 and smaller than 4"
26
+ # ...rather than:
27
+ # # => "be bigger than 2"
28
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
29
+ end
30
+
31
+ # rspec-mocks config goes here. You can use an alternate test double
32
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
33
+ config.mock_with :rspec do |mocks|
34
+ # Prevents you from mocking or stubbing a method that does not exist on
35
+ # a real object. This is generally recommended, and will default to
36
+ # `true` in RSpec 4.
37
+ mocks.verify_partial_doubles = true
38
+ end
39
+
40
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
41
+ # have no way to turn it off -- the option exists only for backwards
42
+ # compatibility in RSpec 3). It causes shared context metadata to be
43
+ # inherited by the metadata hash of host groups and examples, rather than
44
+ # triggering implicit auto-inclusion in groups with matching metadata.
45
+ config.shared_context_metadata_behavior = :apply_to_host_groups
46
+
47
+ # This allows you to limit a spec run to individual examples or groups
48
+ # you care about by tagging them with `:focus` metadata. When nothing
49
+ # is tagged with `:focus`, all examples get run. RSpec also provides
50
+ # aliases for `it`, `describe`, and `context` that include `:focus`
51
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
52
+ config.filter_run_when_matching :focus
53
+
54
+ # Allows RSpec to persist some state between runs in order to support
55
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
56
+ # you configure your source control system to ignore this file.
57
+ config.example_status_persistence_file_path = "spec/examples.txt"
58
+
59
+ # Limits the available syntax to the non-monkey patched syntax that is
60
+ # recommended. For more details, see:
61
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
62
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
63
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
64
+ config.disable_monkey_patching!
65
+
66
+ # This setting enables warnings. It's recommended, but in some cases may
67
+ # be too noisy due to issues in dependencies.
68
+ config.warnings = true
69
+
70
+ # Many RSpec users commonly either run the entire suite or an individual
71
+ # file, and it's useful to allow more verbose output when running an
72
+ # individual spec file.
73
+ if config.files_to_run.one?
74
+ # Use the documentation formatter for detailed output,
75
+ # unless a formatter has already been configured
76
+ # (e.g. via a command-line flag).
77
+ config.default_formatter = "doc"
78
+ end
79
+
80
+ # Print the 10 slowest examples and example groups at the
81
+ # end of the spec run, to help surface which specs are running
82
+ # particularly slow.
83
+ # config.profile_examples = 10
84
+
85
+ # Run specs in random order to surface order dependencies. If you find an
86
+ # order dependency and want to debug it, you can fix the order by providing
87
+ # the seed, which is printed after each run.
88
+ # --seed 1234
89
+ config.order = :random
90
+
91
+ # Seed global randomization in this process using the `--seed` CLI option.
92
+ # Setting this allows you to use `--seed` to deterministically reproduce
93
+ # test failures related to randomization by passing the same `--seed` value
94
+ # as the one that triggered the failure.
95
+ Kernel.srand config.seed
96
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nested_string_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Conan Dalton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ description: Parse multi-line string with indentation, return a structure where nesting
56
+ is based on line indentation
57
+ email:
58
+ - conan@conandalton.net
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - Gemfile
66
+ - README.md
67
+ - Rakefile
68
+ - lib/nested_string_parser.rb
69
+ - lib/nested_string_parser/version.rb
70
+ - mit-license.txt
71
+ - nested_string_parser.gemspec
72
+ - spec/parse_spec.rb
73
+ - spec/spec_helper.rb
74
+ homepage: http://github.com/conanite/nested_string_parser
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Parse multi-line string with indentation, return a structure where nesting
98
+ is based on line indentation
99
+ test_files:
100
+ - spec/parse_spec.rb
101
+ - spec/spec_helper.rb