Skip to content

Commit

Permalink
Finish 2.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Oct 10, 2017
2 parents 72f648d + 5181927 commit 59b6526
Show file tree
Hide file tree
Showing 19 changed files with 2,653 additions and 179 deletions.
19 changes: 10 additions & 9 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ source "http://rubygems.org"

gemspec

gem "rdf", github: "ruby-rdf/rdf", branch: "develop"
gem "rdf-rdfa", github: "ruby-rdf/rdf-rdfa", branch: "develop"
gem "rdf-xsd", github: "ruby-rdf/rdf-xsd", branch: "develop"
gem "rdf", git: "https://github.com/ruby-rdf/rdf", branch: "develop"
gem "rdf-rdfa", git: "https://github.com/ruby-rdf/rdf-rdfa", branch: "develop"
gem "rdf-xsd", git: "https://github.com/ruby-rdf/rdf-xsd", branch: "develop"
gem "nokogumbo", '~> 1.4'

group :development do
gem 'ebnf', github: "gkellogg/ebnf", branch: "develop"
gem 'rdf-aggregate-repo', github: "ruby-rdf/rdf-aggregate-repo", branch: "develop"
gem 'rdf-isomorphic', github: "ruby-rdf/rdf-isomorphic", branch: "develop"
gem "rdf-spec", github: "ruby-rdf/rdf-spec", branch: "develop"
gem 'rdf-turtle', github: "ruby-rdf/rdf-turtle", branch: "develop"
gem 'sxp', github: "dryruby/sxp.rb", branch: "develop"
gem 'linkeddata'
gem 'ebnf', git: "https://github.com/gkellogg/ebnf", branch: "develop"
gem 'rdf-aggregate-repo', git: "https://github.com/ruby-rdf/rdf-aggregate-repo", branch: "develop"
gem 'rdf-isomorphic', git: "https://github.com/ruby-rdf/rdf-isomorphic", branch: "develop"
gem "rdf-spec", git: "https://github.com/ruby-rdf/rdf-spec", branch: "develop"
gem 'rdf-turtle', git: "https://github.com/ruby-rdf/rdf-turtle", branch: "develop"
gem 'sxp', git: "https://github.com/dryruby/sxp.rb", branch: "develop"
end

group :debug do
Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,24 @@ Full documentation available on [Rubydoc.info][Microdata doc]
* {RDF::Microdata::Reader}
* {RDF::Microdata::Reader::Nokogiri}

### Additional vocabularies

### RDFa-based Reader
There is an experimental reader based on transforming Microdata to RDFa within the DOM. To invoke
this, add the `rdfa: true` option to the {RDF::Microdata::Reader.new}, or
use {RDF::Microdata::RdfaReader} directly.

The reader exposes a `#rdfa` method, which can be used to retrieve the transformed HTML+RDFa

### JSON-lD-based Reader
There is an experimental reader based on transforming Microdata to JSON-LD. To invoke
this, add the `jsonld: true` option to the {RDF::Microdata::Reader.new}, or
use {RDF::Microdata::JsonLdReader} directly.

The reader exposes a `#json` method, which can be used to retrieve the generated JSON-LD

## Resources
* [RDF.rb][RDF.rb]
* [Documentation](http://rdf.rubyforge.org/microdata)
* [Documentation](http://www.rubydoc.info/github/ruby-rdf/rdf-microdata/)
* [History](file:History.md)
* [Microdata][]
* [Microdata RDF][]
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.1
2.2.2
34 changes: 34 additions & 0 deletions examples/blog_posting.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE HTML>
<title>My Blog</title>
<article itemscope itemtype="https://schema.org/BlogPosting">
<header>
<h1 itemprop="headline">Progress report</h1>
<p><time itemprop="datePublished" datetime="2013-08-29">today</time></p>
<link itemprop="url" href="?comments=0">
</header>
<p>All in all, he's doing well with his swim lessons. The biggest thing was he had trouble
putting his head in, but we got it down.</p>
<section>
<h1>Comments</h1>
<article itemprop="comment" itemscope itemtype="https://schema.org/Comment" id="c1">
<link itemprop="url" href="#c1">
<footer>
<p>Posted by: <span itemprop="creator" itemscope itemtype="https://schema.org/Person">
<span itemprop="name">Greg</span>
</span></p>
<p><time itemprop="dateCreated" datetime="2013-08-29">15 minutes ago</time></p>
</footer>
<p>Ha!</p>
</article>
<article itemprop="comment" itemscope itemtype="https://schema.org/Comment" id="c2">
<link itemprop="url" href="#c2">
<footer>
<p>Posted by: <span itemprop="creator" itemscope itemtype="https://schema.org/Person">
<span itemprop="name">Charlotte</span>
</span></p>
<p><time itemprop="dateCreated" datetime="2013-08-29">5 minutes ago</time></p>
</footer>
<p>When you say "we got it down"...</p>
</article>
</section>
</article>
7 changes: 7 additions & 0 deletions examples/itemref.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div id="x">
<p itemprop="a">1</p>
</div>
<div itemscope itemref="x" itemtype="http://schema.org/Thing">
<p itemprop="b">test</p>
<p itemprop="a">2</p>
</div>
11 changes: 11 additions & 0 deletions examples/locomotive.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<dl itemscope itemtype="http://md.example.com/loco
http://md.example.com/lighting">
<dt>Name:
<dd itemprop="name">Tank Locomotive (DB 80)
<dt>Product code:
<dd itemprop="product-code">33041
<dt>Scale:
<dd itemprop="scale">HO
<dt>Digital:
<dd itemprop="digital">Delta
</dl>
34 changes: 34 additions & 0 deletions examples/to_jsonld.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE HTML>
<title>My Blog</title>
<article itemscope itemtype="https://schema.org/BlogPosting">
<header>
<h1 itemprop="headline">Progress report</h1>
<p><time itemprop="datePublished" datetime="2013-08-29">today</time></p>
<link itemprop="url" href="?comments=0">
</header>
<p>All in all, he's doing well with his swim lessons. The biggest thing was he had trouble
putting his head in, but we got it down.</p>
<section>
<h1>Comments</h1>
<article itemprop="comment" itemscope itemtype="https://schema.org/Comment" id="c1">
<link itemprop="url" href="#c1">
<footer>
<p>Posted by: <span itemprop="creator" itemscope itemtype="https://schema.org/Person">
<span itemprop="name">Greg</span>
</span></p>
<p><time itemprop="dateCreated" datetime="2013-08-29">15 minutes ago</time></p>
</footer>
<p>Ha!</p>
</article>
<article itemprop="comment" itemscope itemtype="https://schema.org/Comment" id="c2">
<link itemprop="url" href="#c2">
<footer>
<p>Posted by: <span itemprop="creator" itemscope itemtype="https://schema.org/Person">
<span itemprop="name">Charlotte</span>
</span></p>
<p><time itemprop="dateCreated" datetime="2013-08-29">5 minutes ago</time></p>
</footer>
<p>When you say "we got it down"...</p>
</article>
</section>
</article>
14 changes: 9 additions & 5 deletions lib/rdf/microdata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ module RDF
# end
# end
#
# @see http://rdf.rubyforge.org/
# @see http://www.rubydoc.info/github/ruby-rdf/rdf/
# @see http://www.w3.org/TR/2011/WD-microdata-20110525/
#
# @author [Gregg Kellogg](http://greggkellogg.net/)
module Microdata
USES_VOCAB = RDF::URI("http://www.w3.org/ns/rdfa#usesVocabulary")
DEFAULT_REGISTRY = File.expand_path("../../../etc/registry.json", __FILE__)

require 'rdf/microdata/format'
require 'rdf/microdata/vocab'
autoload :Expansion, 'rdf/microdata/expansion'
autoload :Profile, 'rdf/microdata/profile'
autoload :Reader, 'rdf/microdata/reader'
autoload :VERSION, 'rdf/microdata/version'
autoload :Expansion, 'rdf/microdata/expansion'
autoload :JsonLdReader, 'rdf/microdata/jsonld_reader'
autoload :Profile, 'rdf/microdata/profile'
autoload :RdfaReader, 'rdf/microdata/rdfa_reader'
autoload :Reader, 'rdf/microdata/reader'
autoload :Registry, 'rdf/microdata/registry'
autoload :VERSION, 'rdf/microdata/version'
end
end
112 changes: 112 additions & 0 deletions lib/rdf/microdata/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,117 @@ class Format < RDF::Format
def self.detect(sample)
!!sample.match(/<[^>]*(itemprop|itemtype|itemref|itemscope|itemid)[^>]*>/m)
end

##
# Hash of CLI commands appropriate for this format
# @return [Hash{Symbol => Hash}]
def self.cli_commands
{
"to-rdfa": {
description: "Transform HTML+Microdata into HTML+RDFa",
parse: false,
help: "to-rdfa files ...\nTransform HTML+Microdata into HTML+RDFa",
filter: {
format: :microdata
},
option_use: {output_format: :disabled},
lambda: ->(files, options) do
out = options[:output] || $stdout
xsl = Nokogiri::XSLT(%(<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="indent-increment" select="' '"/>
<xsl:output method="html" doctype-system="about:legacy-compat"/>
<xsl:template name="newline">
<xsl:text disable-output-escaping="yes">
</xsl:text>
</xsl:template>
<xsl:template match="comment() | processing-instruction()">
<xsl:param name="indent" select="''"/>
<xsl:call-template name="newline"/>
<xsl:value-of select="$indent"/>
<xsl:copy />
</xsl:template>
<xsl:template match="text()">
<xsl:param name="indent" select="''"/>
<xsl:call-template name="newline"/>
<xsl:value-of select="$indent"/>
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
<xsl:template match="text()[normalize-space(.)='']"/>
<xsl:template match="*">
<xsl:param name="indent" select="''"/>
<xsl:call-template name="newline"/>
<xsl:value-of select="$indent"/>
<xsl:choose>
<xsl:when test="count(child::*) > 0">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="*|text()">
<xsl:with-param name="indent" select="concat ($indent, $indent-increment)"/>
</xsl:apply-templates>
<xsl:call-template name="newline"/>
<xsl:value-of select="$indent"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>).gsub(/^ /, ''))
if files.empty?
# If files are empty, either use options[::evaluate]
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
RDF::Microdata::Reader.new(input, options.merge(rdfa: true)) do |reader|
reader.rdfa.xpath("//text()").each do |txt|
txt.content = txt.content.to_s.strip
end
out.puts xsl.apply_to(reader.rdfa).to_s
end
else
files.each do |file|
RDF::Microdata::Reader.open(file, options.merge(rdfa: true)) do |reader|
reader.rdfa.xpath("//text()").each do |txt|
txt.content = txt.content.to_s.strip
end
out.puts xsl.apply_to(reader.rdfa).to_s
end
end
end
end
},
"to-jsonld": {
description: "Transform HTML+Microdata into JSON-LD",
parse: false,
help: "to-jsonld files ...\nTransform HTML+Microdata into JSON-LD",
filter: {
format: :microdata
},
option_use: {output_format: :disabled},
lambda: ->(files, options) do
out = options[:output] || $stdout
if files.empty?
# If files are empty, either use options[::evaluate]
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
RDF::Microdata::Reader.new(input, options.merge(jsonld: true)) do |reader|
out.puts reader.jsonld.to_json(::JSON::LD::JSON_STATE)
end
else
files.each do |file|
RDF::Microdata::Reader.open(file, options.merge(jsonld: true)) do |reader|
out.puts reader.jsonld.to_json(::JSON::LD::JSON_STATE)
end
end
end
end
},
}
end
end
end
Loading

0 comments on commit 59b6526

Please sign in to comment.