nodaire 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: d9c70a78b831024d450ba7e9cc01ab5c9db7f9d3b70ad4dca16bb6634cc43364
4
+ data.tar.gz: 5ff998283f79e831fee47f4f8f4a987a85a53a18ba239c178d56a5d01def7c1c
5
+ SHA512:
6
+ metadata.gz: 3bd4a7c1ca7584c970b1be353f3d82783c808c784d8408543dd6b7de024dce4fa7068e982aaf0dce5faa66f4860c0945ca9362fa473ca1501326143b1da7596c
7
+ data.tar.gz: be8a98f18ec3cbe88bf4b55eef958fb511b8938737c33b77cf81e71265000e5831147788d3796beb0017f1dd4d3859a8daec832e55f6bd574e0cd2525b09eaeb
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Liam Cooke
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Nodaire
2
+
3
+ Ruby parsers for text file formats. Work in progress.
4
+
5
+ ## File formats
6
+
7
+ - [Oscean](https://wiki.xxiivv.com/#oscean) by Devine Lu Linvega
8
+
9
+ - [Indental](https://wiki.xxiivv.com/#indental) (planned)
10
+
11
+ - [__Tablatal__](https://wiki.xxiivv.com/#tablatal)
12
+
13
+ ## Examples
14
+
15
+ ```ruby
16
+ > input = <<~TBTL
17
+ NAME AGE COLOR
18
+ Erica 12 Opal
19
+ Alex 23 Cyan
20
+ Nike 34 Red
21
+ Ruca 45 Grey
22
+ TBTL
23
+
24
+ > Nodaire::Tablatal.parse(input)
25
+ # [
26
+ # { name: 'Erica', age: '12', color: 'Opal' },
27
+ # { name: 'Alex', age: '23', color: 'Cyan' },
28
+ # { name: 'Nike', age: '34', color: 'Red' },
29
+ # { name: 'Ruca', age: '45', color: 'Grey' },
30
+ # ]
31
+
32
+ > Nodaire::Tablatal.parse(input, preserve_keys: true)
33
+ # [
34
+ # { 'NAME' => 'Erica', 'AGE' => '12', 'COLOR' => 'Opal' },
35
+ # { 'NAME' => 'Alex', 'AGE' => '23', 'COLOR' => 'Cyan' },
36
+ # { 'NAME' => 'Nike', 'AGE' => '34', 'COLOR' => 'Red' },
37
+ # { 'NAME' => 'Ruca', 'AGE' => '45', 'COLOR' => 'Grey' },
38
+ # ]
39
+
40
+ > Nodaire::Tablatal.to_csv(input, preserve_keys: true)
41
+ # NAME,AGE,COLOR
42
+ # Erica,12,Opal
43
+ # Alex,23,Cyan
44
+ # Nike,34,Red
45
+ # Ruca,45,Grey
46
+ ```
47
+
48
+ ## Testing
49
+
50
+ ```
51
+ bundle install --with test
52
+ bundle exec rspec
53
+ ```
data/lib/nodaire.rb ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # Nodaire is a collection of text file parsers.
5
+ #
6
+ module Nodaire
7
+ end
8
+
9
+ require_relative 'nodaire/formats'
10
+
11
+ require_relative 'nodaire/errors'
12
+ require_relative 'nodaire/version'
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nodaire
4
+ class ParserError < StandardError; end
5
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'formats/tablatal'
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../parsers/tablatal_parser'
4
+
5
+ ##
6
+ # Interface for the Tablatal file format.
7
+ #
8
+ # Tablatal is (c) Devine Lu Linvega (MIT License).
9
+ #
10
+ class Nodaire::Tablatal
11
+ ##
12
+ # Parse a string in Tablatal format and return an array of hashes.
13
+ #
14
+ def self.parse(string, preserve_keys: false)
15
+ Parser.new(string, preserve_keys: preserve_keys).rows
16
+ end
17
+
18
+ ##
19
+ # Parse a string in Tablatal format and return a string in CSV format.
20
+ #
21
+ def self.to_csv(string, preserve_keys: false)
22
+ Parser.new(string, preserve_keys: preserve_keys).to_csv
23
+ end
24
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'csv'
4
+
5
+ require_relative '../errors'
6
+
7
+ class Nodaire::Tablatal
8
+ class ParserError < Nodaire::ParserError; end
9
+
10
+ ##
11
+ # Parser for the Tablatal file format.
12
+ #
13
+ # Tablatal is (c) Devine Lu Linvega (MIT License).
14
+ #
15
+ class Parser
16
+ attr_reader :rows
17
+
18
+ def initialize(string, preserve_keys: false)
19
+ lines = (string || '').strip.split("\n")
20
+ .reject { |line| line.match(/^\s*(;.*)?$/) }
21
+ return if lines.empty?
22
+
23
+ @keys = make_keys(lines.shift.scan(/(\S+\s*)/).flatten, preserve_keys)
24
+ @rows = lines.map { |line| make_line(line) }.compact
25
+ end
26
+
27
+ def to_csv
28
+ key_symbols = keys
29
+ CSV.generate do |csv|
30
+ csv << key_symbols
31
+ rows.each do |row|
32
+ csv << key_symbols.map { |key| row[key] }
33
+ end
34
+ end
35
+ end
36
+
37
+ def keys
38
+ @keys.map(&:name) if @keys
39
+ end
40
+
41
+ private
42
+
43
+ Key = Struct.new(:name, :range, keyword_init: true)
44
+
45
+ def make_keys(segs, preserve_keys)
46
+ [].tap do |keys|
47
+ segs.each_with_index do |seg, idx|
48
+ key = seg.strip
49
+ key = key.downcase.to_sym unless preserve_keys
50
+ raise ParserError, 'Duplicate keys' if keys.any? { |k| key == k.name }
51
+
52
+ len = seg.size if idx < segs.size - 1
53
+ start = keys.empty? ? 0 : keys.last.range.last
54
+ keys.push Key.new(name: key, range: start...(len && start + len))
55
+ end
56
+ end
57
+ end
58
+
59
+ def make_line(line)
60
+ @keys.map { |key| [key.name, (line[key.range] || '').strip] }.to_h
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nodaire
4
+ module Version
5
+ STRING = '0.1.0'
6
+ DATE = '2019-08-15'
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nodaire
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Liam Cooke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-08-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Nodaire is a collection of text file parsers.
14
+ email: nodaire@liamcooke.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE
20
+ - README.md
21
+ - lib/nodaire.rb
22
+ - lib/nodaire/errors.rb
23
+ - lib/nodaire/formats.rb
24
+ - lib/nodaire/formats/tablatal.rb
25
+ - lib/nodaire/parsers/tablatal_parser.rb
26
+ - lib/nodaire/version.rb
27
+ homepage: https://github.com/ljcooke/nodaire
28
+ licenses:
29
+ - MIT
30
+ metadata:
31
+ bug_tracker_uri: https://github.com/ljcooke/nodaire/issues
32
+ changelog_uri: https://github.com/ljcooke/nodaire/blob/master/CHANGELOG.md
33
+ documentation_uri: https://github.com/ljcooke/nodaire
34
+ homepage_uri: https://github.com/ljcooke/nodaire
35
+ source_code_uri: https://github.com/ljcooke/nodaire
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 3.0.3
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: Text file parsers.
55
+ test_files: []