On Sat, Sep 12, 2009 at 11:12 PM, Fernando Perez <fperez....@gmail.com> wrote:

> Here's a diff against current trunk to play with this idea.

Updated patch that handles correctly more than one option (I think the
bug was even in the original, not sure).

Cheers,

f
Index: plot_directive.py
===================================================================
--- plot_directive.py	(revision 7753)
+++ plot_directive.py	(working copy)
@@ -46,7 +46,7 @@
 import matplotlib.image as image
 from matplotlib import _pylab_helpers
 
-import only_directives
+from matplotlib.sphinxext import only_directives
 
 if hasattr(os.path, 'relpath'):
     relpath = os.path.relpath
@@ -100,12 +100,16 @@
 
    [%(links)s]
 
-   .. image:: %(prefix)s%(tmpdir)s/%(outname)s.png
-   %(options)s
+   .. figure:: %(prefix)s%(tmpdir)s/%(outname)s.png
+%(options)s
 
+%(caption)s
+
 .. latexonly::
-   .. image:: %(prefix)s%(tmpdir)s/%(outname)s.pdf
-   %(options)s
+   .. figure:: %(prefix)s%(tmpdir)s/%(outname)s.pdf
+%(options)s
+
+%(caption)s
 """
 
 exception_template = """
@@ -228,10 +232,15 @@
 
     return len(fig_managers)
 
-def plot_directive(name, arguments, options, content, lineno,
+def figplot_directive(name, arguments, options, content, lineno,
                    content_offset, block_text, state, state_machine):
     """
     Handle the plot directive.
+
+    Modified from the original matplotlib plot directive.
+
+    This one does NOT allow source code, only filenames, but it produces a
+    'figure' instead of an 'image', along with an optional caption.
     """
     formats = setup.config.plot_formats
     if type(formats) == str:
@@ -243,16 +252,25 @@
         basedir, fname = os.path.split(reference)
         basename, ext = os.path.splitext(fname)
         basedir = relpath(basedir, setup.app.builder.srcdir)
-        if len(content):
-            raise ValueError("plot directive may not specify both a filename and inline content")
-        content = None
+        # If there is content, it will be passed as a caption.
+
+        # Indent to match expansion below.  XXX - The number of spaces matches
+        # that of the 'options' expansion further down.  This should be moved
+        # to common code to prevent them from diverging accidentally.
+        caption = '\n'.join('         %s' % line.strip() for line in content)
+        plot_code = None
     else:
         basedir = "inline"
-        content = '\n'.join(content)
+        plot_code = '\n'.join(content)
+        caption = ''
         # Since we don't have a filename, use a hash based on the content
-        reference = basename = md5(content).hexdigest()[-10:]
+        reference = basename = md5(plot_code).hexdigest()[-10:]
         fname = None
 
+    # One way or another, we've transformed the content, so set it to null
+    # further on.
+    content = None
+    
     # Get the directory of the rst file, and determine the relative
     # path from the resulting html file to the plot_directive links
     # (linkdir).  This relative path is used for html links *only*,
@@ -289,18 +307,19 @@
         cbook.mkdirs(destdir)
 
     # Generate the figures, and return the number of them
-    num_figs = makefig(reference, content, tmpdir)
+    num_figs = makefig(reference, plot_code, tmpdir)
 
     if options.has_key('include-source'):
-        if content is None:
-            content = open(reference, 'r').read()
-        lines = ['::', ''] + ['    %s'%row.rstrip() for row in content.split('\n')]
+        if plot_code is None:
+            plot_code = open(reference, 'r').read()
+        lines = ['::', ''] + ['    %s'%row.rstrip()
+                              for row in plot_code.split('\n')]
         del options['include-source']
     else:
         lines = []
 
     if num_figs > 0:
-        options = ['      :%s: %s' % (key, val) for key, val in
+        options = ['         :%s: %s' % (key, val) for key, val in
                    options.items()]
         options = "\n".join(options)
         if fname is not None:
@@ -339,7 +358,7 @@
     setup.config = app.config
     setup.confdir = app.confdir
 
-    app.add_directive('plot', plot_directive, True, (0, 1, 0), **options)
+    app.add_directive('figplot', figplot_directive, True, (0, 1, 0), **options)
     app.add_config_value(
         'plot_formats',
         ['png', 'hires.png', 'pdf'],
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Reply via email to