|
3 | 3 | Given a path to a .py file, it includes the source code inline, then: |
4 | 4 |
|
5 | 5 | - 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. |
8 | 6 |
|
9 | 7 | - 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. |
10 | 14 | """ |
11 | 15 |
|
12 | 16 | from docutils.parsers.rst import directives |
|
25 | 29 | 'width': directives.length_or_percentage_or_unitless, |
26 | 30 | 'scale': directives.nonnegative_int, |
27 | 31 | '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 | +""" |
29 | 47 |
|
30 | | -template = """ |
| 48 | +template_source = """ |
31 | 49 | .. literalinclude:: %(reference)s.py |
32 | 50 |
|
33 | 51 | .. htmlonly:: |
|
36 | 54 | :target: %(reference)s.hires.png |
37 | 55 | %(options)s |
38 | 56 |
|
39 | | - `[original %(basename)s.py] <%(reference)s.py>`_ |
| 57 | + `[%(basename)s.py] <%(reference)s.py>`_ |
40 | 58 |
|
41 | 59 | .. latexonly:: |
42 | 60 | .. image:: %(reference)s.pdf |
|
46 | 64 |
|
47 | 65 | def run(arguments, options, state_machine, lineno): |
48 | 66 | 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 |
51 | 77 | options = [' :%s: %s' % (key, val) for key, val in |
52 | 78 | options.items()] |
53 | 79 | options = "\n".join(options) |
|
0 commit comments