Skip to main content

A simple ply-based parser for BibTeX

Project description

ply-bibtex-parser

A simple ply-based parser for BibTeX

ply is an easy-to-use, pure-Python parser generator. However it's not the most modern or performant tool.

This is mostly an academic exercise into writing a simple parser, but I used it in my personal website in order to parse a .bib file to identify the different citations and write them back after reformatting them.

Install

pip install ply-bibtex-parser

Usage

Parsing

The main entry point is through parser:

from ply_bibtex_parser import parser
input = '@article{citekey, author={Smith, Micah J.}, title={Foo bar}}'
parser.parse(input)
# [BibtexEntry(type='article', key='citekey', fields={'author': '{Smith, Micah J.}', 'title': '{Foo bar}'})]

The parser produces a possibly-empty list of BibtexEntry objects. Note that values in the entry which are usually delimited with braces contain the braces: this makes it easy to write back the bibtex entry without trying to escape its contents. The caller is responsible for further parsing the values if they intend to use the string after escaping is performed.

Lexing

You can also use the lexer directly:

from ply_bibtex_parser import lexer
input = '@article{citekey, author={Smith, Micah J.}, title={Foo bar}}'
lexer.input(input)
for tok in lexer:
    print(tok)
# LexToken(AT,'@',1,0)
# LexToken(ID,'article',1,1)
# LexToken(ENTRYBEGIN,'{',1,8)
# LexToken(ID,'citekey',1,9)
# LexToken(COMMA,',',1,16)
# LexToken(ID,'author',1,18)
# LexToken(EQUALS,'=',1,24)
# LexToken(VALUE,'{Smith, Micah J.}',1,41)
# LexToken(COMMA,',',1,42)
# LexToken(ID,'title',1,44)
# LexToken(EQUALS,'=',1,49)
# LexToken(VALUE,'{Foo bar}',1,58)
# LexToken(ENTRYEND,'}',1,59)

Development

Install

pip install poetry
poetry install

Check

inv test lint

Limitations

See the TODO notes in ./ply_bibtex_parser/lexer.py and ./ply_bibtex_parser/parser.py.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for ply-bibtex-parser, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size ply_bibtex_parser-0.1.0-py3-none-any.whl (5.1 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size ply-bibtex-parser-0.1.0.tar.gz (4.8 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page