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

Skip to content

Commit 6e4593e

Browse files
committed
Add "include-source" option.
svn path=/trunk/matplotlib/; revision=5505
1 parent abed7c1 commit 6e4593e

7 files changed

Lines changed: 48 additions & 11 deletions

File tree

doc/sphinxext/plot_directive.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
Given a path to a .py file, it includes the source code inline, then:
44
55
- On HTML, will include a .png with a link to a high-res .png.
6-
Underneath that, a [source] link will go to a plain text .py of
7-
the source.
86
97
- On LaTeX, will include a .pdf
8+
9+
This directive supports all of the options of the `image` directive,
10+
except for `target` (since plot will add its own target).
11+
12+
Additionally, if the :include-source: option is provided, the literal
13+
source will be included inline, as well as a link to the source.
1014
"""
1115

1216
from docutils.parsers.rst import directives
@@ -25,9 +29,23 @@
2529
'width': directives.length_or_percentage_or_unitless,
2630
'scale': directives.nonnegative_int,
2731
'align': align,
28-
'class': directives.class_option}
32+
'class': directives.class_option,
33+
'include-source': directives.flag }
34+
35+
template_no_source = """
36+
.. htmlonly::
37+
38+
.. image:: %(reference)s.png
39+
:target: %(reference)s.hires.png
40+
%(options)s
41+
42+
.. latexonly::
43+
.. image:: %(reference)s.pdf
44+
%(options)s
45+
46+
"""
2947

30-
template = """
48+
template_source = """
3149
.. literalinclude:: %(reference)s.py
3250
3351
.. htmlonly::
@@ -36,7 +54,7 @@
3654
:target: %(reference)s.hires.png
3755
%(options)s
3856
39-
`[original %(basename)s.py] <%(reference)s.py>`_
57+
`[%(basename)s.py] <%(reference)s.py>`_
4058
4159
.. latexonly::
4260
.. image:: %(reference)s.pdf
@@ -46,8 +64,16 @@
4664

4765
def run(arguments, options, state_machine, lineno):
4866
reference = directives.uri(arguments[0])
49-
if reference.endswith('.py'):
50-
reference = reference[:-3]
67+
print reference
68+
for ext in ('.py', '.png', '.pdf'):
69+
if reference.endswith(ext):
70+
reference = reference[:-len(ext)]
71+
break
72+
if options.has_key('include-source'):
73+
template = template_source
74+
del options['include-source']
75+
else:
76+
template = template_no_source
5177
options = [' :%s: %s' % (key, val) for key, val in
5278
options.items()]
5379
options = "\n".join(options)

doc/users/annotations.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ consider: the location being annotated represented by the argument
1313
arguments are ``(x,y)`` tuples.
1414

1515
.. plot:: figures/annotation_basic.py
16+
:include-source:
1617
:scale: 75
1718

1819
In this example, both the ``xy`` (arrow tip) and ``xytext`` locations
@@ -73,6 +74,7 @@ keyword args like ``horizontalalignment``, ``verticalalignment`` and
7374

7475
.. plot:: figures/annotation_polar.py
7576
:scale: 75
77+
:include-source:
7678

7779
See the `annotations demo
7880
<http://matplotlib.sf.net/examples/pylab_examples/annotation_demo.py>`_ for more

doc/users/artists.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ drawing of the ticks, tick labels and axis labels.
135135
136136
Try creating the figure below.
137137

138-
.. image:: figures/fig_axes_labels_simple.png
138+
.. plot:: figures/fig_axes_labels_simple
139139
:scale: 75
140140

141141
.. _customizing-artists:
@@ -326,7 +326,7 @@ obtain by setting the ``Artist`` transform to :attr:`fig.transFigure
326326

327327
In [195]: fig.canvas.draw()
328328

329-
.. image:: figures/fig_x.png
329+
.. plot:: figures/fig_x
330330
:scale: 75
331331

332332

@@ -606,6 +606,7 @@ the axes and tick properties
606606

607607
.. plot:: figures/fig_axes_customize_simple.py
608608
:scale: 75
609+
:include-source:
609610

610611

611612
.. _tick-container:
@@ -643,3 +644,4 @@ dollar signs and colors them green on the right side of the yaxis
643644

644645
.. plot:: figures/dollar_ticks.py
645646
:scale: 75
647+
:include-source:

doc/users/mathtext.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ Here is an example illustrating many of these features in context.
273273

274274
.. plot:: figures/pyplot_mathtext.py
275275
:scale: 75
276+
:include-source:
276277

277278

278279

doc/users/pyplot_tutorial.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ functions are directed to the current axes
1414

1515
.. plot:: figures/pyplot_simple
1616
:scale: 75
17+
:include-source:
1718

1819
You may be wondering why the x-axis ranges from 0-3 and the y-axis
1920
from 1-4. If you provide a single list or array to the
@@ -38,6 +39,7 @@ example, to plot the above with red circles, you would issue
3839

3940
.. plot:: figures/pyplot_formatstr.py
4041
:scale: 75
42+
:include-source:
4143

4244
See the :func:`~matplotlib.pyplot.plot` documentation for a complete
4345
list of line styles and format strings. The
@@ -54,6 +56,7 @@ using arrays.
5456

5557
.. plot:: figures/pyplot_three.py
5658
:scale: 75
59+
:include-source:
5760

5861
.. _controlling-line-properties:
5962

@@ -159,6 +162,7 @@ scenes. Below is an script to create two subplots.
159162

160163
.. plot:: figures/pyplot_two_subplots.py
161164
:scale: 75
165+
:include-source:
162166

163167
The :func:`~matplotlib.pyplot.figure` command here is optional because
164168
``figure(1)`` will be created by default, just as a ``subplot(111)``
@@ -218,7 +222,7 @@ for a more detailed example)
218222

219223
.. plot:: figures/pyplot_text.py
220224
:scale: 75
221-
225+
:include-source:
222226

223227
All of the :func:`~matplotlib.pyplot.text` commands return an
224228
:class:`matplotlib.text.Text` instance. Just as with with lines
@@ -264,6 +268,7 @@ these arguments are ``(x,y)`` tuples.
264268

265269
.. plot:: figures/pyplot_annotate.py
266270
:scale: 75
271+
:include-source:
267272

268273
In this basic example, both the ``xy`` (arrow tip) and ``xytext``
269274
locations (text location) are in data coordinates. There are a

doc/users/text_intro.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ these commands in action.
5757

5858
.. plot:: figures/text_commands.py
5959
:scale: 75
60-
60+
:include-source:

doc/users/text_props.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,4 @@ upper right.
5959

6060
.. plot:: figures/text_layout.py
6161
:scale: 75
62+
:include-source:

0 commit comments

Comments
 (0)