1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
|
.. -*- coding: utf-8 -*-
.. include:: <s5defs.txt>
==================================================
Docutils: Architecture, Extending, and Embedding
==================================================
.. class:: huge center
| David Goodger
| &
| Lea Wiemann
.. class:: big center
| http://docutils.sourceforge.net
|
|
.. container:: handout
We will describe the architecture of Docutils, how to add
functionality to Docutils, and how to use Docutils in your own
applications. Not necessarily in that order.
.. topic:: Introductions
:class: handout
David Goodger:
* project founder & architect
* a Python Enhancement Proposal (PEP) Editor
* was just elected Director of the Board of the PSF and appointed Secretary
* from, and currently living in: Montreal, Canada
* work for a large investment organization, writing software in Python
Lea Wiemann:
* joined the project about 2 years ago
* release manager
* from Paderborn, Germany
* work for a large software company in Paderborn, doing
automation work in Python
What is Docutils?
=================
.. class:: incremental
* Text processing framework :handout:`(because we need more
frameworks)`
* A set of tools for processing plaintext documentation into useful
formats, such as HTML, XML, and LaTeX
* 111,111 lines of code, tests, & documentation
.. class:: handout
Split about evenly between the three.
* Existing components:
.. class:: incremental
- reStructuredText parser
- Standalone document, PEP, document tree readers
- HTML (+ S5 & PEP), LaTeX, :handout:`native` XML, pseudo-XML
writers; experimental :handout:`(incomplete) writers for`
OpenDocument, RTF, man page
* Internal document model (tree of element & text nodes)
.. class:: handout
The doctree is the glue that holds everything together.
What is reStructuredText?
=========================
.. class:: incremental
* WYSIWYG plain-text markup language
* Very easy to read, unobtrusive markup
* Easy to write
* Powerful
.. class:: handout
Powerful enough for most uses, yet simple enough to fit your
brain. (DG) I designed it to fit *my* brain, which is relatively
small.
* Extensible
* Used for documentation, for taking notes, and for making
presentations.
.. class:: handout
There’s even a book that has been written with reStructuredText
(“C++ Template Metaprogramming” by David Abrahams & Aleksey
Gurtovoy), but they ran into the limitations of reST and
Docutils, so we wouldn't recommend this approach — yet.
reStructuredText has to (and will) become more powerful.
Status
======
.. class:: incremental
Docutils 0.4 released January 9.
* Experimental
`(That’s what the “0.” means)`
.. class:: incremental
- API subject to change\ `, but no arbitrary changes`
- Document model too
- A few bugs `(details, details)`
- Lots of to-do items `(come join our sprint!)`
* Yet it’s already very usable! :-)
* From release 0.4, micro releases (0.4.x) are bugfix-only.
We’re currently working on 0.5.
Existing Uses
=============
* Docutils front-end tools (rst2html.py, rst2s5.py, rstpep2html.py, etc.)
* Wikis (MoinMoin, ZWiki, Trac, others)
* Blogs
* PEPs, GLEPs :handout:`(Gentoo Linux)`, TIPs :handout:`(TCL)`, PEGs
:handout:`(Gzz)`
* Auto-documentation systems: Epydoc, Pudge, Endo
* Roundup
* Documentation: `from NASA` `to the William Tyndale Society Journal`
What’s Missing?
===============
.. class:: incremental
Major features:
* Plugin support
.. class:: handout
There are many existing extensions to Docutils (mostly in the
sandbox_), but they aren’t easily usable as plugins.
.. _sandbox: http://docutils.sourceforge.net/sandbox/
We want to be able to specify “use extension X, Y, and Z” from
the command line, or have a directory for auto-loaded plugins,
or both.
If you're a plugin guru, we could use your advice!
* Python source reader
.. class:: handout
This was the original “itch” that Docutils was created to
“scratch”, but the PySource reader isn’t functional yet.
Sprint! (Hint, hint)
* Nested inline markup
* Many more things (better GUI, reStructuredText writer, *<insert
your idea here>*, ...)
.. class:: handout
See the `to-do list`_.
.. _to-do list: http://docutils.sourceforge.net/docs/dev/todo.html
Please come to the Docutils Sprint and help out!
Component Architecture
======================
\ \
.. image:: components.png
.. class:: handout
In the component diagram, thick solid lines denote the transfer of
standard document tree data. The double line between Reader and
Transformer denotes a possibly non-standard document tree.
Data Flow (1)
=============
\ \
.. image:: components-small.png
:align: right
.. class:: handout
Docutils components are selected at run time by the client application
or front end.
.. class:: incremental
1. The **Publisher** calls the **Reader**.
.. class:: handout
The Reader understands the context of the input. For example, the
PEP Reader knows that PEPs begin with an RFC-822-style header, that
a table of contents should be added after the header, and that all
hyperlinks should be collected near the end of the document.
Typical text files use the Standalone Reader. To extract
docstrings & comments from Python source code, you’d use the Python
Source Reader (under active development). To reprocess an existing
document tree, use the doctree Reader.
2. The Reader calls an **Input** object to gather text data.
.. class:: handout
The Input classes provide a uniform interface for reading from
arbitrary low-level input sources, such as files, strings, and even
pre-parsed document trees. Input objects handle the decoding of
input text to Unicode. Unicode is exclusively used internally.
3. The Reader calls the **Parser**, passing the input text.
.. class:: handout
There are currently two parsers installed in Docutils: the
reStructuredText Parser, and the "Null" parser (used for
reprocessing existing document trees, in conjunction with the
doctree Reader and Input class). The parser generates a **document
tree**, a tree of element and Text nodes, and returns it to the
Reader.
4. The Reader returns the doctree(s) to the Publisher.
Data Flow (2)
=============
\ \
.. image:: components-small.png
:align: right
.. class:: incremental
5. The Publisher runs the **Transformer**.
.. class:: handout
The Transformer applies various **Transforms** to the document
tree, in a pre-determined order. Transforms modify the document
tree in-place: resolving references, numbering sections, creating
tables of contents, and performing other functions on the entire
document or parts of the document.
6. The Transformer returns the doctree to the Publisher.
.. class:: handout
At this point, the doctree is standard, no matter what Parser was
used or Reader context was in place.
7. The Publisher calls the **Writer**.
.. class:: handout
The Writer translates the document tree to a format like HTML or
LaTeX.
8. The Writer sends the result to an **Output** object.
.. class:: handout
As with Input, the Output object provides a uniform interface for
writing to arbitrary low-level destinations, such as files and
strings. Output objects also handle text encoding.
.. class:: handout
The Publisher directly calls only the Reader, the Transformer, and the
Writer. However it manages *all* objects (Input, Output, Reader,
Parser, Transformer, Transform, and Writer instances) and passes them
where they are needed. For example, the Input and Parser objects are
passed to the Reader.
All of this complexity is encapsulated in the Publisher convenience
functions; more on these later.
Document Tree
=============
.. class:: incremental
Sample input text::
"""
I like the Python_ language.
.. _Python: http://www.python.org/
"""
Resulting doctree::
<document source="doctree-demo.txt">
<paragraph>
I like the
<reference
refuri="http://www.python.org/">
Python
language.
.. class:: handout
The document tree data structure is similar to a DOM tree, but with
specific node names (classes) instead of DOM’s generic nodes. The
schema is documented in an XML Document Type Definition (DTD),
which comes in two parts:
* the Docutils Generic DTD, docutils.dtd, and
* the OASIS Exchange Table Model, soextbl.dtd.
The DTD defines a rich set of elements, suitable for many input and
output formats. The DTD retains all information necessary to
reconstruct the original input text, or a reasonable facsimile
thereof.
The document tree holds the components of Docutils together. The
document tree is the unifying intermediate data structure used
internally throughout Docutils, first created by the Parser and
translated by the Writer. The``docutils.nodes`` module is a class
library implementing the nodes of the document tree.
Docutils as a Library (1)
=========================
.. class:: handout
How to use Docutils from your own application.
.. class:: incremental
Convenience functions, from ``docutils.core``:
* .. parsed-literal::
**publish_cmdline**\ (writer_name='html',
\ \ \ description='...')
.. class:: handout
The ``publish_cmdline`` function is used by all the front-end
tools provided with Docutils. The example above is from
``rst2html.py``.
* .. parsed-literal::
**publish_file**\ (source_path='test.txt',
\ \ \ destination_path='test.tex',
\ \ \ writer_name='latex')
.. class:: handout
You can also pass file objects in the ``source`` and
``destination`` parameters.
* .. parsed-literal::
input = get_rst_document()
output = **publish_string**\ (source=input,
\ \ \ writer_name='html')
.. class:: handout
This is what is typically used in wikis and similar applications.
Docutils as a Library (2)
=========================
* ``publish_doctree``:
.. class:: incremental
.. parsed-literal::
>>> input = open('test.txt', 'r')
>>> document = **publish_doctree**\ (source=input)
`>>> print document.pformat()`
`<document source="<string>">
<paragraph>
This is a test.`
`>>> print document[0].pformat()`
`<paragraph>
This is a test.`
Docutils as a Library (3)
=========================
* ``publish_from_doctree``:
.. class:: incremental
.. parsed-literal::
>>> output = **publish_from_doctree**\ (
... document, writer_name='html')
`>>> print output`
`<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC ...>
<html xmlns="..." xml:lang="en" lang="en">
<head>...</head>
<body>
<div class="document">
<p>This is a test.</p>
</div>
</body>
</html>`
.. class:: handout
Nabu uses the ``publish_doctree`` and ``publish_from_doctree`` functions.
Extending Docutils
==================
.. class:: handout
Docutils is completely modular. New components of all types can be
added:
.. class:: incremental
* Readers
* Parsers
* Writers
..
* Transforms
Test-First Development
======================
The Test Suite
--------------
.. class:: incremental
- based on unittest.py
.. class:: handout
but with
- significant additions
- data-driven
- :handout:`we have` Test *modules* & test *packages*
- ``test_*.py``
- ``test_*/``
.. class:: handout
(requires an __init__.py module; a real package!)
- 1000 tests!
.. class:: handout
(DG) I first learned unit testing when I began Docutils. There is
absolutely no way I could have developed Docutils without unit
testing.
Extending reST
==============
.. class:: handout
reStructuredText has three extension mechanisms:
.. class:: incremental
* directives
* interpreted text roles
* language translations
.. class:: incremental
19 languages supported: `English,` `German,` `French,` `Dutch,`
`Italian,` `Russian,` `Esperanto,` `Japanese,` `Chinese`
`(simplified & traditional!)` `... and it’s easy to add new
languages`
Language Example
================
.. class:: incremental
German input text\ :handout:`(“bild” is German for “image”)`::
"""
.. bild:: test.png
"""
Process with this command line:
.. parsed-literal::
rst2html.py **--language de** test.txt test.html
Write a Transform
=================
Sprint!
=======
.. class:: huge center
Join the Docutils sprint!
.. class:: handout
We will both be here for all 4 sprint days.
And that’s just the beginning!
==============================
.. class:: big center incremental
| http://docutils.sourceforge.net
|
| `docutils-users@lists.sourceforge.net`
|
| `docutils-develop@lists.sourceforge.net`
.. class:: huge center incremental
Did we mention the sprint?
Thanks for listening!
Questions? `We’ve got answers!`
|