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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ply-bibtex-parser-0.1.0.tar.gz
.
File metadata
- Download URL: ply-bibtex-parser-0.1.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.8.3 Darwin/19.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd0ebf310714b666eb52b5d174f72e1c27c13e262d7841faacb499a294d71ecd |
|
MD5 | e81d0603dc4db7da89671d7d5c046415 |
|
BLAKE2b-256 | 89d578251e0456243c176f6a34ad0ec9d53808dcbfee563d4cde104d0ee79d9c |
File details
Details for the file ply_bibtex_parser-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: ply_bibtex_parser-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.8.3 Darwin/19.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e15c03dbe55dba4dbc6ca5e3a7b6bbac11abbd98f135dfa675d15857dac279ce |
|
MD5 | 53af9cf5c77498e7f95126cbcfcfb40a |
|
BLAKE2b-256 | d2caf60f829349cf8a48b536e698a920ce65f8c3fc3bbdb1adbf7811549ac47a |