2 # -*- coding: iso-8859-1 -*-
4 # Copyright (C) 2013 Stefan Merten
6 # odf2docutils.py is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published
8 # by the Free Software Foundation; either version 2 of the License,
9 # or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 Convert an Open Document Format (ODF) file to docutils XML.
25 __docformat__
= 'reStructuredText' # Formatted to be rendered by epydoc
27 ###############################################################################
28 ###############################################################################
33 import docutils
, docutils
.core
, docutils
.frontend
34 import docutils
.io
, docutils
.readers
, docutils
.writers
.docutils_xml
35 import docutils_xml
.io
37 ###############################################################################
38 ###############################################################################
41 ###############################################################################
42 ###############################################################################
45 ###############################################################################
46 ###############################################################################
49 ###############################################################################
50 ###############################################################################
53 class SettingsSpec(docutils
.SettingsSpec
):
55 Settings specifications for odf2docutils.
58 xsltOptions
= ( '-x', '--xslt' )
61 "odf2docutils options", None,
62 (( "Use Python implementation for conversion instead of the XSLT implementation (default).",
64 { 'validator': docutils
.frontend
.validate_boolean
,
65 'action': 'store_true',
66 'default': True, }, ),
67 ( "Use XSLT for conversion instead of the Python implementation.",
69 { 'validator': docutils
.frontend
.validate_boolean
,
70 'action': 'store_false',
75 config_section
= "odf2docutils"
77 ##############################################################################
79 class OdfFileInput(docutils_xml
.io
.ZipFileInput
):
84 contentPath
= 'content.xml'
86 ##############################################################################
87 ##############################################################################
90 if __name__
== '__main__':
91 # Check options to set correct parser and writer
94 # The following "General Docutils Options" are effectively ignored:
96 # Used in `transforms.frontmatter.DocTitle` used by
97 # `readers.standalone.Reader`:
100 # Used in `transforms.universal.Decorations` used by `readers.Reader`:
110 # Used in `transforms.parts.Contents` used by `parsers.rst.directives...`:
111 # --toc-entry-backlinks
112 # --toc-top-backlinks
115 # Used in `transforms.parts.SectNum` used by `parsers.rst.directives...`:
116 # --section-numbering
117 # --no-section-numbering
119 # Used in `transforms.universal.StripComments` used by `readers.Reader`:
123 # Used in `transforms.universal.StripClassesAndElements` used by
125 # --strip-elements-with-class
128 # Used in `writers.html4css1.HTMLTranslator`:
129 # --footnote-backlinks
130 # --no-footnote-backlinks
132 # Used in `core.Publisher`:
135 # Used in `parsers.rst.directives...`
136 # --input-encoding-error-handler
138 settingsSpec
= SettingsSpec()
140 if arg
in settingsSpec
.xsltOptions
:
143 from odf2docutilslib
.python
import Parser
144 from docutils
.writers
.docutils_xml
import Writer
146 from odf2docutilslib
.xslt
import Parser
, Writer
148 pub
= docutils
.core
.Publisher(docutils
.readers
.Reader(), Parser(), Writer(),
149 source_class
=OdfFileInput
,
150 destination_class
=docutils
.io
.BinaryFileOutput
)
151 pub
.process_command_line(settings_spec
=settingsSpec
,
152 description
="Reads ODF file <source> (default is stdin) and writes Docutils XML to <destination> (default is stdout).")
153 if pub
.settings
.python
!= usePython
:
154 raise AssertionError("Internal error: Assumed setting of --xslt/--python not confirmed by explicit parsing")
157 # TODO Use XSLT variant as export filter for LibreOffice - which works from
158 # scratch using https://help.libreoffice.org/Common/Creating_XML_Filters