Debian Codegraph @

docs/user/odt.rst

raw
1 .. include:: ../header.rst
2
3 =======================
4 ODT Writer for Docutils
5 =======================
6
7 :Author: Dave Kuhlman
8 :Contact: [email protected]
9 :Revision: $Revision: 9953 $
10 :Date: $Date: 2024-10-20 19:44:36 +0200 (So, 20. Okt 2024) $
11 :Copyright: This document has been placed in the public domain.
12
13 :abstract: This document describes the Docutils writer for
14 OpenDocument Text (.odt) documents.
15
16 .. sectnum::
17
18 .. contents::
19
20
21 Introduction
22 ============
23
24 The Docutils front end rst2odt_ translates reStructuredText_ into an
25 `OpenDocument Text`_ (.odt) file.
26 OpenDocument files `can be opened by most modern office software`__.
27 It is the native file format for LibreOffice_ Writer.
28
29 __ https://en.wikipedia.org/wiki/OpenDocument#Software
30
31
32 Requirements
33 ============
34
35 In addition to the Docutils standard requirements, ``odtwriter``
36 requires:
37
38 - Optional -- `Pygments`_ is required if you want syntax
39 highlighting of code in literal blocks. See section `Syntax
40 highlighting`_.
41
42 - Optional -- `Python Imaging Library`_ (PIL/Pillow_) is required if
43 you use the image or figure directive but don't specify ``width``
44 and ``height``. See section `Images and figures`_.
45
46
47
48 How to Use It
49 =============
50
51 Run it from the command line as follows::
52
53 $ rst2odt myinput.rst > myoutput.odt
54
55 To see usage information and to learn about command line options
56 that you can use, run the following::
57
58 $ rst2odt --help
59
60 Examples::
61
62 $ rst2odt -s -g python_comments.rst > python_comments.odt
63
64 $ rst2odt --source-url=odtwriter.rst --generator \
65 --stylesheet=/myconfigs/styles.odt odtwriter.rst > odtwriter.odt
66
67
68 Configuration file
69 ------------------
70
71 The options described below can also be set in a `configuration file`_.
72 Use section `[odf_odt writer]`_ to set options specific to the
73 ``odtwriter``. For example::
74
75 [odf_odt writer]
76 stylesheet: styles1.odt
77
78 See the `Docutils Configuration`_ document for more information on
79 Docutils configuration files, including locations which are
80 searched.
81
82 .. _Docutils Configuration: config.html
83 .. _configuration file: config.html#configuration-files
84 .. _[odf_odt writer]: config.html#odf-odt-writer
85
86
87 Command line options
88 --------------------
89
90 The following command line options are specific to ``odtwriter``:
91
92 --stylesheet=<URL> Specify a stylesheet URL, used verbatim.
93 Default: writers/odf_odt/styles.odt in the
94 installation directory.
95 --odf-config-file=<file>
96 Specify a configuration/mapping file relative to the
97 current working directory for additional ODF options.
98 In particular, this file may contain a section named
99 "Formats" that maps default style names to names to be
100 used in the resulting output file allowing for
101 adhering to external standards. For more info and the
102 format of the configuration/mapping file, see the
103 odtwriter doc.
104 --cloak-email-addresses
105 Obfuscate email addresses to confuse harvesters while
106 still keeping email links usable with standards-
107 compliant browsers.
108 --no-cloak-email-addresses
109 Do not obfuscate email addresses.
110 --table-border-thickness=TABLE_BORDER_THICKNESS
111 Specify the thickness of table borders in thousands of
112 a cm. Default is 35.
113 --add-syntax-highlighting
114 Add syntax highlighting in literal code blocks.
115 --no-syntax-highlighting
116 Do not add syntax highlighting in literal code blocks.
117 (default)
118 --create-sections Create sections for headers. (default)
119 --no-sections Do not create sections for headers.
120 --create-links Create links.
121 --no-links Do not create links. (default)
122 --endnotes-end-doc Generate endnotes at end of document, not footnotes at
123 bottom of page.
124 --no-endnotes-end-doc Generate footnotes at bottom of page, not endnotes at
125 end of document. (default)
126 --generate-list-toc Generate a bullet list table of contents, not an
127 ODF/``oowriter`` table of contents.
128 --generate-oowriter-toc
129 Generate an ODF/``oowriter`` table of contents,
130 not a bullet list. (default) **Note:**
131 ``odtwriter`` is not able to determine page
132 numbers, so you will need to open the
133 generated document in ``oowriter``, then
134 right-click on the table of contents and
135 select "Update" to insert page numbers.
136 --custom-odt-header=CUSTOM_HEADER
137 Specify the contents of an custom header line. See
138 odf_odt writer documentation for details about special
139 field character sequences. See section
140 `Custom header/footers: inserting page numbers, date, time, etc`_
141 for details
142 --custom-odt-footer=CUSTOM_FOOTER
143 Specify the contents of an custom footer line. See
144 odf_odt writer documentation for details about special
145 field character sequences. See section
146 `Custom header/footers: inserting page numbers, date, time, etc`_
147 for details
148
149
150
151 Styles and Classes
152 ==================
153
154 ``odtwriter`` uses a number of styles that are defined in
155 ``styles.xml`` in the default ``styles.odt``. This section
156 describes those styles.
157
158 Note that with the ``--stylesheet`` command line option, you can
159 use either ``styles.odt`` or ``styles.xml``, as described below.
160 Use of ``styles.odt`` is recommended over ``styles.xml`` as, e.g.,
161 customizing `table styles`_ does not work with an ``*.xml``
162 stylesheet file.
163
164 You can modify the look of documents generated by ``odtwriter`` in
165 several ways:
166
167 - Open (a copy of) ``styles.odt`` in ``OpenOffice/oowriter`` and
168 modify the style you wish to change. Now, save this document,
169 then generate your documents using this modified copy of
170 ``styles.odt``.
171
172 In my version of ``oowriter``, to modify styles, either (1)
173 press F11 or (2) use menu item "Format/Styles and Formatting",
174 then right-click on the relevant style and select "Modify".
175 Modify the style, then save your document.
176
177 - Open a document generated by ``odtwriter`` in `oowriter``. Now,
178 edit the style you are interested in modifying. Now, you
179 can extract the styles.xml file from your document and either
180 (1) use this as your default styles file or (2) copy and paste
181 the relevant style definition into your styles.xml.
182
183 - Extract ``styles.xml`` from ``styles.odt`` using your favorite
184 ``zip/unzip`` tool. Then modify ``styles.xml`` with a text
185 editor. Now re-zip it back into your own ``styles.odt``, or use
186 it directly by specifying it with the ``--stylesheet`` command
187 line option. **Hint:** If you intend to extract ``styles.xml``
188 from an ``.odt`` file (and then "re-zip" it), you should turn off
189 XML optimization/compression in ``oowriter``. In order to this
190 in ``oowriter``, use Tools --> Options... --> Load-Save -->
191 General and turn off "Size optimization for XML format".
192
193 - Open an empty (or new) document in ``oowriter``. Define all of
194 the styles described in this section. Then, use that document (a
195 .odt file) as your stylesheet. ``odtwriter`` will extract the
196 ``styles.xml`` file from that document and insert it into the
197 output document.
198
199 - Some combination of the above.
200
201
202 Styles used by odtwriter
203 ------------------------
204
205 This section describes the styles used by ``odtwriter``.
206
207 Note that we do not describe the "look" of these styles. That can
208 be easily changed by using ``oowriter`` to edit the document
209 ``styles.odt`` (or a copy of it), and modifying any of the styles
210 described here.
211
212 To change the definition and appearance of these styles, open
213 ``styles.odt`` in ``oowriter`` and open the Styles and Formatting
214 window by using the following menu item::
215
216 Format --> Styles and Formatting
217
218 Then, click on the Paragraph Styles button or the Character Styles
219 button at the top of the Styles and Formatting window. You may
220 also need to select "All Styles" from the drop-down selection list
221 at the bottom of the Styles and Formatting window in order to see
222 the styles used by ``odtwriter``.
223
224 Notice that you can make a copy of file ``styles.odt``, modify it
225 using ``oowriter``, and then use your copy with the
226 ``--stylesheet=<file>`` command line option. Example::
227
228 $ rst2odt --stylesheet=mystyles.odt test2.rst > test2.odt
229
230
231 Paragraph styles
232 ~~~~~~~~~~~~~~~~
233
234 rststyle-attribution
235 The style for attributions, for example, the attribution in a
236 ``.. epigraph::`` directive. Derived from
237 ``rststyle-blockquote``.
238
239 rststyle-blockindent
240 An indented block.
241
242 rststyle-blockquote
243 A block quote.
244
245 rststyle-blockquote-bulletitem
246 The style for bullet list items inside block quote.
247
248 rststyle-blockquote-enumitem
249 The style for enumerated list items inside block quote.
250
251 rststyle-bodyindent
252 An indented block.
253
254 rststyle-bulletitem
255 An item in an bullet list.
256
257 rststyle-caption
258 The caption in a figure or image. Also see
259 ``rststyle-legend``.
260
261 rststyle-codeblock
262 Literal code blocks -- A block of example code. Created with
263 double colon ("::") followed by an indented block or with the
264 ``.. parsed-literal::`` directive. Derived from the
265 ``Preformatted Text`` style in ``oowriter``.
266
267 rststyle-enumitem
268 An item in an enumerated list.
269
270 rststyle-epigraph
271 The style for epigraphs, for example, the body of an
272 ``.. epigraph::`` directive. Derived from
273 ``rststyle-blockquote``.
274
275 rststyle-epigraph-bulletitem
276 The style for bullet list items inside epigraphs.
277
278 rststyle-epigraph-enumitem
279 The style for enumerated list items inside epigraphs.
280
281 rststyle-footer
282 The style for footers. The footer content originates from the
283 ``..footer::`` directive and in response to the command line
284 flags for generator (``--generator``), date/time generated
285 (``--date`` and ``--time``), and view source link
286 (``--source-link`` and ``--source-url=URL``).
287
288 rststyle-header
289 The style for headers. The header content originates from the
290 ``..header::`` directive.
291
292 rststyle-highlights
293 The style for highlightss, for example, the body of an
294 ``.. highlights::`` directive. Derived from
295 ``rststyle-blockquote``.
296
297 rststyle-highlights-bulletitem
298 The style for bullet list items inside highlights.
299
300 rststyle-highlights-enumitem
301 The style for enumerated list items inside highlights.
302
303 rststyle-horizontalline
304 A horizontal line, e.g. used for transitions.
305
306 rststyle-legend
307 The legend in a figure. See the Docutils figure directive. Also
308 see ``rststyle-caption``.
309
310 rststyle-table-title
311 The style for titles of tables. See section `The table
312 directive`_.
313
314 rststyle-textbody
315 Normal text. The style for paragraphs. Derived from the ``Text
316 body`` style in ``oowriter``.
317
318
319 Character styles
320 ~~~~~~~~~~~~~~~~
321
322 rststyle-emphasis
323 Emphasis. Normally rendered as italics.
324
325 rststyle-inlineliteral
326 An inline literal.
327
328 rststyle-strong
329 Strong emphasis. Normally rendered as boldface.
330
331 rststyle-quotation
332 In-line quoted material.
333
334 rststyle-codeblock-classname
335 Syntax highlighting in literal code blocks -- class names.
336
337 rststyle-codeblock-comment
338 Syntax highlighting in literal code blocks -- comments.
339
340 rststyle-codeblock-functionname
341 Syntax highlighting in literal code blocks -- function names.
342
343 rststyle-codeblock-keyword
344 Syntax highlighting in literal code blocks -- Python language
345 keywords.
346
347 rststyle-codeblock-name
348 Syntax highlighting in literal code blocks -- other names, for
349 example, variables.
350
351 rststyle-codeblock-number
352 Syntax highlighting in literal code blocks -- literal numbers,
353 including integers, floats, hex numbers, and octal numbers.
354
355 rststyle-codeblock-operator
356 Syntax highlighting in literal code blocks -- Python operators.
357
358 rststyle-codeblock-string
359 Syntax highlighting in literal code blocks -- literal strings.
360
361
362 List styles
363 ~~~~~~~~~~~
364
365 rststyle-bulletlist
366 Bullet lists (but not in the table of contents)
367
368 rststyle-blockquote-bulletlist
369 Bullet lists in block quotes.
370
371 rststyle-blockquote-enumlist
372 Enumerated lists in block quotes.
373
374 rststyle-enumlist-arabic
375 Enumerated lists, arabic (but not in the table of contents)
376
377 rststyle-enumlist-loweralpha
378 Enumerated lists, lower alpha (but not in the table of contents)
379
380 rststyle-enumlist-lowerroman
381 Enumerated lists, lower roman (but not in the table of contents)
382
383 rststyle-enumlist-upperalpha
384 Enumerated lists, upper alpha (but not in the table of contents)
385
386 rststyle-enumlist-upperroman
387 Enumerated lists, upper roman (but not in the table of contents)
388
389 rststyle-epigraph-bulletlist
390 Bullet lists in epigraphs. See the ``.. epigraph::``
391 directive.
392
393 rststyle-epigraph-enumlist
394 Enumerated lists in epigraphs. See the ``.. epigraph::``
395 directive.
396
397 rststyle-highlights-bulletlist
398 Bullet lists in highlights blocks. See the ``.. highlights::``
399 directive.
400
401 rststyle-highlights-enumlist
402 Enumerated lists in highlights blocks. See the ``.. highlights::``
403 directive.
404
405 rststyle-tocbulletlist
406 Lists in the table of contents when section numbering is off.
407
408 rststyle-tocenumlist
409 Lists in the table of contents when section numbering is on.
410
411
412 Admonition styles
413 ~~~~~~~~~~~~~~~~~
414
415 rststyle-admon-attention-hdr
416 The style for the attention admonition header/title.
417
418 rststyle-admon-attention-body
419 The style for the attention admonition body/paragraph.
420
421 rststyle-admon-caution-hdr
422 The style for the caution admonition header/title.
423
424 rststyle-admon-caution-body
425 The style for the caution admonition body/paragraph.
426
427 rststyle-admon-danger-hdr
428 The style for the admonition header/title.
429
430 rststyle-admon-danger-body
431 The style for the danger admonition body/paragraph.
432
433 rststyle-admon-error-hdr
434 The style for the error admonition header/title.
435
436 rststyle-admon-error-body
437 The style for the error admonition body/paragraph.
438
439 rststyle-admon-hint-hdr
440 The style for the hint admonition header/title.
441
442 rststyle-admon-hint-body
443 The style for the hint admonition body/paragraph.
444
445 rststyle-admon-hint-hdr
446 The style for the hint admonition header/title.
447
448 rststyle-admon-hint-body
449 The style for the hint admonition body/paragraph.
450
451 rststyle-admon-important-hdr
452 The style for the important admonition header/title.
453
454 rststyle-admon-important-body
455 The style for the important admonition body/paragraph.
456
457 rststyle-admon-note-hdr
458 The style for the note admonition header/title.
459
460 rststyle-admon-note-hdr
461 The style for the note admonition header/title.
462
463 rststyle-admon-tip-body
464 The style for the tip admonition body/paragraph.
465
466 rststyle-admon-tip-hdr
467 The style for the tip admonition header/title.
468
469 rststyle-admon-warning-body
470 The style for the warning admonition body/paragraph.
471
472 rststyle-admon-warning-hdr
473 The style for the warning admonition header/title.
474
475 rststyle-admon-generic-body
476 The style for the generic admonition body/paragraph.
477
478 rststyle-admon-generic-hdr
479 The style for the generic admonition header/title.
480
481
482 Rubric style
483 ~~~~~~~~~~~~
484
485 rststyle-rubric
486 The style for the text in a rubric directive.
487
488 The rubric directive recognizes a "class" option. If entered,
489 odtwriter uses the value of that option instead of the
490 ``rststyle-rubric`` style. Here is an example which which attaches
491 the ``rststyle-heading1`` style to the generated rubric::
492
493 .. rubric:: This is my first rubric
494 :class: rststyle-heading1
495
496
497 Table styles
498 ~~~~~~~~~~~~
499
500 A table style is generated by ``oowriter`` for each table that you
501 create. Therefore, ``odtwriter`` attempts to do something similar.
502 These styles are created in the ``content.xml`` document in the
503 generated ``.odt`` file. These styles have names prefixed with
504 "rststyle-table-".
505
506 There are two ways in which you can control the styles of your
507 tables: one simple, the other a bit more complex, but more
508 powerful.
509
510 First, you can change the thickness of the borders of all tables
511 generated in a document using the "--table-border-thickness"
512 command line option.
513
514 Second, you can control additional table properties and you can
515 apply different styles to different tables within the same document
516 by customizing and using tables in your stylesheet: ``styles.odt``
517 or whatever you name your copy of it using the --stylesheet command
518 line option. Then, follow these rules to apply a table style to
519 the tables in your document:
520
521 - The default table style -- Optionally, alter and customize the
522 style applied by default to tables in your document by modifying
523 table "rststyle-table-0" in your stylesheet (``styles.odt`` or a
524 copy). Caution: Do not change the name of this table.
525
526 - User-created table styles -- Add one or more new table styles to
527 be applied selectively to tables in your document by doing the
528 following:
529
530 1. Using ``oowriter``, add a table to your stylesheet and give it
531 a name that starts with the prefix "rststyle-table-", for
532 example "rststyle-table-vegetabledata". Customize the table's
533 border thickness, border color, and table background color.
534
535 2. In your reStructuredText document, apply your new table style
536 to a specific table by placing the ".. class::" directive
537 immediately before the table, for example::
538
539 .. class:: rststyle-table-vegetabledata
540
541 The default table style will be applied to all tables for which you
542 do not specify a style with the ".. class::" directive.
543
544 Customize the table properties in ``oowriter`` using the table
545 properties dialog for the table (style) that you wish to customize.
546
547 Note that "--table-border-thickness" command line option overrides
548 the border thickness specified in the stylesheet.
549
550 The specific properties that you can control with this second
551 method are the following:
552
553 - Border thickness and border color.
554
555 - Background color -- When you change the background color of a
556 table to be used as a style (in ``styles.odt`` or whatever you
557 name it), make sure you change the background color for the
558 *table* and *not* for a cell in the table. ``odtwriter`` picks
559 the background color from the table, not from a cell within the
560 table.
561
562
563 Line block styles
564 ~~~~~~~~~~~~~~~~~~
565
566 The line block styles wrap the various nested levels of line
567 blocks. There is one line block style for each indent level.
568
569 rststyle-lineblock1
570 Line block style for line block with no indent.
571
572 rststyle-lineblock2
573 Line block style for line block indented 1 level.
574
575 rststyle-lineblock3
576 Line block style for line block indented 2 levels.
577
578 rststyle-lineblock4
579 Line block style for line block indented 3 levels.
580
581 rststyle-lineblock5
582 Line block style for line block indented 4 levels.
583
584 rststyle-lineblock6
585 Line block style for line block indented 5 levels.
586
587 Notes:
588
589 - ``odtwriter`` does not check for a maximum level of indents
590 within line blocks. Therefore, you can define additional line
591 block styles for additional levels if you need them. Define
592 these styles with the names ``rststyle-lineblock7``,
593 ``rststyle-lineblock8``, ...
594
595 - Since the line block style is used to create indentation, a line
596 block that is inside a block quote will use
597 ``rststyle-lineblock2`` as its first level of indentation.
598
599
600 Footnote and citation styles
601 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
602
603 rststyle-footnote
604 The style for footnotes. This style affects the footnote
605 content, *not* the footnote reference in the body of the document.
606
607 rststyle-citation
608 The style for citations. This style affects the citation
609 content, *not* the citation reference in the body of the document.
610 You might need to adjust the indentation in this style
611 depending on the length of the label used in your citations.
612
613
614 Heading and title styles
615 ~~~~~~~~~~~~~~~~~~~~~~~~~
616
617 rststyle-heading{1|2|3|4|5}
618 The styles for headings (section titles and sub-titles). Five
619 levels of sub-headings are provided: rststyle-heading1 through
620 rststyle-heading5.
621
622 rststyle-title
623 The style for the document title.
624
625 rststyle-subtitle
626 The style for the document sub-title.
627
628
629 Image and figure styles
630 ~~~~~~~~~~~~~~~~~~~~~~~~~
631
632 rststyle-image
633 The style applied to an image, either an image by itself or an
634 image in a figure.
635
636 rststyle-figureframe
637 The style applied to a figure (actually to the frame that
638 surrounds a figure).
639
640
641
642 Defining and using a custom stylesheet
643 ---------------------------------------
644
645 You can create your own custom stylesheet. Here is how:
646
647 1. Make a copy of ``styles.odt``, which is in the distribution.
648
649 2. Open your copy of ``styles.odt`` in ``oowriter``. Modify styles
650 in that document. Then, save it.
651
652 3. When you run ``rst2odt``, use the ``--stylesheet`` command
653 line option to use your custom stylesheet. Run ``rst2odt
654 --help`` to learn more about these options.
655
656
657 Why custom stylesheets
658 ~~~~~~~~~~~~~~~~~~~~~~~
659
660 Here are a few reasons and ideas:
661
662 - The `page size`_ is stored in the style sheet. The default page
663 size is ``US Letter``. You can change the page size (for example,
664 to ``A4``) in your custom stylesheet by opening it in
665 ``oowriter``, then clicking on menu: ``Format/Page...``, then
666 clicking on the ``Page`` tab.
667
668
669 Defining and using custom style names
670 -------------------------------------
671
672 [Credits: Stefan Merten designed and implemented the custom style names
673 capability. Thank you, Stefan.]
674
675 You can also instruct ``odtwriter`` to use style names of your own
676 choice.
677
678
679 Why custom style names
680 ~~~~~~~~~~~~~~~~~~~~~~
681
682 Here are a few reasons and ideas:
683
684 - Suppose that your organization has a standard set of styles in
685 OOo ``oowriter`` and suppose that the use of these styles is
686 required. You would like to generate ODF documents from
687 reST text files, and you want the generated documents to contain
688 these styles.
689
690 - Suppose that your company or organization has a policy of using a
691 certain MS Word template for some set of documents. You would
692 like to generate ODF documents that use these custom style names,
693 so that you can export these documents from ODF ``oowriter`` to MS
694 Word documents that use these style names.
695
696 - Suppose that your documents are written in a language other than
697 English. You would like the style names visible in the "Styles
698 and Formatting" window in OOo ``oowriter`` (menu item
699 ``Format/Styles and Formatting``) to be understandable in the
700 language of your users.
701
702 - ``odtwriter`` maps single asterisks/stars (for example, \*stuff\*)
703 to emphasis and double stars to strong. You'd like to reverse
704 these. Or, you would like to generate headings level 3 and 4
705 where headings level 1 and 2 would normally be produced.
706
707
708 How to use custom style names
709 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
710
711 In order to define custom style names and to generate documents that
712 contain them, do the following:
713
714
715 1. Create a configuration file containing a "Formats" section. The
716 configuration file obeys the file format supported by the Python
717 ConfigParser module:
718 `ConfigParser -- Configuration file parser --
719 https://docs.python.org/3/library/configparser.html
720 <https://docs.python.org/3/library/configparser.html>`_.
721
722 2. In the "Formats" section of the configuration file, create one
723 option (a name-value pair) for each custom style name that you
724 wish to define. The option name is the standard ``odtwriter``
725 style name (without "rststyle-"), and the value is your custom
726 style name. Here is an example::
727
728 [Formats]
729 textbody: mytextbody
730 bulletitem: mybulletitem
731 heading1: myheading1
732 o
733 o
734 o
735
736 3. Create a styles document that defines the styles generated by
737 ``odtwriter``. You can create and edit the styles in OOo
738 ``oowriter``. It may be helpful to begin by making a copy of the
739 styles document that is part of the ``odtwriter`` distribution
740 (``styles.odt``).
741
742 4. When you run ``odtwriter``, specify the ``--odf-config-file``
743 option. You might also want to specify your styles document
744 using the ``--stylesheet`` option in order to include your
745 custom style definitions. For example::
746
747 rst2odt --odf-config-file=mymappingfile.ini \
748 --stylesheet=mystyles.odt mydoc.rst mydoc.odt
749
750
751 Classes
752 -------
753
754 ``odtwriter`` uses the following Docutils class to provide additional
755 control of the generation of ODF content:
756
757 - Class ``wrap`` -- Use this to cause the wrapping of text around
758 an image. The default is *not* to wrap text around images.
759 Here is an example::
760
761 .. class:: wrap
762 .. image:: images/flower01.png
763 :alt: A bright yellow flower
764 :height: 55
765 :width: 60
766
767
768 Roles
769 -------
770
771 You can use a Docutils custom interpreted text role to attach a
772 character style to an inline area of text. This capability also
773 enables you to attach a new character style (with a new name) that
774 you define yourself. Do this by defining your role in a stylesheet
775 as a character style with "rststyle-" prefixed to your role name,
776 then use the ``role`` directive and inline markup to apply your
777 role.
778
779 In order to use this capability, do the following:
780
781 - Define the character style for your custom role in a stylesheet
782 (a copy of ``styles.odt``) with the prefix "rststyle-".
783 Remember: (1) If the name of your custom role is "pretty", then
784 define a character style named "rststyle-pretty". (2) Define the
785 style as a *character* style, and *not*, for example as a
786 paragraph style.
787
788 - Declare your role in the source reStructuredText document in a
789 ``role`` directive. Example::
790
791 .. role:: pretty
792
793 - Use inline markup to apply your role to text. Example::
794
795 We have :pretty:`very nice` apples.
796
797 Here is another example::
798
799 .. role:: fancy
800
801 Here is some :fancy:`pretty text` that looks fancy.
802
803 For more on roles see:
804 `Custom Interpreted Text Roles --
805 https://docutils.sourceforge.io/docs/ref/rst/directives.html#custom-interpreted-text-roles
806 <https://docutils.sourceforge.io/docs/ref/rst/directives.html#custom-interpreted-text-roles>`_.
807
808 **Note:** The ability to base a role on another existing role is
809 *not* supported by ``odtwriter``.
810
811
812 Hints and Suggestions and Features
813 ==================================
814
815 Table of contents
816 -----------------
817
818 The ``..contents::`` directive causes ``odtwriter`` to generate
819 either:
820
821 1. A static, outline style table of contents, if the
822 ``--generate-list-toc`` command line option is specified, or
823
824 2. An ODF/``oowriter`` style table of contents containing
825 dynamically updated page numbers and with the formatting control
826 that ``oowriter`` gives you. This is the default, or use the
827 command line option ``--generate-list-toc``. **Note:**
828 ``odtwriter`` is not able to determine page numbers, so you will
829 need to open the generated document in ``oowriter``, then
830 right-click on the table of contents and select "Update" to
831 insert correct page numbers.
832
833
834 Syntax highlighting
835 -------------------
836
837 ``odtwriter`` can add syntax highlighting to code in code
838 blocks. In order to activate this, do all of the following:
839
840 1. Install `Pygments`_ and ...
841
842 2. Use the command line option ``--add-syntax-highlighting``.
843 Example::
844
845 $ rst2odt --add-syntax-highlight test.rst > test.odt
846
847 The following styles are defined in styles.odt and are used for
848 literal code blocks and syntax highlighting:
849
850 - Paragraph styles:
851
852 - rststyle-codeblock -- The style for the code block as a whole.
853
854 - Character styles:
855
856 - rststyle-codeblock-classname -- class names.
857
858 - rststyle-codeblock-comment -- comments.
859
860 - rststyle-codeblock-functionname -- function names.
861
862 - rststyle-codeblock-keyword -- Python language keywords.
863
864 - rststyle-codeblock-name -- other names, for example,
865 variables.
866
867 - rststyle-codeblock-number -- literal numbers, including
868 integers, floats, hex numbers, and octal numbers.
869
870 - rststyle-codeblock-operator -- Python operators.
871
872 - rststyle-codeblock-string -- literal strings.
873
874 Each of the above styles has a default appearance that is defined
875 in ``styles.odt``. To change that definition and appearance, open
876 ``styles.odt`` in ``oowriter`` and use menu item::
877
878 Format --> Styles and Formatting
879
880 Then, click on the Paragraph Styles button or the Character Styles
881 button at the top of the Styles and Formatting window. You may
882 also need to select "All Styles" from the drop-down selection list
883 at the bottom of the Styles and Formatting window.
884
885
886
887 The container directive
888 -----------------------
889
890 There is limited support for the ``container`` directive. The
891 limitations and rules for the container directive are the following:
892
893 - Only the first class in the list of classes (arguments) is used.
894
895 - That class/style must be a paragraph style and not (for example) a
896 character style.
897
898 - The style/class given to the container directive will have a
899 "rststyle-" prefix in the odt file.
900
901 So, for example::
902
903 .. container:: style-1 style-2 style-3
904
905 a block of text
906
907 - Only ``style-1`` is used; ``style-2`` and ``style-3`` are ignored.
908
909 - ``rststyle-style-1`` must be defined. It should be an existing,
910 predefined style, or you should define it in your stylesheet
911 (``styles.odt`` or the argument to the ``--stylesheet`` command
912 line option).
913
914 - ``rststyle-style-1`` must be a paragraph style.
915
916 To define a paragraph style, use the following menu item in
917 ``oowriter``::
918
919 Format --> Styles and Formatting
920
921 Then, click on the Paragraph Styles button.
922
923 The following example attaches the ``rststyle-heading2`` style (a
924 predefined style) to each paragraph/line in the container::
925
926 .. container:: heading2
927
928 Line 1 of container.
929
930 Line 2 of container.
931
932 More information on how to define a new style (for example, in your
933 ``styles.odt``) can be found in section
934 `Defining and using custom style names`_.
935
936
937
938 The table directive
939 -------------------
940
941 The ``table`` directive can be used to add a title to a table.
942 Example::
943
944 .. table:: A little test table
945
946 =========== =============
947 Name Value
948 =========== =============
949 Dave Cute
950 Mona Smart
951 =========== =============
952
953 The above will insert the title "A little test table" at the top of the
954 table. You can modify the appearance of the title by modifying the
955 paragraph style ``rststyle-table-title``.
956
957
958 Footnotes and citations
959 -----------------------
960
961 Footnotes and citations are supported.
962
963 There are additional styles ``rststyle-footnote`` and
964 ``rststyle-citation`` for footnotes and citations. See
965 `Footnote and citation styles`_.
966
967 You may need to modify the citation style to fit the length of your
968 citation references.
969
970 Endnotes -- There are command line options that control whether
971 ``odtwriter`` creates endnotes instead of footnotes. Endnotes
972 appear at the end of the document instead of at the bottom of the
973 page. See flags ``--endnotes-end-doc`` and
974 ``--no-endnotes-end-doc`` in section `Command line options`_.
975
976
977 Images and figures
978 ------------------
979
980 The ODT Writer only supports fixed `length units`_ ("cm", "mm", "in",
981 "pc", "pt", "px) for the size attributes "width", and "height".
982 The fallback unit (used for attribute values without unit) is "px".
983
984 If on the image or the figure directive you do not provide the width
985 and height options, then ``odtwriter`` will attempt to determine the
986 size of the image using the Python Imaging Library (PIL/Pillow_).
987 If ``odtwriter`` cannot find and import the
988 Python Imaging Library, it will raise an exception. If this
989 ocurrs, you can fix it by doing one of the following:
990
991 - Install the Python Imaging Library or
992
993 - Add both the ``width`` and the ``height`` options.
994
995 For more information about PIL, see: `Python Imaging Library`_.
996
997
998 The raw directive
999 -----------------
1000
1001 The ``raw`` directive is supported. Use output format type "odt".
1002
1003 You will need to be careful about the formatting of the raw
1004 content. In particular, introduced whitespace might be a problem.
1005
1006 In order to produce content for the raw directive for use by
1007 ``odtwriter``, you might want to extract the file ``content.xml``
1008 from a ``.odt`` file (using some Zip tool), and then clip, paste,
1009 and modify a selected bit of it.
1010
1011 Here is an example::
1012
1013 .. raw:: odt
1014
1015 <text:p text:style-name="rststyle-textbody">Determining
1016 <text:span text:style-name="rststyle-emphasis">which</text:span>
1017 namespace a name is in is static. It can be determined by a
1018 lexical scan of the code. If a variable is assigned a value
1019 <text:span text:style-name="rststyle-emphasis">anywhere</text:span>
1020 in a scope (specifically within a function or method body),
1021 then that variable is local to that scope. If Python does
1022 not find a variable in the local scope, then it looks next
1023 in the global scope (also sometimes called the module scope)
1024 and then in the built-ins scope. But, the
1025 <text:span text:style-name="rststyle-inlineliteral">global</text:span>
1026 statement can be used to force Python to find and use a global
1027 variable (a variable defined at top level in a module) rather
1028 than create a local one.</text:p>
1029
1030
1031 The meta directive
1032 ------------------
1033
1034 ``odtwriter`` supports the ``meta`` directive. "keywords"
1035 and "description" are set in their respective odt fields.
1036 Other meta fields are set as "Custom Properties".
1037 Here is an example::
1038
1039 .. meta::
1040 :keywords: reStructuredText, docutils, formatting
1041 :description lang=en: A reST document, contains formatted
1042 text in a formatted style.
1043 :custom_var: Value
1044
1045 To see the results of the ``meta`` directive in ``oowriter``,
1046 select menu item "File/Properties...", then click on the
1047 "Description" tab ("keywords" and "description" fields) and the
1048 "Custom Properties" tab.
1049
1050
1051 Footnote references inside footnotes
1052 ------------------------------------
1053
1054 Not supported.
1055
1056 Get a grip. Be serious. Try a dose of reality.
1057
1058 ``odtwriter`` ignores them.
1059
1060 They cause ``oowriter`` to croak.
1061
1062
1063 Page size
1064 ---------
1065
1066 The default page size, in documents generated by the ODT writer is
1067 "US Letter". You can change this (for example to ``A4``) by using a
1068 custom stylesheet. See `Defining and using a custom stylesheet`_
1069 for instructions on how to do this.
1070
1071 On machines which support ``paperconf``, the ODT writer can insert
1072 the default page size for your locale. In order for this to work,
1073 the following conditions must be met:
1074
1075 1. The program ``paperconf`` must be available on your system.
1076 ``odtwriter`` uses ``paperconf -s`` to obtain the paper size.
1077 See ``man paperconf`` for more information.
1078
1079 2. The page height and width settings must be absent from the stylesheet
1080 used to generate the document.
1081
1082 You can use the Python script ``prepstyles.py`` distributed with
1083 Docutils to remove the page height and width settings from a
1084 stylesheet file ``STYLES.odt`` with ::
1085
1086 $ python3 -m docutils.writers.odf_odt.prepstyles STYLES.odt
1087
1088 (the command changed in Docutils 0.20.1).
1089
1090 .. warning:: If you edit your stylesheet in ``oowriter`` and then
1091 save it, ``oowriter`` automatically inserts a page height and
1092 width in the styles for that (stylesheet) document. If that is
1093 not the page size that you want and you want ``odtwriter`` to
1094 insert a default page size using ``paperconf``, then you will
1095 need to strip the page size from your stylesheet each time you
1096 edit that stylesheet with ``oowriter``.
1097
1098
1099
1100 Custom header/footers: inserting page numbers, date, time, etc
1101 ----------------------------------------------------------------
1102
1103 You can specify custom headers and footers for your document from
1104 the command line. These headers and footers can be used to insert
1105 fields such as the page number, page count, date, time, etc. See
1106 below for a complete list.
1107
1108 To insert a custom header or footer, use the "--custom-odt-header"
1109 or "--custom-odt-footer" command line options. For example, the
1110 following inserts a footer containing the page number and page
1111 count::
1112
1113 $ rst2odt --custom-odt-footer="Page %p% of %P%" f1.rst > f1.odt
1114
1115
1116 Field specifiers
1117 ~~~~~~~~~~~~~~~~~~
1118
1119 You can use the following field specifiers to insert ``oowriter``
1120 fields in your custom headers and footers:
1121
1122 %p%
1123 The current page number.
1124
1125 %P%
1126 The number of pages in the document.
1127
1128 %d1%
1129 The current date in format 12/31/99.
1130
1131 %d2%
1132 The current date in format 12/31/1999.
1133
1134 %d3%
1135 The current date in format Dec 31, 1999.
1136
1137 %d4%
1138 The current date in format December 31, 1999.
1139
1140 %d5%
1141 The current date in format 1999-12-31.
1142
1143 %t1%
1144 The current time in format 14:22.
1145
1146 %t2%
1147 The current time in format 14:22:33.
1148
1149 %t3%
1150 The current time in format 02:22 PM.
1151
1152 %t4%
1153 The current time in format 02:22:33 PM.
1154
1155 %a%
1156 The author of the document (actually the initial creator).
1157
1158 %t%
1159 The document title.
1160
1161 %s%
1162 The document subject.
1163
1164
1165 **Note:** The use of the above field specifiers in the body of your
1166 reStructuredText document is **not** supported, because these
1167 specifiers are not standard across Docutils writers.
1168
1169
1170
1171 Credits
1172 =======
1173
1174 Stefan Merten designed and implemented the custom style names
1175 capability. Thank you, Stefan.
1176
1177 Michael Schutte supports the Debian GNU/Linux distribution of
1178 ``odtwriter``. Thank you, Michael, for providing and supporting
1179 the Debian package.
1180
1181 Michael Schutte implemented the fix that enables ``odtwriter`` to
1182 pick up the default paper size on platforms where the program
1183 ``paperconf`` is available. Thank you.
1184
1185
1186
1187 .. _rst2odt:
1188 tools.html#rst2odt
1189 .. _reStructuredText:
1190 ../ref/rst/restructuredtext.html
1191 .. _length units:
1192 ../ref/rst/restructuredtext.html#length-units
1193 .. _`OpenDocument Text`:
1194 https://en.wikipedia.org/wiki/OpenDocument
1195 .. _LibreOffice:
1196 https://libreoffice.org/
1197 .. _`Pygments`:
1198 https://pygments.org/
1199 .. _`Python Imaging Library`:
1200 https://en.wikipedia.org/wiki/Python_Imaging_Library
1201 .. _`Pillow`: https://pypi.org/project/pillow/