Documentation update
[pylit.git] / rstdocs / tutorial / index.txt
blob03b7deb5339e93c495c73bbc222ca6c6117444e6
1 .. -*- rst-mode -*-
2 .. 
3   restindex
4       crumb: Tutorial
5       page-title: PyLit - Tutorial
6   /restindex
8 How to write program documents with PyLit
9 =========================================
11 .. sectnum::
12 .. contents::
14 Hello world
15 -----------
17 We start with a classical example in Python_
19   .. include:: hello.py
20      :literal:
22 save it as ``hello.py`` and convert to a `reStructured Text`_ document
23 with ``pylit.py``::
25   #> python pylit.py hello.py
26   extract written to hello.py.txt
28 The output file ``hello.py.txt`` looks like
30   .. include:: hello.py.txt
31      :literal:
33 We can see the difference between "commented code" and "code living in a
34 text document".
36 Points to mention:
38 * One can start literate programming with an existing code file (and without
39   knowledge of reStructured Text syntax).
40   
41 * *Documentation* is uncommented (if it is separated from code by a
42   blank line and has a recognised comment string at the start of each line).
44 * A double colon is added at the end of the text block. It is the
45   `reStructured Text`_ marker for the following `literal block`_.
46   (No marker is added, if the text block already ends with a double colon.)
48 * *Code* is indented to form a literal block. It will be printed using a
49   monospaced font and without reStructured Text substitutions.
51 * PyLit adds ".txt" to the filename for the text version.
53 Now we can add some more documentation and a link (of course, knowledge of
54 `reStructured Text syntax`_ helps in this stage):
56   .. include:: hello_2.py.txt
57      :literal:
59 Pretty-printed with docutils, it looks like
61 .. admonition:: Hello World
63    .. include:: hello_2.py.txt
65 If we re-convert the result to code, ::
67   #> python pylit.py hello_2.py.txt
68   extract written to hello_2.py
70 we get
72   .. include:: hello_2.py
73      :literal:
75 Points to mention:
77 * The double colon that was added in the first conversion is not stripped in
78   the re-conversion. 
79   
80   (Generally, a round-trip should not introduce changes after the first
81   cycle. This way it is ensured that the line-numbers are the same for text
82   and code source.)
83   
84 * The code block ends at the first non indented line (Precisely, at the
85   first line that is not more indented than the preceding text block.)
88 Textblocks and Comments
89 -----------------------
91 Comment lines are only transformed to a text block, if they
93 * start with a matching `comment string` (whitespace counts!, the Python
94   default is ``'# '``), and
95 * are separated from non-text lines by at least one blank[#]_ line
97 Otherwise, they are kept as commented code.
99 An example will illustrate this. The code::
101   # 99bottles.py -- print the famous "99 bottles of beer" song lyrics
102   
103   # This is used as an introductory example to literate programming
104   # in the literateprograms.org Wiki.
105   #
106   # count down from 99 to 1
107   for bottles in range(99,0,-1):
108       ....
110 is mapped to text as::
112   99bottles.py -- print the famous "99 bottles of beer" song lyrics
113   
114   ::
116     # This is used as an introductory example to literate programming
117     # in the literateprograms.org Wiki.
118     #
119     # count down from 99 to 1
120     for bottles in range(99,0,-1):
121         ....
123 The comment in the 5th line marks the "secondary documentation" as part of
124 the code block.
126 However, ::
128   # 99bottles.py -- print the famous "99 bottles of beer" song lyrics
129   #
130   # This is used as an introductory example to literate programming
131   # in the literateprograms.org Wiki.
132   
133   # count down from 99 to 1
134   for bottles in range(99,0,-1):
135       ....
137 is mapped to text as::
139   99bottles.py -- print the famous "99 bottles of beer" song lyrics
141   This is used as an introductory example to literate programming
142   in the literateprograms.org Wiki.
143   
144   ::
145   
146     # count down from 99 to 1
147     for bottles in range(99,0,-1):
148         ....
150 The comment in the 2nd line is removed, as it is inside a documentation block.
152 .. [#] a line is considered blank, if it contains only whitespace
155 File Headers
156 ------------
158 Sometimes code needs to remain on the first line(s) of the document to be
159 valid. The most common example is the shebang_ line that tells a POSIX shell
160 how to process an executable file. In Python, the magic comment specifying
161 the `source code encoding`_ must occure on line one or two:
163   .. include:: hello_with_header.py
164      :literal:
166 Headers are converted to a comment in the text source: 
168   .. include:: hello_with_header.py.txt
169      :literal:
171 Pretty-printed with docutils, it looks like
173    .. include:: hello_with_header.py.txt
175 Everything before the first text block (i.e. before the first paragraph
176 using the matching comment string) will be hidden in HTML or PDF output.
178 It may come as surprise that a part of the file is not "printed".
179 (In the case that there is no matching comment at all, the complete code
180 source will become a comment, however, in this case it is not likely
181 the source is a literate program anyway). But there are advantages also:
183 * line numbers are kept during the text <--> code conversion (which would be
184   impossible with a literal block marker as this needs to be at the end of
185   the preceding paragraph)
186 * you can hide a repeating (or boring) header in a project consisting of
187   many source files.
189 If needed for the documentation, it is possible to repeat the header in the
190 the first text block, e.g. using a `parsed literal block`_:
192 .. parsed-literal::
194   #!/usr/bin/env python
195   # -*- coding: iso-8859-1 -*-
198 Doctests
199 --------
201 Pylit supports `Python doctests`_ in a literate script. 
203 We add a doctest to our example:
205   .. include:: hello_with_doctest.py
206      :literal:
208 * there is no double colon before the doctest. A doctest block is
209   recognised by starting with the Python interpreter prompt ``>>>`` instead.
211 Now try it with ::
213   #>  python -c "import doctest; doctest.testfile('hello_with_doctest.py')"
214   
215 There is no output. So everything is OK? Unfortunatly not:
216 ``doctest.testfile`` does not find the test, as it is "hidden" in a comment.
218 Pylit runs the doctest on the text version of the source and we get ::
220   #> pylit --doctest hello_with_doctest.py  
221   **********************************************************************
222   File "hello_with_doctest.py", line 3, in 
223   Failed example:
224       execfile("hello_with_doctest.py")
225   Expected:
226       Hello world
227   Got:
228       Hello world.
230 Ah yes, we forgot the full-stop in our doctest. Adding it and testing again::
232   #> pylit --doctest hello_with_doctest_2.py  
233   0 failures in 1 tests
235 The printed summary will ensure us that the test actually passed.
238 Including files
239 ---------------
241 PyLit does not allow the specification of a separate output file for
242 individual code blocks like e.g. noweb_. The "dual source" concept limits
243 the choice to one output file per input file. However, this can be
244 compensated by the use of the `include directive`_.
246 Let us assume that for some reason, the friendly greeting should be defined
247 in a separate file ``greeting.py``:
249   .. include:: greeting.py
250      :literal:
252 The documentation of the calling file can include the executed file
254   .. include:: hello_multifile.py
255      :literal:
257 Saved to ``hello_multifile.py.txt`` and pretty-printed with docutils, this
258 looks like
260   .. include:: hello_multifile.py.txt
263 * you have to convert both, ``greeting.py`` and ``hello_multifile.py``.
264   (Currently, pylit cannot do 'batch processing' of multiple input files.)
265   
267 .. References:
269 .. _reStructured Text: 
270      http://docutils.sourceforge.net/docs/user/rst/quickref.html
272 .. _Installation: ../download/index.html#installation
274 .. _shebang: http://en.wikipedia.org/wiki/Shebang_(Unix)
276 .. _reStructured Text syntax: 
277      http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html
279 .. _literal block: 
280      http://docutils.sourceforge.net/docs/user/rst/quickref.html#literal-blocks
282 .. _parsed literal block:
283      http://docutils.sourceforge.net/docs/ref/rst/directives.html#parsed-literal-block
284 .. _noweb: http://www.eecs.harvard.edu/~nr/noweb/
286 .. _include directive:
287      http://docutils.sourceforge.net/docs/ref/rst/directives.html#including-an-external-document-fragment
289 .. _source code encoding:
290     http://docs.python.org/tut/node4.html#SECTION004230000000000000000
292 .. _Python doctests: http://docs.python.org/lib/module-doctest.html