Module: RDF::TriX::Writer::Nokogiri

Defined in:
lib/rdf/trix/writer/nokogiri.rb

Overview

Nokogiri implementation of the TriX writer.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.librarySymbol

Returns the name of the underlying XML library.

Returns:

  • (Symbol)


12
13
14
# File 'lib/rdf/trix/writer/nokogiri.rb', line 12

def self.library
  :nokogiri
end

Instance Method Details

#create_comment(text) ⇒ Nokogiri::XML::Comment

Creates an XML comment element with the given text.

Parameters:

  • text (String, #to_s)

Returns:

  • (Nokogiri::XML::Comment)


71
72
73
# File 'lib/rdf/trix/writer/nokogiri.rb', line 71

def create_comment(text)
  ::Nokogiri::XML::Comment.new(@xml, text.to_s)
end

#create_element(name, content = nil, attributes = {}) {|element| ... } ⇒ Nokogiri::XML::Element

Creates an XML element of the given name, with optional given content and attributes.

Parameters:

  • name (Symbol, String, #to_s)
  • content (String, #to_s) (defaults to: nil)
  • attributes (Hash{Symbol => Object}) (defaults to: {})

Yields:

  • (element)

Yield Parameters:

  • element (Nokogiri::XML::Element)

Returns:

  • (Nokogiri::XML::Element)


85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rdf/trix/writer/nokogiri.rb', line 85

def create_element(name, content = nil, attributes = {}, &block)
  element = @xml.create_element(name.to_s)
  if xmlns = attributes.delete(:xmlns)
    element.default_namespace = xmlns
  end
  fragment = attributes.delete(:fragment)
  attributes.each { |k, v| element[k.to_s] = v }
  element.content = content.to_s unless content.nil?
  element << fragment if fragment
  block.call(element) if block_given?
  element
end

#create_graph(name = nil) {|element| ... } ⇒ Nokogiri::XML::Element

Creates an XML graph element with the given name.

Parameters:

  • name (RDF::Resource) (defaults to: nil)

Yields:

  • (element)

Yield Parameters:

  • element (Nokogiri::XML::Element)

Returns:

  • (Nokogiri::XML::Element)


55
56
57
58
59
60
61
62
63
64
# File 'lib/rdf/trix/writer/nokogiri.rb', line 55

def create_graph(name = nil, &block)
  @trix << (graph = create_element(:graph))
  case name
    when nil then nil
    when RDF::Node then graph << create_element(:id, name.to_s) # non-standard
    else graph << create_element(:uri, name.to_s)
  end
  block.call(graph) if block_given?
  graph
end

#initialize_xml(**options)

This method returns an undefined value.

Initializes the underlying XML library.

Parameters:

  • options (Hash{Symbol => Object})


21
22
23
24
25
# File 'lib/rdf/trix/writer/nokogiri.rb', line 21

def initialize_xml(**options)
  require 'nokogiri' unless defined?(::Nokogiri)
  @xml = ::Nokogiri::XML::Document.new
  @xml.encoding = @encoding
end

#write_epilogue

This method returns an undefined value.

Outputs the TriX document.



42
43
44
45
46
# File 'lib/rdf/trix/writer/nokogiri.rb', line 42

def write_epilogue
  puts @xml.to_xml
  @xml = @trix = nil
  super
end

#write_prologue

This method returns an undefined value.

Generates the TriX root element.



31
32
33
34
35
36
# File 'lib/rdf/trix/writer/nokogiri.rb', line 31

def write_prologue
  options = {xmlns: Format::XMLNS, xml: "http://www.w3.org/XML/1998/namespace"}
  options["xml:base"] = base_uri.to_s if base_uri
  @xml << (@trix = create_element(:TriX, nil, options))
  super
end