Problem/Motivation
I try to import a simple BibTeX reference, like the one at /admin/help/bibcite_bibtex:
@Book{abramowitz+stegun,
author = "Milton {Abramowitz} and Irene A. {Stegun}",
title = "Handbook of Mathematical Functions with
Formulas, Graphs, and Mathematical Tables",
publisher = "Dover",
year = 1964,
address = "New York City",
edition = "ninth Dover printing, tenth GPO printing"
}
That works, but Title, Edition, and Authors are imported with an extra layer of quotation marks. For example, Edition is '"ninth Dover printing, tenth GPO printing"' instead of 'ninth Dover printing, tenth GPO printing'
Worse: if I try to import a reference where the author value has both quotes and braces, like
@Book{abramowitz+stegun-brain,
author = "{BRAIN Initiative Cell Census Network (BICCN)}",
title = "Handbook of Mathematical Functions with
Formulas, Graphs, and Mathematical Tables",
publisher = "Dover",
year = 1964,
address = "New York City",
edition = "ninth Dover printing, tenth GPO printing"
}
then I get an error:
ADCI\FullNameParser\Exception\FirstNameNotFoundException: Couldn't find a first name. in ADCI\FullNameParser\Parser->findFirstName() (line 555 of /var/www/html/vendor/adci/full-name-parser/src/Parser.php).
Steps to reproduce
- Install Drupal.
- Enable the
bibcite_bibtexandbibcite_importsubmodules. - Save one of the BibTeX snippets to a file, say
example.bib. - Visit
/admin/content/bibcite/reference/import. - Browse to the file, Select Format: BibTeX, and submit the form.
Proposed resolution
The problem seems to be in the audiolabs/bibtexparser package, a dependency of this module.
Work around
Site owners can apply the patch bibtexparser-pr-6.patch. This comes from https://github.com/audiolabs/bibtexparser/pull/6, a pull request from 2021.
To be decided
A longer-term solution would be to replace audiolabs/bibtexparser, which seems to be un-maintained, with something more modern. The best option I have found so far is renanbr/bibtex-parser (GitHub repo: https://github.com/renanbr/bibtex-parser): it requires PHP 5.6 instead of 5.3, and it has a release from 2023 instead of 2020. Also, the main branch supports PHP 8.0 to 8.4 (maybe even 5.6 to 8.4).
renanbr/bibtex-parser is not a drop-in replacement (i.e., it does not use the same API), but in this module the library is used in only one file (not counting the implementation of hook_requirements()), so code updates should not be too hard. And I think some of the code in this module could be simplified.
Remaining tasks
User interface changes
N/A
API changes
N/A
Data model changes
N/A
| Comment | File | Size | Author |
|---|---|---|---|
| bibtexparser-pr-6.patch | 779 bytes | benjifisher |
Issue fork bibcite-3580362
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
benjifisherIf the maintainers think that
renanbr/bibtex-parseris the right replacement, then I think I can prepare a MR to do it.Comment #3
benjifisherThere is probably room for improvement in the attached patch.
Comment #4
mark_fullmerThanks for the clear writeup of the problem, the temporary workaround patch, and the initial research for a more recent BibTex parser,
renanbr/bibtex-parser.I agree with your general perspective that while refactoring would be required to change the library, it should be manageable.
Comment #5
benjifisher@mark_fullmer:
Thanks for the encouragement. I will try the newer package and see if that helps.
I also ran into another problem: using the input below, I get four authors instead of six when I import, and the fourth is "Chino" instead of "Haruka Chino". It seems that just the first line of the
authorentry is being processed. I will keep my fingers crossed and see whether the newer parser fixes that problem, too.Comment #7
benjifisherThis is definitely a work in progress, but I think the hard part is done.
For testing purposes, I added the line
to my project's
composer.json, under therequirekey.I tested it on the BibTeX entry in Comment #5. It works pretty well: the only problem I notice is that the braces are still in the title: "{SPFH}". As I hoped, it fixes the problem I mentioned there.
There are some new
@todocomments in the code, and obviously I need to test it on some more varied BibTeX input.Comment #8
benjifisherOh, good: there is a failing test. That tells me what to do next.
Comment #9
benjifisherI still want to do more of my own testing, with real-life
.bibfiles, but the automated tests are now passing, so I am setting the issue status to NR in case someone else is eager to help test.Comment #10
benjifisher@mark_fullmer:
I have imported over 100 BibTeX entries without errors. I will try to get someone at work to spot-check that they imported correctly.
I notice a couple of other problems:
bibcite_datewithout respecting the mm/yyyy format.For (1), BibTeX (by default) maps
monthtobibcite_date. RIS mapsDAtobibcite_date. I added a fix for that (both formats) in the last commit, but I am happy to move that to a separate issue, since it is not really in scope for this one. Also, since it affects at least two formats, I wonder if there is a better place to fix it. At work, we have entity validation (after #3553399: Missing entity validation during import).For (2), I notice the code comment in
Drupal\bibcite_entity\Normalizer\ReferenceNormalizerBase:I have not tested on a plain Drupal install, so maybe we have done something to break it, but I get the error
when the reference type is not mapped. I am not sure whether this is just for BibTeX. Probably I should open a separate issue for that.
Comment #11
mark_fullmerThanks for the summary, @benjifisher.
I haven't looked closely at the new logic for this yet, but if we're sure that this is an incorrect default mapping for both BibTex and RIS, rather than a subjective judgment call, I'm fine including that bugfix in this issue, even if it goes beyond the boundaries of the initial scope of the issue.
Okay, I'll try to reproduce on a plain installation. That ReferenceNormalizeBase class was touched twice recently, first for Drupal 11 compatibility and then to fix incorrect implementation from that (#3405196: Undefined array key "weight" with Layout Builder, #3362645: Declaration of Drupal\bibcite_bibtex\Encoder\BibtexEncoder::encode($data, $format, array $context = []) must be compatible with Symfony\Component\Serializer\Encoder\EncoderInterface::encode(mixed $data, string $format, array $context = []): string), so it's plausible you've tested and found a scenario where that code still isn't right. But yeah, this should be treated as a separate issue.
Assigning myself for review...
Comment #12
mark_fullmerI added some test coverage to help test the library replacement. While
renanbr/bibtex-parserdoes appear to fix quoting of strings in titles/authors, it does not seem to have support for curly braces, which BibTex uses to indicate that something must be capitalized. Looking at https://www.bibtex.com/e/entry-types/ , it looks like this is supposed to be supported in multiple fields -- title, author, etc.When I did a test import in the UI using the example in
admin/help/bibcite_bibtex, therenanbr/bibtex-parserleft hanging curly braces on the author names:Milton Abramowitz}andIrene Stegun}.Similarly, the test coverage I added shows the curly braces retained in a title, rather than removed, and didn't handle the newline:
The same test, using the original library, fails on the quotes, but does appear to correctly handle curly braces and newlines:
Finally, I'm seeing a PHP 8.4 deprecation notice on
renanbr/bibtex-parser:Given that the library seems relatively currently maintained and does have existing test coverage, I think it's worth reporting and offering fixes at https://github.com/renanbr/bibtex-parser , rather than trying to fix things in Bibcite.
I also left a question on the MR about specifying minimum PHP compatibility in Bibcite's
composer.jsonfile.Comment #13
benjifisher@mark_fullmer:
There are six commits on the main branch since the latest release (2.2.0), one with the commit message
Should we reach out to the maintainers and encourage them to make a new release? Should we require the
mainbranch or a specific (untagged) commit? It might also be worth looking at the 16 forks of the project. Maybe one of them is more actively maintained.I will try to find time next week to see what I can do about the braces and the linebreaks.
Comment #14
benjifisherIn Comment #10, I wrote,
I did some more testing, and confirmed that we did something to break it.
Comment #15
benjifisher@mark_fullmer:
Date format
From Comment #11:
I do not think the mapping is the problem. The
DAfield (RIS) and themonthandyearfields (BibTex) should be mapped to thebibcite_datefield. The problem is the format:bibcite_dateexpects "mm/YYYY", and the examples I am looking at have "2026/01/14" (RIS) and "oct", "2021" (BibTex).Braces in author names
The new(er) parser does not treat the braces specially. In the example we have been looking at, I see "Milton {Abramowitz}" and "Irene A. {Stegun}". The problem seems to be when the Contributor entity is created, the name gets parsed into first, middle, and last: "Milton", "{", "Abramowitz}" or "Irene", "A. {", "Stegun}". Then the first and last are combined to get "Milton Abramowitz}" or "Irene Stegun}".
But that is just for the sake of curiosity. I think, to keep backwards compatibility with the old(er) parser, I can just strip braces from author names, titles, abstracts, etc.
Line breaks in titles
We already strip newlines from abstracts. I can do the same thing for titles. Again, this will improve backwards compatibility.
Comment #16
benjifisherComment #17
mark_fullmerThis is great work, @benjifisher. Stripping newlines and curly braces on the Bibcite end of things seems fine to me. The new test coverage is passing, and I'm pretty comfortable with the code review, so just wondering if you think there is a need for any more variations on the test coverage or if this is good enough for now, and also one remaining question about adding a PHP minimum to Bibcite's
composer.jsonin https://git.drupalcode.org/project/bibcite/-/merge_requests/57#note_736314Otherwise, I'm ready to RTBC this...!
Comment #18
benjifisher@mark_fullmer:
Thanks for the kind words, and for adding the test coverage.
I replied on the MR about PHP versions.
The new
decode()method has custom handling for the following fields:and
encode()(indirectly, viabuildEntry()andbuildLine()) has custom handling for author and keywords. So it would be good to add these fields to at least one of the test entities:Another open question: in Comment #13, I asked,
I created an issue for the library: https://github.com/renanbr/bibtex-parser/issues/117.
I do not like the idea of requiring a branch or an untagged commit in the module. We might suggest, in the release notes, that site owners do that in their own `composer.json`. I think that will work, but it is hard to test until this issue gets fixed.
Comment #19
mark_fullmerLook at that responsiveness! https://github.com/renanbr/bibtex-parser/issues/117#issuecomment-4239279839
Comment #20
mark_fullmerOkay, I added test coverage as indicated. This looks good to me...?!
Comment #21
benjifisherThe additional test looks good to me.
The next time we need an example, it will be one of my papers. ;)
Comment #22
mark_fullmer