soml 0.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/soml/document.rb +51 -0
- data/lib/soml/field.rb +37 -0
- data/lib/soml/util.rb +13 -0
- data/lib/soml/version.rb +3 -0
- data/lib/soml.rb +10 -0
- metadata +24 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea91ccdedb5e6156c46c8e224ce17af719aee7408ea734b3188716cae24f2303
|
4
|
+
data.tar.gz: 97b065662bfb4e92e97158fbd4494feb09decf9258792ee04670b3739a9924df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8785257ed55dc1668e766b0eeaec639e8ca716e41f466409ca329bd5d76924db058ab54a3793af12d71f156a033fe314d2c9bbd74027d149fc3acd458e92c53f
|
7
|
+
data.tar.gz: ab7e0a7ad593fe0861ec4ed61e2d6409cb5af5e8485a88741b4e50166cb6d6d4a50a6375fca04da45c0cb55c2a04371c3653fa9e352758258713ef86815cc4f6
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module SOML
|
2
|
+
class Document
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
def initialize(fields = [])
|
6
|
+
@fields = fields
|
7
|
+
end
|
8
|
+
|
9
|
+
def each(&block)
|
10
|
+
@fields.each(&block)
|
11
|
+
end
|
12
|
+
|
13
|
+
def [](i)
|
14
|
+
@fields[i]
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_s
|
18
|
+
@fields.join("\n")
|
19
|
+
end
|
20
|
+
|
21
|
+
def add(name, value)
|
22
|
+
@fields << Field.new(name, value)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.parse(soml)
|
26
|
+
in_field = false
|
27
|
+
state = Struct.new(:key, :value, :fields, :delim).new(nil, [], [], nil)
|
28
|
+
|
29
|
+
soml.lines.map(&:chomp).each do |line|
|
30
|
+
if in_field && line == state.delim
|
31
|
+
state.fields << Field.new(state.key, Util.dedent(state.value.join("\n")))
|
32
|
+
in_field = false
|
33
|
+
elsif in_field
|
34
|
+
state.value << line
|
35
|
+
else
|
36
|
+
next if line.strip[0] == '#'
|
37
|
+
state.key, value = line.strip.split(' ', 2)
|
38
|
+
if value =~ /<<(.*)/
|
39
|
+
in_field = true
|
40
|
+
state.value = []
|
41
|
+
state.delim = $1
|
42
|
+
else
|
43
|
+
state.fields << Field.new(state.key, value)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
Document.new(state.fields)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/soml/field.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
module SOML
|
2
|
+
class Field
|
3
|
+
attr_reader :name, :value
|
4
|
+
|
5
|
+
def initialize(name, value)
|
6
|
+
@name = name
|
7
|
+
@value = value
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_s
|
11
|
+
if is_multi_line?
|
12
|
+
to_multi_line
|
13
|
+
else
|
14
|
+
to_single_line
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def is_multi_line?
|
21
|
+
@value.to_s.include?("\n")
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_single_line
|
25
|
+
'%s %s' % [@name, @value]
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_multi_line
|
29
|
+
template = <<~EOT
|
30
|
+
%s <<EOF
|
31
|
+
%s
|
32
|
+
EOF
|
33
|
+
EOT
|
34
|
+
template.chomp % [@name, Util.indent(@value)]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/soml/util.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module SOML
|
2
|
+
class Util
|
3
|
+
def self.dedent(str)
|
4
|
+
indentation_level = str.lines.map { |line| line[/^\s*/].length }.min
|
5
|
+
return str if indentation_level == 0
|
6
|
+
return str.lines.map { |line| line[indentation_level..-1] }.join
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.indent(str, level: 4)
|
10
|
+
str.lines.map { |line| ' ' * level + line }.join
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/soml/version.rb
ADDED
data/lib/soml.rb
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: soml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- crdx
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -24,12 +24,31 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '12.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.8'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.8'
|
27
41
|
description:
|
28
42
|
email:
|
29
43
|
executables: []
|
30
44
|
extensions: []
|
31
45
|
extra_rdoc_files: []
|
32
|
-
files:
|
46
|
+
files:
|
47
|
+
- lib/soml.rb
|
48
|
+
- lib/soml/document.rb
|
49
|
+
- lib/soml/field.rb
|
50
|
+
- lib/soml/util.rb
|
51
|
+
- lib/soml/version.rb
|
33
52
|
homepage: https://github.com/crdx/soml
|
34
53
|
licenses:
|
35
54
|
- MIT
|
@@ -49,8 +68,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
68
|
- !ruby/object:Gem::Version
|
50
69
|
version: '0'
|
51
70
|
requirements: []
|
52
|
-
rubygems_version: 3.0.
|
71
|
+
rubygems_version: 3.0.6
|
53
72
|
signing_key:
|
54
73
|
specification_version: 4
|
55
|
-
summary:
|
74
|
+
summary: SOML parser
|
56
75
|
test_files: []
|