-
Notifications
You must be signed in to change notification settings - Fork 2
/
format.rb
56 lines (54 loc) · 2.24 KB
/
format.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module RDF::TriG
##
# TriG format specification.
#
# @example Obtaining an TriG format class
# RDF::Format.for("etc/foaf.trig")
# RDF::Format.for(:file_name => "etc/foaf.trig")
# RDF::Format.for(file_extension: "trig")
# RDF::Format.for(:content_type => "application/trig")
#
# @example Obtaining serialization format MIME types
# RDF::Format.content_types #=> {"application/trig" => [RDF::TriG::Format]}
#
# @example Obtaining serialization format file extension mappings
# RDF::Format.file_extensions #=> {trig: "application/trig"}
#
# @see https://www.w3.org/TR/rdf-testcases/#ntriples
class Format < RDF::Format
content_type 'application/trig',
extension: :trig,
uri: 'http://www.w3.org/ns/formats/TriG',
alias: 'application/x-trig;q=0.2'
content_encoding 'utf-8'
reader { RDF::TriG::Reader }
writer { RDF::TriG::Writer }
##
# Sample detection to see if it matches TriG
#
# Use a text sample to detect the format of an input file. Sub-classes implement
# a matcher sufficient to detect probably format matches, including disambiguating
# between other similar formats.
#
# @param [String] sample Beginning several bytes (~ 1K) of input.
# @return [Boolean]
def self.detect(sample)
!!sample.match(%r(
(?:
(?:\s*(?:(?:<[^>]*>) | (?:\w*:\w+) | (?:"[^"]*")))? # IRIref
\s*\{ # Graph Start
(?:
(?:\s*(?:(?:<[^>]*>) | (?:\w*:\w+) | (?:"[^"]*"))\s*[,;]) ||
(?:\s*(?:(?:<[^>]*>) | (?:\w*:\w+) | (?:["']+[^"']*["']+))){3}
)* # triples
[\s\.]*\}\s* # Graph end
)
)mx) && !(
sample.match(%r(@keywords|=)) || # N3
sample.match(%r(<(?:\/|html|rdf))i) || # HTML, RDF/XML
sample.match(%r(^(?:\s*<[^>]*>){4}.*\.\s*$)) || # N-Quads
sample.match(%r("@(context|subject|iri)")) # JSON-LD
)
end
end
end