ldpath 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ldpath/functions.rb +9 -4
- data/lib/ldpath/parser.rb +24 -18
- data/lib/ldpath/transform.rb +17 -1
- data/lib/ldpath/version.rb +1 -1
- data/spec/ldpath_parser_spec.rb +5 -1
- data/spec/ldpath_transform_spec.rb +35 -6
- data/spec/lib/functions/list_spec.rb +3 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 901eaf079202867fe88880b4e6297361dcd4574a
|
4
|
+
data.tar.gz: 9d80b28a1555f00f2400d84bb4573733cc1158c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d110b7f52841afd8dec645ce426a117692ae0a7c64f90585196b481c80765472ad7d148fc132da7e79fcb25a373783007a3d8a23a252c883606ba293a5c4b8db
|
7
|
+
data.tar.gz: b2fd98589dfd3dceec73c5eac70eba71696647878d54f6acd437707f3427b6cc28361b88b0e94d20325738aac2f08c65e9958f4e03a1afdfefd02a58b1da6a20
|
data/lib/ldpath/functions.rb
CHANGED
@@ -71,16 +71,21 @@ module Ldpath
|
|
71
71
|
end
|
72
72
|
|
73
73
|
def get(uri, context, list, idx)
|
74
|
-
|
74
|
+
idx = idx.respond_to?(:to_i) ? idx.to_i : idx.to_s.to_i
|
75
|
+
|
76
|
+
flatten(uri, context, list)[idx]
|
75
77
|
end
|
76
78
|
|
77
79
|
def subList(uri, context, list, idx_start, idx_end = nil)
|
78
80
|
arr = flatten(uri, context, list)
|
79
81
|
|
82
|
+
idx_start = idx_start.respond_to?(:to_i) ? idx_start.to_i : idx_start.to_s.to_i
|
83
|
+
idx_end &&= idx_end.respond_to?(:to_i) ? idx_end.to_i : idx_end.to_s.to_i
|
84
|
+
|
80
85
|
if idx_end
|
81
|
-
arr[(idx_start.to_i..(idx_end
|
86
|
+
arr[(idx_start.to_i..(idx_end - idx_start))]
|
82
87
|
else
|
83
|
-
arr.drop(idx_start
|
88
|
+
arr.drop(idx_start)
|
84
89
|
end
|
85
90
|
end
|
86
91
|
|
@@ -180,7 +185,7 @@ module Ldpath
|
|
180
185
|
def xpath(uri, context, xpath, node)
|
181
186
|
x = Array(xpath).flatten.first
|
182
187
|
Array(node).flatten.compact.map do |n|
|
183
|
-
Nokogiri::XML(n).xpath(x, prefixes.map { |k, v| [k, v.to_s] }).map(&:text)
|
188
|
+
Nokogiri::XML(n.to_s).xpath(x.to_s, prefixes.map { |k, v| [k, v.to_s] }).map(&:text)
|
184
189
|
end
|
185
190
|
end
|
186
191
|
end
|
data/lib/ldpath/parser.rb
CHANGED
@@ -27,36 +27,36 @@ module Ldpath
|
|
27
27
|
rule(:single_line_comment) { str("#") >> (eol.absent? >> any).repeat }
|
28
28
|
|
29
29
|
# simple types
|
30
|
-
rule(:integer) { match("[+-]").maybe >> match("\\d
|
31
|
-
rule(:decimal) { match("[+-]").maybe >> match("\\d
|
30
|
+
rule(:integer) { match("[+-]").maybe >> match("\\d").repeat(1) }
|
31
|
+
rule(:decimal) { match("[+-]").maybe >> match("\\d").repeat >> str(".") >> match("\\d").repeat(1) }
|
32
32
|
rule(:double) do
|
33
33
|
match("[+-]").maybe >> (
|
34
|
-
(match("\\d
|
35
|
-
(str('.') >> match("\\d
|
36
|
-
(match("\\d
|
34
|
+
(match("\\d").repeat(1) >> str('.') >> match("\\d").repeat >> exponent) |
|
35
|
+
(str('.') >> match("\\d").repeat(1) >> exponent) |
|
36
|
+
(match("\\d").repeat(1) >> exponent)
|
37
37
|
)
|
38
38
|
end
|
39
39
|
|
40
|
-
rule(:exponent) { match('[Ee]') >> match("[+-]").maybe >> match("\\d
|
41
|
-
rule(:numeric_literal) { integer | decimal | double }
|
42
|
-
rule(:boolean_literal) { str('true') | str('false') }
|
40
|
+
rule(:exponent) { match('[Ee]') >> match("[+-]").maybe >> match("\\d").repeat(1) }
|
41
|
+
rule(:numeric_literal) { integer.as(:integer) | decimal.as(:decimal) | double.as(:double) }
|
42
|
+
rule(:boolean_literal) { str('true').as(:true) | str('false').as(:false) }
|
43
43
|
|
44
44
|
rule(:string) { string_literal_quote | string_literal_single_quote | string_literal_long_single_quote | string_literal_long_quote }
|
45
45
|
|
46
46
|
rule(:string_literal_quote) do
|
47
|
-
str('"') >> (match("[^\\\"\\\\\\r\\n]") | echar | uchar).repeat.as(:
|
47
|
+
str('"') >> (match("[^\\\"\\\\\\r\\n]") | echar | uchar).repeat.as(:string) >> str('"')
|
48
48
|
end
|
49
49
|
|
50
50
|
rule(:string_literal_single_quote) do
|
51
|
-
str("'") >> (match("[^'\\\\\\r\\n]") | echar | uchar).repeat.as(:
|
51
|
+
str("'") >> (match("[^'\\\\\\r\\n]") | echar | uchar).repeat.as(:string) >> str("'")
|
52
52
|
end
|
53
53
|
|
54
54
|
rule(:string_literal_long_quote) do
|
55
|
-
str('"""') >> (str('"""').absent? >> match("[^\\\\]") | echar | uchar).repeat.as(:
|
55
|
+
str('"""') >> (str('"""').absent? >> match("[^\\\\]") | echar | uchar).repeat.as(:string) >> str('"""')
|
56
56
|
end
|
57
57
|
|
58
58
|
rule(:string_literal_long_single_quote) do
|
59
|
-
str("'''") >> (str("'''").absent? >> match("[^\\\\]") | echar | uchar).repeat.as(:
|
59
|
+
str("'''") >> (str("'''").absent? >> match("[^\\\\]") | echar | uchar).repeat.as(:string) >> str("'''")
|
60
60
|
end
|
61
61
|
|
62
62
|
# operators
|
@@ -126,16 +126,22 @@ module Ldpath
|
|
126
126
|
|
127
127
|
# "xyz"; 0.123e52; true
|
128
128
|
rule(:literal) do
|
129
|
-
|
129
|
+
(
|
130
|
+
rdf_literal |
|
131
|
+
numeric_literal |
|
132
|
+
boolean_literal
|
133
|
+
).as(:literal)
|
130
134
|
end
|
131
135
|
|
132
136
|
# "xyz"; "xyz"^^a; "xyz"@en
|
133
137
|
rule(:rdf_literal) do
|
134
|
-
string >> (
|
138
|
+
string >> lang >> identifier.as(:lang) |
|
139
|
+
string >> type >> iri.as(:type) |
|
140
|
+
string
|
135
141
|
end
|
136
142
|
|
137
143
|
rule(:node) do
|
138
|
-
iri
|
144
|
+
iri | literal
|
139
145
|
end
|
140
146
|
|
141
147
|
# @prefix id = iri ;
|
@@ -252,7 +258,7 @@ module Ldpath
|
|
252
258
|
loose_property_selector |
|
253
259
|
wildcard_selector |
|
254
260
|
reverse_property_selector |
|
255
|
-
|
261
|
+
literal_selector |
|
256
262
|
recursive_path_selector |
|
257
263
|
grouped_selector |
|
258
264
|
tap_selector
|
@@ -316,8 +322,8 @@ module Ldpath
|
|
316
322
|
inverse.as(:reverse) >> iri.as(:property)
|
317
323
|
end
|
318
324
|
|
319
|
-
rule(:
|
320
|
-
|
325
|
+
rule(:literal_selector) do
|
326
|
+
literal.as(:literal)
|
321
327
|
end
|
322
328
|
|
323
329
|
# (x)?; (x)*; (x)+; (x){3,5}
|
data/lib/ldpath/transform.rb
CHANGED
@@ -29,7 +29,23 @@ module Ldpath
|
|
29
29
|
end
|
30
30
|
|
31
31
|
# Core types
|
32
|
-
rule(
|
32
|
+
rule(true: simple(:true)) { true }
|
33
|
+
rule(false: simple(:false)) { false }
|
34
|
+
rule(integer: simple(:integer)) { integer.to_i }
|
35
|
+
rule(double: simple(:double)) { double.to_f }
|
36
|
+
rule(decimal: simple(:decimal)) { decimal.to_f }
|
37
|
+
rule(string: simple(:string), lang: simple(:lang)) { RDF::Literal.new(string, language: lang) }
|
38
|
+
rule(string: simple(:string), type: simple(:type)) { RDF::Literal.new(string, datatype: RDF::URI.new(type)) }
|
39
|
+
rule(string: simple(:string)) { string }
|
40
|
+
rule(literal: simple(:literal)) do
|
41
|
+
case literal
|
42
|
+
when RDF::Literal
|
43
|
+
literal
|
44
|
+
else
|
45
|
+
RDF::Literal(literal)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
33
49
|
rule(iri: simple(:iri)) { RDF::IRI.new(iri) }
|
34
50
|
|
35
51
|
# Namespaces
|
data/lib/ldpath/version.rb
CHANGED
data/spec/ldpath_parser_spec.rb
CHANGED
@@ -154,7 +154,11 @@ describe Ldpath::Parser do
|
|
154
154
|
end
|
155
155
|
|
156
156
|
it "may be a numeric literal" do
|
157
|
-
subject.node.parse '
|
157
|
+
subject.node.parse '123'
|
158
|
+
end
|
159
|
+
|
160
|
+
it "may be a decimal literal" do
|
161
|
+
subject.decimal.parse '0.123'
|
158
162
|
end
|
159
163
|
|
160
164
|
it "may be a boolean literal" do
|
@@ -2,13 +2,42 @@ require 'spec_helper'
|
|
2
2
|
require 'pp'
|
3
3
|
describe Ldpath::Transform do
|
4
4
|
let(:parser) { Ldpath::Parser.new }
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
describe "transforms nodes" do
|
6
|
+
let(:parser) { Ldpath::Parser.new.node }
|
7
|
+
it "handles iris" do
|
8
|
+
actual = subject.apply parser.parse("<info:a>")
|
9
|
+
expect(actual).to eq RDF::URI.new("info:a")
|
10
|
+
end
|
11
|
+
|
12
|
+
it "handles strings" do
|
13
|
+
actual = subject.apply parser.parse('"xyz"')
|
14
|
+
expect(actual).to eq RDF::Literal.new("xyz")
|
15
|
+
end
|
8
16
|
|
9
|
-
|
10
|
-
|
11
|
-
|
17
|
+
it "handles langstrings" do
|
18
|
+
actual = subject.apply parser.parse('"xyz"@fr')
|
19
|
+
expect(actual).to eq RDF::Literal.new("xyz", language: 'fr')
|
20
|
+
end
|
21
|
+
|
22
|
+
it "handles typed literals" do
|
23
|
+
actual = subject.apply parser.parse('"xyz"^^info:x')
|
24
|
+
expect(actual).to eq RDF::Literal.new("xyz", datatype: RDF::URI.new("info:x"))
|
25
|
+
end
|
26
|
+
|
27
|
+
it "handles integers" do
|
28
|
+
actual = subject.apply parser.parse("0")
|
29
|
+
expect(actual).to eq RDF::Literal.new(0)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "handles decimals" do
|
33
|
+
actual = subject.apply parser.parse("0.01")
|
34
|
+
expect(actual).to eq RDF::Literal.new(0.01)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "handles doubles" do
|
38
|
+
actual = subject.apply parser.parse("1e-5")
|
39
|
+
expect(actual).to eq RDF::Literal.new(0.00001)
|
40
|
+
end
|
12
41
|
end
|
13
42
|
|
14
43
|
it "should transform prefix + localNames" do
|
@@ -32,7 +32,7 @@ describe "LDPath list functions" do
|
|
32
32
|
let(:program) do
|
33
33
|
Ldpath::Program.parse <<-EOF
|
34
34
|
@prefix ex : <http://example.com/> ;
|
35
|
-
list_item = fn:get(ex:list,
|
35
|
+
list_item = fn:get(ex:list, 1) :: xsd:string ;
|
36
36
|
EOF
|
37
37
|
end
|
38
38
|
|
@@ -45,8 +45,8 @@ describe "LDPath list functions" do
|
|
45
45
|
let(:program) do
|
46
46
|
Ldpath::Program.parse <<-EOF
|
47
47
|
@prefix ex : <http://example.com/> ;
|
48
|
-
list_items = fn:subList(ex:list,
|
49
|
-
list_items_by_range = fn:subList(ex:list,
|
48
|
+
list_items = fn:subList(ex:list, 1) :: xsd:string ;
|
49
|
+
list_items_by_range = fn:subList(ex:list, 0, 1) :: xsd:string ;
|
50
50
|
EOF
|
51
51
|
end
|
52
52
|
|