pybtex-docutils
pybtex-docutils is a Docutils backend for the Pybtex bibliography processor. It enables the insertion of BibTeX citations into documentation generated by Docutils, particularly for reStructuredText. The current version is 1.0.3, and while releases are not on a strict schedule, the library is actively maintained.
Warnings
- gotcha When using the `simplebibliography` directive directly with Docutils, citation keys in your BibTeX file cannot contain colons. This is because Docutils uses citation keys as labels, and colons are not valid characters for these labels. This limitation is lifted when using `sphinxcontrib-bibtex`.
- gotcha The `simplebibliography` directive provided by `pybtex-docutils` is primarily intended for simple single-document Docutils workflows. For projects built with Sphinx, it is strongly recommended to use `sphinxcontrib-bibtex` instead, as it offers more features and better integration with Sphinx's build process.
- gotcha Extending Docutils with custom directives like `simplebibliography` currently requires writing a custom Python command script that registers the directive before processing the reStructuredText document. There is no simpler plugin mechanism for pure Docutils outside of a custom script.
Install
-
pip install pybtex-docutils
Imports
- SimpleBibliography
from pybtex_docutils import SimpleBibliography
- Backend
from pybtex_docutils import Backend
Quickstart
import io
from docutils.parsers.rst import directives, Directive
from docutils.core import publish_cmdline, default_description
from pybtex_docutils import SimpleBibliography
# Create a dummy .bib file
with open('refs.bib', 'w') as f:
f.write('''
@Book{Nelson1987,
author = {Edward Nelson},
title = {Radically Elementary Probability Theory},
publisher = {Princeton University Press},
year = {1987}
}
''')
# Create a dummy .rst file
with open('example.rst', 'w') as f:
f.write('''
.. highlight:: python
Example Document
================
See {Nelson1987}_ for an introduction to non-standard analysis.
.. simplebibliography:: refs.bib
''')
# Register the directive and publish the document
description = ('Like rst2html5.py, but with .. simplebibliography support' + default_description)
directives.register_directive("simplebibliography", SimpleBibliography)
publish_cmdline(writer_name='html5', description=description, argv=['example.rst', 'example.html'])
print("Generated example.html with bibliography.")