Class: RoadForest::TypeHandlers::RDFaWriter

Inherits:
RDF::Writer
  • Object
show all
Defined in:
lib/roadforest/type-handlers/rdfa-writer.rb,
lib/roadforest/type-handlers/rdfa-writer/render-engine.rb,
lib/roadforest/type-handlers/rdfa-writer/object-environment.rb,
lib/roadforest/type-handlers/rdfa-writer/render-environment.rb,
lib/roadforest/type-handlers/rdfa-writer/subject-environment.rb,
lib/roadforest/type-handlers/rdfa-writer/document-environment.rb,
lib/roadforest/type-handlers/rdfa-writer/property-environment.rb,
lib/roadforest/type-handlers/rdfa-writer/environment-decorator.rb

Overview

An RDFa 1.1 serialiser in Ruby. The RDFa serializer makes use of Haml templates, allowing runtime-replacement with alternate templates. Note, however, that templates should be checked against the W3C test suite to ensure that valid RDFa is emitted.

Note that the natural interface is to write a whole graph at a time. Writing statements or Triples will create a graph to add them to and then serialize the graph.

The writer will add prefix definitions, and use them for creating @prefix definitions, and minting CURIEs

Examples:

Obtaining a RDFa writer class

RDF::Writer.for(:html)          => RDF::RDFa::Writer
RDF::Writer.for("etc/test.html")
RDF::Writer.for(:file_name      => "etc/test.html")
RDF::Writer.for(:file_extension => "html")
RDF::Writer.for(:content_type   => "application/xhtml+xml")
RDF::Writer.for(:content_type   => "text/html")

Serializing RDF graph into an XHTML+RDFa file

RDF::RDFa::Write.open("etc/test.html") do |writer|
  writer << graph
end

Serializing RDF statements into an XHTML+RDFa file

RDF::RDFa::Writer.open("etc/test.html") do |writer|
  graph.each_statement do |statement|
    writer << statement
  end
end

Serializing RDF statements into an XHTML+RDFa string

RDF::RDFa::Writer.buffer do |writer|
  graph.each_statement do |statement|
    writer << statement
  end
end

Creating @base and @prefix definitions in output

RDF::RDFa::Writer.buffer(:base_uri => "http://example.com/", :prefixes => {
    :foaf => "http://xmlns.com/foaf/0.1/"}
) do |writer|
  graph.each_statement do |statement|
    writer << statement
  end
end

Author:

Defined Under Namespace

Classes: AffordanceDecorator, DecorationSet, DocumentAffordanceDecorator, DocumentEnvironment, EnvironmentDecorator, NilObjectEnvironment, NodeObjectEnvironment, ObjectAffordanceDecorator, ObjectEnvironment, PropertyAffordanceDecorator, PropertyEnvironment, RDFPostCurie, RenderEngine, RenderEnvironment, SubjectAffordanceDecorator, SubjectEnvironment, TemplateHandler, UriObjectEnvironment, XMLLiteralObjectEnvironment

Constant Summary collapse

HAML_OPTIONS =
{
  :ugly => true, # to preserve whitespace without using entities
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output = $stdout, options = {}) {|writer| ... } ⇒ RDFaWriter

Initializes the RDFa writer instance.

Parameters:

  • output (IO, File) (defaults to: $stdout)

    the output stream

  • options (Hash{Symbol => Object}) (defaults to: {})

    any additional options

Options Hash (options):

  • :canonicalize (Boolean) — default: false

    whether to canonicalize literals when serializing

  • :prefixes (Hash) — default: Hash.new

    the prefix mappings to use

  • :base_uri (#to_s) — default: nil

    the base URI to use when constructing relative URIs, set as html>head>base.href

  • :validate (Boolean) — default: false

    whether to validate terms when serializing

  • :lang (#to_s) — default: nil

    Output as root @lang attribute, and avoid generation _@lang_ where possible

  • :standard_prefixes (Boolean) — default: false

    Add standard prefixes to prefixes, if necessary.

  • :top_classes (Array<RDF::URI>) — default: [RDF::RDFS.Class]

    Defines rdf:type of subjects to be emitted at the beginning of the document.

  • :predicate_order (Array<RDF::URI>) — default: [RDF.type, RDF::RDFS.label, RDF::DC.title]

    Defines order of predicates to to emit at begninning of a resource description..

  • :heading_predicates (Array<RDF::URI>) — default: [RDF::RDFS.label, RDF::DC.title]

    Defines order of predicates to use in heading.

  • :haml (String, Symbol, Hash{Symbol => String}) — default: DEFAULT_HAML

    HAML templates used for generating code

  • :haml_options (Hash) — default: HAML_OPTIONS

    Options to pass to Haml::Engine.new. Default options set ‘:ugly => false` to ensure that whitespace in literals with newlines is properly preserved.

Yields:

  • (writer)

Yield Parameters:

  • writer (RDF::Writer)


91
92
93
94
95
96
97
98
# File 'lib/roadforest/type-handlers/rdfa-writer.rb', line 91

def initialize(output = $stdout, options = {}, &block)
  super do
    @graph = RDF::Graph.new
    @valise = nil

    block.call(self) if block_given?
  end
end

Instance Attribute Details

#base_uriRDF::URI

Returns Base URI used for relativizing URIs.

Returns:

  • (RDF::URI)

    Base URI used for relativizing URIs



59
60
61
# File 'lib/roadforest/type-handlers/rdfa-writer.rb', line 59

def base_uri
  @base_uri
end

#graphGraph

Returns Graph of statements serialized.

Returns:

  • (Graph)

    Graph of statements serialized



56
57
58
# File 'lib/roadforest/type-handlers/rdfa-writer.rb', line 56

def graph
  @graph
end

Instance Method Details

#write_epiloguevoid

This method returns an undefined value.

Outputs the XHTML+RDFa representation of all stored triples.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/roadforest/type-handlers/rdfa-writer.rb', line 136

def write_epilogue
  require 'roadforest/type-handlers/rdfa-writer/render-engine'
  @base_uri = RDF::URI(@options[:base_uri]) if @options[:base_uri]
  @lang = @options[:lang]
  @debug = @options[:debug]
  engine = RenderEngine.new(@graph, @debug) do |engine|
    engine.valise = Valise.define do
      ro up_to("lib") + "roadforest"
    end
    engine.style_name = @options[:haml]
    engine.base_uri = base_uri
    engine.lang = @lang
    engine.standard_prefixes = @options[:standard_prefixes]
    engine.top_classes = @options[:top_classes] || [RDF::RDFS.Class]
    engine.predicate_order = @options[:predicate_order] || [RDF.type, RDF::RDFS.label, RDF::DC.title]
    engine.heading_predicates = @options[:heading_predicates] || [RDF::RDFS.label, RDF::DC.title]
    engine.haml_options = @options[:haml_options]
  end

  engine.prefixes.merge! @options[:prefixes] unless @options[:prefixes].nil?

  # Generate document
  rendered = engine.render_document

  @output.write(rendered)
end

#write_graph(graph) ⇒ void

This method returns an undefined value.

Write whole graph

Parameters:



105
106
107
# File 'lib/roadforest/type-handlers/rdfa-writer.rb', line 105

def write_graph(graph)
  @graph = graph
end

#write_statement(statement) ⇒ void

This method returns an undefined value.

Addes a statement to be serialized

Parameters:

  • statement (RDF::Statement)

Raises:

  • (RDF::WriterError)

    if validating and attempting to write an invalid RDF::Term.



114
115
116
117
# File 'lib/roadforest/type-handlers/rdfa-writer.rb', line 114

def write_statement(statement)
  raise RDF::WriterError, "Statement #{statement.inspect} is invalid" if validate? && statement.invalid?
  @graph.insert(statement)
end

#write_triple(subject, predicate, object) ⇒ void

This method is abstract.

This method returns an undefined value.

Addes a triple to be serialized

Parameters:

  • subject (RDF::Resource)
  • predicate (RDF::URI)
  • object (RDF::Value)

Raises:

  • (NotImplementedError)

    unless implemented in subclass

  • (RDF::WriterError)

    if validating and attempting to write an invalid RDF::Term.



128
129
130
# File 'lib/roadforest/type-handlers/rdfa-writer.rb', line 128

def write_triple(subject, predicate, object)
  write_statement Statement.new(subject, predicate, object)
end