-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZMSTextformat.py
277 lines (248 loc) · 8.81 KB
/
ZMSTextformat.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
################################################################################
# ZMSTextformat.py
#
# This program 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.
################################################################################
# Imports.
import copy
# Product Imports.
import _globals
# ------------------------------------------------------------------------------
# br_quote
# ------------------------------------------------------------------------------
def br_quote(text, subtag, REQUEST):
if len(subtag) == 0:
return text
if type(text) not in [str,unicode]:
text = str(text)
rtn = ''
qcr = ''
qtab = ' '*6
if _globals.isManagementInterface(REQUEST):
if not REQUEST.has_key('format'):
qcr = '<span class="unicode">↵</span>'
qtab = '<span class="unicode">→</span>' + ' ' * 5
if subtag == 'br':
tmp = []
for s in text.split('<%s>'%subtag):
while len(s) > 0 and ord(s[0]) in [10,13]: s = s[1:]
if len(tmp) > 0:
tmp.append('\n')
tmp.append(s)
text = ''.join(tmp)
# handle nested lists
ts = text.split('\n')
ll = []
c = 0
for line in ts:
if line.find( '\t* ') >= 0 or line.find( '\t# ') >= 0:
i = 0
while i < len( line) and line[ i] == '\t':
i += 1
if line[ i] in [ '*', '#']:
level = i
# open nested lists
if level > len(ll):
if line[ i] == '*':
rtn += '<ul>'*(level-len(ll))
ll.extend(['ul']*(level-len(ll)))
elif line[ i] == '#':
rtn += '<ol>'*(level-len(ll))
ll.extend(['ol']*(level-len(ll)))
# close nested lists
elif level < len(ll):
for li in range(len(ll)-level):
rtn += '</%s>'%ll[-1]
ll = ll[0:-1]
rtn += '<li>%s</li>'%line[i+2:]
continue
# close nested lists
if len(ll) > 0:
level = 0
for li in range(len(ll)-level):
rtn += '</%s>'%ll[-1]
ll = ll[0:-1]
# handle leading whitespaces and tabs
i = 0
while i < len( line) and line[ i] in [ ' ', '\t']:
if line[ i] == ' ':
rtn += ' '
elif line[ i] == '\t':
rtn += qtab
i += 1
rtn += '\n'
line = line[ i:].strip()
if subtag == 'br':
rtn += line+qcr
if c < len( ts):
rtn += '<%s />'%subtag
elif len(line) > 0:
rtn += '<%s'%subtag
rtn += '>'
rtn += line+qcr
rtn += '</%s>'%subtag
c += 1
# close nested lists
if len(ll) > 0:
level = 0
for li in range(len(ll)-level):
rtn += '</%s>'%ll[-1]
ll = ll[0:-1]
return rtn
################################################################################
################################################################################
###
### Class
###
################################################################################
################################################################################
class ZMSTextformat:
# ----------------------------------------------------------------------------
# ZMSTextformat.__init__:
#
# Constructor.
# ----------------------------------------------------------------------------
def __init__(self, id, ob, REQUEST):
self.setId(id)
if REQUEST is not None and \
REQUEST.has_key('manage_lang') and \
ob['display'].has_key(REQUEST['manage_lang']):
self.setDisplay(ob['display'][REQUEST['manage_lang']])
else:
self.setDisplay(id)
self.setTag(ob['tag'])
self.setSubTag(ob['subtag'])
self.setAttrs(ob['attrs'])
richedit = 0
if ob.has_key('richedit'):
richedit = ob['richedit']
self.setRichedit(richedit)
# ----------------------------------------------------------------------------
# Get/Set Id.
# ----------------------------------------------------------------------------
getId__roles__ = None
def getId(self): return self.id
def setId(self, id): self.id = id
# ----------------------------------------------------------------------------
# Get/Set Display.
# ----------------------------------------------------------------------------
getDisplay__roles__ = None
def getDisplay(self): return self.display
def setDisplay(self, display): self.display = display
# ----------------------------------------------------------------------------
# Get/Set <Tag>.
# ----------------------------------------------------------------------------
getTag__roles__ = None
def getTag(self): return self.tag
def setTag(self, tag): self.tag = tag
# ----------------------------------------------------------------------------
# Assemble <Start-Tag>.
# ----------------------------------------------------------------------------
getStartTag__roles__ = None
def getStartTag(self, id=None, clazz=None):
html = ''
tag = self.getTag()
if len(tag) > 0:
html += '<%s'%tag
if id is not None:
html += ' id="%s"'%id
if clazz is not None:
html += ' class="%s"'%clazz
attrs = self.getAttrs()
if len(attrs) > 0:
html += ' ' + attrs
html += '>'
return html
# ----------------------------------------------------------------------------
# Assemble <End-Tag>.
# ----------------------------------------------------------------------------
getEndTag__roles__ = None
def getEndTag(self):
html = ''
tag = self.getTag()
if len(tag) > 0:
html += '</%s'%tag
html += '>'
return html
# ----------------------------------------------------------------------------
# Get/Set <Sub-Tag>.
# ----------------------------------------------------------------------------
getSubTag__roles__ = None
def getSubTag(self): return self.subtag
def setSubTag(self, subtag): self.subtag = subtag
# ----------------------------------------------------------------------------
# Get/Set <Tag>-Attributes.
# ----------------------------------------------------------------------------
getAttrs__roles__ = None
def getAttrs(self): return self.attrs
def setAttrs(self, attrs): self.attrs = attrs
# ----------------------------------------------------------------------------
# Get/Set Richedit.
# ----------------------------------------------------------------------------
getRichedit__roles__ = None
def getRichedit(self): return self.richedit
def setRichedit(self, richedit): self.richedit = richedit
# ----------------------------------------------------------------------------
# HTML.
# ----------------------------------------------------------------------------
getHtml__roles__ = None
def getHtml(self):
html = ''
# Open tag.
if len(self.getTag()) > 0:
html += '<'
html += self.getTag()
if len(self.getAttrs()) > 0:
html += ' ' + self.getAttrs()
html += '>'
html += '<br />'
# Sub tag.
subtag = self.getSubTag()
if len(subtag)>0:
if subtag == 'br':
html += ' ...<' + subtag + ' />'
else:
html += ' <' + subtag + '>...</' + subtag + '>'
html += '<br />'
else:
html += '...'
# Close tag.
if len(self.getTag()) > 0:
html += '</'
html += self.getTag()
html += '>'
html += '<br />'
# Return.
return html
# ----------------------------------------------------------------------------
# Render text.
# ----------------------------------------------------------------------------
renderText__roles__ = None
def renderText(self, text, REQUEST, id=None, clazz=None):
html = ''
# Open tag.
html += self.getStartTag( id, clazz)
# Sub tag.
text = br_quote( text, self.getSubTag(), REQUEST)
# Value.
try:
html += str( text)
except:
html += text
# Close tag.
html += self.getEndTag()
# Return.
return html
################################################################################