| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import os |
| 4 |
import sys |
| 5 |
import warnings |
| 6 |
from optparse import OptionParser |
| 7 |
|
| 8 |
# The references are parsed from a BibTeX database, so we import the |
| 9 |
# corresponding parser. |
| 10 |
from citeproc.source.bibtex import BibTeX |
| 11 |
|
| 12 |
# Import the citeproc-py classes we'll use below. |
| 13 |
from citeproc import frontend |
| 14 |
from citeproc import CitationStylesStyle, CitationStylesBibliography |
| 15 |
from citeproc import formatter |
| 16 |
from citeproc import Citation, CitationItem |
| 17 |
|
| 18 |
|
| 19 |
def main(): |
| 20 |
usage = \ |
| 21 |
"""usage: %prog [options] <bib_file>' |
| 22 |
|
| 23 |
Output a formatted bibliography from bibtex file <bib_file> to stdout |
| 24 |
|
| 25 |
You might want to put the path to your CSL styles in the `CSL_STYLES_PATH` |
| 26 |
environment variable with something like (bash shell):: |
| 27 |
|
| 28 |
export CSL_STYLES_PATH=$HOME/dev_trees/styles |
| 29 |
""" |
| 30 |
parser = OptionParser(usage) |
| 31 |
parser.add_option('-s', '--style', dest='style', |
| 32 |
default='harvard-cite-them-right', |
| 33 |
help='style file', metavar='STYLE_FILE') |
| 34 |
parser.add_option('-f', '--format', dest='format', default='rst', |
| 35 |
help='output type', metavar='FORMAT') |
| 36 |
(options, args) = parser.parse_args() |
| 37 |
if len(args) != 1: |
| 38 |
parser.print_usage() |
| 39 |
sys.exit(1) |
| 40 |
|
| 41 |
output_format = options.format |
| 42 |
try: |
| 43 |
output_format = getattr(formatter, options.format) |
| 44 |
except AttributeError: |
| 45 |
available_formatters = (attr for attr in dir(formatter) |
| 46 |
if not attr.startswith('__')) |
| 47 |
print('style should be one of: ' + ', '.join(available_formatters)) |
| 48 |
sys.exit(1) |
| 49 |
|
| 50 |
# Get styles path out of the environment variable |
| 51 |
env_path = os.environ.get('CSL_STYLES_PATH') |
| 52 |
if env_path is not None: |
| 53 |
frontend.STYLES_PATH = env_path |
| 54 |
|
| 55 |
# Silence lots of warnings |
| 56 |
warnings.simplefilter('ignore') |
| 57 |
# Read bibliography |
| 58 |
bib_source = BibTeX(args[0]) |
| 59 |
# load a CSL style (full path or name of style in style path) |
| 60 |
bib_style = CitationStylesStyle(options.style) |
| 61 |
# Create the citeproc-py bibliography, passing it the: |
| 62 |
# * CitationStylesStyle, |
| 63 |
# * BibliographySource (BibTeX in this case), and |
| 64 |
# * a formatter (plain, html, or rst) |
| 65 |
bibliography = CitationStylesBibliography(bib_style, bib_source, |
| 66 |
output_format) |
| 67 |
# Processing citations in a document need to be done in two passes as for |
| 68 |
# some CSL styles, a citation can depend on the order of citations in the |
| 69 |
# bibliography and thus on citations following the current one. |
| 70 |
# For this reason, we first need to register all citations with the |
| 71 |
# CitationStylesBibliography. |
| 72 |
# Just read every one in key order |
| 73 |
for name in sorted(bib_source): |
| 74 |
bibliography.register(Citation([CitationItem(name)])) |
| 75 |
# In the second pass, CitationStylesBibliography can generate citations. |
| 76 |
# CitationStylesBibliography.cite() requires a callback function to be |
| 77 |
# passed along to be called in case a CitationItem's key is not present in |
| 78 |
# the bibliography. |
| 79 |
for item in bibliography.bibliography(): |
| 80 |
print(str(item)) |
| 81 |
|
| 82 |
|
| 83 |
if __name__ == '__main__': |
| 84 |
main() |