python-bibtexparser
python-bibtexparser copied to clipboard
Handle undefined bibtex strings
The idea is to be able to represent undefined bibtex strings as such even if the definition does not appear in the current bibdata. This would be an alternative to including and interpolating common strings and would enable exporting bibtex files identical to the input even when they include things such as month = jan
.
@omangin any news on this issue? i was actually just in the process of trying to add this feature and thought that i should check the issues first to see if there was already a method for doing this.
actually if @omangin or @sciunto could chime in on a design that'd be useful. something as simple as a "strict = False" flag on the parser could work, but we could also imagine something like passing in a function to resolve strings not in the database. That might be more general and just as easy to implement.
just added https://github.com/sciunto-org/python-bibtexparser/pull/205 please have a look. it works for my tests, but probably needs some work to fit in more naturally.
I've solved this by creating a BibtexParser instance with interpolate_strings=False
. This leaves expressions involving strings as BibDataStringExpression
s, which can be inspected or manually interpolated.
Indeed, both solutions seem good. I believe the motivation of the issue was about being able to see string values in a way similar to using common_strings
but not include them when calling the bwriter
.
For that specific point, I like the implementation from @noahdesu in #205 and I think it would actually be good to advise using it instead of include_common_strings
(by providing the corresponding function) in the documentation. We would keep the other one for backward compatibility.
Any though on it before moving toward merging #205?
@omangin it looks you had some provided some minor feedback on the #205 changes. Let me know if / when you decide to go with that approach and I'll address those comments.
@sciunto Do you have an opinion on this?
As a summary the idea is to be able to set a function that is used as a default to interpolate undefined strings. This has the advantage of handling undefined-but-common strings without having to actually add definitions into the bibdata's string dictionary.
As far as I am concerned I think it is more elegant then the current implementation and would be in favor of including it.
@omangin Thanks for raising this to my attention. Yes, I like this idea. So, +1.
Turning off string interpolation is helpful but not enough. I'd like a warning but the undefined string unchanged (so that the .bib file is still successfully parsed). Imo, a runtime crash on an undefined string seems like overkill: could you change bibdatabase.expand_string
to warn instead of raise (at least until another solution is implemented)?
Note: #205 was close to a merge (tests & docs missing) but was otherwise decided to be acceptable.
However, it was closed by the PR opener. We'd need someone to clone that branch and complete it.
Stale PR no one is working on anymore. Seems not relevant for v2. Closing issue (#203) and PR (#205). If anyone wants to go back and look at the PR for a v1 fix, feel free.