rdf-rdfa 0.3.3 → 0.3.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,7 +22,6 @@ module RDF::RDFa
22
22
  content_type 'text/html', :extension => :html
23
23
  reader { RDF::RDFa::Reader }
24
24
  writer { RDF::RDFa::Writer }
25
- XMLNS = 'http://www.w3.org/1999/xhtml' # FIXME: This or XHV or none at all?
26
25
  end
27
26
 
28
27
  # Aliases for RDFa::Format
@@ -12,6 +12,7 @@ module RDF::RDFa
12
12
  # @author [Gregg Kellogg](http://kellogg-assoc.com/)
13
13
  class Reader < RDF::Reader
14
14
  format Format
15
+ XHTML = "http://www.w3.org/1999/xhtml"
15
16
 
16
17
  SafeCURIEorCURIEorURI = {
17
18
  :"rdfa1.0" => [:term, :safe_curie, :uri, :bnode],
@@ -191,33 +192,20 @@ module RDF::RDFa
191
192
  @debug = options[:debug]
192
193
  @base_uri = uri(options[:base_uri])
193
194
 
194
- @host_language = options[:host_language]
195
+ detect_host_language_version(input, options)
196
+
195
197
  @processor_graph = options[:processor_graph]
196
198
 
197
199
  @doc = case input
198
- when Nokogiri::HTML::Document
199
- @host_language ||= :xhtml1
200
- when Nokogiri::XML::Document
200
+ when Nokogiri::HTML::Document, Nokogiri::XML::Document
201
201
  input
202
202
  else
203
- # Intuit from content type
204
- @host_language ||= case input.respond_to?(:content_type) && input.content_type
205
- when "text/xml", "application/xml"
206
- :xml1
207
- when "text/html", "application/xhtml+xml"
208
- :xhtml1
209
- when "image/svg+xml"
210
- :svg
211
- end
212
-
213
- # Intuit from file extension
214
- @host_language ||= case input.respond_to?(:path) && File.extname(input.path.to_s)
215
- when ".html" then :html5
216
- when ".xhtml" then :xhtml1
217
- when ".svg" then :svg
203
+ case @host_language
204
+ when :html4, :html5
205
+ Nokogiri::HTML.parse(input, @base_uri.to_s)
206
+ else
207
+ Nokogiri::XML.parse(input, @base_uri.to_s)
218
208
  end
219
-
220
- Nokogiri::XML.parse(input, @base_uri.to_s)
221
209
  end
222
210
 
223
211
  if (@doc.nil? || @doc.root.nil?)
@@ -226,33 +214,6 @@ module RDF::RDFa
226
214
  end
227
215
  add_warning(nil, "Synax errors:\n#{@doc.errors}", RDF::RDFA.DocumentError) if !@doc.errors.empty? && validate?
228
216
 
229
- @version = options[:version] ? options[:version].to_sym : nil
230
-
231
- # Check for version of the processor to use:
232
- # * Check document type for "XHTML+RDFa 1.0"
233
- # * Check @version attribute on the html element for the value "XHTML+RDFa 1.0"
234
- @version ||= :"rdfa1.0" if @doc.doctype.to_s =~ /RDFa 1\.0/
235
- @version ||= :"rdfa1.0" if @doc.root && @doc.root.attribute("version").to_s =~ /RDFa 1\.0/
236
- @version ||= :"rdfa1.1" if @doc.root && @doc.root.attribute("version").to_s =~ /RDFa 1\.1/
237
- @version ||= :"rdfa1.1"
238
-
239
- # Intuit host_language from doctype
240
- @host_language ||= case @doc.doctype.to_s
241
- when /html 4/i then :html4
242
- when /xhtml\+rdfa/i then :xhtml1
243
- when /html/ then :html5
244
- end
245
-
246
- # Determine host language from element name
247
- @host_language ||= case @doc.root.name.downcase.to_sym
248
- when :html then :xhtml1
249
- when :svg then :svg
250
- else :xml
251
- end
252
-
253
- # Otherwise, treat it as XML
254
- @host_language ||= :xml1
255
-
256
217
  # Section 4.2 RDFa Host Language Conformance
257
218
  #
258
219
  # The Host Language may require the automatic inclusion of one or more default RDFa Profiles.
@@ -284,6 +245,76 @@ module RDF::RDFa
284
245
  self.profile_repository = options[:profile_repository] if options[:profile_repository]
285
246
  end
286
247
 
248
+ # Determine the host language and/or version from options and the input document
249
+ def detect_host_language_version(input, options)
250
+ @host_language = options[:host_language] ? options[:host_language].to_sym : nil
251
+ @version = options[:version] ? options[:version].to_sym : nil
252
+ return if @host_language && @version
253
+
254
+ # Snif version based on input
255
+ case input
256
+ when Nokogiri::XML::Document, Nokogiri::HTML::Document
257
+ doc_type_string = input.doc_type.to_s
258
+ version_attr = input.root && @doc.root.attribute("version").to_s
259
+ root_element = input.root.name.downcase
260
+ root_namespace = input.root.namespace.to_s
261
+ root_attrs = input.root.attributes
262
+ content_type = case
263
+ when root_element == "html" && input.is_a?(Nokogiri::HTML::Document)
264
+ "text/html"
265
+ when root_element == "html" && input.is_a?(Nokogiri::XML::Document)
266
+ "application/xhtml+html"
267
+ end
268
+ else
269
+ content_type = input.content_type if input.respond_to?(:content_type)
270
+
271
+ # Determine from head of document
272
+ head = if input.respond_to?(:read)
273
+ input.rewind
274
+ string = input.read(1000)
275
+ input.rewind
276
+ string
277
+ else
278
+ input.to_s[0..1000]
279
+ end
280
+
281
+ doc_type_string = head.match(%r(<!DOCTYPE[^>]*>)m).to_s
282
+ root = head.match(%r(<[^!\?>]*>)m).to_s
283
+ root_element = root.match(%r(^<(\S+)[ >])) ? $1 : ""
284
+ version_attr = root.match(/version\s+=\s+(\S+)[\s">]/m) ? $1 : ""
285
+ end
286
+
287
+ # Already using XML parser, determine from DOCTYPE and/or root element
288
+ @version ||= :"rdfa1.0" if doc_type_string =~ /RDFa 1\.0/
289
+ @version ||= :"rdfa1.0" if version_attr =~ /RDFa 1\.0/
290
+ @version ||= :"rdfa1.1" if version_attr =~ /RDFa 1\.1/
291
+ @version ||= :"rdfa1.1"
292
+
293
+ @host_language ||= case content_type
294
+ when "application/xml" then :xml1
295
+ when "image/svg+xml" then :svg
296
+ when "text/html"
297
+ case doc_type_string
298
+ when /html 4/i then :html4
299
+ when /xhtml/i then :xhtml1
300
+ when /html/i then :html5
301
+ end
302
+ when "application/xhtml+xml"
303
+ case doc_type_string
304
+ when /html 4/i then :html4
305
+ when /xhtml/i then :xhtml1
306
+ when /html/i then :xhtml5
307
+ end
308
+ else
309
+ case root_element
310
+ when /svg/i then :svg
311
+ when /html/i then :html4
312
+ end
313
+ end
314
+
315
+ @host_language ||= :xml1
316
+ end
317
+
287
318
  # @return [RDF::Repository]
288
319
  def profile_repository
289
320
  Profile.repository
data/rdf-rdfa.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rdf-rdfa}
8
- s.version = "0.3.3"
8
+ s.version = "0.3.3.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Gregg Kellogg"]
12
- s.date = %q{2011-04-25}
12
+ s.date = %q{2011-04-26}
13
13
  s.description = %q{ RDF::RDFa is an RDFa reader/writer for Ruby using the RDF.rb library suite.
14
14
  }
15
15
  s.email = %q{gregg@kellogg-assoc.com}
@@ -34,6 +34,8 @@ Gem::Specification.new do |s|
34
34
  "etc/foaf.html",
35
35
  "etc/profile.html",
36
36
  "etc/xhv.html",
37
+ "example-files/bb-test.rb",
38
+ "example-files/best-buy.html",
37
39
  "example-files/data-view.xhtml",
38
40
  "example-files/erdf_profile.html",
39
41
  "example-files/payswarm.html",
data/script/tc CHANGED
@@ -76,7 +76,7 @@ opts = GetoptLong.new(
76
76
  ["--quiet", GetoptLong::NO_ARGUMENT],
77
77
  ["--validate", GetoptLong::NO_ARGUMENT],
78
78
  ["--verbose", GetoptLong::NO_ARGUMENT],
79
- ["--version", "-v", GetoptLong::OPTIONAL_ARGUMENT],
79
+ ["--version", "-v", GetoptLong::OPTIONAL_ARGUMENT]
80
80
  )
81
81
 
82
82
  def help(options)
data/spec/reader_spec.rb CHANGED
@@ -528,12 +528,26 @@ describe "RDF::RDFa::Reader" do
528
528
  specify "test #{t.name}: #{t.title}#{", (negative test)" if t.expectedResults.false?}" do
529
529
  begin
530
530
  t.debug = []
531
- graph = RDF::Graph.load(t.input(host_language, version), :debug => t.debug, :format => :rdfa)
531
+ reader = RDF::Reader.open(t.input(host_language, version),
532
+ :base_uri => t.input(host_language, version),
533
+ :debug => t.debug,
534
+ :format => :rdfa)
535
+ reader.should be_a RDF::Reader
536
+
537
+ # Make sure auto-detect works
538
+ unless host_language =~ /svg/
539
+ reader.host_language.should == host_language.to_sym
540
+ reader.version.should == version.to_sym
541
+ end
542
+
543
+ graph = RDF::Graph.new << reader
532
544
  query = Kernel.open(t.results(host_language, version))
533
545
  graph.should pass_query(query, t)
534
546
  rescue RSpec::Expectations::ExpectationNotMetError => e
535
547
  if %w(0198).include?(t.name) || query =~ /XMLLiteral/m
536
548
  pending("XMLLiteral canonicalization not implemented yet")
549
+ elsif %w(html4 html5).include?(host_language) && Kernel.open(t.input(host_language, version)) {|f| f.read =~ /xmlns/}
550
+ pending("HTML parsing does not use xmlns")
537
551
  elsif classification != "required"
538
552
  pending("#{classification} test") { raise }
539
553
  else
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rdf-rdfa
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.3
5
+ version: 0.3.3.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Gregg Kellogg
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-25 00:00:00 -07:00
13
+ date: 2011-04-26 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -281,6 +281,8 @@ files:
281
281
  - etc/foaf.html
282
282
  - etc/profile.html
283
283
  - etc/xhv.html
284
+ - example-files/bb-test.rb
285
+ - example-files/best-buy.html
284
286
  - example-files/data-view.xhtml
285
287
  - example-files/erdf_profile.html
286
288
  - example-files/payswarm.html