Guest User

Untitled

a guest
Jul 20th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const debug_1 = require("./lib/debug");
  4. const chunkSize = 0x100000;
  5. Translator.detectImport = () => {
  6. let str;
  7. debug_1.debug('BetterBibTeX JSON.detect: start');
  8. let json = '';
  9. while ((str = Zotero.read(chunkSize)) !== false) {
  10. json += str;
  11. if (json[0] !== '{')
  12. return false;
  13. }
  14. // a failure to parse will throw an error which a) is actually logged, and b) will count as "false"
  15. const data = JSON.parse(json);
  16. if (!data.config || (data.config.id !== Translator.header.translatorID))
  17. throw new Error(`ID mismatch: got ${data.config && data.config.id}, expected ${Translator.header.translatorID}`);
  18. if (!data.items || !data.items.length)
  19. throw new Error('No items');
  20. return true;
  21. };
  22. Translator.doImport = async () => {
  23. let str;
  24. let json = '';
  25. while ((str = Zotero.read(chunkSize)) !== false) {
  26. json += str;
  27. }
  28. const data = JSON.parse(json);
  29. const validFields = Zotero.BetterBibTeX.validFields();
  30. const items = new Set;
  31. debug_1.debug('importing', data.items.length, 'items');
  32. for (const source of data.items) {
  33. Zotero.BetterBibTeX.simplifyFields(source);
  34. if (!validFields[source.itemType])
  35. throw new Error(`unexpected item type '${source.itemType}'`);
  36. for (const field of Object.keys(source)) {
  37. if (!validFields[source.itemType][field])
  38. throw new Error(`unexpected ${source.itemType}.${field} in ${JSON.stringify(source)}`);
  39. }
  40. const item = new Zotero.Item();
  41. Object.assign(item, source);
  42. for (const att of item.attachments || []) {
  43. if (att.url)
  44. delete att.path;
  45. }
  46. await item.complete();
  47. items.add(source.itemID);
  48. Zotero.setProgress(items.size / data.items.length * 100); // tslint:disable-line:no-magic-numbers
  49. }
  50. Zotero.setProgress(100); // tslint:disable-line:no-magic-numbers
  51. const collections = Object.values(data.collections || {});
  52. for (const collection of collections) {
  53. collection.zoteroCollection = (new Zotero.Collection());
  54. collection.zoteroCollection.type = 'collection';
  55. collection.zoteroCollection.name = collection.name;
  56. collection.zoteroCollection.children = collection.items.filter(id => {
  57. if (items.has(id))
  58. return true;
  59. debug_1.debug(`Collection ${collection.key} has non-existent item ${id}`);
  60. return false;
  61. }).map(id => ({ type: 'item', id }));
  62. }
  63. for (const collection of collections) {
  64. if (collection.parent && data.collections[collection.parent]) {
  65. data.collections[collection.parent].zoteroCollection.children.push(collection.zoteroCollection);
  66. }
  67. else {
  68. if (collection.parent)
  69. debug_1.debug(`Collection ${collection.key} has non-existent parent ${collection.parent}`);
  70. collection.parent = false;
  71. }
  72. }
  73. for (const collection of collections) {
  74. if (collection.parent)
  75. continue;
  76. collection.zoteroCollection.complete();
  77. }
  78. };
  79. Translator.doExport = () => {
  80. let item;
  81. debug_1.debug('starting export');
  82. const data = {
  83. config: {
  84. id: Translator.header.translatorID,
  85. label: Translator.header.label,
  86. release: Zotero.BetterBibTeX.version(),
  87. preferences: Translator.preferences,
  88. options: Translator.options,
  89. },
  90. collections: Translator.collections,
  91. items: [],
  92. };
  93. debug_1.debug('header ready');
  94. const validFields = Zotero.BetterBibTeX.validFields();
  95. const validAttachmentFields = new Set(['itemType', 'title', 'path', 'tags', 'dateAdded', 'dateModified', 'seeAlso', 'mimeType']);
  96. while ((item = Zotero.nextItem())) {
  97. Zotero.BetterBibTeX.simplifyFields(item);
  98. delete item.relations;
  99. for (const field of Object.keys(item)) {
  100. if (validFields[item.itemType] && !validFields[item.itemType][field]) {
  101. delete item[field];
  102. }
  103. }
  104. for (const att of item.attachments || []) {
  105. delete att.relations;
  106. att.path = att.localpath;
  107. for (const field of Object.keys(att)) {
  108. if (!validAttachmentFields.has(field))
  109. delete att[field];
  110. }
  111. }
  112. debug_1.debug('adding item', item.itemID);
  113. data.items.push(item);
  114. }
  115. debug_1.debug('data ready');
  116. Zotero.write(JSON.stringify(data, null, ' '));
  117. debug_1.debug('export done');
  118. };
Add Comment
Please, Sign In to add comment