1 | .. $Id: howto-markup.rst 1027 2012-07-16 04:41:05Z 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:: ../markup_example/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 | (On my development system, this directory is called ``../markup_example``.) |
---|
42 | Copy the file above into this directory with the name *hkl_ioc.mac*. |
---|
43 | Then run:: |
---|
44 | |
---|
45 | sphinx-quickstart |
---|
46 | |
---|
47 | Take most of the defaults. |
---|
48 | These are the non-defaults for the example project: |
---|
49 | |
---|
50 | ======================================================================================== ================================ |
---|
51 | prompt response |
---|
52 | ======================================================================================== ================================ |
---|
53 | ``> Project name:`` **example** |
---|
54 | ``> Author name(s):`` **SPEC Macro Writer** |
---|
55 | ``> Project version:`` **1** |
---|
56 | ``> autodoc: automatically insert docstrings from modules (y/N) [n]:`` **y** |
---|
57 | ``> viewcode: include links to the source code of documented Python objects (y/N) [n]:`` **y** |
---|
58 | ======================================================================================== ================================ |
---|
59 | |
---|
60 | Edit the document ``index.rst`` so it looks like this:: |
---|
61 | |
---|
62 | .. example documentation master file, created by |
---|
63 | sphinx-quickstart on Sun Jul 15 17:54:46 2012. |
---|
64 | You can adapt this file completely to your liking, but it should at least |
---|
65 | contain the root `toctree` directive. |
---|
66 | |
---|
67 | Welcome to example's documentation! |
---|
68 | =================================== |
---|
69 | |
---|
70 | .. autospecmacro:: hkl_ioc.mac |
---|
71 | |
---|
72 | Contents: |
---|
73 | |
---|
74 | .. toctree:: |
---|
75 | :maxdepth: 2 |
---|
76 | |
---|
77 | |
---|
78 | |
---|
79 | Indices and tables |
---|
80 | ================== |
---|
81 | |
---|
82 | * :ref:`genindex` |
---|
83 | * :ref:`modindex` |
---|
84 | * :ref:`search` |
---|
85 | |
---|
86 | Edit *conf.py* and make these changes: |
---|
87 | |
---|
88 | =================================================== ============================== |
---|
89 | change directions |
---|
90 | =================================================== ============================== |
---|
91 | ``sys.path.insert(0, os.path.abspath('..'))`` *replace* line 19 |
---|
92 | ``extensions.append( 'sphinxcontrib.specdomain' )`` insert *after* line 28 |
---|
93 | =================================================== ============================== |
---|
94 | |
---|
95 | The revised file should look like this file: :download:`../markup_example/conf.py`. |
---|
96 | |
---|
97 | Now, build the documentation by typing:: |
---|
98 | |
---|
99 | make html |
---|
100 | firefox _build/html/index.html & |
---|
101 | |
---|
102 | You should expect a page that looks like this figure: |
---|
103 | |
---|
104 | .. figure:: example1.png |
---|
105 | :alt: view of original hkl_ioc.mac HTML documentation |
---|
106 | |
---|
107 | Documentation of the original **hkl_ioc.mac** file. |
---|
108 | |
---|
109 | |
---|
110 | Apply Markup |
---|
111 | ============================== |
---|
112 | |
---|
113 | .. tip:: We will edit the *hkl_ioc.mac* file now. |
---|
114 | Before we start applying markup, |
---|
115 | it's a good idea to make a backup copy of the original:: |
---|
116 | |
---|
117 | cp hkl_ioc.mac hkl_ioc.mac.original |
---|
118 | |
---|
119 | Here are the steps to consider when converting SPEC comments to reST markup. |
---|
120 | |
---|
121 | #. identify the SPEC comments |
---|
122 | #. extended comments (docstrings) |
---|
123 | #. global docstring |
---|
124 | #. macro docstring |
---|
125 | #. others are ignored by Sphinx |
---|
126 | #. descriptive comments |
---|
127 | |
---|
128 | SPEC comments |
---|
129 | ------------------ |
---|
130 | |
---|
131 | Obvious as this sounds, it may help someone to see that the SPEC comments |
---|
132 | are on lines 1-32. It is easy to place this entire block within an |
---|
133 | extended comment [#]_ (known hereafter as a *docstring*). Make these |
---|
134 | additions to lines 1 and 32: |
---|
135 | |
---|
136 | ===== ============================================================================================= |
---|
137 | line new |
---|
138 | ===== ============================================================================================= |
---|
139 | 1 ``"""#===============================================================================`` |
---|
140 | 32 ``#==============================================================================="""`` |
---|
141 | ===== ============================================================================================= |
---|
142 | |
---|
143 | But this is not enough, as the content will look like a wreck. |
---|
144 | |
---|
145 | -------------------------- |
---|
146 | |
---|
147 | #=============================================================================== |
---|
148 | #**************SPEC macros for the Advanced Photon Source*********************** |
---|
149 | #=============================================================================== |
---|
150 | # |
---|
151 | # Beamline/Sector: 4ID |
---|
152 | # |
---|
153 | # Macro Package: hkl_ioc.mac |
---|
154 | # |
---|
155 | # Version: 1.0 (August,2005) |
---|
156 | # |
---|
157 | |
---|
158 | -------------------------- |
---|
159 | |
---|
160 | ... and so forth |
---|
161 | |
---|
162 | So, there is a choice to make. Either: |
---|
163 | |
---|
164 | #. format the comment as literal text (least work) |
---|
165 | #. reformat the as reST. (more work, looks better) |
---|
166 | |
---|
167 | Literal text |
---|
168 | ++++++++++++ |
---|
169 | |
---|
170 | To format a SPEC comment as literal text, consider this brief SPEC comment: |
---|
171 | |
---|
172 | .. code-block:: guess |
---|
173 | :linenos: |
---|
174 | |
---|
175 | # Summary: This macro scans the sample in a circular mesh. |
---|
176 | # |
---|
177 | # Dependencies: It is part of the circular mesh macro package. |
---|
178 | |
---|
179 | Its literal text rendition in reST is written: |
---|
180 | |
---|
181 | .. code-block:: guess |
---|
182 | :linenos: |
---|
183 | |
---|
184 | """ |
---|
185 | :: |
---|
186 | |
---|
187 | # Summary: This macro scans the sample in a circular mesh. |
---|
188 | # |
---|
189 | # Dependencies: It is part of the circular mesh macro package. |
---|
190 | |
---|
191 | """ |
---|
192 | |
---|
193 | which looks like: |
---|
194 | |
---|
195 | ------------------- |
---|
196 | |
---|
197 | :: |
---|
198 | |
---|
199 | # Summary: This macro scans the sample in a circular mesh. |
---|
200 | # |
---|
201 | # Dependencies: It is part of the circular mesh macro package. |
---|
202 | |
---|
203 | ------------------- |
---|
204 | |
---|
205 | Note that the two colons indicate literal text follows. |
---|
206 | Both the blank line after the colons and the final blank line are required to avoid warnings. |
---|
207 | And, the entire comment must be indented at least one column to the right of the two colons. |
---|
208 | |
---|
209 | reST markup |
---|
210 | +++++++++++++ |
---|
211 | |
---|
212 | In reST, the SPEC comment above might be written as two definition list items [#]_: |
---|
213 | |
---|
214 | .. code-block:: guess |
---|
215 | :linenos: |
---|
216 | |
---|
217 | """ |
---|
218 | Summary |
---|
219 | This macro scans the sample in a circular mesh. |
---|
220 | |
---|
221 | Dependencies |
---|
222 | It is part of the circular mesh macro package. |
---|
223 | |
---|
224 | """ |
---|
225 | |
---|
226 | which looks like: |
---|
227 | |
---|
228 | ------------------- |
---|
229 | |
---|
230 | Summary |
---|
231 | This macro scans the sample in a circular mesh. |
---|
232 | |
---|
233 | Dependencies |
---|
234 | It is part of the circular mesh macro package. |
---|
235 | |
---|
236 | ------------------- |
---|
237 | |
---|
238 | (The final blank line is necessary to avoid warnings.) |
---|
239 | |
---|
240 | This markup does not look too complicated until we reach the *Modification history* |
---|
241 | starting at line 23. The content here might be coded as either literal text (above) |
---|
242 | or a reST table. Since the table is easy *and* CVS is no longer used, we'll |
---|
243 | format it as a table. |
---|
244 | |
---|
245 | Consider this SPEC comment: |
---|
246 | |
---|
247 | .. code-block:: guess |
---|
248 | :linenos: |
---|
249 | |
---|
250 | #Revision 1.3 2006/05/22 20:34:35 jiaox |
---|
251 | #removed unsed lines in ioc_HKL. |
---|
252 | # |
---|
253 | #Revision 1.2 2006/05/11 17:46:31 jiaox |
---|
254 | #Added CVS Log entry. |
---|
255 | |
---|
256 | It might be put into a table [#]_ such as: |
---|
257 | |
---|
258 | .. code-block:: guess |
---|
259 | :linenos: |
---|
260 | |
---|
261 | ======== =================== ======= ===================================== |
---|
262 | Revision date/time author remarks |
---|
263 | ======== =================== ======= ===================================== |
---|
264 | 1.3 2006/05/22 20:34:35 jiaox removed unsed lines in ioc_HKL. |
---|
265 | 1.2 2006/05/11 17:46:31 jiaox Added CVS Log entry. |
---|
266 | ======== =================== ======= ===================================== |
---|
267 | |
---|
268 | which looks like: |
---|
269 | |
---|
270 | ------------------- |
---|
271 | |
---|
272 | ======== =================== ======= ===================================== |
---|
273 | Revision date/time author remarks |
---|
274 | ======== =================== ======= ===================================== |
---|
275 | 1.3 2006/05/22 20:34:35 jiaox removed unsed lines in ioc_HKL. |
---|
276 | 1.2 2006/05/11 17:46:31 jiaox Added CVS Log entry. |
---|
277 | ======== =================== ======= ===================================== |
---|
278 | |
---|
279 | ------------------- |
---|
280 | |
---|
281 | |
---|
282 | |
---|
283 | Global Docstring |
---|
284 | ------------------------- |
---|
285 | |
---|
286 | The convention is to treat the first docstring in a macro file as the *global docstring*. |
---|
287 | |
---|
288 | With these ideas in mind, |
---|
289 | here is the markup of the first 32 lines: |
---|
290 | |
---|
291 | .. code-block:: guess |
---|
292 | :linenos: |
---|
293 | |
---|
294 | """ |
---|
295 | SPEC macros for the Advanced Photon Source |
---|
296 | |
---|
297 | Beamline/Sector |
---|
298 | 4ID |
---|
299 | |
---|
300 | Macro Package |
---|
301 | hkl_ioc.mac |
---|
302 | |
---|
303 | Version |
---|
304 | 1.0 (August,2005) |
---|
305 | |
---|
306 | Description |
---|
307 | A user defined calcHKL to write the current HKL postion to a |
---|
308 | soft IOC. It requires spec softioc running. |
---|
309 | |
---|
310 | Written by |
---|
311 | X. Jiao 08/08/2005 |
---|
312 | |
---|
313 | Modified by: |
---|
314 | |
---|
315 | User macros |
---|
316 | ioc_HKL -> to turn on/off the feature of putting HKL to shared |
---|
317 | memory. |
---|
318 | |
---|
319 | Internal macros |
---|
320 | ioc_put_HKL -> write HKL to the soft IOC |
---|
321 | |
---|
322 | Modification history: |
---|
323 | |
---|
324 | ======== =================== ======= ===================================== |
---|
325 | Revision date/time author remarks |
---|
326 | ======== =================== ======= ===================================== |
---|
327 | 1.3 2006/05/22 20:34:35 jiaox removed unsed lines in ioc_HKL. |
---|
328 | 1.2 2006/05/11 17:46:31 jiaox Added CVS Log entry. |
---|
329 | ======== =================== ======= ===================================== |
---|
330 | """ |
---|
331 | |
---|
332 | which (except for the section title which is hard to render in this document) looks like: |
---|
333 | |
---|
334 | ------------------- |
---|
335 | |
---|
336 | Beamline/Sector |
---|
337 | 4ID |
---|
338 | |
---|
339 | Macro Package |
---|
340 | hkl_ioc.mac |
---|
341 | |
---|
342 | Version |
---|
343 | 1.0 (August,2005) |
---|
344 | |
---|
345 | Description |
---|
346 | A user defined calcHKL to write the current HKL postion to a |
---|
347 | soft IOC. It requires spec softioc running. |
---|
348 | |
---|
349 | Written by |
---|
350 | X. Jiao 08/08/2005 |
---|
351 | |
---|
352 | Modified by: |
---|
353 | |
---|
354 | User macros |
---|
355 | ioc_HKL -> to turn on/off the feature of putting HKL to shared |
---|
356 | memory. |
---|
357 | |
---|
358 | Internal macros |
---|
359 | ioc_put_HKL -> write HKL to the soft IOC |
---|
360 | |
---|
361 | Modification history: |
---|
362 | |
---|
363 | ======== =================== ======= ===================================== |
---|
364 | Revision date/time author remarks |
---|
365 | ======== =================== ======= ===================================== |
---|
366 | 1.3 2006/05/22 20:34:35 jiaox removed unsed lines in ioc_HKL. |
---|
367 | 1.2 2006/05/11 17:46:31 jiaox Added CVS Log entry. |
---|
368 | ======== =================== ======= ===================================== |
---|
369 | |
---|
370 | ------------------- |
---|
371 | |
---|
372 | |
---|
373 | Test |
---|
374 | ----------- |
---|
375 | |
---|
376 | Be sure to test your changes as you progress, until you are confident with reST markup. |
---|
377 | The *make* process is efficient, only rebuilding the documentation from affected |
---|
378 | .rst souce file changes. Usually, this also considers changes in the .mac files. |
---|
379 | This command is usually all it takes to rebuild the HTML documentation:: |
---|
380 | |
---|
381 | make html |
---|
382 | |
---|
383 | However, you might wish to make sure changes in the .mac files |
---|
384 | cause documentation to be rebuilt. It might be easier, although less efficient, |
---|
385 | to rebuild your HTML documentation each time using this command:: |
---|
386 | |
---|
387 | make clean html |
---|
388 | |
---|
389 | The builds are usually very fast (seconds). |
---|
390 | |
---|
391 | |
---|
392 | Docstring markup in each macro definition |
---|
393 | --------------------------------------------- |
---|
394 | |
---|
395 | Each of the macro definitions can be marked up to provide documentation with the definition. |
---|
396 | The convention is to supply a short one-line summary first, then additional information as appropriate. |
---|
397 | |
---|
398 | Consider the definition for ``ioc_HKL``. |
---|
399 | A summary of it is given in the first comment section. |
---|
400 | We'll apply that as the docstring: |
---|
401 | |
---|
402 | .. code-block:: guess |
---|
403 | :linenos: |
---|
404 | |
---|
405 | def ioc_HKL '{ |
---|
406 | """to turn on/off the feature of putting HKL to shared memory.""" |
---|
407 | |
---|
408 | if($# != 1) { eprint "Usage: ioc_HKL on/off ";exit} |
---|
409 | |
---|
410 | |
---|
411 | if(("$1" == "on")) { |
---|
412 | cdef("user_save_HKL","ioc_put_HKL","ioc_HKL","0x20") |
---|
413 | print "Now put HKL to softioc." |
---|
414 | exit |
---|
415 | } |
---|
416 | if(("$1" == "off")) { |
---|
417 | cdef("user_save_HKL","","ioc_HKL","delete") |
---|
418 | print "Stop put HKL to softioc." |
---|
419 | exit |
---|
420 | } |
---|
421 | eprint "Usage: ioc_HKL on/off " |
---|
422 | }' |
---|
423 | |
---|
424 | Do the same thing for the *ioc_put_HKL* macro definition. |
---|
425 | |
---|
426 | Document the global variable |
---|
427 | ---------------------------------- |
---|
428 | |
---|
429 | On line 45 (in the original file), there is a global variable declaration: ``global SIOC_PV``. |
---|
430 | The description for this variable has been deduced from its usage in this file with EPICS. [#]_ |
---|
431 | It is possible to document :spec:global:`SIOC_PV` using a :ref:`descriptive comment`. |
---|
432 | Insert the line containing ``global SIOC_PV`` with this one:: |
---|
433 | |
---|
434 | global SIOC_PV ;#: soft IOC PV name |
---|
435 | |
---|
436 | The semicolon is used to ensure that the spec command is finished. It might be unnecessary. |
---|
437 | The descriptive comment can be used *in-line* to define the item on that line. |
---|
438 | |
---|
439 | Similarly, add another :ref:`descriptive comment` to document line 38 (original file) by inserting this |
---|
440 | line *before* the line that reads ``cdef("user_save_HKL","","ioc_HKL")``:: |
---|
441 | |
---|
442 | #: preload check/setting here |
---|
443 | |
---|
444 | Here the descriptive comment appearing on one line will provide a summary |
---|
445 | for the item defined on the next line only. |
---|
446 | |
---|
447 | Final Results |
---|
448 | ================= |
---|
449 | |
---|
450 | Rebuild the completed :download:`../markup_example/hkl_ioc.mac` documentation with:: |
---|
451 | |
---|
452 | make html |
---|
453 | |
---|
454 | .. note:: Don't be concerned about the warnings of |
---|
455 | ``SEVERE: Duplicate ID: "cdef-user_save_HKL".`` |
---|
456 | These messages are only informative. |
---|
457 | This :ref:`bug <bugs>` should be resolved in a future version of ``specdomain``. |
---|
458 | |
---|
459 | After refreshing the page in the WWW browser, it should like this: |
---|
460 | |
---|
461 | .. figure:: example2.png |
---|
462 | :alt: view of marked-up hkl_ioc.mac HTML documentation |
---|
463 | |
---|
464 | Documentation of the marked-up **hkl_ioc.mac** file. |
---|
465 | |
---|
466 | and the index will look like: |
---|
467 | |
---|
468 | .. figure:: example3.png |
---|
469 | :alt: index of marked-up hkl_ioc.mac HTML documentation |
---|
470 | |
---|
471 | Index of the marked-up **hkl_ioc.mac** file. |
---|
472 | |
---|
473 | .. note:: It is :ref:`planned for the future <todo>` to provide options for sorting the output alphabetically |
---|
474 | and to provide other features. |
---|
475 | |
---|
476 | ----------------- |
---|
477 | |
---|
478 | .. rubric:: Footnotes |
---|
479 | |
---|
480 | .. [#] a SPEC :ref:`extended comment <spec-extended-comments>` is text that is surrounded by three |
---|
481 | double-quote characters (``"""``), such as: |
---|
482 | |
---|
483 | """this triple-quoted text is a docstring""" |
---|
484 | |
---|
485 | In the Python language, this is known as a docstring. |
---|
486 | But, **unlike Python**, SPEC does not recognize single quotes |
---|
487 | to mark extended comments. Only use the double quote character. |
---|
488 | .. [#] definition list: http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#definition-lists |
---|
489 | .. [#] table: http://sphinx.pocoo.org/rest.html#tables |
---|
490 | .. [#] EPICS: http://www.aps.anl.gov/epics |
---|