1
1
#!/usr/bin/env python
2
- """Helper to automagically generate ReST versions of examples.
3
-
4
- The MIT License
5
-
6
- Copyright (c) 2006-2009 Michael Hanke
7
- 2007-2009 Yaroslav Halchenko
8
-
9
- Permission is hereby granted, free of charge, to any person obtaining a copy
10
- of this software and associated documentation files (the "Software"), to deal
11
- in the Software without restriction, including without limitation the rights
12
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
- copies of the Software, and to permit persons to whom the Software is
14
- furnished to do so, subject to the following conditions:
15
-
16
- The above copyright notice and this permission notice shall be included in
17
- all copies or substantial portions of the Software.
18
-
19
- """
2
+ #
3
+ # Note: this file is copied (possibly with minor modifications) from the
4
+ # sources of the PyMVPA project - http://pymvpa.org. It remains licensed as
5
+ # the rest of PyMVPA (MIT license as of October 2010).
6
+ #
7
+ ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
8
+ #
9
+ # See COPYING file distributed along with the PyMVPA package for the
10
+ # copyright and license terms.
11
+ #
12
+ ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
13
+
14
+ """Helper to automagically generate ReST versions of examples"""
20
15
21
16
__docformat__ = 'restructuredtext'
22
17
@@ -28,6 +23,31 @@ import glob
28
23
from optparse import OptionParser
29
24
30
25
26
+ def auto_image (line ):
27
+ """Automatically replace generic image markers with ones that have full
28
+ size (width/height) info, plus a :target: link to the original png, to be
29
+ used in the html docs.
30
+ """
31
+ img_re = re .compile (r'(\s*)\.\. image::\s*(.*)$' )
32
+ m = img_re .match (line )
33
+ if m is None :
34
+ # Not an image declaration, leave the line alone and return unmodified
35
+ return line
36
+
37
+ # Match means it's an image spec, we rewrite it with extra tags
38
+ ini_space = m .group (1 )
39
+ lines = [line ,
40
+ ini_space + ' :width: 500\n ' ,
41
+ #ini_space + ' :height: 350\n'
42
+ ]
43
+ fspec = m .group (2 )
44
+ if fspec .endswith ('.*' ):
45
+ fspec = fspec .replace ('.*' , '.png' )
46
+ fspec = fspec .replace ('fig/' , '../_images/' )
47
+ lines .append (ini_space + (' :target: %s\n ' % fspec ) )
48
+ lines .append ('\n ' )
49
+ return '' .join (lines )
50
+
31
51
def exfile2rst (filename ):
32
52
"""Open a Python script and convert it into an ReST string.
33
53
"""
@@ -42,8 +62,6 @@ def exfile2rst(filename):
42
62
indocs = False
43
63
doc2code = False
44
64
code2doc = False
45
- interactiveblock = False
46
- preserved_indent = False
47
65
# an empty line found in the example enables the check for a potentially
48
66
# indented docstring starting on the next line (as an attempt to exclude
49
67
# function or class docstrings)
@@ -67,7 +85,6 @@ def exfile2rst(filename):
67
85
cleanline = line [:line .find ('#' )].lstrip ()
68
86
indent_level = len (line ) - len (cleanline ) - 1
69
87
cleanline = cleanline .rstrip ()
70
- interactiveblock = False
71
88
else :
72
89
cleanline = line [:line .find ('#' )].rstrip ()
73
90
@@ -92,19 +109,16 @@ def exfile2rst(filename):
92
109
proc_line = cleanline [3 :- 3 ]
93
110
else :
94
111
# must be start of multiline block
95
- interactiveblock = False
96
112
indocs = True
97
113
code2doc = True
98
114
# rescue what is left on the line
99
115
proc_line = cleanline [3 :] # strip """
100
- first_codeline_in_block = False
101
116
else :
102
117
# we are already in the docs
103
118
# handle doc end
104
119
if cleanline .endswith ('"""' ) or cleanline .endswith ("'''" ):
105
120
indocs = False
106
121
doc2code = True
107
- preserved_indent = False
108
122
# rescue what is left on the line
109
123
proc_line = cleanline [:- 3 ]
110
124
# reset the indentation
@@ -122,40 +136,18 @@ def exfile2rst(filename):
122
136
code2doc = False
123
137
s += '\n '
124
138
139
+ proc_line = auto_image (proc_line )
140
+
125
141
if proc_line :
126
142
s += proc_line .rstrip () + '\n '
127
143
128
144
else :
129
- # we are in a code block
130
- if line .startswith ("if cfg.getboolean('examples', 'interactive'" ):
131
- interactiveblock = True
132
- elif not interactiveblock :
133
- # we exclude the code that is used to disable the interactive
134
- # plots.
135
- if doc2code :
136
- doc2code = False
137
- first_codeline_in_block = True
138
- # the spaces at the end are crucial to get intentation of
139
- # indented code block -- otherwise sphinx removes common
140
- # whitespace
141
- s += '\n ::\n '
142
-
143
- # has to be code
144
- leading_whitespace = len (line ) > 2 \
145
- and len (line ) > len (line .lstrip ())
146
- line = line .rstrip ()
147
- # anything left?
148
- if line :
149
- if first_codeline_in_block and leading_whitespace \
150
- and not preserved_indent :
151
- s += ' >>%s\n ' % line
152
- preserved_indent = True
153
- else :
154
- s += ' %s\n ' % line
155
-
156
- first_codeline_in_block = False
157
- else :
158
- s += '\n '
145
+ if doc2code :
146
+ doc2code = False
147
+ s += '\n ::\n '
148
+
149
+ # has to be code
150
+ s += ' %s' % line
159
151
160
152
xfile .close ()
161
153
@@ -179,15 +171,20 @@ def exfile2rstfile(filename, opts):
179
171
180
172
# write converted ReST
181
173
dfile .write (exfile2rst (filename ))
182
-
183
- # add links
184
- dfile .write ('.. include:: ../../links_names.txt\n \n ' )
185
174
186
175
if opts .sourceref :
187
176
# write post example see also box
188
- dfile .write ("\n .. seealso::\n The full source code of this example is "
189
- "included in the %s source distribution (`%s`).\n "
190
- % (opts .project , filename ))
177
+ msg = """
178
+
179
+ .. admonition:: Example source code
180
+
181
+ You can download :download:`the full source code of this example <%s>`.
182
+ This same script is also included in the %s source distribution under the
183
+ :file:`examples` directory.
184
+
185
+ """ % (filename , opts .project )
186
+
187
+ dfile .write (msg )
191
188
192
189
dfile .close ()
193
190
@@ -215,7 +212,7 @@ the respective indentation is removed in the ReST output.
215
212
216
213
The parser algorithm automatically excludes file headers and starts with the
217
214
first (module-level) docstring instead.
218
- """ )
215
+ """ ) #'
219
216
220
217
# define options
221
218
parser .add_option ('--verbose' , action = 'store_true' , dest = 'verbose' ,
@@ -269,7 +266,7 @@ Name of the project that contains the examples. This name is used in the
269
266
print ('Ignoring duplicate parse targets.' )
270
267
271
268
if not os .path .exists (opts .outdir ):
272
- os .mkdir (opts . outdir )
269
+ os .mkdir (outdir )
273
270
274
271
# finally process all examples
275
272
for t in toparse :
0 commit comments