skip 3.10 for now
[docutils.git] / sandbox / odf2docutils / odf2docutilslib / python.py
blob43ae6710f9a3c6401dcb3fb5af7844d8091a5e29
1 # Copyright (C) 2013 Stefan Merten
3 # This file is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published
5 # by the Free Software Foundation; either version 2 of the License,
6 # or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
16 # 02111-1307, USA.
18 """
19 Do conversion by using Python.
20 """
22 __docformat__ = 'reStructuredText' # Formatted to be rendered by epydoc
24 ###############################################################################
25 ###############################################################################
26 # Import
28 import docutils.nodes
30 from docutils_xml.parsers.xml import XmlParser, XmlVisitor, SomeChildren, Uri2Prefixes
32 ###############################################################################
33 ###############################################################################
34 # Constants
36 ###############################################################################
37 ###############################################################################
38 # Variables
40 ###############################################################################
41 ###############################################################################
42 # Functions
44 ###############################################################################
45 ###############################################################################
46 # Classes
48 class OdfVisitor(XmlVisitor):
49 """
50 Visitor class for visiting an ODF element tree.
51 """
53 ###########################################################################
54 # Presentation
56 def visit_draw_page(self, elem):
57 self.push(docutils.nodes.section())
59 def depart_draw_page(self, elem):
60 self.pop()
62 ###########################################################################
63 # Lists
65 def visit_text_list(self, elem):
66 self.push(docutils.nodes.bullet_list(bullet="*"))
67 raise SomeChildren(( ( "text", "list-item" ), ))
69 def depart_text_list(self, elem):
70 self.pop()
72 def visit_text_listitem(self, elem):
73 self.push(docutils.nodes.list_item())
74 raise SomeChildren(( ( "text", "list" ), ( "text", "p" ), ))
76 def depart_text_listitem(self, elem):
77 self.pop()
79 ###########################################################################
80 # Text
82 def visit_text_p(self, elem):
83 para = docutils.nodes.paragraph()
84 if elem.text is not None:
85 para.append(docutils.nodes.Text(elem.text))
86 self.push(para)
87 # TODO tails of embedded inline elements must be considered
89 def depart_text_p(self, elem):
90 self.pop()
92 # TODO Implement
94 ###############################################################################
96 class Parser(XmlParser):
97 """
98 Parse the input file and translate it to the output file by Python.
99 """
101 uri2Prefixes = Uri2Prefixes((
102 ( "http://www.w3.org/2001/xml-events", 'dom' ),
103 ( "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0", 'draw' ),
104 ( "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0", 'fo' ),
105 ( "urn:oasis:names:tc:opendocument:xmlns:presentation:1.0", 'presentation' ),
106 ( "urn:oasis:names:tc:opendocument:xmlns:script:1.0", 'script' ),
107 ( "urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0", 'smil' ),
108 ( "urn:oasis:names:tc:opendocument:xmlns:style:1.0", 'style' ),
109 ( "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0", 'svg' ),
110 ( "urn:oasis:names:tc:opendocument:xmlns:table:1.0", 'table' ),
111 ( "urn:oasis:names:tc:opendocument:xmlns:text:1.0", 'text' ),
112 ( "http://www.w3.org/1999/xlink", 'xlink' ),
113 ( "urn:oasis:names:tc:opendocument:xmlns:office:1.0", 'office' ),
116 visitorClass = OdfVisitor