Skip to content

Commit

Permalink
Refactor filter parsing during run to reduce function complexity.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmtroffaes committed Jul 31, 2021
1 parent 81865d0 commit 53c3ce5
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/sphinxcontrib/bibtex/directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,9 @@ class BibliographyDirective(Directive):
'keyprefix': directives.unchanged,
}

def run(self):
"""Process .bib files, set file dependencies, and create a
node that is to be transformed to the entries of the
bibliography.
"""
def _get_filter(self):
"""Get parsed filter from options."""
env = cast("BuildEnvironment", self.state.document.settings.env)
domain = cast("BibtexDomain", env.get_domain('cite'))
if "filter" in self.options:
if "all" in self.options:
logger.warning(":filter: overrides :all:",
Expand All @@ -110,22 +106,31 @@ def run(self):
location=(env.docname, self.lineno),
type="bibtex", subtype="filter_overrides")
try:
filter_ = ast.parse(self.options["filter"])
return ast.parse(self.options["filter"])
except SyntaxError:
logger.warning(
"syntax error in :filter: expression" +
" (" + self.options["filter"] + "); "
"the option will be ignored",
location=(env.docname, self.lineno),
type="bibtex", subtype="filter_syntax_error")
filter_ = ast.parse("cited")
return ast.parse("cited")
elif "all" in self.options:
filter_ = ast.parse("True")
return ast.parse("True")
elif "notcited" in self.options:
filter_ = ast.parse("not cited")
return ast.parse("not cited")
else:
# the default filter: include only cited entries
filter_ = ast.parse("cited")
return ast.parse("cited")

def run(self):
"""Process .bib files, set file dependencies, and create a
node that is to be transformed to the entries of the
bibliography.
"""
env = cast("BuildEnvironment", self.state.document.settings.env)
domain = cast("BibtexDomain", env.get_domain('cite'))
filter_ = self._get_filter()
if self.arguments:
bibfiles = []
for bibfile in self.arguments[0].split():
Expand Down

0 comments on commit 53c3ce5

Please sign in to comment.