diegodlh / zotero-cita

Cita: a Wikidata addon for Zotero with citations metadata support
GNU General Public License v3.0
235 stars 12 forks source link

Can't export citations to Better BibTeX #145

Closed Dominic-DallOsto closed 3 months ago

Dominic-DallOsto commented 2 years ago

Describe the bug When trying to export the citations for an item using Better BibTeX format, an error occurs and the export fails. Checking the error console, it seems to be because the cited items don't have citation keys.

[JavaScript Error: "Error: No citation key in {"key":null,"version":null,"itemType":"journalArticle","volume":"38-38","issue":"1","pages":"173-198","publicationTitle":"Monatshefte fuer Mathematik","date":"1931-12","extra":"qid: Q54006652","DOI":"10.1007/BF01700692","libraryCatalog":"Wikidata API","language":"German","title":"Über formal unentscheidbare Sätze der Principia Mathematica und verwandter Systeme I","creators":[{"firstName":"Kurt","lastName":"Gödel","creatorType":"author"}],"tags":[],"collections":[],"relations":{},"uri":"http://zotero.org/users/local/XfKg3DUs/items/null","itemID":null,"attachments":[],"notes":[],"citationKey":"","citekey":"","itemKey":null,"libraryID":1,"$cacheable":false}
itemsGenerator@resource://zotero-better-bibtex/Better%20BibTeX.js:59390:17
doExport@resource://zotero-better-bibtex/Better%20BibTeX.js:63721:5
Zotero__workerContext</ctx.onmessage@resource://zotero-better-bibtex/worker/zotero.js?version=5.0.96.3&platform=win&translator=Better%20BibTeX&output=C%3A%5CUsers%5CDominic%5CDesktop%5CExported%20citations.bib&localeDateOrder=dmy&debugEnabled=false&worker=1:46592:11
"]

Exporting to normal BibTeX does work, and it automatically generates citation keys for the items.

To Reproduce Install the Better BibTeX extension alongside Cita. Add this DOI to Zotero. Get QID. Sync citations with Wikidata. Export citations to file, choosing the Better BibTeX format.

Expected behavior Cited items should be exported to bibtex file.

Environment:

retorquere commented 2 years ago

Does a regular manual export using Better BibTeX work for you, or is that also broken?

Dominic-DallOsto commented 2 years ago

Exporting the item itself works fine, yeah.

The code for creating the citation items to be exported is here. Is there something I need to do to generate keys for them? https://github.com/diegodlh/zotero-cita/blob/master/src/sourceItemWrapper.js#L620-627

retorquere commented 2 years ago

I don't fully understand what that code does, but it looks to me like it might be offering unsaved items to the exporter? BBT won't have generated a key for that item, keys are created post-save.

I don't currently see a safe way to get around this. I can't have BBT generate citekeys when new Zotero.Item is called because at that time it won't have any fields filled, and not all paths to a "ready to save" item go through fromJSON, so that's not the right moment either.

Does it matter to you whether these items have a (meaningful) citation key? If not, I can just remove the check for the presence of a citation key.

Dominic-DallOsto commented 2 years ago

Yes, a citation is a wrapper connecting a citing item (saved in the library) to a cited item (not necessarily saved in the library). For export, we take the list of cited items, convert them to unsaved Zotero items, then pass that on to the translator.

That makes sense. I don't think these items need to have a meaningful / persistent citation key. I think something like the behaviour of the normal BibTeX exporter - generating keys on the fly so there are no conflicts within the list - would be fine.

Would it work for you to generate keys upon export for items without them - I guess without saving these keys so after exporting the list a few times I don't end up with turingComputableNumbersApplication1937d? Would this conflict with the default export behaviour at all?

retorquere commented 2 years ago

I could easily add missing-key-N or something like it, but I'd rather not generate more complicated keys. The key manager needs access to parts of zotero that translators don't have, and I'd rather not complicate that implementation to exhibit context-dependent behavior.

Dominic-DallOsto commented 2 years ago

Ahh yep, that makes sense!

Would it be possible for me to pass these items to the key manager and generate keys before the items get to the translator, in a way that doesn't break the key manager if these items aren't saved?

retorquere commented 2 years ago

With minor changes to the export translator it is possible. You can request a key for an item, saved or not, using

const key = Zotero.BetterBibTeX.KeyManager.propose(item).citekey

You can then append that to the extra field as:

Citation Key: <key>

if there currently is no such line. The changed translator will then pick that up as usual (it currently relies on the pickup already being done). All in all it would look like

const proposed = []
....
const extra = item.getField('extra')
if (!extra.match(/(\n|^)Citation Key:/i)) {
  const proposal = Zotero.BetterBibTeX.KeyManager.propose(item, proposed).citekey
  proposed.push(proposal)
  item.setField('extra', `${extra}\nCitation Key: ${proposal}`.trim())
}

if you open an issue on the BBT project I can get you a test build with the required BBT change.

The key you get from propose is disambiguated against existing keys in BBTs databases and against the string array you pass in the second parameter. This way you will get unambiguous keys for the entire export.

Dominic-DallOsto commented 2 years ago

Thanks for the code snippet - that works perfectly. The items have citation keys in the extra field before being passed to the translator. I'm still getting the same error, despite the extra field seemingly being set correctly in the error message.

I reported this over at BBT

retorquere commented 2 years ago

That's because it needs companion changes in BBT. A build that implements them has been made available on https://github.com/retorquere/zotero-better-bibtex/issues/2018. Once I get confirmation that this combination works, I'll be happy to fold that into a new release and put that out.

Dominic-DallOsto commented 2 years ago

Yep, that works. Just to check does if(Zotero.BetterBibTeX) work to check whether Better BibTeX is installed and ready?

retorquere commented 2 years ago

Yep, that works.

Super, I'll roll that out in a release today or tomorrow

Just to check does if(Zotero.BetterBibTeX) work to check whether Better BibTeX is installed

Yes

and ready?

No, unfortunately. BBT, like all Zotero extensions, may only start part of it's initialization once Zotero.Schema.schemaUpdatePromise clears; it takes Zotero quite a bit of time to get there, and then BBT still has a few things to finish. So in my experience there's at least a good 2-5 seconds after start where your extension may have loaded but Zotero and BBT are not ready for everything (this is the "waiting for Zotero" popup that BBT puts up).

These are things you can test for:

diegodlh commented 2 years ago

@Dominic-DallOsto, if a citation is linked to a saved Zotero item, would it make sense to use its key instead of one generated on the fly? As I understand this may require further coding, if it does make sense we could close this and open a separate "enhancement" issue.

Dominic-DallOsto commented 2 years ago

Yeah, that would definitely make sense.

At the moment this works if you copy an item to bibtex, then import a citation using this bibtex (ie. the extra field gets preserved and saved with the citation). In this case, this citation key is used when exporting the item.

But if a cited item happens to be in your library and you use the "link to existing Zotero items" functionality, the citation data isn't updated so the citation key from the item won't be used. If we resolve #15 this should work.

Edit: but yeah, probably with a modification to also sync the extra field

github-actions[bot] commented 3 months ago

:rocket: This ticket has been resolved in v1.0.0-beta.0. See Release 1.0.0-beta.0 for release notes.