On Sat, Sep 12, 2009 at 8:29 PM, Fernando Perez <fperez....@gmail.com> wrote:
> Before I dive into the code too far, I figured I'd ask the experts.
Too late for that, common sense has never been my forte.
Here's a diff against current trunk to play with this idea.
WARNING: Please note that this is NOT meant to be applied for mpl yet!!!
I've actually modified the plot directive and renamed it to 'figplot',
so we can experiment a little to better understand things. The point
is that this now lets us write reST of this type:
.. figplot:: code/make_figure_brainx.py
:width: 3.6 in
**Example from fMRI data:** In the graph presented, the nodes represent the
areas of the brain described in Figure 1. Nodes are labeled accordingly and
etc...
The text block is then passed as a caption to latex.
I renamed it because there's a bit of a conflict with the current
'plot' directive, which allows a filename *or* a content block, but in
that case the content block is meant to be the source code, as
illustrated in sampledoc:
http://matplotlib.sourceforge.net/sampledoc/extensions.html#inserting-matplotlib-plots
Since I'm not sure if we can find a clean solution to:
- path to script: goes into arg list
- inlined (multiline) code: goes into content block
- inlined (possibly multiline) caption: goes into content block
I put this version so we can start experimenting. This does what
Ariel and I need, but I hope over time we can figure out a good
long-term solution.
Speaking of sphinx for books, as I've mentioned before to John, the
last big problem is being able to cross-reference arbitrary text
elements like you can in latex, be they chapters or sections or
whatever, and get a number or something that's meaningful in print.
I looked around, and apparently it's on the main docutils todo list:
http://docutils.sourceforge.net/docs/dev/todo.html#object-numbering-and-object-references
I hope we don't have to be the ones fixing that one...
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
+ .. figure:: %(prefix)s%(tmpdir)s/%(outname)s.png
%(options)s
+%(caption)s
+
.. latexonly::
- .. image:: %(prefix)s%(tmpdir)s/%(outname)s.pdf
+ .. 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,26 @@
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 (plus 3, which is the
+ # indent in the templates above). 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,12 +308,13 @@
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 = []
@@ -339,7 +359,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