forked from zotero/zotero-bits
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmbedded RDF.js
More file actions
135 lines (122 loc) · 4.59 KB
/
Embedded RDF.js
File metadata and controls
135 lines (122 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
{
"translatorID":"951c027d-74ac-47d4-a107-9c3069ab7b48",
"translatorType":4,
"label":"Embedded RDF",
"creator":"Simon Kornblith",
"target":null,
"minVersion":"1.0.0b3.r1",
"maxVersion":"",
"priority":400,
"inRepository":true,
"detectXPath":"//meta[substring(@name, 1, 3)='dc.' or substring(@name, 1, 9)='citation_'] | //link[substring(@rel, 1, 7)='schema.']",
"lastUpdated":"2011-01-11 04:31:00"
}
// Known formats
// Reverse
var _n = {
"http://purl.org/net/biblio#":"bib",
"http://purl.org/dc/elements/1.1/":"dc",
"http://purl.org/dc/terms/":"dcterms",
"http://prismstandard.org/namespaces/1.2/basic/":"prism",
"http://xmlns.com/foaf/0.1/":"foaf",
"http://nwalsh.com/rdf/vCard#":"vcard",
"http://www.w3.org/2006/vcard/ns#":"vcard2", // currently used only for NSF, but is probably
// very similar to the nwalsh vcard ontology in a
// different namespace
"http://purl.org/rss/1.0/modules/link/":"link",
"http://www.zotero.org/namespaces/export#":"z",
"http://purl.org/eprint/terms/":"eprint",
"http://www.zotero.org/namespaces/ghcitation#":"gh" // XXX Hack until we can get a PURL for the citation_ stuff
};
// Maps actual prefix in use to URI
var _prefixes = {};
// These are the ones that we will read without a declared schema
var _dcDefined = false;
var _ghDefined = false;
function getPrefixes(doc) {
if(_prefixes.length) {
return _prefixes;
}
var links = doc.getElementsByTagName("link");
for(var i=0; i<links.length; i++) {
// Look for the schema's URI in our known schemata
if(links[i].getAttribute("href") in _n) {
var rel = links[i].getAttribute("rel");
if(rel) {
var matches = rel.match(/^schema\.([a-zA-Z]+)/);
if(matches) {
Zotero.debug("Prefix '" + matches[1].toLowerCase() +"' => '" + links[i].getAttribute("href") + "'");
_prefixes[matches[1].toLowerCase()] = _n[links[i].getAttribute("href")];
if (_n[links[i].getAttribute("href")] == "dc") {
_dcDefined = true;
} else if (_n[links[i].getAttribute("href")] == "gh") {
_ghDefined = true;
}
}
}
}
}
// We allow prefix-less DC and Google/Highwire
if (!_dcDefined && !_prefixes["dc"]) {
_prefixes["dc"] = "http://purl.org/dc/elements/1.1/";
}
if (!_ghDefined && !_prefixes["citation"]) {
_prefixes["citation"] = "http://www.zotero.org/namespaces/ghcitation#"; // XXX Hack until we can get a PURL for the citation_ stuff
}
return _prefixes;
}
function detectWeb(doc, url) {
var prefixes = getPrefixes(doc);
var metaTags = doc.getElementsByTagName("meta");
for(var i=0; i<metaTags.length; i++) {
var tag = metaTags[i].getAttribute("name");
// See if the supposed prefix is there, split by period or underscore
if(tag && (prefixes[tag.split('.')[0].toLowerCase()]
|| prefixes[tag.split('_')[0].toLowerCase()])) {
return "webpage";
}
}
return false;
}
function doWeb(doc, url) {
var prefixes = getPrefixes(doc);
// load RDF translator, so that we don't need to replicate import code
var translator = Zotero.loadTranslator("import");
translator.setTranslator("5e3ad958-ac79-463d-812b-a86a9235c28f");
translator.setHandler("itemDone", function(obj, newItem) {
if (!newItem.url) newItem.url = doc.location.href;
// add attachment
newItem.attachments.push({document:doc});
// add access date
newItem.accessDate = 'CURRENT_TIMESTAMP';
newItem.complete();
});
var rdf = translator.getTranslatorObject();
var metaTags = doc.getElementsByTagName("meta");
var foundTitle = false; // We can use the page title if necessary
for(var i=0; i<metaTags.length; i++) {
var tag = metaTags[i].getAttribute("name");
var value = metaTags[i].getAttribute("content");
// See if the supposed prefix is there, split by period or underscore
if(tag && (prefixes[tag.split('.')[0].toLowerCase()]
|| prefixes[tag.split('_')[0].toLowerCase()])) {
// Set the delimiter for this tag
var delim = (prefixes[tag.split('_')[0].toLowerCase()]) ? '_' : '.';
var pieces = tag.split(delim);
var prefix = pieces.shift();
if (prefix.toLowerCase() == "dc" && pieces.join(delim) == "title") {
foundTitle = true;
}
rdf.Zotero.RDF.addStatement(url, prefixes[prefix] + pieces.join(delim).toLowerCase(), value, true);
} else if(tag && value && (tag == "author" || tag == "author-personal")) {
rdf.Zotero.RDF.addStatement(url, prefixes["dc"] + "creator", value, true);
} else if(tag && value && tag == "author-corporate") {
rdf.Zotero.RDF.addStatement(url, prefixes["dc"] + "creator", value, true);
}
}
if (!foundTitle) {
rdf.Zotero.RDF.addStatement(url, prefixes["dc"] + "title", doc.title, true);
}
rdf.defaultUnknownType = "webpage";
rdf.doImport();
}