esmljaos / flexirest (http://aspektratio.net/flexirest)

The medium-featured, flexible reStructuredText utility

Clone this repository (size: 427.3 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/esmljaos/flexirest/
commit 182: 96a860312149
parent 181: 37a6322d64f0
branch: default
The beginning of a functional (admittedly almost braindead) RTF Writer
Jacob Oscarson / esmljaos
2 months ago

Changed (Δ79 bytes):

raw changeset »

flexirest/rtf.py (29 lines added, 34 lines removed)

Up to file-list flexirest/rtf.py:

1
1
# -*- coding: utf-8 -*-
2
2
3
# Ref, convert RTF to HTML via AbiWord (verification purpose):
4
# $ abiword --to=outfile.html abiword.rtf
5
3
6
import re
4
7
5
8
from collections import deque
6
9
from cStringIO import StringIO
7
10
8
11
from docutils import nodes, utils, writers, languages
9
import PyRTF
12
import PyRTF as rtf
10
13
11
14
__docformat__ = 'reStructuredText'
12
15
@@ -41,47 +44,42 @@ class RtfTranslator(nodes.NodeVisitor):
41
44
        self.language = languages.get_language(lcode)
42
45
        self.section_level = 0
43
46
44
        self.doc = PyRTF.Document()
47
        self.doc = rtf.Document()
45
48
        self.style = self.doc.StyleSheet
46
        self.nextParaStyle = self.style.ParagraphStyles.Normal
47
48
        self.head = deque()
49
        self.body = deque()
50
51
        self.out = self.body
52
53
    def write_at_end(self, obj):
54
        self.topsections[-1].append(obj)
49
        #self.head = rtf.Section()
50
        self.body = rtf.Section()
51
        #self.doc.Sections.append(self.head)
52
        self.doc.Sections.append(self.body)
53
        self.next_style = 'Normal'
55
54
56
55
    def astext(self):
57
56
        """
58
57
        'Renders' the contents.
59
58
        """
60
        doc = PyRTF.Document()
61
        for section in self.head:
62
#            import ipdb; ipdb.set_trace()
63
            doc.Sections.append(section)
59
        out = StringIO()
60
        rtf.Renderer().Write(self.doc, out)
61
        return out.getvalue()
64
62
65
        for section in self.body:
66
            doc.Sections.append(section)
63
    def build_paragraph(self, text, style='Normal'):
64
        para = rtf.Paragraph(getattr(self.style.ParagraphStyles,
65
                                     style))
66
        para.append(text)
67
        return para
67
68
68
        out = StringIO()
69
        PyRTF.Renderer().Write(doc, out)
70
        return out.getvalue()
69
    def add_paragraph(self, section, text, style='Normal'):
70
        section.append(self.build_paragraph(text, style))
71
71
72
72
73
    def nop_visit(self, node):
73
74
        pass
74
75
75
76
    def visit_Text(self, node):
76
77
        print('visit_Text: adding text %s' % node.astext().encode('utf-8'))
77
        print('            style = %r' % self.nextParaStyle)
78
        para = PyRTF.Paragraph(self.nextParaStyle)
79
        para.append(node.astext().encode('utf-8'))
80
        print('            para: %r' % para)
81
        section = PyRTF.Section()
82
        section.append(para)
83
        self.out.append(section)
84
        self.nextParaStyle = self.style.ParagraphStyles.Normal
78
79
        p = self.add_paragraph(self.body,
80
                               node.astext().encode('utf-8'),
81
                               self.next_style)
82
        self.next_style = 'Normal'
85
83
86
84
    depart_Text = nop_visit
87
85
@@ -111,9 +109,8 @@ class RtfTranslator(nodes.NodeVisitor):
111
109
        elif self.section_level == 0:
112
110
            # Document title
113
111
            # raise nodes.SkipNode
114
            self.out = self.head
115
            self.nextParaStyle = self.style.ParagraphStyles.Heading1
116
            print('visit_tilet, main title')
112
            self.next_style = 'Heading1'
113
            print('visits main title: %s' % node.astext())
117
114
        elif self.section_level > 0:
118
115
            pass
119
116
@@ -125,7 +122,5 @@ class RtfTranslator(nodes.NodeVisitor):
125
122
        if self.section_level == 0:
126
123
            self.out = self.body
127
124
128
    def visit_paragraph(self, node):
129
        self.body.append('\nP:')
130
125
    visit_paragraph = nop_visit
131
126
    depart_paragraph = nop_visit