shyamd / mkdocs-bibtex

A MkDocs plugin for citation management using bibtex
Other
79 stars 21 forks source link

Error occurs when "journal" is empty #144

Closed yuriever closed 2 years ago

yuriever commented 2 years ago

Hi!

When citing preprints such that the bibtex field "journal" is empty, I got errors like

Error reading page 'xx': missing journal in xx
Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.9/site-packages/pybtex/database/__init__.py", line 503, in _find_field
    return self.fields[name]
  File "/opt/homebrew/lib/python3.9/site-packages/pybtex/utils.py", line 163, in __getitem__
    return self._dict[key.lower()]
KeyError: 'journal'

During handling of the above exception, another exception occurred:
......

Is there a way to escape empty fields? Thx!

shyamd commented 2 years ago

Can you provide more of the error message so I can tell if this is happening while building the internal entry list or if its from something during the conversion to markdown?

yuriever commented 2 years ago

Sorry for the late reply. Here is a least mkdocs-material project reproducing the error

mkdocs-bibtex.zip

The bibtex of the example preprint is

@article{Craig:2022cef,
    archiveprefix = {arXiv},
    author = {Craig, N. and others},
    date-added = {2022-11-13 02:26:19 +0800},
    date-modified = {2022-11-13 02:26:19 +0800},
    eprint = {2211.05772},
    month = {11},
    primaryclass = {hep-ph},
    reportnumber = {FERMILAB-FN-1212-T},
    title = {{Snowmass Theory Frontier Report}},
    year = {2022}}

which is copied at the inspirehep page. As you can see that most of preprints on arxiv don't have the journal entry.

My versions are

Python 3.10.8
mkdocs                     1.4.2
mkdocs-bibtex              2.8.5
mkdocs-exclude             1.0.2
mkdocs-material            8.5.9
mkdocs-material-extensions 1.1

The error messages are in error.txt in mkdocs-bibtex.zip.

shyamd commented 2 years ago

Ah! Ok, you're using the built-in PyBTex based style which is a pretty strict standard built-into PyBTex. I can't modify that. If you want to do any variation off of this I recommend using pandoc mode instead by providing a csl_file. That will let you choose a style that is accomodating to journal-less entries. The alternative is to add a journal={} line into the entries that don't have a journal.

yuriever commented 2 years ago

Ah! Ok, you're using the built-in PyBTex based style which is a pretty strict standard built-into PyBTex. I can't modify that. If you want to do any variation off of this I recommend using pandoc mode instead by providing a csl_file. That will let you choose a style that is accomodating to journal-less entries. The alternative is to add a journal={} line into the entries that don't have a journal.

Thx! After adding the csl file everything works.

yuriever commented 2 years ago

Just for the future comers.

    def get_article_template(self, e):
        volume_and_pages = first_of [
            # volume and pages, with optional issue number
            optional [
                join [
                    field('volume'),
                    optional['(', field('number'),')'],
                    ':', pages
                ],
            ],
            # pages only
            words ['pages', pages],
        ]
        template = toplevel [
            self.format_names('author'),
            self.format_title(e, 'title'),
            sentence [
                #tag('em') [field('journal')],
                optional[ field('journal') ],
                optional[ volume_and_pages ],
                date],
            sentence [ optional_field('note') ],
            self.format_web_refs(e),
        ]
        return template

This function controls the output of article type entries.

shyamd commented 2 years ago

Do you have timings for how much slower it was using pandoc than pybtex? It's also generally not a good idea to modify installed python packages as you might be breaking something else. I doubt thats a huge issue here, but it's just no good practice.

yuriever commented 2 years ago

Do you have timings for how much slower it was using pandoc than pybtex? It's also generally not a good idea to modify installed python packages as you might be breaking something else. I doubt thats a huge issue here, but it's just no good practice.

I have delete pandoc. In my memory, when mkdocs serve my site, using pandoc takes >2s to update changes, while pybtex almost instantaneously.

Maybe the timing is because I have modified the csl file in a wrong way. I'm not familiar with that language.