Always follow ref-format. Fixes #1067
Ignore ref provided by import data, and always follow ref-format when adding a new reference.
I did some limited testing and this seems to work. Following feedback in #1067, I did not bother adding a config flag to restore the previous behavior for now, but I could work on that if necessary.
Thanks for the PR, looks good to me! What do you think @alexfikl, should we have an option to preserve the previous behaviour?
EDIT: Maybe when collaborating, people might get bibtex entries from others and want to keep refs consistent? Just trying to come up with use cases...
So, even without a flag, there is a workaround to allow for this behavior:
formatter=jinja2
ref-format = '{{ doc["ref"] if doc["ref"] else doc["author_list"][0]["family"].lower() ~ doc["year"]}}'
would be equivalent to "keep ref if provided, else use"
ref-format = '{doc[author_list][0][family]!l}{doc[year]}'
Jinja is necessary because AFAIK the PythonFormatter method does not allow conditionals, and if we have ref-format = '{doc[ref]}' but with an empty ref it just does not create one (which is expected).
This is somewhat tricky, so maybe we could add an example to the documentation? Even just mentioning in ref-format documentation that the previously-given value for ref is available for use.
Thanks for working on this! I agree with the general idea, but not the implementation here. I would recommend:
- Actually fixing the BibTeX importer to not save the reference key (i.e. just deleting it in the
fetch_datamethod, if it's there). Most of the importers don't do this by default, so it's good to be consistent. - Add few lines like in
papis/commands/update.py:
https://github.com/papis/papis/blob/6ac9b4383241b07fbb12ee04ed497bc13f5d2ff6/papis/commands/update.py#L557-L564
somewhere around here (after the importer data is merged):
https://github.com/papis/papis/blob/6ac9b4383241b07fbb12ee04ed497bc13f5d2ff6/papis/commands/add.py#L540-L542
Just straight up ignoring the ref key like in this PR doesn't seem like a good idea, since it would also overwrite whatever the user set manually using --set ref <MY_REF>.
I think this should also fix your issue. What do you think?
EDIT: Maybe when collaborating, people might get bibtex entries from others and want to keep refs consistent? Just trying to come up with use cases...
Hm, I'm not sure we have a good workflow around something like that.. although it would be lovely, since I also hit that annoying issue quite a few times :(.
My thought was that that would be a better fit for the papis bibtex command? That could be a bit more careful with reading/writing existing refs, while papis add is more of a "chug this into my library with my preferred formats" sort of command.
My thought was that that would be a better fit for the
papis bibtexcommand? That could be a bit more careful with reading/writing existingrefs, whilepapis addis more of a "chug this into my library with my preferred formats" sort of command.
Yes, that makes sense to me!