Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Backport PR #13777 on branch v3.1.x (Use class-based directive for mathmpl sphinxext.) #13781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions doc/api/next_api_changes/2019-01-09-deprecations.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
Deprecations
````````````

The ``matplotlib.sphinxext.plot_directive`` interface has changed from
the (Sphinx-)deprecated function-based interface to a class-based interface.
This should not affect end users, but the
``matplotlib.sphinxext.plot_directive.plot_directive`` function is now
The ``matplotlib.sphinxext.mathmpl`` and
``matplotlib.sphinxext.plot_directive`` interfaces have changed from the
(Sphinx-)deprecated function-based interface to a class-based interface. This
should not affect end users, but the
``matplotlib.sphinxext.mathmpl.math_directive`` and
``matplotlib.sphinxext.plot_directive.plot_directive`` functions are now
deprecated.
31 changes: 20 additions & 11 deletions lib/matplotlib/sphinxext/mathmpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys

from docutils import nodes
from docutils.parsers.rst import directives
from docutils.parsers.rst import Directive, directives
import sphinx

from matplotlib import rcParams
Expand All @@ -22,9 +22,6 @@ def fontset_choice(arg):
return directives.choice(arg, ['cm', 'stix', 'stixsans'])


options_spec = {'fontset': fontset_choice}


def math_role(role, rawtext, text, lineno, inliner,
options={}, content=[]):
i = rawtext.find('`')
Expand All @@ -33,11 +30,10 @@ def math_role(role, rawtext, text, lineno, inliner,
node['latex'] = latex
node['fontset'] = options.get('fontset', 'cm')
return [node], []
math_role.options = {'fontset': fontset_choice}


math_role.options = options_spec


@cbook.deprecated("3.1", alternative="MathDirective")
def math_directive(name, arguments, options, content, lineno,
content_offset, block_text, state, state_machine):
latex = ''.join(content)
Expand All @@ -47,6 +43,21 @@ def math_directive(name, arguments, options, content, lineno,
return [node]


class MathDirective(Directive):
has_content = True
required_arguments = 0
optional_arguments = 0
final_argument_whitespace = False
option_spec = {'fontset': fontset_choice}

def run(self):
latex = ''.join(self.content)
node = latex_math(self.block_text)
node['latex'] = latex
node['fontset'] = self.options.get('fontset', 'cm')
return [node]


# This uses mathtext to render the expression
def latex2png(latex, filename, fontset='cm'):
latex = "$%s$" % latex
Expand Down Expand Up @@ -121,12 +132,10 @@ def depart_latex_math_latex(self, node):
html=(visit_latex_math_html, depart_latex_math_html),
latex=(visit_latex_math_latex, depart_latex_math_latex))
app.add_role('mathmpl', math_role)
app.add_directive('mathmpl', math_directive,
True, (0, 0, 0), **options_spec)
app.add_directive('mathmpl', MathDirective)
if sphinx.version_info < (1, 8):
app.add_role('math', math_role)
app.add_directive('math', math_directive,
True, (0, 0, 0), **options_spec)
app.add_directive('math', MathDirective)

metadata = {'parallel_read_safe': True, 'parallel_write_safe': True}
return metadata