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

  1. Install Drupal.
  2. Enable the bibcite_bibtex and bibcite_import submodules.
  3. Save one of the BibTeX snippets to a file, say example.bib.
  4. Visit /admin/content/bibcite/reference/import.
  5. 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

CommentFileSizeAuthor
bibtexparser-pr-6.patch779 bytesbenjifisher

Issue fork bibcite-3580362

Command icon 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

benjifisher created an issue. See original summary.

benjifisher’s picture

Title: [BibTeX] Import fails when using "..." instead of {...} » [BibTeX] Import fails when using "{...}" instead of {...}
Issue summary: View changes

If the maintainers think that renanbr/bibtex-parser is the right replacement, then I think I can prepare a MR to do it.

benjifisher’s picture

There is probably room for improvement in the attached patch.

mark_fullmer’s picture

Thanks 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.

benjifisher’s picture

@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 author entry is being processed. I will keep my fingers crossed and see whether the newer parser fixes that problem, too.

@ARTICLE{Gao2025-hy,
  title    = "Structures of human organellar {SPFH} protein complexes",
  author   = "Gao, Jingjing and Sherpa, Dawafuti and Kupko, Nikita and Chino,
              Haruka and Zeng, Jianwei and Shao, Sichen",
  abstract = "Stomatin, Prohibitin, Flotillin, and HflK/C (SPFH) family
              proteins are found in all kingdoms of life and in multiple
              eukaryotic organelles. SPFH proteins assemble into homo- or
              hetero-oligomeric rings that form domed structures. Most SPFH
              assemblies also abut a cellular membrane, where they are
              implicated in diverse functions ranging from membrane
              organization to protein quality control. However, the precise
              architectures of different SPFH complexes remain unclear. Here,
              we report single-particle cryo-EM structures of the endoplasmic
              reticulum (ER)-resident Erlin1/2 complex and the mitochondrial
              prohibitin (PHB1/2) complex, revealing assemblies of 13
              heterodimers of Erlin1 and Erlin2 and 11 heterodimers of PHB1 and
              PHB2, respectively. We also describe key interactions underlying
              the architecture of each complex and conformational heterogeneity
              of the PHB1/2 complex. Our findings elucidate the distinct
              stoichiometries and properties of human organellar SPFH complexes
              and highlight common principles of SPFH complex organization.",
  journal  = "Nature Communications",
  volume   =  16,
  number   =  1,
  pages    = "10064",
  month    =  nov,
  year     =  2025
}

benjifisher’s picture

Status: Active » Needs work

This is definitely a work in progress, but I think the hard part is done.

For testing purposes, I added the line

        "renanbr/bibtex-parser": "^2.2.0"

to my project's composer.json, under the require key.

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 @todo comments in the code, and obviously I need to test it on some more varied BibTeX input.

benjifisher’s picture

Oh, good: there is a failing test. That tells me what to do next.

benjifisher’s picture

Status: Needs work » Needs review

I still want to do more of my own testing, with real-life .bib files, 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.

benjifisher’s picture

Status: Needs review » Needs work

@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:

  1. Both BibTeX and RIS formats set bibcite_date without respecting the mm/yyyy format.
  2. Unmapped reference types do not seem to default to Miscellaneous.

For (1), BibTeX (by default) maps month to bibcite_date. RIS maps DA to bibcite_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:

  /**
   * Default reference type. Will be assigned for types without mapping.
   *
   * @var string
   */
  const DEFAULT_REF_TYPE = 'miscellaneous';

I have not tested on a plain Drupal install, so maybe we have done something to break it, but I get the error

Entry has not been parsed. 'incollection' type is not mapped to reference type. Check mapping configuration.

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.

mark_fullmer’s picture

Assigned: Unassigned » mark_fullmer

Thanks for the summary, @benjifisher.

Both BibTeX and RIS formats set bibcite_date without respecting the mm/yyyy format...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.

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.

Unmapped reference types do not seem to default to Miscellaneous....I have not tested on a plain Drupal install, so maybe we have done something to break it, but I get the error Entry has not been parsed. 'incollection' type is not mapped to reference type. Check mapping configuration.

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...

mark_fullmer’s picture

I added some test coverage to help test the library replacement. While renanbr/bibtex-parser does 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, the renanbr/bibtex-parser left hanging curly braces on the author names: Milton Abramowitz} and Irene Stegun}.

Similarly, the test coverage I added shows the curly braces retained in a title, rather than removed, and didn't handle the newline:

   ├ Failed asserting that two strings are equal.
   ┊ ---·Expected
   ┊ +++·Actual
   ┊ @@ @@
   ┊ -'Canoe·Tours·in·Sweden'
   ┊ +'Canoe·Tours\n
   ┊ +··in·{S}weden'

The same test, using the original library, fails on the quotes, but does appear to correctly handle curly braces and newlines:

   ├ Failed asserting that two strings are equal.
   ┊ ---·Expected
   ┊ +++·Actual
   ┊ @@ @@
   ┊ -'Canoe·Tours·in·Sweden'
   ┊ +'"Canoe·Tours·in·Sweden"'

Finally, I'm seeing a PHP 8.4 deprecation notice on renanbr/bibtex-parser:

RenanBr\BibTexParser\Processor\TrimProcessor::__construct(): Implicitly marking parameter $fields as nullable is deprecated, the explicit nullable type must be used instead

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.json file.

benjifisher’s picture

@mark_fullmer:

There are six commits on the main branch since the latest release (2.2.0), one with the commit message

PHP 8.4 support

Should we reach out to the maintainers and encourage them to make a new release? Should we require the main branch 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.

benjifisher’s picture

In Comment #10, I wrote,

Unmapped reference types do not seem to default to Miscellaneous.
...
I have not tested on a plain Drupal install, so maybe we have done something to break it, ...

I did some more testing, and confirmed that we did something to break it.

benjifisher’s picture

@mark_fullmer:

Date format

From Comment #11:

… if we’re sure that this is an incorrect default mapping for both BibTex and RIS, rather than a subjective judgment call, …

I do not think the mapping is the problem. The DA field (RIS) and the month and year fields (BibTex) should be mapped to the bibcite_date field. The problem is the format: bibcite_date expects "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.

benjifisher’s picture

Status: Needs work » Needs review
mark_fullmer’s picture

This 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.json in https://git.drupalcode.org/project/bibcite/-/merge_requests/57#note_736314

Otherwise, I'm ready to RTBC this...!

benjifisher’s picture

@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:

  • abstract
  • title
  • author
  • editor
  • month

and encode() (indirectly, via buildEntry() and buildLine()) has custom handling for author and keywords. So it would be good to add these fields to at least one of the test entities:

  • abstract (with line breaks)
  • editor (same format as author: separated by "and", maybe with line breaks)
  • keywords

Another open question: in Comment #13, I asked,

Should we reach out to the maintainers and encourage them to make a new release? Should we require the `main` branch or a specific (untagged) commit?

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.

mark_fullmer’s picture

Should we reach out to the maintainers and encourage them to make a new release? Should we require the `main` branch or a specific (untagged) commit?

Look at that responsiveness! https://github.com/renanbr/bibtex-parser/issues/117#issuecomment-4239279839

mark_fullmer’s picture

Okay, I added test coverage as indicated. This looks good to me...?!

benjifisher’s picture

The additional test looks good to me.

The next time we need an example, it will be one of my papers. ;)

mark_fullmer’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.