rdf-turtle 3.1.0 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 23a25ff951065479aaa42feb25d8646ed7e87270556e759ea10d219e24b4ac72
4
- data.tar.gz: ee4be6acf9c89792f5df29e018a3ab22c667477de4e95178517a4002da68707b
3
+ metadata.gz: 96b5531c582f4d0cd8d8fda744147e74501bf4efc0c721b1b3cf05a1d2cc60b3
4
+ data.tar.gz: 61e7f1e122300b7e822d81d9429dfee943a7ef7cfb12bb031f9ff133edd6a263
5
5
  SHA512:
6
- metadata.gz: 34c956ae30fbbe235344fc2ada8ed41539e912022b6ec0699afde1108d98d6b83a8c3008bc131535e0f96ef9b9f481079aa58dc5f8b384b250b20b4b33b72d90
7
- data.tar.gz: 405cfd25f8899631bac219096670d36969602329c7f3ea4905f0a36a47835ee6ac8aac65c163c6b8d4cc4ce284d2e162cb00ff1e7db193c8bf072c00c84c77dd
6
+ metadata.gz: faad4f256da8872159efeb82c150b30566770ad8fe1fdf504aa2b993b2b8e070192365ecfa5cea8c3f99d0087e3da4b21f0794b17883f17e0a8d6eaac6b82f26
7
+ data.tar.gz: f3c231a0ba031d14ff8273ec17a134144387626fe0594c6cb7d6b039d3aaac88d673344e8cf5cbb9019c3e3caba62929fa21ed0beb84c79f2896ea03bdd7028f
data/README.md CHANGED
@@ -3,9 +3,9 @@
3
3
  [Turtle][] reader/writer for [RDF.rb][RDF.rb] .
4
4
 
5
5
  [![Gem Version](https://badge.fury.io/rb/rdf-turtle.png)](https://badge.fury.io/rb/rdf-turtle)
6
- [![Build Status](https://travis-ci.org/ruby-rdf/rdf-turtle.png?branch=master)](https://travis-ci.org/ruby-rdf/rdf-turtle)
7
- [![Coverage Status](https://coveralls.io/repos/ruby-rdf/rdf-turtle/badge.svg)](https://coveralls.io/r/ruby-rdf/rdf-turtle)
8
- [![Dependency Status](https://gemnasium.com/ruby-rdf/rdf-turtle.png)](https://gemnasium.com/ruby-rdf/rdf-turtle)
6
+ [![Build Status](https://github.com/ruby-rdf/rdf-turtle/workflows/CI/badge.svg?branch=develop)](https://github.com/ruby-rdf/rdf-turtle/actions?query=workflow%3ACI)
7
+ [![Coverage Status](https://coveralls.io/repos/ruby-rdf/rdf-turtle/badge.svg?branch=develop)](https://coveralls.io/github/ruby-rdf/rdf-turtle?branch=develop)
8
+ [![Gitter chat](https://badges.gitter.im/ruby-rdf/rdf.png)](https://gitter.im/ruby-rdf/rdf)
9
9
 
10
10
  ## Description
11
11
  This is a [Ruby][] implementation of a [Turtle][] parser for [RDF.rb][].
@@ -17,8 +17,9 @@ Install with `gem install rdf-turtle`
17
17
 
18
18
  * 100% free and unencumbered [public domain](https://unlicense.org/) software.
19
19
  * Implements a complete parser for [Turtle][].
20
- * Compatible with Ruby >= 2.2.2.
20
+ * Compatible with Ruby >= 2.6.
21
21
  * Optional streaming writer, to serialize large graphs
22
+ * Provisional support for [Turtle-star][RDF-star].
22
23
 
23
24
  ## Usage
24
25
  Instantiate a reader from a local file:
@@ -35,6 +36,60 @@ Write a graph to a file:
35
36
  writer << graph
36
37
  end
37
38
 
39
+ ## Turtle-star (RDF-star)
40
+
41
+ Both reader and writer include provisional support for [Turtle-star][RDF-star].
42
+
43
+ Internally, an `RDF::Statement` is treated as another resource, along with `RDF::URI` and `RDF::Node`, which allows an `RDF::Statement` to have a `#subject` or `#object` which is also an `RDF::Statement`.
44
+
45
+ **Note: This feature is subject to change or elimination as the standards process progresses.**
46
+
47
+ ### Serializing a Graph containing embedded statements
48
+
49
+ require 'rdf/turtle'
50
+ statement = RDF::Statement(RDF::URI('bob'), RDF::Vocab::FOAF.age, RDF::Literal(23))
51
+ graph = RDF::Graph.new << [statement, RDF::URI("ex:certainty"), RDF::Literal(0.9)]
52
+ graph.dump(:ttl, validate: false, standard_prefixes: true)
53
+ # => '<<<bob> foaf:age 23>> <ex:certainty> 9.0e-1 .'
54
+
55
+ ### Reading a Graph containing embedded statements
56
+
57
+ By default, the Turtle reader will reject a document containing a subject resource.
58
+
59
+ ttl = %(
60
+ @prefix foaf: <http://xmlns.com/foaf/0.1/> .
61
+ @prefix ex: <http://example.com/> .
62
+ <<<bob> foaf:age 23>> ex:certainty 9.0e-1 .
63
+ )
64
+ graph = RDF::Graph.new do |graph|
65
+ RDF::Turtle::Reader.new(ttl) {|reader| graph << reader}
66
+ end
67
+ # => RDF::ReaderError
68
+
69
+ Readers support a boolean valued `rdfstar` option; only one statement is asserted, although the reified statement is contained within the graph.
70
+
71
+ graph = RDF::Graph.new do |graph|
72
+ RDF::Turtle::Reader.new(ttl, rdfstar: true) {|reader| graph << reader}
73
+ end
74
+ graph.count #=> 1
75
+
76
+ ### Reading a Graph containing statement annotations
77
+
78
+ Annotations are introduced using the `{| ... |}` syntax, which is treated like a `blankNodePropertyList`,
79
+ where the subject is the the triple ending with that annotation.
80
+
81
+ ttl = %(
82
+ @prefix foaf: <http://xmlns.com/foaf/0.1/> .
83
+ @prefix ex: <http://example.com/> .
84
+ <bob> foaf:age 23 {| ex:certainty 9.0e-1 |} .
85
+ )
86
+ graph = RDF::Graph.new do |graph|
87
+ RDF::Turtle::Reader.new(ttl) {|reader| graph << reader}
88
+ end
89
+ # => RDF::ReaderError
90
+
91
+ Note that this requires the `rdfstar` option to be set.
92
+
38
93
  ## Documentation
39
94
  Full documentation available on [Rubydoc.info][Turtle doc]
40
95
 
@@ -85,9 +140,9 @@ This version uses a hand-written parser using the Lexer from the [EBNF][] gem in
85
140
 
86
141
  ## Dependencies
87
142
 
88
- * [Ruby](https://ruby-lang.org/) (>= 2.4)
89
- * [RDF.rb](https://rubygems.org/gems/rdf) (~> 3.1)
90
- * [EBNF][] (~> 1.1)
143
+ * [Ruby](https://ruby-lang.org/) (>= 2.6)
144
+ * [RDF.rb](https://rubygems.org/gems/rdf) (~> 3.2)
145
+ * [EBNF][] (~> 1.2)
91
146
 
92
147
  ## Installation
93
148
 
@@ -115,7 +170,9 @@ This repository uses [Git Flow](https://github.com/nvie/gitflow) to mange develo
115
170
  list in the the `README`. Alphabetical order applies.
116
171
  * Do note that in order for us to merge any non-trivial changes (as a rule
117
172
  of thumb, additions larger than about 15 lines of code), we need an
118
- explicit [public domain dedication][PDD] on record from you.
173
+ explicit [public domain dedication][PDD] on record from you,
174
+ which you will be asked to agree to on the first commit to a repo within the organization.
175
+ Note that the agreement applies to all repos in the [Ruby RDF](https://github.com/ruby-rdf/) organization.
119
176
 
120
177
  ## License
121
178
  This is free and unencumbered public domain software. For more information,
@@ -127,12 +184,13 @@ A copy of the [Turtle EBNF][] and derived parser files are included in the repos
127
184
  [RDF]: https://www.w3.org/RDF/
128
185
  [YARD]: https://yardoc.org/
129
186
  [YARD-GS]: https://rubydoc.info/docs/yard/file/docs/GettingStarted.md
130
- [PDD]: https://lists.w3.org/Archives/Public/public-rdf-ruby/2010May/0013.html
187
+ [PDD]: https://unlicense.org/#unlicensing-contributions
131
188
  [RDF.rb]: https://rubydoc.info/github/ruby-rdf/rdf
132
189
  [EBNF]: https://rubygems.org/gems/ebnf
133
190
  [Backports]: https://rubygems.org/gems/backports
134
191
  [N-Triples]: https://www.w3.org/TR/rdf-testcases/#ntriples
135
192
  [Turtle]: https://www.w3.org/TR/2012/WD-turtle-20120710/
193
+ [RDF-star]: https://w3c.github.io/rdf-star/rdf-star-cg-spec.html
136
194
  [Turtle doc]: https://rubydoc.info/github/ruby-rdf/rdf-turtle/master/file/README.md
137
195
  [Turtle EBNF]: https://dvcs.w3.org/hg/rdf/file/default/rdf-turtle/turtle.bnf
138
196
  [Freebase Dumps]: https://developers.google.com/freebase/data
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.1.0
1
+ 3.2.0
@@ -25,7 +25,14 @@ module RDF::Turtle
25
25
  terminal(:STRING_LITERAL_SINGLE_QUOTE, STRING_LITERAL_SINGLE_QUOTE, unescape: true)
26
26
 
27
27
  # String terminals
28
- terminal(nil, %r([\(\),.;\[\]Aa]|\^\^|true|false))
28
+ terminal(nil, %r(
29
+ [\(\),.;\[\]Aa]
30
+ | \^\^
31
+ | \{\|
32
+ | \|\}
33
+ | true|false
34
+ | <<|>>
35
+ )x)
29
36
 
30
37
  terminal(:PREFIX, PREFIX)
31
38
  terminal(:BASE, BASE)
@@ -87,16 +94,16 @@ module RDF::Turtle
87
94
  @options = {
88
95
  anon_base: "b0",
89
96
  whitespace: WS,
90
- log_depth: 0,
97
+ depth: 0,
91
98
  }.merge(@options)
92
99
  @prod_stack = []
93
100
 
94
101
  @options[:base_uri] = RDF::URI(base_uri || "")
95
- log_debug("base IRI") {base_uri.inspect}
102
+ debug("base IRI") {base_uri.inspect}
96
103
 
97
- log_debug("validate") {validate?.inspect}
98
- log_debug("canonicalize") {canonicalize?.inspect}
99
- log_debug("intern") {intern?.inspect}
104
+ debug("validate") {validate?.inspect}
105
+ debug("canonicalize") {canonicalize?.inspect}
106
+ debug("intern") {intern?.inspect}
100
107
 
101
108
  @lexer = EBNF::LL1::Lexer.new(input, self.class.patterns, **@options)
102
109
 
@@ -163,7 +170,7 @@ module RDF::Turtle
163
170
  # @return [RDF::Statement] Added statement
164
171
  # @raise [RDF::ReaderError] Checks parameter types and raises if they are incorrect if parsing mode is _validate_.
165
172
  def add_statement(production, statement)
166
- error("Statement is invalid: #{statement.inspect.inspect}", production: produciton) if validate? && statement.invalid?
173
+ error("Statement is invalid: #{statement.inspect}", production: produciton) if validate? && statement.invalid?
167
174
  @callback.call(statement) if statement.subject &&
168
175
  statement.predicate &&
169
176
  statement.object &&
@@ -185,7 +192,7 @@ module RDF::Turtle
185
192
 
186
193
  # Create a literal
187
194
  def literal(value, **options)
188
- log_debug("literal") do
195
+ debug("literal", depth: @options[:depth]) do
189
196
  "value: #{value.inspect}, " +
190
197
  "options: #{options.inspect}, " +
191
198
  "validate: #{validate?.inspect}, " +
@@ -221,7 +228,7 @@ module RDF::Turtle
221
228
  ''
222
229
  end
223
230
  suffix = suffix.to_s.sub(/^\#/, "") if base.index("#")
224
- log_debug("pname") {"base: '#{base}', suffix: '#{suffix}'"}
231
+ debug("pname", depth: options[:depth]) {"base: '#{base}', suffix: '#{suffix}'"}
225
232
  process_iri(base + suffix.to_s)
226
233
  end
227
234
 
@@ -283,7 +290,7 @@ module RDF::Turtle
283
290
  terminated = token.value == '@prefix'
284
291
  error("Expected PNAME_NS", production: :prefix, token: pfx) unless pfx === :PNAME_NS
285
292
  error("Expected IRIREF", production: :prefix, token: iri) unless iri === :IRIREF
286
- log_debug("prefixID") {"Defined prefix #{pfx.inspect} mapping to #{iri.inspect}"}
293
+ debug("prefixID", depth: options[:depth]) {"Defined prefix #{pfx.inspect} mapping to #{iri.inspect}"}
287
294
  prefix(pfx.value[0..-2], process_iri(iri))
288
295
  error("prefixId", "#{token} should be downcased") if token.value.start_with?('@') && token.value != '@prefix'
289
296
 
@@ -340,6 +347,10 @@ module RDF::Turtle
340
347
  last_object = nil
341
348
  while object = prod(:_objectList_2) {read_object(subject, predicate)}
342
349
  last_object = object
350
+
351
+ # If object is followed by an annotation, read that and also emit an embedded triple.
352
+ read_annotation(subject, predicate, object)
353
+
343
354
  break unless @lexer.first === ','
344
355
  @lexer.shift while @lexer.first === ','
345
356
  end
@@ -362,6 +373,7 @@ module RDF::Turtle
362
373
  read_iri ||
363
374
  read_BlankNode ||
364
375
  read_collection ||
376
+ read_quotedTriple ||
365
377
  error( "Expected subject", production: :subject, token: @lexer.first)
366
378
  end
367
379
  end
@@ -373,7 +385,8 @@ module RDF::Turtle
373
385
  read_BlankNode ||
374
386
  read_collection ||
375
387
  read_blankNodePropertyList ||
376
- read_literal
388
+ read_literal ||
389
+ read_quotedTriple
377
390
 
378
391
  add_statement(:object, RDF::Statement(subject, predicate, object)) if subject && predicate
379
392
  object
@@ -381,13 +394,71 @@ module RDF::Turtle
381
394
  end
382
395
  end
383
396
 
397
+ # Read an RDF-star reified statement
398
+ # @return [RDF::Statement]
399
+ def read_quotedTriple
400
+ return unless @options[:rdfstar]
401
+ if @lexer.first.value == '<<'
402
+ prod(:quotedTriple) do
403
+ @lexer.shift # eat <<
404
+ subject = read_qtSubject || error("Failed to parse subject", production: :quotedTriple, token: @lexer.first)
405
+ predicate = read_verb || error("Failed to parse predicate", production: :quotedTriple, token: @lexer.first)
406
+ object = read_qtObject || error("Failed to parse object", production: :quotedTriple, token: @lexer.first)
407
+ unless @lexer.first.value == '>>'
408
+ error("Failed to end of embedded triple", production: :quotedTriple, token: @lexer.first)
409
+ end
410
+ @lexer.shift
411
+ statement = RDF::Statement(subject, predicate, object)
412
+ statement
413
+ end
414
+ end
415
+ end
416
+
417
+ # @return [RDF::Resource]
418
+ def read_qtSubject
419
+ prod(:qtSubject) do
420
+ read_iri ||
421
+ read_BlankNode ||
422
+ read_quotedTriple ||
423
+ error( "Expected embedded subject", production: :qtSubject, token: @lexer.first)
424
+ end
425
+ end
426
+
427
+ # @return [RDF::Term]
428
+ def read_qtObject(subject = nil, predicate = nil)
429
+ prod(:qtObject) do
430
+ read_iri ||
431
+ read_BlankNode ||
432
+ read_literal ||
433
+ read_quotedTriple
434
+ end
435
+ end
436
+
437
+ # Read an annotation on a triple
438
+ def read_annotation(subject, predicate, object)
439
+ error("Unexpected end of file", production: :annotation) unless token = @lexer.first
440
+ if token === '{|'
441
+ prod(:annotation, %(|})) do
442
+ @lexer.shift
443
+
444
+ # Statement becomes subject for predicateObjectList
445
+ statement = RDF::Statement(subject, predicate, object)
446
+ read_predicateObjectList(statement) ||
447
+ error("Expected predicateObjectList", production: :annotation, token: @lexer.first)
448
+ error("annotation", "Expected closing '|}'") unless @lexer.first === '|}'
449
+ @lexer.shift
450
+ end
451
+ end
452
+
453
+ end
454
+
384
455
  # @return [RDF::Literal]
385
456
  def read_literal
386
457
  error("Unexpected end of file", production: :literal) unless token = @lexer.first
387
458
  case token.type || token.value
388
459
  when :INTEGER then prod(:literal) {literal(@lexer.shift.value, datatype: RDF::XSD.integer)}
389
460
  when :DECIMAL
390
- prod(:litearl) do
461
+ prod(:literal) do
391
462
  value = @lexer.shift.value
392
463
  value = "0#{value}" if value.start_with?(".")
393
464
  literal(value, datatype: RDF::XSD.decimal)
@@ -431,7 +502,7 @@ module RDF::Turtle
431
502
  if token === '['
432
503
  prod(:blankNodePropertyList, %{]}) do
433
504
  @lexer.shift
434
- log_info("blankNodePropertyList") {"token: #{token.inspect}"}
505
+ progress("blankNodePropertyList", depth: options[:depth]) {"token: #{token.inspect}"}
435
506
  node = bnode
436
507
  read_predicateObjectList(node)
437
508
  error("blankNodePropertyList", "Expected closing ']'") unless @lexer.first === ']'
@@ -447,7 +518,7 @@ module RDF::Turtle
447
518
  prod(:collection, %{)}) do
448
519
  @lexer.shift
449
520
  token = @lexer.first
450
- log_info("collection") {"token: #{token.inspect}"}
521
+ progress("collection", depth: options[:depth]) {"token: #{token.inspect}"}
451
522
  objects = []
452
523
  while object = read_object
453
524
  objects << object
@@ -483,8 +554,8 @@ module RDF::Turtle
483
554
 
484
555
  def prod(production, recover_to = [])
485
556
  @prod_stack << {prod: production, recover_to: recover_to}
486
- @options[:log_depth] += 1
487
- log_recover("#{production}(start)") {"token: #{@lexer.first.inspect}"}
557
+ @options[:depth] += 1
558
+ recover("#{production}(start)", depth: options[:depth], token: @lexer.first)
488
559
  yield
489
560
  rescue EBNF::LL1::Lexer::Error, SyntaxError, Recovery => e
490
561
  # Lexer encountered an illegal token or the parser encountered
@@ -504,13 +575,13 @@ module RDF::Turtle
504
575
  end
505
576
  end
506
577
  raise EOFError, "End of input found when recovering" if @lexer.first.nil?
507
- log_debug("recovery", "current token: #{@lexer.first.inspect}")
578
+ debug("recovery", "current token: #{@lexer.first.inspect}", depth: options[:depth])
508
579
 
509
580
  unless e.is_a?(Recovery)
510
581
  # Get the list of follows for this sequence, this production and the stacked productions.
511
- log_debug("recovery", "stack follows:")
582
+ debug("recovery", "stack follows:", depth: options[:depth])
512
583
  @prod_stack.reverse.each do |prod|
513
- log_debug("recovery", level: 4) {" #{prod[:prod]}: #{prod[:recover_to].inspect}"}
584
+ debug("recovery", level: 4, depth: options[:depth]) {" #{prod[:prod]}: #{prod[:recover_to].inspect}"}
514
585
  end
515
586
  end
516
587
 
@@ -520,9 +591,9 @@ module RDF::Turtle
520
591
  # Skip tokens until one is found in follows
521
592
  while (token = (@lexer.first rescue @lexer.recover)) && follows.none? {|t| token === t}
522
593
  skipped = @lexer.shift
523
- log_debug("recovery") {"skip #{skipped.inspect}"}
594
+ debug("recovery", depth: options[:depth]) {"skip #{skipped.inspect}"}
524
595
  end
525
- log_debug("recovery") {"found #{token.inspect} in follows"}
596
+ debug("recovery", depth: options[:depth]) {"found #{token.inspect} in follows"}
526
597
 
527
598
  # Re-raise the error unless token is a follows of this production
528
599
  raise Recovery unless Array(recover_to).any? {|t| token === t}
@@ -530,11 +601,35 @@ module RDF::Turtle
530
601
  # Skip that token to get something reasonable to start the next production with
531
602
  @lexer.shift
532
603
  ensure
533
- log_info("#{production}(finish)")
534
- @options[:log_depth] -= 1
604
+ progress("#{production}(finish)", depth: options[:depth])
605
+ @options[:depth] -= 1
535
606
  @prod_stack.pop
536
607
  end
537
608
 
609
+ def progress(*args, &block)
610
+ lineno = (options[:token].lineno if options[:token].respond_to?(:lineno)) || (@lexer && @lexer.lineno)
611
+ opts = args.last.is_a?(Hash) ? args.pop : {}
612
+ opts[:level] ||= 1
613
+ opts[:lineno] ||= lineno
614
+ log_info(*args, **opts, &block)
615
+ end
616
+
617
+ def recover(*args, &block)
618
+ lineno = (options[:token].lineno if options[:token].respond_to?(:lineno)) || (@lexer && @lexer.lineno)
619
+ opts = args.last.is_a?(Hash) ? args.pop : {}
620
+ opts[:level] ||= 1
621
+ opts[:lineno] ||= lineno
622
+ log_recover(*args, **opts, &block)
623
+ end
624
+
625
+ def debug(*args, &block)
626
+ lineno = (options[:token].lineno if options[:token].respond_to?(:lineno)) || (@lexer && @lexer.lineno)
627
+ opts = args.last.is_a?(Hash) ? args.pop : {}
628
+ opts[:level] ||= 0
629
+ opts[:lineno] ||= lineno
630
+ log_debug(*args, **opts, &block)
631
+ end
632
+
538
633
  ##
539
634
  # Error information, used as level `0` debug messages.
540
635
  #
@@ -554,7 +649,8 @@ module RDF::Turtle
554
649
  lineno: lineno,
555
650
  token: options[:token],
556
651
  production: options[:production],
557
- exception: SyntaxError)
652
+ depth: options[:depth],
653
+ exception: SyntaxError,)
558
654
  end
559
655
 
560
656
  # Used for internal error recovery
@@ -1,9 +1,9 @@
1
+ # encoding: utf-8
1
2
  require 'ebnf/ll1/lexer'
2
3
 
3
4
  module RDF::Turtle
4
5
  module Terminals
5
6
  # Definitions of token regular expressions used for lexical analysis
6
-
7
7
  ##
8
8
  # Unicode regular expressions for Ruby 1.9+ with the Oniguruma engine.
9
9
  U_CHARS1 = Regexp.compile(<<-EOS.gsub(/\s+/, ''))
@@ -12,66 +12,66 @@ module RDF::Turtle
12
12
  [\\u2070-\\u218F]|[\\u2C00-\\u2FEF]|[\\u3001-\\uD7FF]|
13
13
  [\\uF900-\\uFDCF]|[\\uFDF0-\\uFFFD]|[\\u{10000}-\\u{EFFFF}]
14
14
  EOS
15
- U_CHARS2 = Regexp.compile("\\u00B7|[\\u0300-\\u036F]|[\\u203F-\\u2040]").freeze
16
- IRI_RANGE = Regexp.compile("[[^<>\"{}|^`\\\\]&&[^\\x00-\\x20]]").freeze
15
+ U_CHARS2 = Regexp.compile("\\u00B7|[\\u0300-\\u036F]|[\\u203F-\\u2040]", Regexp::FIXEDENCODING).freeze
16
+ IRI_RANGE = Regexp.compile("[[^<>\"{}|^`\\\\]&&[^\\x00-\\x20]]", Regexp::FIXEDENCODING).freeze
17
17
 
18
18
  # 26
19
19
  UCHAR = EBNF::LL1::Lexer::UCHAR
20
20
  # 170s
21
- PERCENT = /%[0-9A-Fa-f]{2}/.freeze
21
+ PERCENT = /%[0-9A-Fa-f]{2}/u.freeze
22
22
  # 172s
23
- PN_LOCAL_ESC = /\\[_~\.\-\!$\&'\(\)\*\+,;=\/\?\#@%]/.freeze
23
+ PN_LOCAL_ESC = /\\[_~\.\-\!$\&'\(\)\*\+,;=\/\?\#@%]/u.freeze
24
24
  # 169s
25
- PLX = /#{PERCENT}|#{PN_LOCAL_ESC}/.freeze.freeze
25
+ PLX = /#{PERCENT}|#{PN_LOCAL_ESC}/u.freeze
26
26
  # 163s
27
- PN_CHARS_BASE = /[A-Z]|[a-z]|#{U_CHARS1}/.freeze
27
+ PN_CHARS_BASE = /[A-Z]|[a-z]|#{U_CHARS1}/u.freeze
28
28
  # 164s
29
- PN_CHARS_U = /_|#{PN_CHARS_BASE}/.freeze
29
+ PN_CHARS_U = /_|#{PN_CHARS_BASE}/u.freeze
30
30
  # 166s
31
- PN_CHARS = /-|[0-9]|#{PN_CHARS_U}|#{U_CHARS2}/.freeze
32
- PN_LOCAL_BODY = /(?:(?:\.|:|#{PN_CHARS}|#{PLX})*(?:#{PN_CHARS}|:|#{PLX}))?/.freeze
33
- PN_CHARS_BODY = /(?:(?:\.|#{PN_CHARS})*#{PN_CHARS})?/.freeze
31
+ PN_CHARS = /-|[0-9]|#{PN_CHARS_U}|#{U_CHARS2}/u.freeze
32
+ PN_LOCAL_BODY = /(?:(?:\.|:|#{PN_CHARS}|#{PLX})*(?:#{PN_CHARS}|:|#{PLX}))?/u.freeze
33
+ PN_CHARS_BODY = /(?:(?:\.|#{PN_CHARS})*#{PN_CHARS})?/u.freeze
34
34
  # 167s
35
- PN_PREFIX = /#{PN_CHARS_BASE}#{PN_CHARS_BODY}/.freeze
35
+ PN_PREFIX = /#{PN_CHARS_BASE}#{PN_CHARS_BODY}/u.freeze
36
36
  # 168s
37
- PN_LOCAL = /(?:[0-9]|:|#{PN_CHARS_U}|#{PLX})#{PN_LOCAL_BODY}/.freeze
37
+ PN_LOCAL = /(?:[0-9]|:|#{PN_CHARS_U}|#{PLX})#{PN_LOCAL_BODY}/u.freeze
38
38
  # 154s
39
- EXPONENT = /[eE][+-]?[0-9]+/
39
+ EXPONENT = /[eE][+-]?[0-9]+/u.freeze
40
40
  # 159s
41
- ECHAR = /\\[tbnrf\\"']/
41
+ ECHAR = /\\[tbnrf\\"']/u.freeze
42
42
  # 18
43
- IRIREF = /<(?:#{IRI_RANGE}|#{UCHAR})*>/.freeze
43
+ IRIREF = /<(?:#{IRI_RANGE}|#{UCHAR})*>/u.freeze
44
44
  # 139s
45
- PNAME_NS = /#{PN_PREFIX}?:/.freeze
45
+ PNAME_NS = /#{PN_PREFIX}?:/u.freeze
46
46
  # 140s
47
- PNAME_LN = /#{PNAME_NS}#{PN_LOCAL}/.freeze
47
+ PNAME_LN = /#{PNAME_NS}#{PN_LOCAL}/u.freeze
48
48
  # 141s
49
- BLANK_NODE_LABEL = /_:(?:[0-9]|#{PN_CHARS_U})(?:(?:#{PN_CHARS}|\.)*#{PN_CHARS})?/.freeze
49
+ BLANK_NODE_LABEL = /_:(?:[0-9]|#{PN_CHARS_U})(?:(?:#{PN_CHARS}|\.)*#{PN_CHARS})?/u.freeze
50
50
  # 144s
51
- LANGTAG = /@[a-zA-Z]+(?:-[a-zA-Z0-9]+)*/.freeze
51
+ LANGTAG = /@[a-zA-Z]+(?:-[a-zA-Z0-9]+)*/u.freeze
52
52
  # 19
53
- INTEGER = /[+-]?[0-9]+/.freeze
53
+ INTEGER = /[+-]?[0-9]+/u.freeze
54
54
  # 20
55
- DECIMAL = /[+-]?(?:[0-9]*\.[0-9]+)/.freeze
55
+ DECIMAL = /[+-]?(?:[0-9]*\.[0-9]+)/u.freeze
56
56
  # 21
57
- DOUBLE = /[+-]?(?:[0-9]+\.[0-9]*#{EXPONENT}|\.?[0-9]+#{EXPONENT})/.freeze
57
+ DOUBLE = /[+-]?(?:[0-9]+\.[0-9]*#{EXPONENT}|\.?[0-9]+#{EXPONENT})/u.freeze
58
58
  # 22
59
- STRING_LITERAL_SINGLE_QUOTE = /'(?:[^\'\\\n\r]|#{ECHAR}|#{UCHAR})*'/.freeze
59
+ STRING_LITERAL_SINGLE_QUOTE = /'(?:[^\'\\\n\r]|#{ECHAR}|#{UCHAR})*'/u.freeze
60
60
  # 23
61
- STRING_LITERAL_QUOTE = /"(?:[^\"\\\n\r]|#{ECHAR}|#{UCHAR})*"/.freeze
61
+ STRING_LITERAL_QUOTE = /"(?:[^\"\\\n\r]|#{ECHAR}|#{UCHAR})*"/u.freeze
62
62
  # 24
63
- STRING_LITERAL_LONG_SINGLE_QUOTE = /'''(?:(?:'|'')?(?:[^'\\]|#{ECHAR}|#{UCHAR}))*'''/m.freeze
63
+ STRING_LITERAL_LONG_SINGLE_QUOTE = /'''(?:(?:'|'')?(?:[^'\\]|#{ECHAR}|#{UCHAR}))*'''/um.freeze
64
64
  # 25
65
- STRING_LITERAL_LONG_QUOTE = /"""(?:(?:"|"")?(?:[^"\\]|#{ECHAR}|#{UCHAR}))*"""/m.freeze
65
+ STRING_LITERAL_LONG_QUOTE = /"""(?:(?:"|"")?(?:[^"\\]|#{ECHAR}|#{UCHAR}))*"""/um.freeze
66
66
 
67
67
  # 161s
68
- WS = /(?:\s|(?:#[^\n\r]*))+/m.freeze
68
+ WS = /(?:\s|(?:#[^\n\r]*))+/um.freeze
69
69
  # 162s
70
- ANON = /\[#{WS}*\]/m.freeze
70
+ ANON = /\[#{WS}*\]/um.freeze
71
71
  # 28t
72
- PREFIX = /@?prefix/i.freeze
72
+ PREFIX = /@?prefix/ui.freeze
73
73
  # 29t
74
- BASE = /@?base/i.freeze
74
+ BASE = /@?base/ui.freeze
75
75
 
76
76
  end
77
77
  end
@@ -276,10 +276,23 @@ module RDF::Turtle
276
276
  case literal
277
277
  when RDF::Literal
278
278
  case @options[:literal_shorthand] && literal.valid? ? literal.datatype : false
279
- when RDF::XSD.boolean, RDF::XSD.integer, RDF::XSD.decimal
280
- literal.canonicalize.to_s
279
+ when RDF::XSD.boolean
280
+ %w(true false).include?(literal.value) ? literal.value : literal.canonicalize.to_s
281
+ when RDF::XSD.integer
282
+ literal.value.match?(/^[\+\-]?\d+$/) && !canonicalize? ? literal.value : literal.canonicalize.to_s
283
+ when RDF::XSD.decimal
284
+ literal.value.match?(/^[\+\-]?\d+\.\d+?$/) && !canonicalize? ?
285
+ literal.value :
286
+ literal.canonicalize.to_s
281
287
  when RDF::XSD.double
282
- literal.canonicalize.to_s.sub('E', 'e') # Favor lower case exponent
288
+ in_form = case literal.value
289
+ when /[\+\-]?\d+\.\d*E[\+\-]?\d+$/i then true
290
+ when /[\+\-]?\.\d+E[\+\-]?\d+$/i then true
291
+ when /[\+\-]?\d+E[\+\-]?\d+$/i then true
292
+ else false
293
+ end && !canonicalize?
294
+
295
+ in_form ? literal.value : literal.canonicalize.to_s.sub('E', 'e').to_s
283
296
  else
284
297
  text = quoted(literal.value)
285
298
  text << "@#{literal.language}" if literal.has_language?
@@ -313,6 +326,17 @@ module RDF::Turtle
313
326
  options[:unique_bnodes] ? node.to_unique_base : node.to_base
314
327
  end
315
328
 
329
+ ##
330
+ # Returns an embedded triple.
331
+ #
332
+ # @param [RDF::Statement] statement
333
+ # @param [Hash{Symbol => Object}] options
334
+ # @return [String]
335
+ def format_quotedTriple(statement, **options)
336
+ log_debug("rdfstar") {"#{statement.to_ntriples}"}
337
+ "<<%s %s %s>>" % statement.to_a.map { |value| format_term(value, **options) }
338
+ end
339
+
316
340
  protected
317
341
  # Output @base and @prefix definitions
318
342
  def start_document
@@ -363,16 +387,16 @@ module RDF::Turtle
363
387
  # Mark as seen lists that are part of another list
364
388
  @lists.values.map(&:statements).
365
389
  flatten.each do |st|
366
- seen[st.object] = true if @lists.has_key?(st.object)
390
+ seen[st.object] = true if @lists.key?(st.object)
367
391
  end
368
392
 
369
- # List elements which are bnodes should not be targets for top-level serialization
370
- list_elements = @lists.values.map(&:to_a).flatten.select(&:node?).compact
393
+ # List elements which are bnodes should not be targets for top-level serialization
394
+ list_elements = @lists.values.map(&:to_a).flatten.select(&:node?).compact
371
395
 
372
- # Sort subjects by resources over bnodes, ref_counts and the subject URI itself
396
+ # Sort subjects by resources and statements over bnodes, ref_counts and the subject URI itself
373
397
  recursable = (@subjects.keys - list_elements).
374
398
  select {|s| !seen.include?(s)}.
375
- map {|r| [r.node? ? 1 : 0, ref_count(r), r]}.
399
+ map {|r| [r.node? ? 2 : (r.statement? ? 1 : 0), ref_count(r), r]}.
376
400
  sort
377
401
 
378
402
  subjects + recursable.map{|r| r.last}
@@ -457,8 +481,8 @@ module RDF::Turtle
457
481
 
458
482
  # Can subject be represented as a blankNodePropertyList?
459
483
  def blankNodePropertyList?(resource, position)
460
- resource.node? &&
461
- !is_valid_list?(resource) &&
484
+ !resource.statement? && resource.node? &&
485
+ !collection?(resource) &&
462
486
  (!is_done?(resource) || position == :subject) &&
463
487
  ref_count(resource) == (position == :object ? 1 : 0)
464
488
  end
@@ -496,35 +520,33 @@ module RDF::Turtle
496
520
  private
497
521
 
498
522
  # Checks if l is a valid RDF list, i.e. no nodes have other properties.
499
- def is_valid_list?(l)
500
- #log_debug("is_valid_list?") {l.inspect}
501
- return @lists[l] && @lists[l].valid?
502
- end
503
-
504
- def do_list(l, position)
505
- list = @lists[l]
506
- log_debug("do_list") {list.inspect}
507
- subject_done(RDF.nil)
508
- index = 0
509
- list.each_statement do |st|
510
- next unless st.predicate == RDF.first
511
- log_debug {" list this: #{st.subject} first: #{st.object}[#{position}]"}
512
- @output.write(" ") if index > 0
513
- path(st.object, position)
514
- subject_done(st.subject)
515
- position = :object
516
- index += 1
517
- end
523
+ def collection?(l)
524
+ #log_debug("collection?") {l.inspect}
525
+ return @lists.key?(l)
518
526
  end
519
527
 
520
528
  def collection(node, position)
521
- return false if !is_valid_list?(node)
529
+ return false if !collection?(node)
522
530
  return false if position == :subject && ref_count(node) > 0
523
531
  return false if position == :object && prop_count(node) > 0
524
532
  #log_debug("collection") {"#{node.to_ntriples}, #{position}"}
525
533
 
526
534
  @output.write("(")
527
- log_depth {do_list(node, position)}
535
+ log_depth do
536
+ list = @lists[node]
537
+ log_debug("collection") {list.inspect}
538
+ subject_done(RDF.nil)
539
+ index = 0
540
+ list.each_statement do |st|
541
+ next unless st.predicate == RDF.first
542
+ log_debug {" list this: #{st.subject} first: #{st.object}[#{position}]"}
543
+ @output.write(" ") if index > 0
544
+ path(st.object, position)
545
+ subject_done(st.subject)
546
+ position = :object
547
+ index += 1
548
+ end
549
+ end
528
550
  @output.write(')')
529
551
  end
530
552
 
@@ -557,7 +579,7 @@ module RDF::Turtle
557
579
  log_debug("path") do
558
580
  "#{resource.to_ntriples}, " +
559
581
  "pos: #{position}, " +
560
- "()?: #{is_valid_list?(resource)}, " +
582
+ "()?: #{collection?(resource)}, " +
561
583
  "[]?: #{blankNodePropertyList?(resource, position)}, " +
562
584
  "rc: #{ref_count(resource)}"
563
585
  end
@@ -577,7 +599,7 @@ module RDF::Turtle
577
599
  end
578
600
 
579
601
  # Render an objectList having a common subject and predicate
580
- def objectList(objects)
602
+ def objectList(subject, predicate, objects)
581
603
  log_debug("objectList") {objects.inspect}
582
604
  return if objects.empty?
583
605
 
@@ -588,6 +610,15 @@ module RDF::Turtle
588
610
  @output.write ",\n#{indent(4)}"
589
611
  end
590
612
  path(obj, :object)
613
+
614
+ # If subject, predicate, and object are embedded, write those bits out too.
615
+ emb = RDF::Statement(subject, predicate, obj)
616
+ if !@graph.query({subject: emb}).empty?
617
+ @output.write ' {| '
618
+ predicateObjectList(emb, true)
619
+ @output.write ' |}'
620
+ subject_done(emb)
621
+ end
591
622
  end
592
623
  end
593
624
 
@@ -600,17 +631,18 @@ module RDF::Turtle
600
631
  end
601
632
 
602
633
  prop_list = sort_properties(properties)
603
- prop_list -= [RDF.first.to_s, RDF.rest.to_s] if @lists.include?(subject)
634
+ prop_list -= [RDF.first.to_s, RDF.rest.to_s] if @lists.key?(subject)
604
635
  log_debug("predicateObjectList") {prop_list.inspect}
605
636
  return 0 if prop_list.empty?
606
637
 
607
638
  @output.write("\n#{indent(2)}") if properties.keys.length > 1 && from_bpl
608
639
  prop_list.each_with_index do |prop, i|
609
640
  begin
641
+ pred = RDF::URI.intern(prop)
610
642
  @output.write(";\n#{indent(2)}") if i > 0
611
- predicate(RDF::URI.intern(prop))
643
+ predicate(pred)
612
644
  @output.write(" ")
613
- objectList(properties[prop])
645
+ objectList(subject, pred, properties[prop])
614
646
  end
615
647
  end
616
648
  properties.keys.length
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdf-turtle
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregg Kellogg
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-10 00:00:00.000000000 Z
11
+ date: 2021-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rdf
@@ -16,140 +16,140 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '3.1'
19
+ version: '3.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '3.1'
26
+ version: '3.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: ebnf
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.2'
33
+ version: '2.3'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.2'
40
+ version: '2.3'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rspec
42
+ name: erubis
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '3.9'
47
+ version: '2.7'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '3.9'
54
+ version: '2.7'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rspec-its
56
+ name: htmlentities
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '1.3'
61
+ version: '4.3'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '1.3'
68
+ version: '4.3'
69
69
  - !ruby/object:Gem::Dependency
70
- name: rdf-isomorphic
70
+ name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '3.1'
75
+ version: '3.10'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '3.1'
82
+ version: '3.10'
83
83
  - !ruby/object:Gem::Dependency
84
- name: json-ld
84
+ name: rspec-its
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '3.1'
89
+ version: '1.3'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '3.1'
96
+ version: '1.3'
97
97
  - !ruby/object:Gem::Dependency
98
- name: rdf-spec
98
+ name: rdf-isomorphic
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '3.1'
103
+ version: '3.2'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '3.1'
110
+ version: '3.2'
111
111
  - !ruby/object:Gem::Dependency
112
- name: rdf-vocab
112
+ name: json-ld
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: '3.1'
117
+ version: '3.2'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: '3.1'
124
+ version: '3.2'
125
125
  - !ruby/object:Gem::Dependency
126
- name: rake
126
+ name: rdf-spec
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - "~>"
130
130
  - !ruby/object:Gem::Version
131
- version: '13.0'
131
+ version: '3.2'
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - "~>"
137
137
  - !ruby/object:Gem::Version
138
- version: '13.0'
138
+ version: '3.2'
139
139
  - !ruby/object:Gem::Dependency
140
- name: yard
140
+ name: rdf-vocab
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
143
  - - "~>"
144
144
  - !ruby/object:Gem::Version
145
- version: 0.9.20
145
+ version: '3.2'
146
146
  type: :development
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
- version: 0.9.20
152
+ version: '3.2'
153
153
  description: RDF::Turtle is an Turtle reader/writer for the RDF.rb library suite.
154
154
  email: public-rdf-ruby@w3.org
155
155
  executables: []
@@ -169,11 +169,11 @@ files:
169
169
  - lib/rdf/turtle/terminals.rb
170
170
  - lib/rdf/turtle/version.rb
171
171
  - lib/rdf/turtle/writer.rb
172
- homepage: https://ruby-rdf.github.com/rdf-turtle
172
+ homepage: https://github.com/ruby-rdf/rdf-turtle
173
173
  licenses:
174
174
  - Unlicense
175
175
  metadata: {}
176
- post_install_message:
176
+ post_install_message:
177
177
  rdoc_options: []
178
178
  require_paths:
179
179
  - lib
@@ -181,15 +181,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
181
181
  requirements:
182
182
  - - ">="
183
183
  - !ruby/object:Gem::Version
184
- version: '2.4'
184
+ version: '2.6'
185
185
  required_rubygems_version: !ruby/object:Gem::Requirement
186
186
  requirements:
187
187
  - - ">="
188
188
  - !ruby/object:Gem::Version
189
189
  version: '0'
190
190
  requirements: []
191
- rubygems_version: 3.0.6
192
- signing_key:
191
+ rubygems_version: 3.3.3
192
+ signing_key:
193
193
  specification_version: 4
194
194
  summary: Turtle reader/writer for Ruby.
195
195
  test_files: []