From: <md...@us...> - 2009-02-16 14:12:18
|
Revision: 6915 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=6915&view=rev Author: mdboom Date: 2009-02-16 14:12:13 +0000 (Mon, 16 Feb 2009) Log Message: ----------- Move the mathmpl Sphinx extension to the installed tree so that other projects can take advantage of it. Modified Paths: -------------- branches/v0_98_5_maint/doc/conf.py branches/v0_98_5_maint/setup.py Added Paths: ----------- branches/v0_98_5_maint/lib/matplotlib/sphinxext/ branches/v0_98_5_maint/lib/matplotlib/sphinxext/__init__.py branches/v0_98_5_maint/lib/matplotlib/sphinxext/mathmpl.py branches/v0_98_5_maint/lib/matplotlib/sphinxext/only_directives.py Removed Paths: ------------- branches/v0_98_5_maint/doc/sphinxext/mathmpl.py branches/v0_98_5_maint/doc/sphinxext/only_directives.py Modified: branches/v0_98_5_maint/doc/conf.py =================================================================== --- branches/v0_98_5_maint/doc/conf.py 2009-02-15 00:12:19 UTC (rev 6914) +++ branches/v0_98_5_maint/doc/conf.py 2009-02-16 14:12:13 UTC (rev 6915) @@ -27,9 +27,9 @@ # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. -extensions = ['mathmpl', 'math_symbol_table', 'sphinx.ext.autodoc', - 'only_directives', 'plot_directive', 'inheritance_diagram', - 'gen_gallery', 'gen_rst'] +extensions = ['matplotlib.sphinxext.mathmpl', 'math_symbol_table', + 'sphinx.ext.autodoc', 'matplotlib.sphinxext.only_directives', + 'plot_directive', 'inheritance_diagram', 'gen_gallery', 'gen_rst'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] Deleted: branches/v0_98_5_maint/doc/sphinxext/mathmpl.py =================================================================== --- branches/v0_98_5_maint/doc/sphinxext/mathmpl.py 2009-02-15 00:12:19 UTC (rev 6914) +++ branches/v0_98_5_maint/doc/sphinxext/mathmpl.py 2009-02-16 14:12:13 UTC (rev 6915) @@ -1,119 +0,0 @@ -import os -import sys -try: - from hashlib import md5 -except ImportError: - from md5 import md5 - -from docutils import nodes -from docutils.parsers.rst import directives -import warnings - -from matplotlib import rcParams -from matplotlib.mathtext import MathTextParser -rcParams['mathtext.fontset'] = 'cm' -mathtext_parser = MathTextParser("Bitmap") - -# Define LaTeX math node: -class latex_math(nodes.General, nodes.Element): - pass - -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('`') - latex = rawtext[i+1:-1] - node = latex_math(rawtext) - node['latex'] = latex - node['fontset'] = options.get('fontset', 'cm') - return [node], [] -math_role.options = options_spec - -def math_directive(name, arguments, options, content, lineno, - content_offset, block_text, state, state_machine): - latex = ''.join(content) - node = latex_math(block_text) - node['latex'] = latex - node['fontset'] = options.get('fontset', 'cm') - return [node] - -# This uses mathtext to render the expression -def latex2png(latex, filename, fontset='cm'): - latex = "$%s$" % latex - orig_fontset = rcParams['mathtext.fontset'] - rcParams['mathtext.fontset'] = fontset - if os.path.exists(filename): - depth = mathtext_parser.get_depth(latex, dpi=100) - else: - try: - depth = mathtext_parser.to_png(filename, latex, dpi=100) - except: - warnings.warn("Could not render math expression %s" % latex, - Warning) - depth = 0 - rcParams['mathtext.fontset'] = orig_fontset - sys.stdout.write("#") - sys.stdout.flush() - return depth - -# LaTeX to HTML translation stuff: -def latex2html(node, source): - inline = isinstance(node.parent, nodes.TextElement) - latex = node['latex'] - name = 'math-%s' % md5(latex).hexdigest()[-10:] - - destdir = os.path.join(setup.app.builder.outdir, '_images', 'mathmpl') - if not os.path.exists(destdir): - os.makedirs(destdir) - dest = os.path.join(destdir, '%s.png' % name) - path = os.path.join(setup.app.builder.imgpath, 'mathmpl') - - depth = latex2png(latex, dest, node['fontset']) - - if inline: - cls = '' - else: - cls = 'class="center" ' - if inline and depth != 0: - style = 'style="position: relative; bottom: -%dpx"' % (depth + 1) - else: - style = '' - - return '<img src="%s/%s.png" %s%s/>' % (path, name, cls, style) - -def setup(app): - setup.app = app - - app.add_node(latex_math) - app.add_role('math', math_role) - - # Add visit/depart methods to HTML-Translator: - def visit_latex_math_html(self, node): - source = self.document.attributes['source'] - self.body.append(latex2html(node, source)) - def depart_latex_math_html(self, node): - pass - - # Add visit/depart methods to LaTeX-Translator: - def visit_latex_math_latex(self, node): - inline = isinstance(node.parent, nodes.TextElement) - if inline: - self.body.append('$%s$' % node['latex']) - else: - self.body.extend(['\\begin{equation}', - node['latex'], - '\\end{equation}']) - def depart_latex_math_latex(self, node): - pass - - app.add_node(latex_math, html=(visit_latex_math_html, - depart_latex_math_html)) - app.add_node(latex_math, latex=(visit_latex_math_latex, - depart_latex_math_latex)) - app.add_role('math', math_role) - app.add_directive('math', math_directive, - True, (0, 0, 0), **options_spec) Deleted: branches/v0_98_5_maint/doc/sphinxext/only_directives.py =================================================================== --- branches/v0_98_5_maint/doc/sphinxext/only_directives.py 2009-02-15 00:12:19 UTC (rev 6914) +++ branches/v0_98_5_maint/doc/sphinxext/only_directives.py 2009-02-16 14:12:13 UTC (rev 6915) @@ -1,63 +0,0 @@ -# -# A pair of directives for inserting content that will only appear in -# either html or latex. -# - -from docutils.nodes import Body, Element -from docutils.parsers.rst import directives - -class only_base(Body, Element): - def dont_traverse(self, *args, **kwargs): - return [] - -class html_only(only_base): - pass - -class latex_only(only_base): - pass - -def run(content, node_class, state, content_offset): - text = '\n'.join(content) - node = node_class(text) - state.nested_parse(content, content_offset, node) - return [node] - -def html_only_directive(name, arguments, options, content, lineno, - content_offset, block_text, state, state_machine): - return run(content, html_only, state, content_offset) - -def latex_only_directive(name, arguments, options, content, lineno, - content_offset, block_text, state, state_machine): - return run(content, latex_only, state, content_offset) - -def builder_inited(app): - if app.builder.name == 'html': - latex_only.traverse = only_base.dont_traverse - else: - html_only.traverse = only_base.dont_traverse - -def setup(app): - app.add_directive('htmlonly', html_only_directive, True, (0, 0, 0)) - app.add_directive('latexonly', latex_only_directive, True, (0, 0, 0)) - app.add_node(html_only) - app.add_node(latex_only) - - # This will *really* never see the light of day As it turns out, - # this results in "broken" image nodes since they never get - # processed, so best not to do this. - # app.connect('builder-inited', builder_inited) - - # Add visit/depart methods to HTML-Translator: - def visit_perform(self, node): - pass - def depart_perform(self, node): - pass - def visit_ignore(self, node): - node.children = [] - def depart_ignore(self, node): - node.children = [] - - app.add_node(html_only, html=(visit_perform, depart_perform)) - app.add_node(html_only, latex=(visit_ignore, depart_ignore)) - app.add_node(latex_only, latex=(visit_perform, depart_perform)) - app.add_node(latex_only, html=(visit_ignore, depart_ignore)) Added: branches/v0_98_5_maint/lib/matplotlib/sphinxext/__init__.py =================================================================== --- branches/v0_98_5_maint/lib/matplotlib/sphinxext/__init__.py (rev 0) +++ branches/v0_98_5_maint/lib/matplotlib/sphinxext/__init__.py 2009-02-16 14:12:13 UTC (rev 6915) @@ -0,0 +1 @@ + Copied: branches/v0_98_5_maint/lib/matplotlib/sphinxext/mathmpl.py (from rev 6843, branches/v0_98_5_maint/doc/sphinxext/mathmpl.py) =================================================================== --- branches/v0_98_5_maint/lib/matplotlib/sphinxext/mathmpl.py (rev 0) +++ branches/v0_98_5_maint/lib/matplotlib/sphinxext/mathmpl.py 2009-02-16 14:12:13 UTC (rev 6915) @@ -0,0 +1,119 @@ +import os +import sys +try: + from hashlib import md5 +except ImportError: + from md5 import md5 + +from docutils import nodes +from docutils.parsers.rst import directives +import warnings + +from matplotlib import rcParams +from matplotlib.mathtext import MathTextParser +rcParams['mathtext.fontset'] = 'cm' +mathtext_parser = MathTextParser("Bitmap") + +# Define LaTeX math node: +class latex_math(nodes.General, nodes.Element): + pass + +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('`') + latex = rawtext[i+1:-1] + node = latex_math(rawtext) + node['latex'] = latex + node['fontset'] = options.get('fontset', 'cm') + return [node], [] +math_role.options = options_spec + +def math_directive(name, arguments, options, content, lineno, + content_offset, block_text, state, state_machine): + latex = ''.join(content) + node = latex_math(block_text) + node['latex'] = latex + node['fontset'] = options.get('fontset', 'cm') + return [node] + +# This uses mathtext to render the expression +def latex2png(latex, filename, fontset='cm'): + latex = "$%s$" % latex + orig_fontset = rcParams['mathtext.fontset'] + rcParams['mathtext.fontset'] = fontset + if os.path.exists(filename): + depth = mathtext_parser.get_depth(latex, dpi=100) + else: + try: + depth = mathtext_parser.to_png(filename, latex, dpi=100) + except: + warnings.warn("Could not render math expression %s" % latex, + Warning) + depth = 0 + rcParams['mathtext.fontset'] = orig_fontset + sys.stdout.write("#") + sys.stdout.flush() + return depth + +# LaTeX to HTML translation stuff: +def latex2html(node, source): + inline = isinstance(node.parent, nodes.TextElement) + latex = node['latex'] + name = 'math-%s' % md5(latex).hexdigest()[-10:] + + destdir = os.path.join(setup.app.builder.outdir, '_images', 'mathmpl') + if not os.path.exists(destdir): + os.makedirs(destdir) + dest = os.path.join(destdir, '%s.png' % name) + path = os.path.join(setup.app.builder.imgpath, 'mathmpl') + + depth = latex2png(latex, dest, node['fontset']) + + if inline: + cls = '' + else: + cls = 'class="center" ' + if inline and depth != 0: + style = 'style="position: relative; bottom: -%dpx"' % (depth + 1) + else: + style = '' + + return '<img src="%s/%s.png" %s%s/>' % (path, name, cls, style) + +def setup(app): + setup.app = app + + app.add_node(latex_math) + app.add_role('math', math_role) + + # Add visit/depart methods to HTML-Translator: + def visit_latex_math_html(self, node): + source = self.document.attributes['source'] + self.body.append(latex2html(node, source)) + def depart_latex_math_html(self, node): + pass + + # Add visit/depart methods to LaTeX-Translator: + def visit_latex_math_latex(self, node): + inline = isinstance(node.parent, nodes.TextElement) + if inline: + self.body.append('$%s$' % node['latex']) + else: + self.body.extend(['\\begin{equation}', + node['latex'], + '\\end{equation}']) + def depart_latex_math_latex(self, node): + pass + + app.add_node(latex_math, html=(visit_latex_math_html, + depart_latex_math_html)) + app.add_node(latex_math, latex=(visit_latex_math_latex, + depart_latex_math_latex)) + app.add_role('math', math_role) + app.add_directive('math', math_directive, + True, (0, 0, 0), **options_spec) Property changes on: branches/v0_98_5_maint/lib/matplotlib/sphinxext/mathmpl.py ___________________________________________________________________ Added: svn:mergeinfo + /branches/v0_91_maint/doc/sphinxext/mathmpl.py:5753-5771 Copied: branches/v0_98_5_maint/lib/matplotlib/sphinxext/only_directives.py (from rev 6843, branches/v0_98_5_maint/doc/sphinxext/only_directives.py) =================================================================== --- branches/v0_98_5_maint/lib/matplotlib/sphinxext/only_directives.py (rev 0) +++ branches/v0_98_5_maint/lib/matplotlib/sphinxext/only_directives.py 2009-02-16 14:12:13 UTC (rev 6915) @@ -0,0 +1,63 @@ +# +# A pair of directives for inserting content that will only appear in +# either html or latex. +# + +from docutils.nodes import Body, Element +from docutils.parsers.rst import directives + +class only_base(Body, Element): + def dont_traverse(self, *args, **kwargs): + return [] + +class html_only(only_base): + pass + +class latex_only(only_base): + pass + +def run(content, node_class, state, content_offset): + text = '\n'.join(content) + node = node_class(text) + state.nested_parse(content, content_offset, node) + return [node] + +def html_only_directive(name, arguments, options, content, lineno, + content_offset, block_text, state, state_machine): + return run(content, html_only, state, content_offset) + +def latex_only_directive(name, arguments, options, content, lineno, + content_offset, block_text, state, state_machine): + return run(content, latex_only, state, content_offset) + +def builder_inited(app): + if app.builder.name == 'html': + latex_only.traverse = only_base.dont_traverse + else: + html_only.traverse = only_base.dont_traverse + +def setup(app): + app.add_directive('htmlonly', html_only_directive, True, (0, 0, 0)) + app.add_directive('latexonly', latex_only_directive, True, (0, 0, 0)) + app.add_node(html_only) + app.add_node(latex_only) + + # This will *really* never see the light of day As it turns out, + # this results in "broken" image nodes since they never get + # processed, so best not to do this. + # app.connect('builder-inited', builder_inited) + + # Add visit/depart methods to HTML-Translator: + def visit_perform(self, node): + pass + def depart_perform(self, node): + pass + def visit_ignore(self, node): + node.children = [] + def depart_ignore(self, node): + node.children = [] + + app.add_node(html_only, html=(visit_perform, depart_perform)) + app.add_node(html_only, latex=(visit_ignore, depart_ignore)) + app.add_node(latex_only, latex=(visit_perform, depart_perform)) + app.add_node(latex_only, html=(visit_ignore, depart_ignore)) Property changes on: branches/v0_98_5_maint/lib/matplotlib/sphinxext/only_directives.py ___________________________________________________________________ Added: svn:mergeinfo + /branches/v0_91_maint/doc/sphinxext/only_directives.py:5753-5771 Modified: branches/v0_98_5_maint/setup.py =================================================================== --- branches/v0_98_5_maint/setup.py 2009-02-15 00:12:19 UTC (rev 6914) +++ branches/v0_98_5_maint/setup.py 2009-02-16 14:12:13 UTC (rev 6915) @@ -58,7 +58,8 @@ 'matplotlib.numerix.npyma', 'matplotlib.numerix.linear_algebra', 'matplotlib.numerix.random_array', - 'matplotlib.numerix.fft' + 'matplotlib.numerix.fft', + 'matplotlib.sphinxext' ] py_modules = ['pylab'] This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |