Author: | Dave Kuhlman |
---|---|
Contact: | docutils-develop@lists.sourceforge.net |
Revision: | 7528 |
Date: | 2012-10-13 |
Copyright: | This document has been placed in the public domain. |
Abstract
This document describes the Docutils odtwriter (rst2odt.py).
Contents
What it does -- rst2odt.py translates reST (reStructuredText) into a Open Document Format .odt file. You can learn more about the ODF format here:
You should be able to open documents (.odt files) generated with rst2odt.py in OpenOffice/oowriter.
You can learn more about Docutils and reST here: Docutils
In addition to the Docutils standard requirements, odtwriter requires:
Run it from the command line as follows:
$ rst2odt.py myinput.txt myoutput.odt
To see usage information and to learn about command line options that you can use, run the following:
$ rst2odt.py --help
Examples:
$ rst2odt.py -s -g python_comments.txt python_comments.odt $ rst2odt.py --source-url=odtwriter.txt --generator --stylesheet=/myconfigs/styles.odt odtwriter.txt odtwriter.odt
The options described below can also be set in a configuration file. Use section [odf_odt writer] to set options specific to the odtwriter. For example:
[odf_odt writer] stylesheet: styles1.odt
See the "Docutils Configuration" document for more information on Docutils configuration files, including locations which are searched.
The following command line options are specific to odtwriter:
--stylesheet=<URL> | |
Specify a stylesheet URL, used verbatim. Default: writers/odf_odt/styles.odt in the installation directory. | |
--odf-config-file=<file> | |
Specify a configuration/mapping file relative to the current working directory for additional ODF options. In particular, this file may contain a section named "Formats" that maps default style names to names to be used in the resulting output file allowing for adhering to external standards. For more info and the format of the configuration/mapping file, see the odtwriter doc. | |
--cloak-email-addresses | |
Obfuscate email addresses to confuse harvesters while still keeping email links usable with standards- compliant browsers. | |
--no-cloak-email-addresses | |
Do not obfuscate email addresses. | |
--table-border-thickness=TABLE_BORDER_THICKNESS | |
Specify the thickness of table borders in thousands of a cm. Default is 35. | |
--add-syntax-highlighting | |
Add syntax highlighting in literal code blocks. | |
--no-syntax-highlighting | |
Do not add syntax highlighting in literal code blocks. (default) | |
--create-sections | |
Create sections for headers. (default) | |
--no-sections | Do not create sections for headers. |
--create-links | Create links. |
--no-links | Do not create links. (default) |
--endnotes-end-doc | |
Generate endnotes at end of document, not footnotes at bottom of page. | |
--no-endnotes-end-doc | |
Generate footnotes at bottom of page, not endnotes at end of document. (default) | |
--generate-list-toc | |
Generate a bullet list table of contents, not an ODF/oowriter table of contents. | |
--generate-oowriter-toc | |
Generate an ODF/oowriter table of contents, not a bullet list. (default) Note: odtwriter is not able to determine page numbers, so you will need to open the generated document in oowriter, then right-click on the table of contents and select "Update" to insert page numbers. | |
--custom-odt-header=CUSTOM_HEADER | |
Specify the contents of an custom header line. See odf_odt writer documentation for details about special field character sequences. See section Custom header/footers: inserting page numbers, date, time, etc for details | |
--custom-odt-footer=CUSTOM_FOOTER | |
Specify the contents of an custom footer line. See odf_odt writer documentation for details about special field character sequences. See section Custom header/footers: inserting page numbers, date, time, etc for details |
odtwriter uses a number of styles that are defined in styles.xml in the default styles.odt. This section describes those styles.
Note that with the --stylesheet command line option, you can use either styles.odt or styles.xml, as described below. Use of styles.odt is recommended over styles.xml.
You can modify the look of documents generated by odtwriter in several ways:
Open (a copy of) styles.odt in OpenOffice/oowriter and modify the style you wish to change. Now, save this document, then generate your documents using this modified copy of styles.odt.
In my version of oowriter, to modify styles, either (1) press F11 or (2) use menu item "Format/Styles and Formatting", then right-click on the relevant style and select "Modify". Modify the style, then save your document.
Open a document generated by odtwriter in oowriter`. Now, edit the style you are interested in modifying. Now, you can extract the styles.xml file from your document and either (1) use this as your default styles file or (2) copy and paste the relevant style definition into your styles.xml.
Extract styles.xml from styles.odt using your favorite zip/unzip tool. Then modify styles.xml with a text editor. Now re-zip it back into your own styles.odt, or use it directly by specifying it with the --stylesheet command line option. Hint: If you intend to extract styles.xml from an .odt file (and then "re-zip" it), you should turn off XML optimization/compression in oowriter. In order to this in oowriter, use Tools --> Options... --> Load-Save --> General and turn off "Size optimization for XML format".
Open an empty (or new) document in oowriter. Define all of the styles described in this section. Then, use that document (a .odt file) as your stylesheet. odtwriter will extract the styles.xml file from that document and insert it into the output document.
Some combination of the above.
This section describes the styles used by odtwriter.
Note that we do not describe the "look" of these styles. That can be easily changed by using oowriter to edit the document styles.odt (or a copy of it), and modifying any of the styles described here.
To change the definition and appearance of these styles, open styles.odt in oowriter and open the Styles and Formatting window by using the following menu item:
Format --> Styles and Formatting
Then, click on the Paragraph Styles button or the Character Styles button at the top of the Styles and Formatting window. You may also need to select "All Styles" from the drop-down selection list at the bottom of the Styles and Formatting window in order to see the styles used by odtwriter.
Notice that you can make a copy of file styles.odt, modify it using oowriter, and then use your copy with the --stylesheet=<file> command line option. Example:
$ rst2odt.py --stylesheet=mystyles.odt test2.txt test2.odt
The rubric directive recognizes a "class" option. If entered, odtwriter uses the value of that option instead of the rststyle-rubric style. Here is an example which which attaches the rststyle-heading1 style to the generated rubric:
.. rubric:: This is my first rubric :class: rststyle-heading1
A table style is generated by oowriter for each table that you create. Therefore, odtwriter attempts to do something similar. These styles are created in the content.xml document in the generated .odt file. These styles have names prefixed with "rststyle-table-".
There are two ways in which you can control the styles of your tables: one simple, the other a bit more complex, but more powerful.
First, you can change the thickness of the borders of all tables generated in a document using the "--table-border-thickness" command line option.
Second, you can control additional table properties and you can apply different styles to different tables within the same document by customizing and using tables in your stylesheet: styles.odt or whatever you name your copy of it using the --stylesheet command line option. Then, follow these rules to apply a table style to the tables in your document:
The default table style -- Optionally, alter and customize the style applied by default to tables in your document by modifying table "rststyle-table-0" in your stylesheet (styles.odt or a copy). Caution: Do not change the name of this table.
User-created table styles -- Add one or more new table styles to be applied selectively to tables in your document by doing the following:
Using oowriter, add a table to your stylesheet and give it a name that starts with the prefix "rststyle-table-", for example "rststyle-table-vegetabledata". Customize the table's border thickness, border color, and table background color.
In your reStructuredText document, apply your new table style to a specific table by placing the ".. class::" directive immediately before the table, for example:
.. class:: rststyle-table-vegetabledata
The default table style will be applied to all tables for which you do not specify a style with the ".. class::" directive.
Customize the table properties in oowriter using the table properties dialog for the table (style) that you wish to customize.
Note that "--table-border-thickness" command line option overrides the border thickness specified in the stylesheet.
The specific properties that you can control with this second method are the following:
The line block styles wrap the various nested levels of line blocks. There is one line block style for each indent level.
Notes:
You can create your own custom stylesheet. Here is how:
Here are a few reasons and ideas:
[Credits: Stefan Merten designed and implemented the custom style names capability. Thank you, Stefan.]
You can also instruct odtwriter to use style names of your own choice.
Here are a few reasons and ideas:
In order to define custom style names and to generate documents that contain them, do the following:
Create a configuration file containing a "Formats" section. The configuration file obeys the file format supported by the Python ConfigParser module: ConfigParser -- Configuration file parser -- http://docs.python.org/lib/module-ConfigParser.html.
In the "Formats" section of the configuration file, create one option (a name-value pair) for each custom style name that you wish to define. The option name is the standard odtwriter style name (without "rststyle-"), and the value is your custom style name. Here is an example:
[Formats] textbody: mytextbody bulletitem: mybulletitem heading1: myheading1 o o o
Create a styles document that defines the styles generated by odtwriter. You can create and edit the styles in OOo oowriter. It may be helpful to begin by making a copy of the styles document that is part of the odtwriter distribution (styles.odt).
When you run odtwriter, specify the --odf-config-file option. You might also want to specify your styles document using the --stylesheet option in order to include your custom style definitions. For example:
rst2odt.py --odf-config-file=mymappingfile.ini --stylesheet=mystyles.odt mydoc.txt mydoc.odt
odtwriter uses the following Docutils class to provide additional control of the generation of ODF content:
Class wrap -- Use this to cause the wrapping of text around an image. The default is not to wrap text around images. Here is an example:
.. class:: wrap .. image:: images/flower01.png :alt: A bright yellow flower :height: 55 :width: 60
You can use a Docutils custom interpreted text role to attach a character style to an inline area of text. This capability also enables you to attach a new character style (with a new name) that you define yourself. Do this by defining your role in a stylesheet as a character style with "rststyle-" prefixed to your role name, then use the role directive and inline markup to apply your role.
In order to use this capability, do the following:
Define the character style for your custom role in a stylesheet (a copy of styles.odt) with the prefix "rststyle-". Remember: (1) If the name of your custom role is "pretty", then define a character style named "rststyle-pretty". (2) Define the style as a character style, and not, for example as a paragraph style.
Declare your role in the source reStructuredText document in a role directive. Example:
.. role:: pretty
Use inline markup to apply your role to text. Example:
We have :pretty:`very nice` apples.
Here is another example:
.. role:: fancy Here is some :fancy:`pretty text` that looks fancy.
For more on roles see: Custom Interpreted Text Roles -- http://docutils.sourceforge.net/docs/ref/rst/directives.html#custom-interpreted-text-roles.
Note: The ability to base a role on another existing role is not supported by odtwriter.
The ..contents:: directive causes odtwriter to generate either:
odtwriter can add syntax highlighting to code in code blocks. In order to activate this, do all of the following:
Install Pygments and ...
Use the command line option --add-syntax-highlighting. Example:
$ rst2odt.py --add-syntax-highlight test.txt test.odt
The following styles are defined in styles.odt and are used for literal code blocks and syntax highlighting:
Each of the above styles has a default appearance that is defined in styles.odt. To change that definition and appearance, open styles.odt in oowriter and use menu item:
Format --> Styles and Formatting
Then, click on the Paragraph Styles button or the Character Styles button at the top of the Styles and Formatting window. You may also need to select "All Styles" from the drop-down selection list at the bottom of the Styles and Formatting window.
There is limited support for the container directive. The limitations and rules for the container directive are the following:
So, for example:
.. container:: style-1 style-2 style-3 a block of text
To define a paragraph style, use the following menu item in oowriter:
Format --> Styles and Formatting
Then, click on the Paragraph Styles button.
The following example attaches the rststyle-heading2 style (a predefined style) to each paragraph/line in the container:
.. container:: heading2 Line 1 of container. Line 2 of container.
More information on how to define a new style (for example, in your styles.odt) can be found in section Defining and using custom style names.
The table directive can be used to add a title to a table. Example:
.. table:: A little test table =========== ============= Name Value =========== ============= Dave Cute Mona Smart =========== =============
The above will insert the title "A little test table" at the top of the table. You can modify the appearance of the title by modifying the paragraph style rststyle-table-title.
Footnotes and citations are supported.
There are additional styles rststyle-footnote and rststyle-citation for footnotes and citations. See Footnote and citation styles.
You may need to modify the citation style to fit the length of your citation references.
Endnotes -- There are command line options that control whether odtwriter creates endnotes instead of footnotes. Endnotes appear at the end of the document instead of at the bottom of the page. See flags --endnotes-end-doc and --no-endnotes-end-doc in section Command line options.
If on the image or the figure directive you provide the scale option but do not provide the width and height options, then odtwriter will attempt to determine the size of the image using the Python Imaging Library (PIL). If odtwriter cannot find and import Python Imaging Library, it will raise an exception. If this ocurrs, you can fix it by doing one of the following:
So, the rule is: if on any image or figure, you specify scale but not both width and height, you must install the Python Imaging Library library.
For more information about PIL, see: Python Imaging Library.
The raw directive is supported. Use output format type "odt".
You will need to be careful about the formatting of the raw content. In particular, introduced whitespace might be a problem.
In order to produce content for the raw directive for use by odtwriter, you might want to extract the file content.xml from a .odt file (using some Zip tool), and then clip, paste, and modify a selected bit of it.
Here is an example:
.. raw:: odt <text:p text:style-name="rststyle-textbody">Determining <text:span text:style-name="rststyle-emphasis">which</text:span> namespace a name is in is static. It can be determined by a lexical scan of the code. If a variable is assigned a value <text:span text:style-name="rststyle-emphasis">anywhere</text:span> in a scope (specifically within a function or method body), then that variable is local to that scope. If Python does not find a variable in the local scope, then it looks next in the global scope (also sometimes called the module scope) and then in the built-ins scope. But, the <text:span text:style-name="rststyle-inlineliteral">global</text:span> statement can be used to force Python to find and use a global variable (a variable defined at top level in a module) rather than create a local one.</text:p>
odtwriter supports the meta directive. Two fields are recognized: "keywords" and "description". Here is an example:
.. meta:: :keywords: reStructuredText, docutils, formatting :description lang=en: A reST document, contains formatted text in a formatted style.
To see the results of the meta directive in oowriter, select menu item "File/Properties...", then click on the "Description" tab.
Not supported.
Get a grip. Be serious. Try a dose of reality.
odtwriter ignores them.
They cause oowriter to croak.
The default page size, in documents generated by odtwriter is Letter. You can change this (for example to A4) by using a custom stylesheet. See Defining and using a custom stylesheet for instructions on how to do this.
On machines which support paperconf, odtwriter can insert the default page size for your locale. In order for this to work, the following conditions must be met:
The program paperconf must be available on your system. odtwriter uses paperconf -s to obtain the paper size. See man paperconf for more information.
The default page height and width must be removed from the styles.odt used to generate the document. A Python script rst2odt_prepstyles.py is distributed with odtwriter and is installed in the bin directory. You can remove the page height and width with something like the following:
$ rst2odt_prepstyles.py styles.odt
Warning
If you edit your stylesheet in oowriter and then save it, oowriter automatically inserts a page height and width in the styles for that (stylesheet) document. If that is not the page size that you want and you want odtwriter to insert a default page size using paperconf, then you will need to strip the page size from your stylesheet each time you edit that stylesheet with oowriter.
Stefan Merten designed and implemented the custom style names capability. Thank you, Stefan.
Michael Schutte supports the Debian GNU/Linux distribution of odtwriter. Thank you, Michael, for providing and supporting the Debian package.
Michael Schutte implemented the fix that enables odtwriter to pick up the default paper size on platforms where the program paperconf is available. Thank you.