1
# ------------------------------------------------------------------------------
2
#
3
#  Copyright (c) 2005, Enthought, Inc.
4
#  All rights reserved.
5
#
6
#  This software is provided without warranty under the terms of the BSD
7
#  license included in LICENSE.txt and may be redistributed only
8
#  under the conditions described in the aforementioned license.  The license
9
#  is also available online at http://www.enthought.com/licenses/BSD.txt
10
#
11
#  Thanks for using Enthought open source!
12
#
13
#  Author: David C. Morrill
14
#  Date:   10/21/2004
15
#
16
# ------------------------------------------------------------------------------
17

18 11
""" Defines the HTML editor factory. HTML editors interpret and display
19
    HTML-formatted text, but do not modify it.
20
"""
21

22

23

24 11
from traits.api import Bool, Str
25

26 11
from ..basic_editor_factory import BasicEditorFactory
27

28 11
from ..toolkit import toolkit_object
29

30
# Callable that returns the editor to use in the UI.
31

32

33 11
def html_editor(*args, **traits):
34 8
    return toolkit_object("html_editor:SimpleEditor")(*args, **traits)
35

36

37
# Template used to create code blocks embedded in the module comment
38 11
block_template = """<center><table width="95%%"><tr><td bgcolor="#ECECEC"><tt>
39
%s</tt></td></tr></table></center>"""
40

41
# Template used to create lists embedded in the module comment
42 11
list_template = """<%s>
43
%s
44
</%s>"""
45

46
# ------------------------------------------------------------------------------
47
#  'ToolkitEditorFactory' class:
48
# ------------------------------------------------------------------------------
49

50

51 11
class ToolkitEditorFactory(BasicEditorFactory):
52
    """ Editor factory for HTML editors.
53
    """
54

55
    # --------------------------------------------------------------------------
56
    #  Trait definitions:
57
    # --------------------------------------------------------------------------
58

59
    #: Should implicit text formatting be converted to HTML?
60 11
    format_text = Bool(False)
61

62
    #: External objects referenced in the HTML are relative to this url
63 11
    base_url = Str()
64

65
    #: The object trait containing the base URL
66 11
    base_url_name = Str()
67

68
    #: Should links be opened in an external browser?
69 11
    open_externally = Bool(False)
70

71 11
    def parse_text(self, text):
72
        """ Parses the contents of a formatted text string into the
73
            corresponding HTML.
74
        """
75 8
        text = text.replace("\r\n", "\n")
76 11
        lines = [("." + line).strip()[1:] for line in text.split("\n")]
77 11
        ind = min(
78
            *(
79
                [self.indent(line) for line in lines if line != ""]
80
                + [1000, 1000]
81
            )
82
        )
83 11
        if ind >= 1000:
84 8
            ind = 0
85 11
        lines = [line[ind:] for line in lines]
86 8
        new_lines = []
87 8
        i = 0
88 8
        n = len(lines)
89 11
        while i < n:
90 8
            line = lines[i]
91 8
            m = self.indent(line)
92 11
            if m > 0:
93 11
                if line[m] in "-*":
94 0
                    i, line = self.parse_list(lines, i)
95
                else:
96 0
                    i, line = self.parse_block(lines, i)
97 0
                new_lines.append(line)
98
            else:
99 8
                new_lines.append(line)
100 8
                i += 1
101 8
        text = "\n".join(new_lines)
102 11
        paragraphs = [p.strip() for p in text.split("\n\n")]
103 11
        for i, paragraph in enumerate(paragraphs):
104 11
            if paragraph[:3].lower() != "<p>":
105 8
                paragraphs[i] = "<p>%s</p>" % paragraph
106 8
        return "\n".join(paragraphs)
107

108 11
    def parse_block(self, lines, i):
109
        """ Parses a code block.
110
        """
111 0
        m = 1000
112 0
        n = len(lines)
113 0
        j = i
114 11
        while j < n:
115 0
            line = lines[j]
116 11
            if line != "":
117 0
                k = self.indent(line)
118 11
                if k == 0:
119 0
                    break
120 0
                m = min(m, k)
121 0
            j += 1
122 0
        j -= 1
123 11
        while (j > i) and (lines[j] == ""):
124 0
            j -= 1
125 0
        j += 1
126 11
        temp = [
127
            (("&nbsp;" * (self.indent(line) - m)) + line.strip())
128
            for line in lines[i:j]
129
        ]
130 0
        return (j, block_template % "\n<br>".join(temp))
131

132 11
    def parse_list(self, lines, i):
133
        """ Parses a list.
134
        """
135 0
        line = lines[i]
136 0
        m = self.indent(line)
137 0
        kind = line[m]
138 0
        result = ["<li>" + line[m + 1 :].strip()]
139 0
        n = len(lines)
140 0
        j = i + 1
141 11
        while j < n:
142 0
            line = lines[j]
143 0
            k = self.indent(line)
144 11
            if k < m:
145 0
                break
146 11
            if k == m:
147 11
                if line[k] != kind:
148 0
                    break
149 0
                result.append("<li>" + line[k + 1 :].strip())
150 0
                j += 1
151 11
            elif line[k] in "-*":
152 0
                j, line = self.parse_list(lines, j)
153 0
                result.append(line)
154
            else:
155 0
                result.append(line.strip())
156 0
                j += 1
157 0
        style = ["ul", "ol"][kind == "*"]
158 0
        return (j, list_template % (style, "\n".join(result), style))
159

160 11
    def indent(self, line):
161
        """ Calculates the amount of white space at the beginning of a line.
162
        """
163 8
        return len(line) - len((line + ".").strip()) + 1
164

165

166 11
HTMLEditor = ToolkitEditorFactory(klass=html_editor)

Read our documentation on viewing source code .

Loading