summaryrefslogtreecommitdiff
path: root/Library Catalog (BiblioCommons).js
blob: d3c6011535890fd84ec62f9d9b8ad76a6e694c66 (plain)
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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
{
	"translatorID": "5d506fe3-dbde-4424-90e8-d219c63faf72",
	"label": "Library Catalog (BiblioCommons)",
	"creator": "Avram Lyon",
	"target": "^https?://[^.]+\\.bibliocommons\\.com\\/",
	"minVersion": "2.1",
	"maxVersion": "",
	"priority": 250,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcsibv",
	"lastUpdated": "2014-08-26 03:54:52"
}

/*
   BiblioCommons Translator
   Copyright (C) 2011 Avram Lyon, ajlyon@gmail.com

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

function detectWeb(doc, url) {
	if (url.match(/\/item\/(?:show|catalogue_info)/))
		return "book";
	if (url.match(/\/search\?t=/))
		return "multiple";
	return false;
}

function doWeb(doc, url) {
	var n = doc.documentElement.namespaceURI;
	var ns = n ? function(prefix) {
		if (prefix == 'x') return n; else return null;
	} : null;

	// Load MARC
	var translator = Z.loadTranslator("import");
	translator.setTranslator("a6ee60df-1ddc-4aae-bb25-45e0537be973");

	var domain = url.match(/https?:\/\/([^.\/]+)/)[1];

	if (url.match(/\/item\/show/)) {
		Zotero.Utilities.doGet(url.replace(/\/item\/show/,"/item/catalogue_info"),
					function (text) {
						translator.getTranslatorObject(function (obj) {
							processor({	
								translator: obj,
								text: text,
								domain: domain
							});
						})
					}, function() {Zotero.done()});
	} else if (url.match(/\/item\/catalogue_info/)) {
		translator.getTranslatorObject(function (obj) {
			processor({	
				translator: obj,
				text: doc.documentElement.innerHTML,
				domain: domain
			});
		})
	} else if (url.match(/\/search\?t=/)) {
		var results = doc.evaluate('//div[@id="bibList"]/div/div//span[@class="title"]/a[1]', doc, ns, XPathResult.ANY_TYPE, null);
		var items = new Array();
		var result;
		while(result = results.iterateNext()) {
				var title = result.textContent;
				var url = result.href.replace(/\/show\//,"/catalogue_info/");
				items[url] = title;
		}
		Zotero.selectItems(items, function (items) {
			var urls = [];
			var i;
			for (i in items) urls.push(i);
			Zotero.Utilities.doGet(urls, function (text) {
				translator.getTranslatorObject(function (obj) {
					processor({
						translator: obj,
						text: text,
						domain: domain
					});
				})
			}, function() {Zotero.done()});
		});
		Zotero.wait();
	}
}

function processor (obj) {
		// Gets {translator: , text: }
		//Z.debug(obj.text)
		// Here, we split up the table and insert little placeholders between record bits
		var marced = obj.text.replace(/\s+/g," ")
					.replace(/^.*<div id="marc_details">(?:\s*<[^>"]+>\s*)*/,"")
					.replace(/<tr +class="(?:odd|even)">\s*/g,"")
					.replace(/<td +scope="row" +class="marcTag"><strong>(\d+)<\/strong><\/td>\s*/g,"$1\x1F")
					// We may be breaking the indicator here
					.replace(/<td\s+class="marcIndicator">\s*(\d*)\s*<\/td>\s*/g,"$1\x1F")
					.replace(/<td +class="marcTagData">(.*?)<\/td>\s*<\/tr>\s*/g,"$1\x1E")
					.replace(/\x1F(?:[^\x1F]*)$/,"\x1F")
					// We have some extra 0's at the start of the leader
					.replace(/^000/,"");
		//Z.debug(marced);
		// We've used the record delimiter to delimit fields
		var fields = marced.split("\x1E");
		
		// The preprocess function gets the translator object, if available
		// This is pretty vital for fancy translators like MARC
		var marc = obj["translator"];
		// Make a record, only one.
		var record = new marc.record();
		// The first piece is the MARC leader
		record.leader = fields.shift();
		for each (var field in fields) {
			//Z.debug(field)
			// Skip blanks
			if (field.replace(/\x1F|\s/g,"") == "") continue;
			// We're using the subfield delimiter to separate the field code,
			// indicator, and the content.
			var pieces = field.split("\x1F");
			if (pieces.length>2){
			record.addField(pieces[0].trim(),
							pieces[1].trim(),
							// Now we insert the subfield delimiter
							pieces[2].replace(/\$([a-z]|$)/g,"\x1F$1").trim());
			}				
		}
		// returns {translator: , text: false, items: [Zotero.Item[]]}
		var item = new Zotero.Item();
		record.translate(item);
		item.libraryCatalog = obj.domain + " Library Catalog";
		item.complete();
		return true;
}

/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://bostonpl.bibliocommons.com/item/show/2051015075_labor",
		"items": [
			{
				"itemType": "book",
				"creators": [
					{
						"firstName": "Marcia McKenna",
						"lastName": "Biddle",
						"creatorType": "author"
					}
				],
				"notes": [
					{
						"note": "Brief biographies of five women prominently involved in the labor movement in the United States: Mother Jones, Mary Heaton Vorse, Frances Perkins, Addie Wyatt, and Dolores Huerta. Also includes 11 other women who have made outstanding contributions"
					}
				],
				"tags": [
					"Women labor union members",
					"United States",
					"Women",
					"United States",
					"Women labor union members",
					"Working class"
				],
				"seeAlso": [],
				"attachments": [],
				"ISBN": "0875181678",
				"title": "Labor",
				"place": "Minneapolis",
				"publisher": "Dillon Press",
				"date": "1979",
				"numPages": "126",
				"series": "Contributions of women",
				"callNumber": "HD6079.2.U5 B52",
				"libraryCatalog": "bostonpl Library Catalog"
			}
		]
	},
	{
		"type": "web",
		"url": "http://bostonpl.bibliocommons.com/search?t=smart&search_category=keyword&q=labor&commit=Search",
		"items": "multiple"
	},
	{
		"type": "web",
		"url": "http://nypl.bibliocommons.com/item/show/10974089052_labour",
		"items": [
			{
				"itemType": "book",
				"creators": [
					{
						"firstName": "György",
						"lastName": "Lukács",
						"creatorType": "author"
					},
					{
						"firstName": "György",
						"lastName": "Lukács",
						"creatorType": "author"
					}
				],
				"notes": [],
				"tags": [
					"Labor",
					"Philosophy",
					"Philosophy, Marxist"
				],
				"seeAlso": [],
				"attachments": [],
				"title": "Labour",
				"place": "London",
				"publisher": "Merlin Press",
				"date": "1980",
				"numPages": "139",
				"series": "The Ontology of social being",
				"seriesNumber": "3",
				"callNumber": "JFD 87-5272",
				"libraryCatalog": "nypl Library Catalog"
			}
		]
	}
]
/** END TEST CASES **/