Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- const debug_1 = require("./lib/debug");
- const chunkSize = 0x100000;
- Translator.detectImport = () => {
- let str;
- debug_1.debug('BetterBibTeX JSON.detect: start');
- let json = '';
- while ((str = Zotero.read(chunkSize)) !== false) {
- json += str;
- if (json[0] !== '{')
- return false;
- }
- // a failure to parse will throw an error which a) is actually logged, and b) will count as "false"
- const data = JSON.parse(json);
- if (!data.config || (data.config.id !== Translator.header.translatorID))
- throw new Error(`ID mismatch: got ${data.config && data.config.id}, expected ${Translator.header.translatorID}`);
- if (!data.items || !data.items.length)
- throw new Error('No items');
- return true;
- };
- Translator.doImport = async () => {
- let str;
- let json = '';
- while ((str = Zotero.read(chunkSize)) !== false) {
- json += str;
- }
- const data = JSON.parse(json);
- const validFields = Zotero.BetterBibTeX.validFields();
- const items = new Set;
- debug_1.debug('importing', data.items.length, 'items');
- for (const source of data.items) {
- Zotero.BetterBibTeX.simplifyFields(source);
- if (!validFields[source.itemType])
- throw new Error(`unexpected item type '${source.itemType}'`);
- for (const field of Object.keys(source)) {
- if (!validFields[source.itemType][field])
- throw new Error(`unexpected ${source.itemType}.${field} in ${JSON.stringify(source)}`);
- }
- const item = new Zotero.Item();
- Object.assign(item, source);
- for (const att of item.attachments || []) {
- if (att.url)
- delete att.path;
- }
- await item.complete();
- items.add(source.itemID);
- Zotero.setProgress(items.size / data.items.length * 100); // tslint:disable-line:no-magic-numbers
- }
- Zotero.setProgress(100); // tslint:disable-line:no-magic-numbers
- const collections = Object.values(data.collections || {});
- for (const collection of collections) {
- collection.zoteroCollection = (new Zotero.Collection());
- collection.zoteroCollection.type = 'collection';
- collection.zoteroCollection.name = collection.name;
- collection.zoteroCollection.children = collection.items.filter(id => {
- if (items.has(id))
- return true;
- debug_1.debug(`Collection ${collection.key} has non-existent item ${id}`);
- return false;
- }).map(id => ({ type: 'item', id }));
- }
- for (const collection of collections) {
- if (collection.parent && data.collections[collection.parent]) {
- data.collections[collection.parent].zoteroCollection.children.push(collection.zoteroCollection);
- }
- else {
- if (collection.parent)
- debug_1.debug(`Collection ${collection.key} has non-existent parent ${collection.parent}`);
- collection.parent = false;
- }
- }
- for (const collection of collections) {
- if (collection.parent)
- continue;
- collection.zoteroCollection.complete();
- }
- };
- Translator.doExport = () => {
- let item;
- debug_1.debug('starting export');
- const data = {
- config: {
- id: Translator.header.translatorID,
- label: Translator.header.label,
- release: Zotero.BetterBibTeX.version(),
- preferences: Translator.preferences,
- options: Translator.options,
- },
- collections: Translator.collections,
- items: [],
- };
- debug_1.debug('header ready');
- const validFields = Zotero.BetterBibTeX.validFields();
- const validAttachmentFields = new Set(['itemType', 'title', 'path', 'tags', 'dateAdded', 'dateModified', 'seeAlso', 'mimeType']);
- while ((item = Zotero.nextItem())) {
- Zotero.BetterBibTeX.simplifyFields(item);
- delete item.relations;
- for (const field of Object.keys(item)) {
- if (validFields[item.itemType] && !validFields[item.itemType][field]) {
- delete item[field];
- }
- }
- for (const att of item.attachments || []) {
- delete att.relations;
- att.path = att.localpath;
- for (const field of Object.keys(att)) {
- if (!validAttachmentFields.has(field))
- delete att[field];
- }
- }
- debug_1.debug('adding item', item.itemID);
- data.items.push(item);
- }
- debug_1.debug('data ready');
- Zotero.write(JSON.stringify(data, null, ' '));
- debug_1.debug('export done');
- };
Add Comment
Please, Sign In to add comment