Skip to content

Commit

Permalink
Merge pull request zotero#496 from aurimasv/em-override
Browse files Browse the repository at this point in the history
[EM] Allow translators to override itemType for Embedded Metadata and RDF
  • Loading branch information
simonster committed Nov 24, 2012
2 parents a2d4ecc + 6caa65f commit 8c822b9
Show file tree
Hide file tree
Showing 3 changed files with 769 additions and 45 deletions.
56 changes: 42 additions & 14 deletions Embedded Metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2012-11-17 04:36:32"
"lastUpdated": "2012-11-23 01:51:00"
}

/*
Expand Down Expand Up @@ -92,6 +92,12 @@ var _prefixes = {
book:"http://ogp.me/ns/book#"
};

var _prefixRemap = {
//DC should be in lower case
"http://purl.org/DC/elements/1.0/": "http://purl.org/dc/elements/1.0/",
"http://purl.org/DC/elements/1.1/": "http://purl.org/dc/elements/1.1/"
};

var _rdfPresent = false,
_haveItem = false,
_itemType;
Expand All @@ -104,6 +110,15 @@ function addCustomFields(customFields) {
CUSTOM_FIELD_MAPPINGS = customFields;
}

function setPrefixRemap(map) {
_prefixRemap = map;
}

function remapPrefix(uri) {
if(_prefixRemap[uri]) return _prefixRemap[uri];
return uri;
}

function getPrefixes(doc) {
var links = doc.getElementsByTagName("link");
for(var i=0, link; link = links[i]; i++) {
Expand All @@ -113,7 +128,7 @@ function getPrefixes(doc) {
var matches = rel.match(/^schema\.([a-zA-Z]+)/);
if(matches) {
//Zotero.debug("Prefix '" + matches[1].toLowerCase() +"' => '" + links[i].getAttribute("href") + "'");
_prefixes[matches[1].toLowerCase()] = link.getAttribute("href");
_prefixes[matches[1].toLowerCase()] = remapPrefix(link.getAttribute("href"));
}
}
}
Expand Down Expand Up @@ -166,6 +181,8 @@ function completeItem(doc, newItem) {
}

function detectWeb(doc, url) {
if(exports.itemType) return exports.itemType;

init(doc, url, Zotero.done);
}

Expand All @@ -192,14 +209,14 @@ function init(doc, url, callback, forceLoadRDF) {
if(delimIndex === -1) delimIndex = tag.indexOf('_');
if(delimIndex === -1) continue;

var prefix = tag.substr(0, delimIndex).toLowerCase();
var prefix = tag.substr(0, delimIndex).toLowerCase();

if(_prefixes[prefix]) {
var prop = tag.substr(delimIndex+1, 1).toLowerCase()+tag.substr(delimIndex+2);
// This debug is for seeing what is being sent to RDF
//Zotero.debug(_prefixes[prefix]+prop +"=>"+value);
statements.push([url, _prefixes[prefix]+prop, value]);
} else {
if(_prefixes[prefix]) {
var prop = tag.substr(delimIndex+1, 1).toLowerCase()+tag.substr(delimIndex+2);
// This debug is for seeing what is being sent to RDF
//Zotero.debug(_prefixes[prefix]+prop +"=>"+value);
statements.push([url, _prefixes[prefix]+prop, value]);
} else {
var shortTag = tag.slice(tag.lastIndexOf('citation_'));
switch(shortTag) {
case "citation_journal_title":
Expand Down Expand Up @@ -240,17 +257,25 @@ function init(doc, url, callback, forceLoadRDF) {
var statement = statements[i];
rdf.Zotero.RDF.addStatement(statement[0], statement[1], statement[2], true);
}

var nodes = rdf.getNodes(true);
rdf.defaultUnknownType = hwType || hwTypeGuess ||
//if we have RDF data, then default to webpage
(nodes.length ? "webpage":false);

_itemType = nodes.length ? rdf.detectType({},nodes[0],{}) : rdf.defaultUnknownType;

//if itemType is overridden, no reason to run RDF.detectWeb
if(exports.itemType) {
rdf.itemType = exports.itemType;
_itemType = exports.itemType;
} else {
_itemType = nodes.length ? rdf.detectType({},nodes[0],{}) : rdf.defaultUnknownType;
}

RDF = rdf;
callback(_itemType);
});
} else {
callback(hwType || hwTypeGuess);
callback(exports.itemType || hwType || hwTypeGuess);
}
}

Expand Down Expand Up @@ -435,8 +460,11 @@ function addHighwireMetadata(doc, newItem) {
}

var exports = {
"doWeb":doWeb,
"addCustomFields": addCustomFields
"doWeb": doWeb,
"detectWeb": detectWeb,
"addCustomFields": addCustomFields,
"itemType": false,
"fixSchemaURI": setPrefixRemap
}

/** BEGIN TEST CASES **/
Expand Down
Loading

0 comments on commit 8c822b9

Please sign in to comment.