Giter Club home page Giter Club logo

Comments (4)

MiWeiss avatar MiWeiss commented on September 28, 2024

Hey :-)

Your case is a bit tricky, as it does not only contain a duplicate block key, but also a duplicate field key.

In principle, middleware (which is responsible for the transformations of the parsed bibtex, e.g. to remove the enclosings is not applied to error blocks (as in most cases, their behavior would be undefined). The deliberate default is thus to just pass on these blocks when applying the middleware.

Note however that you can change the used middleware, and may just start by adding a middleware layer which attempts to ignore all errors:

from bibtexparser import parse_string
from bibtexparser.middlewares import BlockMiddleware, ResolveStringReferencesMiddleware, RemoveEnclosingMiddleware
from bibtexparser.model import Entry, Block, DuplicateBlockKeyBlock, DuplicateFieldKeyBlock
from bibtexparser import Library
from typing import Union, Collection


class IgnoreAllErrors(BlockMiddleware):
  
  def transform_block(self, block: Block, library: Library) -> Union[Block, Collection[Block], None]:
    print("bla")
    try:
      print("try")
      return self.transform_entry(block.ignore_error_block, library)
    except AttributeError:
      print("nf")
      return block

parse_stack = [IgnoreAllErrors(True), ResolveStringReferencesMiddleware(True), RemoveEnclosingMiddleware(True)]


library = parse_string(r"""
@article{foobar,
    doi = {abc},
    note = {Note 1},
    note = {Note 2},
}""", parse_stack=parse_stack)

print(library.blocks[0])

from python-bibtexparser.

nschloe avatar nschloe commented on September 28, 2024

Your case is a bit tricky, as it does not only contain a duplicate block key, but also a duplicate field key.

There's only one duplication here, right? Just the note field.

Anyway, I'll try this out. I'd like to have as little code as possible in my repo, so I'm voting for a duplicate_fields: Literal["ignore", "error", "parse"] argument to parse() here.

from python-bibtexparser.

nschloe avatar nschloe commented on September 28, 2024

Importing BlockMiddleware fails. Please reopen.

from bibtexparser.middlewares import BlockMiddleware
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[1], line 1
----> 1 from bibtexparser.middlewares import BlockMiddleware

ImportError: cannot import name 'BlockMiddleware' from 'bibtexparser.middlewares' (/home/nschloe/venv/lib/python3.11/site-packages/bibtexparser/middlewares/__init__.py)

from python-bibtexparser.

nschloe avatar nschloe commented on September 28, 2024

Suggestion: Remove DuplicateBlockKeyBlock and just parse an entry with duplicated field keys as Entry. Perhaps add a duplicate_keys attribute to Entry. Better still a function has_duplicate_fields() that runs over the fields list.

from python-bibtexparser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.