summaryrefslogtreecommitdiff
path: root/sandbox/odf2docutils/odf2docutilslib/python.py
blob: 43ae6710f9a3c6401dcb3fb5af7844d8091a5e29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Copyright (C) 2013 Stefan Merten

# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2 of the License,
# or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

"""
Do conversion by using Python.
"""

__docformat__ = 'reStructuredText' # Formatted to be rendered by epydoc

###############################################################################
###############################################################################
# Import

import docutils.nodes

from docutils_xml.parsers.xml import XmlParser, XmlVisitor, SomeChildren, Uri2Prefixes

###############################################################################
###############################################################################
# Constants

###############################################################################
###############################################################################
# Variables

###############################################################################
###############################################################################
# Functions

###############################################################################
###############################################################################
# Classes

class OdfVisitor(XmlVisitor):
    """
    Visitor class for visiting an ODF element tree.
    """

    ###########################################################################
    # Presentation

    def visit_draw_page(self, elem):
        self.push(docutils.nodes.section())

    def depart_draw_page(self, elem):
        self.pop()

    ###########################################################################
    # Lists

    def visit_text_list(self, elem):
        self.push(docutils.nodes.bullet_list(bullet="*"))
        raise SomeChildren(( ( "text", "list-item" ), ))

    def depart_text_list(self, elem):
        self.pop()

    def visit_text_listitem(self, elem):
        self.push(docutils.nodes.list_item())
        raise SomeChildren(( ( "text", "list" ), ( "text", "p" ), ))

    def depart_text_listitem(self, elem):
        self.pop()

    ###########################################################################
    # Text

    def visit_text_p(self, elem):
        para = docutils.nodes.paragraph()
        if elem.text is not None:
            para.append(docutils.nodes.Text(elem.text))
        self.push(para)
        # TODO tails of embedded inline elements must be considered

    def depart_text_p(self, elem):
        self.pop()

    # TODO Implement

###############################################################################

class Parser(XmlParser):
    """
    Parse the input file and translate it to the output file by Python.
    """

    uri2Prefixes = Uri2Prefixes((
            ( "http://www.w3.org/2001/xml-events", 'dom' ),
            ( "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0", 'draw' ),
            ( "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0", 'fo' ),
            ( "urn:oasis:names:tc:opendocument:xmlns:presentation:1.0", 'presentation' ),
            ( "urn:oasis:names:tc:opendocument:xmlns:script:1.0", 'script' ),
            ( "urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0", 'smil' ),
            ( "urn:oasis:names:tc:opendocument:xmlns:style:1.0", 'style' ),
            ( "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0", 'svg' ),
            ( "urn:oasis:names:tc:opendocument:xmlns:table:1.0", 'table' ),
            ( "urn:oasis:names:tc:opendocument:xmlns:text:1.0", 'text' ),
            ( "http://www.w3.org/1999/xlink", 'xlink' ),
            ( "urn:oasis:names:tc:opendocument:xmlns:office:1.0", 'office' ),
            ))

    visitorClass = OdfVisitor