1 | .. $Id: howto-markup.rst 1022 2012-07-16 01:24:17Z jemian $ |
---|
2 | |
---|
3 | ========================================================== |
---|
4 | How to Markup a SPEC Macro File |
---|
5 | ========================================================== |
---|
6 | |
---|
7 | #. start with SPEC macro file that is not marked up |
---|
8 | #. create a Sphinx documentation project |
---|
9 | #. apply markup |
---|
10 | #. test |
---|
11 | |
---|
12 | .. tip:: In addition to the Sphinx documentation (http://sphinx.pocoo.org), |
---|
13 | a good reference how to document your macros can be found here: |
---|
14 | http://stackoverflow.com/questions/4547849/good-examples-of-python-docstrings-for-sphinx |
---|
15 | but a Google search for *sphinx python docstrings examples* will turn up a wealth of alternatives. |
---|
16 | |
---|
17 | Basic SPEC Macro file |
---|
18 | ============================== |
---|
19 | |
---|
20 | This example is a SPEC macro file from the APS subversion repository: |
---|
21 | https://subversion.xor.aps.anl.gov/spec/macros/trunk/common/hkl_ioc.mac |
---|
22 | because it is simple, brief, does not contain references to other |
---|
23 | macro files, provides its documentation in SPEC comments, and has not been |
---|
24 | marked up previously for documentation with Sphinx. Here is the file |
---|
25 | *hkl_ioc.mac* in its entirety. |
---|
26 | |
---|
27 | .. literalinclude:: ex_markup/hkl_ioc.mac.original |
---|
28 | :tab-width: 4 |
---|
29 | :linenos: |
---|
30 | :language: guess |
---|
31 | |
---|
32 | |
---|
33 | |
---|
34 | Create a Sphinx Project |
---|
35 | ============================== |
---|
36 | |
---|
37 | .. index:: in-source configuration |
---|
38 | .. tip:: Use an :ref:`in-source configuration` |
---|
39 | |
---|
40 | Make a project directory and change directory into it. |
---|
41 | Copy the file above into this directory with the name *hkl_ioc.mac*. |
---|
42 | Then run:: |
---|
43 | |
---|
44 | sphinx-quickstart |
---|
45 | |
---|
46 | Take most of the defaults. |
---|
47 | These are the non-defaults for the example project: |
---|
48 | |
---|
49 | ======================================================================================== ======================== |
---|
50 | prompt response |
---|
51 | ======================================================================================== ======================== |
---|
52 | ``> Project name:`` **example** |
---|
53 | ``> Author name(s):`` **SPEC Macro Writer** |
---|
54 | ``> Project version:`` **1** |
---|
55 | ``> autodoc: automatically insert docstrings from modules (y/N) [n]:`` **y** |
---|
56 | ``> viewcode: include links to the source code of documented Python objects (y/N) [n]:`` **y** |
---|
57 | ======================================================================================== ======================== |
---|
58 | |
---|
59 | Edit the document ``index.rst`` so it looks like this:: |
---|
60 | |
---|
61 | .. example documentation master file, created by |
---|
62 | sphinx-quickstart on Sun Jul 15 17:54:46 2012. |
---|
63 | You can adapt this file completely to your liking, but it should at least |
---|
64 | contain the root `toctree` directive. |
---|
65 | |
---|
66 | Welcome to example's documentation! |
---|
67 | =================================== |
---|
68 | |
---|
69 | .. autospecmacro:: hkl_ioc.mac |
---|
70 | |
---|
71 | Contents: |
---|
72 | |
---|
73 | .. toctree:: |
---|
74 | :maxdepth: 2 |
---|
75 | |
---|
76 | |
---|
77 | |
---|
78 | Indices and tables |
---|
79 | ================== |
---|
80 | |
---|
81 | * :ref:`genindex` |
---|
82 | * :ref:`modindex` |
---|
83 | * :ref:`search` |
---|
84 | |
---|
85 | Edit *conf.py* and make these changes (in order): |
---|
86 | |
---|
87 | =================================================== ============================== |
---|
88 | change location |
---|
89 | =================================================== ============================== |
---|
90 | ``extensions.append( 'sphinxcontrib.specdomain' )`` insert *after* line 28 |
---|
91 | =================================================== ============================== |
---|
92 | |
---|
93 | The result should look like this file: :download:`ex_markup/conf.py`. |
---|
94 | |
---|
95 | Apply Markup |
---|
96 | ============================== |
---|
97 | |
---|
98 | .. tip:: We will edit the *hkl_ioc.mac* file now. |
---|
99 | Before we start applying markup, |
---|
100 | it's a good idea to make a backup copy of the original:: |
---|
101 | |
---|
102 | cp hkl_ioc.mac hkl_ioc.mac.original |
---|
103 | |
---|
104 | Here are the steps to consider when converting SPEC comments to reST markup. |
---|
105 | |
---|
106 | #. identify the SPEC comments |
---|
107 | #. extended comments (docstrings) |
---|
108 | #. global docstring |
---|
109 | #. macro docstring |
---|
110 | #. others are ignored by Sphinx |
---|
111 | #. descriptive comments |
---|
112 | |
---|
113 | SPEC comments |
---|
114 | ------------------ |
---|
115 | |
---|
116 | Obvious as this sounds, it may help someone to see that the SPEC comments |
---|
117 | are on lines 1-32. It is easy to place this entire block within an |
---|
118 | extended comment [#]_ (known hereafter as a *docstring*). Make these |
---|
119 | additions to lines 1 and 32: |
---|
120 | |
---|
121 | ===== ============================================================================================= |
---|
122 | line new |
---|
123 | ===== ============================================================================================= |
---|
124 | 1 ``"""#===============================================================================`` |
---|
125 | 32 ``#==============================================================================="""`` |
---|
126 | ===== ============================================================================================= |
---|
127 | |
---|
128 | But this is not enough, as the content will look like a wreck. |
---|
129 | |
---|
130 | -------------------------- |
---|
131 | |
---|
132 | #=============================================================================== |
---|
133 | #**************SPEC macros for the Advanced Photon Source*********************** |
---|
134 | #=============================================================================== |
---|
135 | # |
---|
136 | # Beamline/Sector: 4ID |
---|
137 | # |
---|
138 | # Macro Package: hkl_ioc.mac |
---|
139 | # |
---|
140 | # Version: 1.0 (August,2005) |
---|
141 | # |
---|
142 | |
---|
143 | -------------------------- |
---|
144 | |
---|
145 | ... and so forth |
---|
146 | |
---|
147 | So, there is a choice to make. Either: |
---|
148 | |
---|
149 | #. format the comment as literal text (least work) |
---|
150 | #. reformat the as reST. (more work, looks better) |
---|
151 | |
---|
152 | Literal text |
---|
153 | ++++++++++++ |
---|
154 | |
---|
155 | To format a SPEC comment as literal text, consider this brief SPEC comment: |
---|
156 | |
---|
157 | .. code-block:: guess |
---|
158 | :linenos: |
---|
159 | |
---|
160 | # Summary: This macro scans the sample in a circular mesh. |
---|
161 | # |
---|
162 | # Dependencies: It is part of the circular mesh macro package. |
---|
163 | |
---|
164 | Its literal text rendition in reST is written: |
---|
165 | |
---|
166 | .. code-block:: guess |
---|
167 | :linenos: |
---|
168 | |
---|
169 | """ |
---|
170 | :: |
---|
171 | |
---|
172 | # Summary: This macro scans the sample in a circular mesh. |
---|
173 | # |
---|
174 | # Dependencies: It is part of the circular mesh macro package. |
---|
175 | |
---|
176 | """ |
---|
177 | |
---|
178 | which looks like: |
---|
179 | |
---|
180 | ------------------- |
---|
181 | |
---|
182 | :: |
---|
183 | |
---|
184 | # Summary: This macro scans the sample in a circular mesh. |
---|
185 | # |
---|
186 | # Dependencies: It is part of the circular mesh macro package. |
---|
187 | |
---|
188 | ------------------- |
---|
189 | |
---|
190 | Note that the two colons indicate literal text follows. |
---|
191 | Both the blank line after the colons and the final blank line are required to avoid warnings. |
---|
192 | And, the entire comment must be indented at least one column to the right of the two colons. |
---|
193 | |
---|
194 | reST markup |
---|
195 | +++++++++++++ |
---|
196 | |
---|
197 | In reST, the SPEC comment above might be written as two definition list items [#]_: |
---|
198 | |
---|
199 | .. code-block:: guess |
---|
200 | :linenos: |
---|
201 | |
---|
202 | """ |
---|
203 | Summary |
---|
204 | This macro scans the sample in a circular mesh. |
---|
205 | |
---|
206 | Dependencies |
---|
207 | It is part of the circular mesh macro package. |
---|
208 | |
---|
209 | """ |
---|
210 | |
---|
211 | which looks like: |
---|
212 | |
---|
213 | ------------------- |
---|
214 | |
---|
215 | Summary |
---|
216 | This macro scans the sample in a circular mesh. |
---|
217 | |
---|
218 | Dependencies |
---|
219 | It is part of the circular mesh macro package. |
---|
220 | |
---|
221 | ------------------- |
---|
222 | |
---|
223 | (The final blank line is necessary to avoid warnings.) |
---|
224 | |
---|
225 | This markup does not look too complicated until we reach the *Modification history* |
---|
226 | starting at line 23. The content here might be coded as either literal text (above) |
---|
227 | or a reST table. Since the table is easy *and* CVS is no longer used, we'll |
---|
228 | format it as a table. |
---|
229 | |
---|
230 | Consider this SPEC comment: |
---|
231 | |
---|
232 | .. code-block:: guess |
---|
233 | :linenos: |
---|
234 | |
---|
235 | #Revision 1.3 2006/05/22 20:34:35 jiaox |
---|
236 | #removed unsed lines in ioc_HKL. |
---|
237 | # |
---|
238 | #Revision 1.2 2006/05/11 17:46:31 jiaox |
---|
239 | #Added CVS Log entry. |
---|
240 | |
---|
241 | It might be put into a table [#]_ such as: |
---|
242 | |
---|
243 | .. code-block:: guess |
---|
244 | :linenos: |
---|
245 | |
---|
246 | ======== =================== ======= ===================================== |
---|
247 | Revision date/time author remarks |
---|
248 | ======== =================== ======= ===================================== |
---|
249 | 1.3 2006/05/22 20:34:35 jiaox removed unsed lines in ioc_HKL. |
---|
250 | 1.2 2006/05/11 17:46:31 jiaox Added CVS Log entry. |
---|
251 | ======== =================== ======= ===================================== |
---|
252 | |
---|
253 | which looks like: |
---|
254 | |
---|
255 | ------------------- |
---|
256 | |
---|
257 | ======== =================== ======= ===================================== |
---|
258 | Revision date/time author remarks |
---|
259 | ======== =================== ======= ===================================== |
---|
260 | 1.3 2006/05/22 20:34:35 jiaox removed unsed lines in ioc_HKL. |
---|
261 | 1.2 2006/05/11 17:46:31 jiaox Added CVS Log entry. |
---|
262 | ======== =================== ======= ===================================== |
---|
263 | |
---|
264 | ------------------- |
---|
265 | |
---|
266 | |
---|
267 | |
---|
268 | Global Docstring |
---|
269 | ------------------------- |
---|
270 | |
---|
271 | The convention is to treat the first docstring in a macro file as the *global docstring*. |
---|
272 | |
---|
273 | With these ideas in mind, |
---|
274 | here is the markup of the first 32 lines: |
---|
275 | |
---|
276 | .. code-block:: guess |
---|
277 | :linenos: |
---|
278 | |
---|
279 | """ |
---|
280 | SPEC macros for the Advanced Photon Source |
---|
281 | |
---|
282 | Beamline/Sector |
---|
283 | 4ID |
---|
284 | |
---|
285 | Macro Package |
---|
286 | hkl_ioc.mac |
---|
287 | |
---|
288 | Version |
---|
289 | 1.0 (August,2005) |
---|
290 | |
---|
291 | Description |
---|
292 | A user defined calcHKL to write the current HKL postion to a |
---|
293 | soft IOC. It requires spec softioc running. |
---|
294 | |
---|
295 | Written by |
---|
296 | X. Jiao 08/08/2005 |
---|
297 | |
---|
298 | Modified by: |
---|
299 | |
---|
300 | User macros |
---|
301 | ioc_HKL -> to turn on/off the feature of putting HKL to shared |
---|
302 | memory. |
---|
303 | |
---|
304 | Internal macros |
---|
305 | ioc_put_HKL -> write HKL to the soft IOC |
---|
306 | |
---|
307 | Modification history: |
---|
308 | |
---|
309 | ======== =================== ======= ===================================== |
---|
310 | Revision date/time author remarks |
---|
311 | ======== =================== ======= ===================================== |
---|
312 | 1.3 2006/05/22 20:34:35 jiaox removed unsed lines in ioc_HKL. |
---|
313 | 1.2 2006/05/11 17:46:31 jiaox Added CVS Log entry. |
---|
314 | ======== =================== ======= ===================================== |
---|
315 | """ |
---|
316 | |
---|
317 | which (except for the section title which is hard to render in this document) looks like: |
---|
318 | |
---|
319 | ------------------- |
---|
320 | |
---|
321 | Beamline/Sector |
---|
322 | 4ID |
---|
323 | |
---|
324 | Macro Package |
---|
325 | hkl_ioc.mac |
---|
326 | |
---|
327 | Version |
---|
328 | 1.0 (August,2005) |
---|
329 | |
---|
330 | Description |
---|
331 | A user defined calcHKL to write the current HKL postion to a |
---|
332 | soft IOC. It requires spec softioc running. |
---|
333 | |
---|
334 | Written by |
---|
335 | X. Jiao 08/08/2005 |
---|
336 | |
---|
337 | Modified by: |
---|
338 | |
---|
339 | User macros |
---|
340 | ioc_HKL -> to turn on/off the feature of putting HKL to shared |
---|
341 | memory. |
---|
342 | |
---|
343 | Internal macros |
---|
344 | ioc_put_HKL -> write HKL to the soft IOC |
---|
345 | |
---|
346 | Modification history: |
---|
347 | |
---|
348 | ======== =================== ======= ===================================== |
---|
349 | Revision date/time author remarks |
---|
350 | ======== =================== ======= ===================================== |
---|
351 | 1.3 2006/05/22 20:34:35 jiaox removed unsed lines in ioc_HKL. |
---|
352 | 1.2 2006/05/11 17:46:31 jiaox Added CVS Log entry. |
---|
353 | ======== =================== ======= ===================================== |
---|
354 | |
---|
355 | ------------------- |
---|
356 | |
---|
357 | |
---|
358 | Test |
---|
359 | ----------- |
---|
360 | |
---|
361 | Be sure to test your changes as you progress, until you are confident with reST markup. |
---|
362 | The *make* process is efficient, only rebuilding the documentation from affected |
---|
363 | .rst souce file changes. However, you might need to make sure changes in the .mac files |
---|
364 | cause documentation to be rebuilt. It might be easier, although less efficient, |
---|
365 | to rebuild your documentation each time. |
---|
366 | |
---|
367 | :: |
---|
368 | |
---|
369 | make clean html |
---|
370 | |
---|
371 | |
---|
372 | This is usually very fast (seconds). |
---|
373 | |
---|
374 | |
---|
375 | Docstring markup in each macro definition |
---|
376 | --------------------------------------------- |
---|
377 | |
---|
378 | Each of the macro definitions can be marked up to provide documentation with the definition. |
---|
379 | The convention is to supply a short one-line summary first, then additional information as appropriate. |
---|
380 | |
---|
381 | Consider the definition for ``ioc_HKL``. |
---|
382 | A summary of it is given in the first comment section. |
---|
383 | We'll apply that as the docstring: |
---|
384 | |
---|
385 | .. code-block:: guess |
---|
386 | :linenos: |
---|
387 | |
---|
388 | def ioc_HKL '{ |
---|
389 | """to turn on/off the feature of putting HKL to shared memory.""" |
---|
390 | |
---|
391 | if($# != 1) { eprint "Usage: ioc_HKL on/off ";exit} |
---|
392 | |
---|
393 | |
---|
394 | if(("$1" == "on")) { |
---|
395 | cdef("user_save_HKL","ioc_put_HKL","ioc_HKL","0x20") |
---|
396 | print "Now put HKL to softioc." |
---|
397 | exit |
---|
398 | } |
---|
399 | if(("$1" == "off")) { |
---|
400 | cdef("user_save_HKL","","ioc_HKL","delete") |
---|
401 | print "Stop put HKL to softioc." |
---|
402 | exit |
---|
403 | } |
---|
404 | eprint "Usage: ioc_HKL on/off " |
---|
405 | }' |
---|
406 | |
---|
407 | Do the same thing for the *ioc_put_HKL* macro definition. |
---|
408 | |
---|
409 | |
---|
410 | ----------------- |
---|
411 | |
---|
412 | .. rubric:: Footnotes |
---|
413 | |
---|
414 | .. [#] a SPEC :ref:`extended comment <spec-extended-comments>` is text that is surrounded by three |
---|
415 | double-quote characters (``"""``), such as: |
---|
416 | |
---|
417 | """this is triple-quoted text is a docstring""" |
---|
418 | |
---|
419 | In the Python language, this is known as a docstring. |
---|
420 | But, **unlike Python**, SPEC does not recognize single quotes |
---|
421 | to mark extended comments. Only use the double quote character. |
---|
422 | .. [#] definition list: http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#definition-lists |
---|
423 | .. [#] table: http://sphinx.pocoo.org/rest.html#tables |
---|