Apply patch [ 119 ] by Anatoly Techtonik
[docutils.git] / sandbox / odf2docutils / odf2docutils.py
bloba6a6fdffee5cdd8c6308900111aa563afc0f7012
1 #! /usr/bin/env python
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
19 # 02111-1307, USA.
21 """
22 Convert an Open Document Format (ODF) file to docutils XML.
23 """
25 __docformat__ = 'reStructuredText' # Formatted to be rendered by epydoc
27 ###############################################################################
28 ###############################################################################
29 # Import
31 import sys
33 import docutils, docutils.core, docutils.frontend
34 import docutils.io, docutils.readers, docutils.writers.docutils_xml
35 import docutils_xml.io
37 ###############################################################################
38 ###############################################################################
39 # Constants
41 ###############################################################################
42 ###############################################################################
43 # Variables
45 ###############################################################################
46 ###############################################################################
47 # Functions
49 ###############################################################################
50 ###############################################################################
51 # Classes
53 class SettingsSpec(docutils.SettingsSpec):
54 """
55 Settings specifications for odf2docutils.
56 """
58 xsltOptions = ( '-x', '--xslt' )
60 settings_spec = (
61 "odf2docutils options", None,
62 (( "Use Python implementation for conversion instead of the XSLT implementation (default).",
63 ( '-p', '--python' ),
64 { 'validator': docutils.frontend.validate_boolean,
65 'action': 'store_true',
66 'default': True, }, ),
67 ( "Use XSLT for conversion instead of the Python implementation.",
68 xsltOptions,
69 { 'validator': docutils.frontend.validate_boolean,
70 'action': 'store_false',
71 'dest': 'python', },
72 ), ),
75 config_section = "odf2docutils"
77 ##############################################################################
79 class OdfFileInput(docutils_xml.io.ZipFileInput):
80 """
81 Input for ODF files.
82 """
84 contentPath = 'content.xml'
86 ##############################################################################
87 ##############################################################################
88 # Now work
90 if __name__ == '__main__':
91 # Check options to set correct parser and writer
92 usePython = True
94 # The following "General Docutils Options" are effectively ignored:
96 # Used in `transforms.frontmatter.DocTitle` used by
97 # `readers.standalone.Reader`:
98 # --title
100 # Used in `transforms.universal.Decorations` used by `readers.Reader`:
101 # --generator
102 # --no-generator
103 # --date
104 # --time
105 # --no-datestamp
106 # --source-link
107 # --source-url
108 # --no-source-link
110 # Used in `transforms.parts.Contents` used by `parsers.rst.directives...`:
111 # --toc-entry-backlinks
112 # --toc-top-backlinks
113 # --no-toc-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`:
120 # --strip-comments
121 # --leave-comments
123 # Used in `transforms.universal.StripClassesAndElements` used by
124 # `writers.Writer`:
125 # --strip-elements-with-class
126 # --strip-class
128 # Used in `writers.html4css1.HTMLTranslator`:
129 # --footnote-backlinks
130 # --no-footnote-backlinks
132 # Used in `core.Publisher`:
133 # --input-encoding
135 # Used in `parsers.rst.directives...`
136 # --input-encoding-error-handler
138 settingsSpec = SettingsSpec()
139 for arg in sys.argv:
140 if arg in settingsSpec.xsltOptions:
141 usePython = False
142 if usePython:
143 from odf2docutilslib.python import Parser
144 from docutils.writers.docutils_xml import Writer
145 else:
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")
155 pub.publish()
157 # TODO Use XSLT variant as export filter for LibreOffice - which works from
158 # scratch using https://help.libreoffice.org/Common/Creating_XML_Filters