Skip to content

Commit

Permalink
Merge pull request #97 from mcmtroffaes/feature/style_config_value
Browse files Browse the repository at this point in the history
Add bibtex_default_style option to override the default bibliography style.

Resolves issue #91.
  • Loading branch information
mcmtroffaes committed Mar 1, 2016
2 parents aaf7d10 + 593c34e commit 56dcb03
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 19 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
* Document LaTeX workaround for ``:cite:`` in figure captions
(contributed by xuhdev, see issue #92 and pull request #93).

* Add ``bibtex_default_style`` config value to override the default
bibliography style (see issue #91 and pull request #97).

0.3.3 (23 October 2015)
-----------------------

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.4a0
0.3.4a1
2 changes: 2 additions & 0 deletions sphinxcontrib/bibtex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def check_duplicate_labels(app, env):
def setup(app):
"""Set up the bibtex extension:
* register config values
* register directives
* register nodes
* register roles
Expand All @@ -121,6 +122,7 @@ def setup(app):
:type app: :class:`sphinx.application.Sphinx`
"""

app.add_config_value("bibtex_default_style", "alpha", "html")
app.add_directive("bibliography", BibliographyDirective)
app.add_role("cite", CiteRole())
app.add_node(bibliography)
Expand Down
8 changes: 3 additions & 5 deletions sphinxcontrib/bibtex/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
:members:
"""

import sys
import six
if sys.version_info < (2, 7): # pragma: no cover
from ordereddict import OrderedDict
else: # pragma: no cover
try: # pragma: no cover
from collections import OrderedDict

except ImportError: # pragma: no cover
from ordereddict import OrderedDict
import ast
import collections
import copy
Expand Down
7 changes: 4 additions & 3 deletions sphinxcontrib/bibtex/directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def run(self):
filter_ = ast.parse(self.options["filter"])
except SyntaxError:
env.app.warn(
standout("syntax error in :filter: expression")
+ " (" + self.options["filter"] + "); "
standout("syntax error in :filter: expression") +
" (" + self.options["filter"] + "); "
"the option will be ignored"
)
filter_ = ast.parse("cited")
Expand All @@ -117,7 +117,8 @@ def run(self):
list_=self.options.get("list", "citation"),
enumtype=self.options.get("enumtype", "arabic"),
start=self.options.get("start", 1),
style=self.options.get("style", "alpha"),
style=self.options.get(
"style", env.app.config.bibtex_default_style),
filter_=filter_,
encoding=self.options.get(
'encoding',
Expand Down
10 changes: 5 additions & 5 deletions test/custom_style/conf.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
extensions = ['sphinxcontrib.bibtex']
exclude_patterns = ['_build']

# create and register pybtex plugins

from pybtex.style.formatting.unsrt import Style as UnsrtStyle
from pybtex.style.template import words
from pybtex.plugin import register_plugin


extensions = ['sphinxcontrib.bibtex']
exclude_patterns = ['_build']


class NoWebRefStyle(UnsrtStyle):

def format_web_refs(self, e):
# the following is just one simple way to return an empty node
return words['']


register_plugin('pybtex.style.formatting', 'nowebref', NoWebRefStyle)
9 changes: 4 additions & 5 deletions test/issue77/conf.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
extensions = ['sphinxcontrib.bibtex']
exclude_patterns = ['_build']

# create and register pybtex plugins

from pybtex.style.formatting.unsrt import Style as UnsrtStyle
from pybtex.style.labels.alpha import LabelStyle as AlphaLabelStyle
from pybtex.plugin import register_plugin


extensions = ['sphinxcontrib.bibtex']
exclude_patterns = ['_build']


class ApaLabelStyle(AlphaLabelStyle):
def format_label(self, entry):
return "APA"
Expand Down
3 changes: 3 additions & 0 deletions test/issue91/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extensions = ['sphinxcontrib.bibtex']
exclude_patterns = ['_build']
bibtex_default_style = 'plain'
3 changes: 3 additions & 0 deletions test/issue91/contents.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:cite:`2009:mandel`

.. bibliography:: test.bib
10 changes: 10 additions & 0 deletions test/issue91/test.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@Misc{2009:mandel,
author = {Jan Mandel},
title = {A Brief Tutorial on the Ensemble {K}alman Filter},
howpublished = {arXiv:0901.3725v1 [physics.ao-ph]},
month = jan,
year = {2009},
archivePrefix = {arXiv},
eprint = {0901.3725},
primaryClass = {physics.ao-ph}
}
28 changes: 28 additions & 0 deletions test/test_issue91.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
test_issue91
~~~~~~~~~~~~
Test bibtex_default_style config value.
"""

import os.path
import re

from sphinx_testing.util import path, with_app

srcdir = path(__file__).dirname().joinpath('issue91').abspath()


def teardown_module():
(srcdir / '_build').rmtree(True)


@with_app(srcdir=srcdir, warningiserror=True)
def test_issue91(app, status, warning):
app.builder.build_all()
# default style is plain; check output
with open(os.path.join(app.outdir, "contents.html")) as stream:
output = stream.read()
# ensure Man09 is cited with plain style and not with alpha style
assert len(re.findall('\\[1\\]', output)) == 2
assert len(re.findall('\\[Man09\\]', output)) == 0

0 comments on commit 56dcb03

Please sign in to comment.