yaml-ld 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/AUTHORS +1 -0
- data/README.md +150 -0
- data/UNLICENSE +24 -0
- data/VERSION +1 -0
- data/lib/yaml_ld/api.rb +295 -0
- data/lib/yaml_ld/format.rb +56 -0
- data/lib/yaml_ld/reader.rb +40 -0
- data/lib/yaml_ld/version.rb +20 -0
- data/lib/yaml_ld/writer.rb +42 -0
- data/lib/yaml_ld.rb +37 -0
- data/spec/api_spec.rb +92 -0
- data/spec/compact_spec.rb +358 -0
- data/spec/expand_spec.rb +565 -0
- data/spec/flatten_spec.rb +225 -0
- data/spec/format_spec.rb +54 -0
- data/spec/frame_spec.rb +662 -0
- data/spec/from_rdf_spec.rb +730 -0
- data/spec/matchers.rb +22 -0
- data/spec/reader_spec.rb +138 -0
- data/spec/spec_helper.rb +265 -0
- data/spec/support/extensions.rb +44 -0
- data/spec/test-files/test-1-compacted.jsonld +10 -0
- data/spec/test-files/test-1-context.jsonld +7 -0
- data/spec/test-files/test-1-expanded.jsonld +5 -0
- data/spec/test-files/test-1-input.yamlld +8 -0
- data/spec/test-files/test-1-rdf.ttl +8 -0
- data/spec/test-files/test-2-compacted.jsonld +20 -0
- data/spec/test-files/test-2-context.jsonld +7 -0
- data/spec/test-files/test-2-expanded.jsonld +16 -0
- data/spec/test-files/test-2-input.yamlld +16 -0
- data/spec/test-files/test-2-rdf.ttl +14 -0
- data/spec/test-files/test-3-compacted.jsonld +11 -0
- data/spec/test-files/test-3-context.jsonld +8 -0
- data/spec/test-files/test-3-expanded.jsonld +10 -0
- data/spec/test-files/test-3-input.yamlld +13 -0
- data/spec/test-files/test-3-rdf.ttl +8 -0
- data/spec/test-files/test-4-compacted.jsonld +10 -0
- data/spec/test-files/test-4-context.jsonld +7 -0
- data/spec/test-files/test-4-expanded.jsonld +6 -0
- data/spec/test-files/test-4-input.yamlld +9 -0
- data/spec/test-files/test-4-rdf.ttl +5 -0
- data/spec/test-files/test-5-compacted.jsonld +13 -0
- data/spec/test-files/test-5-context.jsonld +7 -0
- data/spec/test-files/test-5-expanded.jsonld +9 -0
- data/spec/test-files/test-5-input.yamlld +10 -0
- data/spec/test-files/test-5-rdf.ttl +7 -0
- data/spec/test-files/test-6-compacted.jsonld +10 -0
- data/spec/test-files/test-6-context.jsonld +7 -0
- data/spec/test-files/test-6-expanded.jsonld +10 -0
- data/spec/test-files/test-6-input.yamlld +12 -0
- data/spec/test-files/test-6-rdf.ttl +6 -0
- data/spec/test-files/test-7-compacted.jsonld +23 -0
- data/spec/test-files/test-7-context.jsonld +4 -0
- data/spec/test-files/test-7-expanded.jsonld +20 -0
- data/spec/test-files/test-7-input.yamlld +16 -0
- data/spec/test-files/test-7-rdf.ttl +14 -0
- data/spec/test-files/test-8-compacted.jsonld +34 -0
- data/spec/test-files/test-8-context.jsonld +11 -0
- data/spec/test-files/test-8-expanded.jsonld +24 -0
- data/spec/test-files/test-8-frame.jsonld +18 -0
- data/spec/test-files/test-8-framed.jsonld +25 -0
- data/spec/test-files/test-8-input.yamlld +24 -0
- data/spec/test-files/test-8-rdf.ttl +15 -0
- data/spec/test-files/test-9-compacted.jsonld +20 -0
- data/spec/test-files/test-9-context.jsonld +13 -0
- data/spec/test-files/test-9-expanded.jsonld +14 -0
- data/spec/test-files/test-9-input.yamlld +16 -0
- data/spec/to_rdf_spec.rb +556 -0
- data/spec/writer_spec.rb +441 -0
- metadata +350 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c57dc93098e9dbf2ab68a8ffd6342fe1b838015e1335496ed918293d1fee4381
|
4
|
+
data.tar.gz: d1f7c955b184e680448354bab9f3b4ced1c0f11150f7c9cec67ec6feed4148fd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4917c179d4ef8c0d1f472e9e2be96fbfb82a76d3b554f2a90fdc610864a4ff0355e3a80806d2bf02de75831f84d17bebc056673d41f4ccf3e7cd2d80726864cd
|
7
|
+
data.tar.gz: f8d302927836ecfaf4c8e7d6afb209071e772fbd6e75a5622683fd35fbf2f54a123d8e73752934e9d441c791cb6e712e05c0b0b39f0cb5105e9383008f2712be
|
data/AUTHORS
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
* Gregg Kellogg <gregg@greggkellogg.net>
|
data/README.md
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
# YAML-LD reader/writer
|
2
|
+
|
3
|
+
Ruby [YAML-LD][] reader/writer for RDF.rb
|
4
|
+
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/yaml-ld.png)](https://rubygems.org/gems/yaml-ld)
|
6
|
+
[![Build Status](https://secure.travis-ci.org/ruby-rdf/yaml-ld.png?branch=develop)](https://github.com/ruby-rdf/yaml-ld/actions?query=workflow%3ACI)
|
7
|
+
[![Coverage Status](https://coveralls.io/repos/ruby-rdf/yaml-ld/badge.svg?branch=develop)](https://coveralls.io/github/ruby-rdf/yaml-ld?branch=develop)
|
8
|
+
[![Gitter chat](https://badges.gitter.im/ruby-rdf.png)](https://gitter.im/gitterHQ/gitter)
|
9
|
+
|
10
|
+
## Features
|
11
|
+
|
12
|
+
YAML_LD parses and serializes [YAML-LD][] into [RDF][].
|
13
|
+
|
14
|
+
As the specification is under development, this gem should be considered **experimental** and is subject to change at any time.
|
15
|
+
|
16
|
+
YAML-LD documents may use frames or contexts described either using [JSON-LD][] or [YAML-LD][].
|
17
|
+
|
18
|
+
* Process YAML-LD source using JSON-LD Context or Frame.
|
19
|
+
* Process JSON-LD source using YAML-LD Context or Frame.
|
20
|
+
|
21
|
+
## Examples
|
22
|
+
|
23
|
+
require 'rubygems'
|
24
|
+
require 'yaml\_ld'
|
25
|
+
|
26
|
+
### Expand a YAML-LD Document
|
27
|
+
|
28
|
+
input = StringIO.new(%(
|
29
|
+
"@context":
|
30
|
+
"@vocab": http://xmlns.com/foaf/0.1/
|
31
|
+
name: Gregg Kellogg
|
32
|
+
homepage: https://greggkellogg.net
|
33
|
+
depiction: http://www.gravatar.com/avatar/42f948adff3afaa52249d963117af7c8
|
34
|
+
))
|
35
|
+
|
36
|
+
YAML_LD::API.expand(input) # => %(
|
37
|
+
%YAML 1.2
|
38
|
+
---
|
39
|
+
- http://xmlns.com/foaf/0.1/name:
|
40
|
+
- "@value": Gregg Kellogg
|
41
|
+
http://xmlns.com/foaf/0.1/homepage:
|
42
|
+
- "@value": https://greggkellogg.net
|
43
|
+
http://xmlns.com/foaf/0.1/depiction:
|
44
|
+
- "@value": http://www.gravatar.com/avatar/42f948adff3afaa52249d963117af7c8
|
45
|
+
)
|
46
|
+
|
47
|
+
### Expand a YAML-LD Document to JSON-LD
|
48
|
+
|
49
|
+
input = StringIO.new(%(
|
50
|
+
"@context":
|
51
|
+
"@vocab": http://xmlns.com/foaf/0.1/
|
52
|
+
name: Gregg Kellogg
|
53
|
+
homepage: https://greggkellogg.net
|
54
|
+
depiction: http://www.gravatar.com/avatar/42f948adff3afaa52249d963117af7c8
|
55
|
+
))
|
56
|
+
|
57
|
+
YAML_LD::API.expand(input, serializer: JSON::LD::API.method(:serializer)) # => %(
|
58
|
+
[
|
59
|
+
{
|
60
|
+
"http://xmlns.com/foaf/0.1/name": [{ "@value": "Gregg Kellogg" }],
|
61
|
+
"http://xmlns.com/foaf/0.1/homepage": [{ "@value": "https://greggkellogg.net" }],
|
62
|
+
"http://xmlns.com/foaf/0.1/depiction": [{
|
63
|
+
"@value": "http://www.gravatar.com/avatar/42f948adff3afaa52249d963117af7c8"
|
64
|
+
}]
|
65
|
+
}
|
66
|
+
]
|
67
|
+
)
|
68
|
+
|
69
|
+
### Expand a JSON-LD Document to YAML-LD
|
70
|
+
|
71
|
+
input = StringIO.new(%(
|
72
|
+
{
|
73
|
+
"@context": {
|
74
|
+
"@vocab":"http://xmlns.com/foaf/0.1/"
|
75
|
+
},
|
76
|
+
"name": "Gregg Kellogg",
|
77
|
+
"homepage": "https://greggkellogg.net",
|
78
|
+
"depiction": "http://www.gravatar.com/avatar/42f948adff3afaa52249d963117af7c8"
|
79
|
+
}
|
80
|
+
))
|
81
|
+
|
82
|
+
YAML_LD::API.expand(input, content_type: 'application/ld+json') # => %(
|
83
|
+
%YAML 1.2
|
84
|
+
---
|
85
|
+
- http://xmlns.com/foaf/0.1/name:
|
86
|
+
- "@value": Gregg Kellogg
|
87
|
+
http://xmlns.com/foaf/0.1/homepage:
|
88
|
+
- "@value": https://greggkellogg.net
|
89
|
+
http://xmlns.com/foaf/0.1/depiction:
|
90
|
+
- "@value": http://www.gravatar.com/avatar/42f948adff3afaa52249d963117af7c8
|
91
|
+
)
|
92
|
+
|
93
|
+
## Implementation
|
94
|
+
|
95
|
+
The gem largely acts as a front-end for the [JSON-LD gem][] with differences largely in the serialization format only.
|
96
|
+
|
97
|
+
In addition to the input, both a `context` and `frame` may be specified using either JSON-LD or YAML-LD.
|
98
|
+
|
99
|
+
## Dependencies
|
100
|
+
* [Ruby](https://ruby-lang.org/) (>= 2.6)
|
101
|
+
* [JSON-LD](https://rubygems.org/gems/json-ld) (>= 3.2.2)
|
102
|
+
* [Psych](https://rubygems.org/gems/psych) (>= 4.0)
|
103
|
+
* [RDF.rb](https://rubygems.org/gems/rdf) (~> 3.2)
|
104
|
+
|
105
|
+
## Installation
|
106
|
+
The recommended installation method is via [RubyGems](https://rubygems.org/).
|
107
|
+
To install the latest official release of the `JSON-LD` gem, do:
|
108
|
+
|
109
|
+
% [sudo] gem install yaml-ld
|
110
|
+
|
111
|
+
## Download
|
112
|
+
To get a local working copy of the development repository, do:
|
113
|
+
|
114
|
+
% git clone git://github.com/ruby-rdf/yaml-ld.git
|
115
|
+
|
116
|
+
## Mailing List
|
117
|
+
* <https://lists.w3.org/Archives/Public/public-rdf-ruby/>
|
118
|
+
|
119
|
+
## Author
|
120
|
+
* [Gregg Kellogg](https://github.com/gkellogg) - <https://greggkellogg.net/>
|
121
|
+
|
122
|
+
## Contributing
|
123
|
+
* Do your best to adhere to the existing coding conventions and idioms.
|
124
|
+
* Don't use hard tabs, and don't leave trailing whitespace on any line.
|
125
|
+
* Do document every method you add using [YARD][] annotations. Read the
|
126
|
+
[tutorial][YARD-GS] or just look at the existing code for examples.
|
127
|
+
* Don't touch the `json-ld.gemspec`, `VERSION` or `AUTHORS` files. If you need to
|
128
|
+
change them, do so on your private branch only.
|
129
|
+
* Do feel free to add yourself to the `CREDITS` file and the corresponding
|
130
|
+
list in the the `README`. Alphabetical order applies.
|
131
|
+
* Do note that in order for us to merge any non-trivial changes (as a rule
|
132
|
+
of thumb, additions larger than about 15 lines of code), we need an
|
133
|
+
explicit [public domain dedication][PDD] on record from you,
|
134
|
+
which you will be asked to agree to on the first commit to a repo within the organization.
|
135
|
+
Note that the agreement applies to all repos in the [Ruby RDF](https://github.com/ruby-rdf/) organization.
|
136
|
+
|
137
|
+
## License
|
138
|
+
|
139
|
+
This is free and unencumbered public domain software. For more information,
|
140
|
+
see <https://unlicense.org/> or the accompanying {file:UNLICENSE} file.
|
141
|
+
|
142
|
+
[Ruby]: https://ruby-lang.org/
|
143
|
+
[RDF]: https://www.w3.org/RDF/
|
144
|
+
[YARD]: https://yardoc.org/
|
145
|
+
[YARD-GS]: https://rubydoc.info/docs/yard/file/docs/GettingStarted.md
|
146
|
+
[PDD]: https://unlicense.org/#unlicensing-contributions
|
147
|
+
[RDF.rb]: https://rubygems.org/gems/rdf
|
148
|
+
[JSON-LD gem]: https://rubygems.org/gems/json-ld
|
149
|
+
[JSON-LD]: https://www.w3.org/TR/json-ld11/ "JSON-LD 1.1"
|
150
|
+
[YAML-LD]: https://json-ld.github.io/yaml-ld/spec/
|
data/UNLICENSE
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
2
|
+
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
4
|
+
distribute this software, either in source code form or as a compiled
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
6
|
+
means.
|
7
|
+
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
9
|
+
of this software dedicate any and all copyright interest in the
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
11
|
+
of the public at large and to the detriment of our heirs and
|
12
|
+
successors. We intend this dedication to be an overt act of
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
14
|
+
software under copyright law.
|
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 NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
For more information, please refer to <https://unlicense.org>
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/yaml_ld/api.rb
ADDED
@@ -0,0 +1,295 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
require 'json/ld/api'
|
4
|
+
|
5
|
+
module YAML_LD
|
6
|
+
##
|
7
|
+
# A YAML-LD processor based on JSON-LD.
|
8
|
+
#
|
9
|
+
# @see https://www.w3.org/TR/json-ld11-api/#the-application-programming-interface
|
10
|
+
# @author [Gregg Kellogg](http://greggkellogg.net/)
|
11
|
+
class API < ::JSON::LD::API
|
12
|
+
|
13
|
+
# The following constants are used to reduce object allocations
|
14
|
+
LINK_REL_CONTEXT = %w(rel http://www.w3.org/ns/yaml-ld#context).freeze
|
15
|
+
LINK_REL_ALTERNATE = %w(rel alternate).freeze
|
16
|
+
LINK_TYPE_JSONLD = %w(type application/ld+yaml).freeze
|
17
|
+
|
18
|
+
##
|
19
|
+
# Expands the given input according to the steps in the Expansion Algorithm. The input must be copied, expanded and returned if there are no errors. If the expansion fails, an appropriate exception must be thrown.
|
20
|
+
#
|
21
|
+
# The resulting `Array` either returned or yielded
|
22
|
+
#
|
23
|
+
# @param [String, #read, Hash, Array] input
|
24
|
+
# The YAML-LD object to copy and perform the expansion upon.
|
25
|
+
# @param [Proc] documentLoader
|
26
|
+
# The callback of the loader to be used to retrieve remote documents and contexts, and to parse IO objects.
|
27
|
+
# If specified, it must be used to retrieve remote documents and contexts; otherwise, if not specified, the processor's built-in loader must be used. See {documentLoader} for the method signature.
|
28
|
+
# The remote document returned must be parsed if it is YAML.
|
29
|
+
# @param [Proc] serializer (nil)
|
30
|
+
# A Serializer method used for generating the YAML serialization of the result. If absent, the internal Ruby objects are returned, which can be transformed to YAML externally via `#to_yaml`.
|
31
|
+
# See {YAML_LD::API.serializer}.
|
32
|
+
# @param [Hash{Symbol => Object}] options
|
33
|
+
# @raise [JsonLdError]
|
34
|
+
# @yield YAML_LD, base_iri
|
35
|
+
# @yieldparam [Array<Hash>] yamlld
|
36
|
+
# The expanded YAML-LD document
|
37
|
+
# @yieldparam [RDF::URI
|
38
|
+
# The document base as determined during expansion
|
39
|
+
# @yieldreturn [String] returned YAML serialization
|
40
|
+
# @return [String]
|
41
|
+
# If a block is given, the result of evaluating the block is returned, otherwise, the expanded YAML-LD document
|
42
|
+
# @see https://www.w3.org/TR/json-ld11-api/#expansion-algorithm
|
43
|
+
def self.expand(input,
|
44
|
+
documentLoader: self.method(:documentLoader),
|
45
|
+
serializer: self.method(:serializer),
|
46
|
+
**options,
|
47
|
+
&block)
|
48
|
+
JSON::LD::API.expand(input,
|
49
|
+
documentLoader: documentLoader,
|
50
|
+
serializer: serializer,
|
51
|
+
**options,
|
52
|
+
&block)
|
53
|
+
end
|
54
|
+
|
55
|
+
##
|
56
|
+
# Compacts the given input according to the steps in the Compaction Algorithm. The input must be copied, compacted and returned if there are no errors. If the compaction fails, an appropirate exception must be thrown.
|
57
|
+
#
|
58
|
+
# If no context is provided, the input document is compacted using the top-level context of the document
|
59
|
+
#
|
60
|
+
# The resulting `Hash` is either returned or yielded, if a block is given.
|
61
|
+
#
|
62
|
+
# @param [String, #read, Hash, Array] input
|
63
|
+
# The YAML-LD object to copy and perform the expansion upon.
|
64
|
+
# @param [String, #read, Hash, Array, JSON::LD::Context] context
|
65
|
+
# The base context to use when compacting the input.
|
66
|
+
# @param [Proc] serializer (nil)
|
67
|
+
# A Serializer method used for generating the YAML serialization of the result. If absent, the internal Ruby objects are returned, which can be transformed to YAML externally via `#to_yaml`.
|
68
|
+
# See {YAML_LD::API.serializer}.
|
69
|
+
# @param [Boolean] expanded (false) Input is already expanded
|
70
|
+
# @param [Hash{Symbol => Object}] options
|
71
|
+
# @option options (see #initialize)
|
72
|
+
# @yield YAML_LD
|
73
|
+
# @yieldparam [Array<Hash>] yamlld
|
74
|
+
# The expanded YAML-LD document
|
75
|
+
# @yieldreturn [String] returned YAML serialization
|
76
|
+
# @return [String]
|
77
|
+
# If a block is given, the result of evaluating the block is returned, otherwise, the expanded YAML-LD document
|
78
|
+
# @raise [JsonLdError]
|
79
|
+
# @see https://www.w3.org/TR/json-ld11-api/#compaction-algorithm
|
80
|
+
def self.compact(input, context, expanded: false,
|
81
|
+
documentLoader: self.method(:documentLoader),
|
82
|
+
serializer: self.method(:serializer),
|
83
|
+
**options,
|
84
|
+
&block)
|
85
|
+
JSON::LD::API.compact(input, context, expanded: expanded,
|
86
|
+
documentLoader: documentLoader,
|
87
|
+
serializer: serializer,
|
88
|
+
**options,
|
89
|
+
&block)
|
90
|
+
end
|
91
|
+
|
92
|
+
##
|
93
|
+
# This algorithm flattens an expanded YAML-LD document by collecting all properties of a node in a single object and labeling all blank nodes with blank node identifiers. This resulting uniform shape of the document, may drastically simplify the code required to process YAML-LD data in certain applications.
|
94
|
+
#
|
95
|
+
# The resulting `Array` is either returned, or yielded if a block is given.
|
96
|
+
#
|
97
|
+
# @param [String, #read, Hash, Array] input
|
98
|
+
# The YAML-LD object or array of JSON-LD objects to flatten or an IRI referencing the JSON-LD document to flatten.
|
99
|
+
# @param [String, #read, Hash, Array, JSON::LD::EvaluationContext] context
|
100
|
+
# An optional external context to use additionally to the context embedded in input when expanding the input.
|
101
|
+
# @param [Boolean] expanded (false) Input is already expanded
|
102
|
+
# @param [Proc] serializer (nil)
|
103
|
+
# A Serializer method used for generating the YAML serialization of the result. If absent, the internal Ruby objects are returned, which can be transformed to YAML externally via `#to_yaml`.
|
104
|
+
# See {YAML_LD::API.serializer}.
|
105
|
+
# @param [Hash{Symbol => Object}] options
|
106
|
+
# @option options (see #initialize)
|
107
|
+
# @option options [Boolean] :createAnnotations
|
108
|
+
# Unfold embedded nodes which can be represented using `@annotation`.
|
109
|
+
# @yield YAML_LD
|
110
|
+
# @yieldparam [Array<Hash>] yamlld
|
111
|
+
# The expanded YAML-LD document
|
112
|
+
# @yieldreturn [String] returned YAML serialization
|
113
|
+
# @return [Object, Hash]
|
114
|
+
# If a block is given, the result of evaluating the block is returned, otherwise, the flattened JSON-LD document
|
115
|
+
# @see https://www.w3.org/TR/json-ld11-api/#framing-algorithm
|
116
|
+
def self.flatten(input, context, expanded: false,
|
117
|
+
documentLoader: self.method(:documentLoader),
|
118
|
+
serializer: self.method(:serializer),
|
119
|
+
**options,
|
120
|
+
&block)
|
121
|
+
JSON::LD::API.flatten(input, context, expanded: expanded,
|
122
|
+
documentLoader: documentLoader,
|
123
|
+
serializer: serializer,
|
124
|
+
**options,
|
125
|
+
&block)
|
126
|
+
end
|
127
|
+
|
128
|
+
##
|
129
|
+
# Frames the given input using the frame according to the steps in the Framing Algorithm. The input is used to build the framed output and is returned if there are no errors. If there are no matches for the frame, null must be returned. Exceptions must be thrown if there are errors.
|
130
|
+
#
|
131
|
+
# The resulting `Array` is either returned, or yielded if a block is given.
|
132
|
+
#
|
133
|
+
# @param [String, #read, Hash, Array] input
|
134
|
+
# The YAML-LD object or array of YAML-LD objects to flatten or an IRI referencing the JSON-LD document to flatten.
|
135
|
+
# @param [String, #read, Hash, Array] frame
|
136
|
+
# The frame to use when re-arranging the data.
|
137
|
+
# @param [Boolean] expanded (false) Input is already expanded
|
138
|
+
# @option options (see #initialize)
|
139
|
+
# @option options ['@always', '@link', '@once', '@never'] :embed ('@once')
|
140
|
+
# a flag specifying that objects should be directly embedded in the output, instead of being referred to by their IRI.
|
141
|
+
# @option options [Boolean] :explicit (false)
|
142
|
+
# a flag specifying that for properties to be included in the output, they must be explicitly declared in the framing context.
|
143
|
+
# @option options [Boolean] :requireAll (false)
|
144
|
+
# A flag specifying that all properties present in the input frame must either have a default value or be present in the JSON-LD input for the frame to match.
|
145
|
+
# @option options [Boolean] :omitDefault (false)
|
146
|
+
# a flag specifying that properties that are missing from the JSON-LD input should be omitted from the output.
|
147
|
+
# @option options [Boolean] :pruneBlankNodeIdentifiers (true) removes blank node identifiers that are only used once.
|
148
|
+
# @option options [Boolean] :omitGraph does not use `@graph` at top level unless necessary to describe multiple objects, defaults to `true` if processingMode is 1.1, otherwise `false`.
|
149
|
+
# @yield YAML_LD
|
150
|
+
# @yieldparam [Array<Hash>] yamlld
|
151
|
+
# The expanded YAML-LD document
|
152
|
+
# @yieldreturn [String] returned YAML serialization
|
153
|
+
# @return [Object, Hash]
|
154
|
+
# If a block is given, the result of evaluating the block is returned, otherwise, the framed JSON-LD document
|
155
|
+
# @raise [InvalidFrame]
|
156
|
+
# @see https://www.w3.org/TR/json-ld11-api/#framing-algorithm
|
157
|
+
def self.frame(input, frame, expanded: false,
|
158
|
+
documentLoader: self.method(:documentLoader),
|
159
|
+
serializer: self.method(:serializer),
|
160
|
+
**options,
|
161
|
+
&block)
|
162
|
+
JSON::LD::API.frame(input, frame, expanded: expanded,
|
163
|
+
documentLoader: documentLoader,
|
164
|
+
serializer: serializer,
|
165
|
+
**options,
|
166
|
+
&block)
|
167
|
+
end
|
168
|
+
|
169
|
+
##
|
170
|
+
# Processes the input according to the RDF Conversion Algorithm, calling the provided callback for each triple generated.
|
171
|
+
#
|
172
|
+
# @param [String, #read, Hash, Array] input
|
173
|
+
# The YAML-LD object to process when outputting statements.
|
174
|
+
# @param [Boolean] expanded (false) Input is already expanded
|
175
|
+
# @option options (see #initialize)
|
176
|
+
# @option options [Boolean] :produceGeneralizedRdf (false)
|
177
|
+
# If true, output will include statements having blank node predicates, otherwise they are dropped.
|
178
|
+
# @raise [JsonLdError]
|
179
|
+
# @yield statement
|
180
|
+
# @yieldparam [RDF::Statement] statement
|
181
|
+
# @return [RDF::Enumerable] set of statements, unless a block is given.
|
182
|
+
def self.toRdf(input, expanded: false,
|
183
|
+
documentLoader: self.method(:documentLoader),
|
184
|
+
**options,
|
185
|
+
&block)
|
186
|
+
JSON::LD::API.toRdf(input, expanded: expanded,
|
187
|
+
documentLoader: documentLoader,
|
188
|
+
**options,
|
189
|
+
&block)
|
190
|
+
end
|
191
|
+
|
192
|
+
##
|
193
|
+
# Take an ordered list of RDF::Statements and turn them into a JSON-LD document.
|
194
|
+
#
|
195
|
+
# The resulting `Array` is either returned or yielded, if a block is given.
|
196
|
+
#
|
197
|
+
# @param [RDF::Enumerable] input
|
198
|
+
# @param [Boolean] useRdfType (false)
|
199
|
+
# If set to `true`, the JSON-LD processor will treat `rdf:type` like a normal property instead of using `@type`.
|
200
|
+
# @param [Boolean] useNativeTypes (false) use native representations
|
201
|
+
# @param [Proc] serializer (nil)
|
202
|
+
# A Serializer method used for generating the YAML serialization of the result. If absent, the internal Ruby objects are returned, which can be transformed to YAML externally via `#to_yaml`.
|
203
|
+
# See {YAML_LD::API.serializer}.
|
204
|
+
# @param [Hash{Symbol => Object}] options
|
205
|
+
# @option options (see #initialize)
|
206
|
+
# @yield jsonld
|
207
|
+
# @yieldparam [Hash] jsonld
|
208
|
+
# The JSON-LD document in expanded form
|
209
|
+
# @yieldreturn [Object] returned object
|
210
|
+
# @return [Object, Hash]
|
211
|
+
# If a block is given, the result of evaluating the block is returned, otherwise, the expanded JSON-LD document
|
212
|
+
def self.fromRdf(input, useRdfType: false, useNativeTypes: false,
|
213
|
+
documentLoader: self.method(:documentLoader),
|
214
|
+
serializer: self.method(:serializer),
|
215
|
+
**options,
|
216
|
+
&block)
|
217
|
+
JSON::LD::API.fromRdf(input,
|
218
|
+
useRdfType: useRdfType,
|
219
|
+
useNativeTypes: useNativeTypes,
|
220
|
+
documentLoader: documentLoader,
|
221
|
+
serializer: serializer,
|
222
|
+
**options,
|
223
|
+
&block)
|
224
|
+
end
|
225
|
+
|
226
|
+
##
|
227
|
+
# Default document loader for YAML_LD.
|
228
|
+
# @param [RDF::URI, String] url
|
229
|
+
# @param [Boolean] extractAllScripts
|
230
|
+
# If set to `true`, when extracting JSON-LD script elements from HTML, unless a specific fragment identifier is targeted, extracts all encountered JSON-LD script elements using an array form, if necessary.
|
231
|
+
# @param [String] profile
|
232
|
+
# When the resulting `contentType` is `text/html` or `application/xhtml+xml`, this option determines the profile to use for selecting a JSON-LD script elements.
|
233
|
+
# @param [String] requestProfile
|
234
|
+
# One or more IRIs to use in the request as a profile parameter.
|
235
|
+
# @param [Hash<Symbol => Object>] options
|
236
|
+
# @yield remote_document
|
237
|
+
# @yieldparam [RemoteDocument, RDF::Util::File::RemoteDocument] remote_document
|
238
|
+
# @raise [IOError]
|
239
|
+
def self.documentLoader(url, extractAllScripts: false, profile: nil, requestProfile: nil, **options, &block)
|
240
|
+
if url.respond_to?(:read)
|
241
|
+
base_uri = options[:base]
|
242
|
+
base_uri ||= url.base_uri if url.respond_to?(:base_uri)
|
243
|
+
content_type = options[:content_type]
|
244
|
+
content_type ||= url.content_type if url.respond_to?(:content_type)
|
245
|
+
context_url = if url.respond_to?(:links) && url.links &&
|
246
|
+
# Any JSON type other than ld+json
|
247
|
+
(content_type == 'application/json' || content_type.match?(%r(application/(^ld)+json)))
|
248
|
+
link = url.links.find_link(JSON::LD::API::LINK_REL_CONTEXT)
|
249
|
+
link.href if link
|
250
|
+
elsif url.respond_to?(:links) && url.links &&
|
251
|
+
# Any YAML type
|
252
|
+
content_type.match?(%r(application/(\w+\+)*yaml))
|
253
|
+
link = url.links.find_link(LINK_REL_CONTEXT)
|
254
|
+
link.href if link
|
255
|
+
end
|
256
|
+
|
257
|
+
content = case content_type
|
258
|
+
when nil, %r(application/(\w+\+)*yaml)
|
259
|
+
# Parse YAML
|
260
|
+
Psych.safe_load(url.read, aliases: true)
|
261
|
+
else
|
262
|
+
url.read
|
263
|
+
end
|
264
|
+
block.call(RemoteDocument.new(content,
|
265
|
+
documentUrl: base_uri,
|
266
|
+
contentType: content_type,
|
267
|
+
contextUrl: context_url))
|
268
|
+
elsif url.to_s.match?(/\.yaml\w*$/) || content_type.to_s.match?(%r(application/(\w+\+)*yaml))
|
269
|
+
# Parse YAML
|
270
|
+
block.call(RemoteDocument.new(Psych.load_file(url.to_s, aliases: true),
|
271
|
+
documentUrl: base_uri,
|
272
|
+
contentType: content_type,
|
273
|
+
contextUrl: context_url))
|
274
|
+
else
|
275
|
+
RDF::Util::File.open_file(url, **options, &block)
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
##
|
280
|
+
# The default serializer for serialzing Ruby Objects to JSON.
|
281
|
+
#
|
282
|
+
# Defaults to `MultiJson.dump`
|
283
|
+
#
|
284
|
+
# @param [Object] object
|
285
|
+
# @param [Array<Object>] args
|
286
|
+
# other arguments that may be passed for some specific implementation.
|
287
|
+
# @param [Hash<Symbol, Object>] options
|
288
|
+
# options passed from the invoking context.
|
289
|
+
def self.serializer(object, *args, **options)
|
290
|
+
# de-alias any objects to avoid the use of aliases and anchors
|
291
|
+
"%YAML 1.2\n" + Psych.dump(object, **options)
|
292
|
+
end
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
module YAML_LD
|
4
|
+
##
|
5
|
+
# YAML-LD format specification.
|
6
|
+
#
|
7
|
+
# @example Obtaining an YAML-LD format class
|
8
|
+
# RDF::Format.for(:YAML_LD) #=> YAML_LD::Format
|
9
|
+
# RDF::Format.for("etc/foaf.YAML_LD")
|
10
|
+
# RDF::Format.for(:file_name => "etc/foaf.YAML_LD")
|
11
|
+
# RDF::Format.for(file_extension: "YAML_LD")
|
12
|
+
# RDF::Format.for(:content_type => "application/ld+yaml")
|
13
|
+
#
|
14
|
+
# @example Obtaining serialization format MIME types
|
15
|
+
# RDF::Format.content_types #=> {"application/ld+yaml" => [YAML_LD::Format],
|
16
|
+
#
|
17
|
+
# @example Obtaining serialization format file extension mappings
|
18
|
+
# RDF::Format.file_extensions #=> {:YAML_LD => [YAML_LD::Format] }
|
19
|
+
#
|
20
|
+
# @see https://json-ld.github.io/yaml-ld/spec/
|
21
|
+
# @see https://json-ld.github.io/yaml/tests/
|
22
|
+
class Format < RDF::Format
|
23
|
+
content_type 'application/ld+yaml',
|
24
|
+
extension: :yamlld,
|
25
|
+
uri: 'http://www.w3.org/ns/formats/YAML-LD'
|
26
|
+
content_encoding 'utf-8'
|
27
|
+
|
28
|
+
reader { YAML_LD::Reader }
|
29
|
+
writer { YAML_LD::Writer }
|
30
|
+
|
31
|
+
##
|
32
|
+
# Sample detection to see if it matches YAML-LD
|
33
|
+
#
|
34
|
+
# Use a text sample to detect the format of an input file. Sub-classes implement
|
35
|
+
# a matcher sufficient to detect probably format matches, including disambiguating
|
36
|
+
# between other similar formats.
|
37
|
+
#
|
38
|
+
# @param [String] sample Beginning several bytes (~ 1K) of input.
|
39
|
+
# @return [Boolean]
|
40
|
+
def self.detect(sample)
|
41
|
+
!!sample.match(/---/m)
|
42
|
+
end
|
43
|
+
|
44
|
+
##
|
45
|
+
# Override normal symbol generation
|
46
|
+
def self.to_sym
|
47
|
+
:yamlld
|
48
|
+
end
|
49
|
+
|
50
|
+
##
|
51
|
+
# Override normal format name
|
52
|
+
def self.name
|
53
|
+
"YAML-LD"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require 'json/ld/reader'
|
3
|
+
|
4
|
+
module YAML_LD
|
5
|
+
##
|
6
|
+
# A YAML-LD parser in Ruby.
|
7
|
+
class Reader < JSON::LD::Reader
|
8
|
+
format Format
|
9
|
+
|
10
|
+
##
|
11
|
+
# Initializes the YAML-LD reader instance.
|
12
|
+
#
|
13
|
+
# @param [IO, File, String] input
|
14
|
+
# @param [Proc] documentLoader
|
15
|
+
# The callback of the loader to be used to retrieve remote documents and contexts, and to parse IO objects.
|
16
|
+
# If specified, it must be used to retrieve remote documents and contexts; otherwise, if not specified, the processor's built-in loader must be used.
|
17
|
+
# The remote document returned must be parsed if it is YAML.
|
18
|
+
# @param [Hash{Symbol => Object}] options
|
19
|
+
# any additional options (see `RDF::Reader#initialize` and `JSON::LD::API.initialize`)
|
20
|
+
# @yield [reader] `self`
|
21
|
+
# @yieldparam [RDF::Reader] reader
|
22
|
+
# @yieldreturn [void] ignored
|
23
|
+
# @raise [RDF::ReaderError] if the JSON document cannot be loaded
|
24
|
+
def initialize(input = $stdin,
|
25
|
+
documentLoader: YAML_LD::API.method(:documentLoader),
|
26
|
+
**options, &block)
|
27
|
+
input = StringIO.new(input) if input.is_a?(String)
|
28
|
+
super(input, documentLoader: documentLoader, **options, &block)
|
29
|
+
end
|
30
|
+
|
31
|
+
##
|
32
|
+
# @private
|
33
|
+
# @see RDF::Reader#each_statement
|
34
|
+
def each_statement(&block)
|
35
|
+
API.toRdf(@doc, **@options, &block)
|
36
|
+
rescue ::Psych::SyntaxError, ::JSON::LD::JsonLdError => e
|
37
|
+
log_fatal("Failed to parse input document: #{e.message}", exception: RDF::ReaderError)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
module YAML_LD::VERSION
|
4
|
+
VERSION_FILE = File.join(File.expand_path(File.dirname(__FILE__)), "..", "..", "..", "VERSION")
|
5
|
+
MAJOR, MINOR, TINY, EXTRA = File.read(VERSION_FILE).chomp.split(".")
|
6
|
+
|
7
|
+
STRING = [MAJOR, MINOR, TINY, EXTRA].compact.join('.')
|
8
|
+
|
9
|
+
##
|
10
|
+
# @return [String]
|
11
|
+
def self.to_s() STRING end
|
12
|
+
|
13
|
+
##
|
14
|
+
# @return [String]
|
15
|
+
def self.to_str() STRING end
|
16
|
+
|
17
|
+
##
|
18
|
+
# @return [Array(Integer, Integer, Integer)]
|
19
|
+
def self.to_a() STRING.split(".") end
|
20
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require 'json/ld/writer'
|
3
|
+
|
4
|
+
module YAML_LD
|
5
|
+
##
|
6
|
+
# A YAML-LD serializer in Ruby.
|
7
|
+
class Writer < JSON::LD::Writer
|
8
|
+
##
|
9
|
+
# Initializes the YAML-LD writer instance.
|
10
|
+
#
|
11
|
+
# @param [IO, File] output
|
12
|
+
# the output stream
|
13
|
+
# @param [Hash{Symbol => Object}] options
|
14
|
+
# any additional options
|
15
|
+
# @option options [Encoding] :encoding (Encoding::UTF_8)
|
16
|
+
# the encoding to use on the output stream (Ruby 1.9+)
|
17
|
+
# @option options [Boolean] :canonicalize (false)
|
18
|
+
# whether to canonicalize literals when serializing
|
19
|
+
# @option options [Hash] :prefixes ({})
|
20
|
+
# the prefix mappings to use (not supported by all writers)
|
21
|
+
# @option options [Boolean] :standard_prefixes (false)
|
22
|
+
# Add standard prefixes to @prefixes, if necessary.
|
23
|
+
# @option options [IO, Array, Hash, String, Context] :context ({})
|
24
|
+
# context to use when serializing. Constructed context for native serialization.
|
25
|
+
# @option options [IO, Array, Hash, String, Context] :frame ({})
|
26
|
+
# frame to use when serializing.
|
27
|
+
# @option options [Boolean] :unique_bnodes (false)
|
28
|
+
# Use unique bnode identifiers, defaults to using the identifier which the node was originall initialized with (if any).
|
29
|
+
# @option options [Proc] serializer (YAML_LD::API.serializer)
|
30
|
+
# A Serializer method used for generating the YAML serialization of the result.
|
31
|
+
# @option options [Boolean] :stream (false)
|
32
|
+
# Do not attempt to optimize graph presentation, suitable for streaming large graphs.
|
33
|
+
# @yield [writer] `self`
|
34
|
+
# @yieldparam [RDF::Writer] writer
|
35
|
+
# @yieldreturn [void]
|
36
|
+
# @yield [writer]
|
37
|
+
# @yieldparam [RDF::Writer] writer
|
38
|
+
def initialize(output = $stdout, **options, &block)
|
39
|
+
super(output, **options.merge(serializer: YAML_LD::API.method(:serializer)), &block)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|