Skip to content

Instantly share code, notes, and snippets.

@nevali
Created March 16, 2010 16:04
Show Gist options
  • Save nevali/334145 to your computer and use it in GitHub Desktop.
Save nevali/334145 to your computer and use it in GitHub Desktop.
This is an extension to rdfQuery to glean DC-HTML metadata from a document. It's quick, it's dirty, it probably doesn't work properly.
(function ($) {
var ns = {}, parsedNS = false;
parseNS = function() {
parsedNS = true;
var l =$('link[rel^=schema\.]');
if(l && l[0])
{
l.each(function() {
var uri;
if((uri = $(this).attr('href')))
{
var prefix = $(this).attr('rel').substring(7);
ns[prefix] = uri;
}
});
}
}
gleaner = function(options)
{
var triples = [], triple, m, p = new RegExp(/^([^\.]+)\.(.*)/), context = context || {}, parent;
if(!parsedNS) parseNS();
parent = context.subject || $.rdf.resource('<>');
m = $(this).find('meta[name*=\.]');
if(m && m[0])
{
m.each(function() {
var prefix = '', name = p.exec($(this).attr('name')), content = $(this).attr('content');
if(name.length != 3) continue;
prefix = name[1];
if(!content || prefix == 'schema') return;
name = name[2];
if(typeof ns[prefix] == 'undefined')
{
console.log('skipping <meta> with undefined prefix ' + prefix);
return;
}
triple = $.rdf.triple(parent, $.rdf.resource($.uri(ns[prefix] + name)), $.rdf.literal(content, { datatype: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral' }), { source: this[0] });
triples = triples.concat(triple);
});
}
m = $(this).find('*[rel*=\.]');
if(m && m[0])
{
m.each(function() {
var prefix = '', name = p.exec($(this).attr('rel')), content = $(this).attr('href');
if(name.length != 3) continue;
prefix = name[1];
if(!content || prefix == 'schema') return;
name = name[2];
if(typeof ns[prefix] == 'undefined')
{
console.log('skipping <* rel="..."> with undefined prefix ' + prefix);
return;
}
triple = $.rdf.triple(parent, $.rdf.resource($.uri(ns[prefix] + name)), $.rdf.literal(content, { datatype: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral' }), { source: this[0] });
triples = triples.concat(triple);
});
}
return triples;
}
$.rdf.gleaners.push(gleaner);
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment