source: ZMS/trunk/ZMSTextformat.py @ 1557

Revision 1557, 8.8 KB checked in by zmsdev, 11 months ago (diff)

removed old zms artefacts

Line 
1################################################################################
2# ZMSTextformat.py
3#
4# This program is free software; you can redistribute it and/or
5# modify it under the terms of the GNU General Public License
6# as published by the Free Software Foundation; either version 2
7# of the License, or (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17################################################################################
18
19# Imports.
20import copy
21# Product Imports.
22import _globals
23
24
25# ------------------------------------------------------------------------------
26#  br_quote
27# ------------------------------------------------------------------------------
28def br_quote(text, subtag, REQUEST):
29  if len(subtag) == 0:
30    return text
31  if type(text) not in [str,unicode]:
32    text = str(text)
33  rtn = ''
34  qcr = ''
35  qtab = ' '*6
36 
37  if _globals.isManagementInterface(REQUEST):
38    if not REQUEST.has_key('format'):
39      qcr = '<span class="unicode">&crarr;</span>'
40      qtab = '<span class="unicode">&rarr;</span>' + '&nbsp;' * 5
41 
42  if subtag == 'br':
43    tmp = []
44    for s in text.split('<%s>'%subtag):
45      while len(s) > 0 and ord(s[0]) in [10,13]: s = s[1:]
46      if len(tmp) > 0:
47        tmp.append('\n')
48      tmp.append(s)
49    text = ''.join(tmp)
50 
51  # handle nested lists
52  ts = text.split('\n')
53  ll = []
54  c = 0
55  for line in ts:
56    if line.find( '\t* ') >= 0 or line.find( '\t# ') >= 0:
57      i = 0
58      while i < len( line) and line[ i] == '\t':
59        i += 1
60      if line[ i] in [ '*', '#']:
61        level = i
62       
63        # open nested lists
64        if level > len(ll):
65          if line[ i] == '*':
66            rtn += '<ul>'*(level-len(ll))
67            ll.extend(['ul']*(level-len(ll)))
68          elif line[ i] == '#':
69            rtn += '<ol>'*(level-len(ll))
70            ll.extend(['ol']*(level-len(ll)))
71       
72        # close nested lists
73        elif level < len(ll):
74          for li in range(len(ll)-level):
75            rtn += '</%s>'%ll[-1]
76            ll = ll[0:-1]
77       
78        rtn += '<li>%s</li>'%line[i+2:]
79        continue
80   
81    # close nested lists
82    if len(ll) > 0:
83      level = 0
84      for li in range(len(ll)-level):
85        rtn += '</%s>'%ll[-1]
86        ll = ll[0:-1]
87   
88    # handle leading whitespaces and tabs
89    i = 0
90    while i < len( line) and line[ i] in [ ' ', '\t']:
91      if line[ i] == ' ':
92        rtn += '&nbsp;'
93      elif line[ i] == '\t':
94        rtn += qtab
95      i += 1
96    rtn += '\n'
97    line = line[ i:].strip()
98   
99    if subtag == 'br':
100      rtn += line+qcr
101      if c < len( ts):
102        rtn += '<%s />'%subtag
103    elif len(line) > 0:
104      rtn += '<%s'%subtag
105      rtn += '>'
106      rtn += line+qcr
107      rtn += '</%s>'%subtag
108    c += 1
109 
110  # close nested lists
111  if len(ll) > 0:
112    level = 0
113    for li in range(len(ll)-level):
114      rtn += '</%s>'%ll[-1]
115      ll = ll[0:-1]
116 
117  return rtn
118
119
120################################################################################
121################################################################################
122###
123###   Class
124###
125################################################################################
126################################################################################
127class ZMSTextformat:
128
129  # ----------------------------------------------------------------------------
130  #  ZMSTextformat.__init__:
131  #
132  #  Constructor.
133  # ----------------------------------------------------------------------------
134  def __init__(self, id, ob, REQUEST):
135    self.setId(id)
136    if REQUEST is not None and \
137       REQUEST.has_key('manage_lang') and \
138       ob['display'].has_key(REQUEST['manage_lang']):
139      self.setDisplay(ob['display'][REQUEST['manage_lang']])
140    else:
141      self.setDisplay(id)
142    self.setTag(ob['tag'])
143    self.setSubTag(ob['subtag'])
144    self.setAttrs(ob['attrs'])
145    richedit = 0
146    if ob.has_key('richedit'):
147      richedit = ob['richedit']
148    self.setRichedit(richedit)
149   
150
151  # ----------------------------------------------------------------------------
152  #  Get/Set Id.
153  # ----------------------------------------------------------------------------
154  getId__roles__ = None
155  def getId(self): return self.id
156  def setId(self, id): self.id = id
157
158  # ----------------------------------------------------------------------------
159  #  Get/Set Display.
160  # ----------------------------------------------------------------------------
161  getDisplay__roles__ = None
162  def getDisplay(self): return self.display
163  def setDisplay(self, display): self.display = display
164
165  # ----------------------------------------------------------------------------
166  #  Get/Set <Tag>.
167  # ----------------------------------------------------------------------------
168  getTag__roles__ = None
169  def getTag(self): return self.tag
170  def setTag(self, tag): self.tag = tag
171
172  # ----------------------------------------------------------------------------
173  #  Assemble <Start-Tag>.
174  # ----------------------------------------------------------------------------
175  getStartTag__roles__ = None
176  def getStartTag(self, id=None, clazz=None):
177    html = ''
178    tag = self.getTag()
179    if len(tag) > 0:
180      html += '<%s'%tag
181      if id is not None:
182        html += ' id="%s"'%id
183      if clazz is not None:
184        html += ' class="%s"'%clazz
185      attrs = self.getAttrs()
186      if len(attrs) > 0:
187        html += ' ' + attrs
188      html += '>'
189    return html
190
191  # ----------------------------------------------------------------------------
192  #  Assemble <End-Tag>.
193  # ----------------------------------------------------------------------------
194  getEndTag__roles__ = None
195  def getEndTag(self):
196    html = ''
197    tag = self.getTag()
198    if len(tag) > 0:
199      html += '</%s'%tag
200      html += '>'
201    return html
202
203  # ----------------------------------------------------------------------------
204  #  Get/Set <Sub-Tag>.
205  # ----------------------------------------------------------------------------
206  getSubTag__roles__ = None
207  def getSubTag(self): return self.subtag
208  def setSubTag(self, subtag): self.subtag = subtag
209
210  # ----------------------------------------------------------------------------
211  #  Get/Set <Tag>-Attributes.
212  # ----------------------------------------------------------------------------
213  getAttrs__roles__ = None
214  def getAttrs(self): return self.attrs
215  def setAttrs(self, attrs): self.attrs = attrs
216
217  # ----------------------------------------------------------------------------
218  #  Get/Set Richedit.
219  # ----------------------------------------------------------------------------
220  getRichedit__roles__ = None
221  def getRichedit(self): return self.richedit
222  def setRichedit(self, richedit): self.richedit = richedit
223
224  # ----------------------------------------------------------------------------
225  #  HTML.
226  # ----------------------------------------------------------------------------
227  getHtml__roles__ = None
228  def getHtml(self):
229    html = ''
230    # Open tag.
231    if len(self.getTag()) > 0:
232      html += '&lt;'
233      html += self.getTag()
234      if len(self.getAttrs()) > 0:
235        html += ' ' + self.getAttrs()
236      html += '&gt;'
237      html += '<br />'
238    # Sub tag.
239    subtag = self.getSubTag()
240    if len(subtag)>0:
241      if subtag == 'br':
242        html += '&nbsp;&nbsp;...&lt;' + subtag + ' /&gt;'
243      else:
244        html += '&nbsp;&nbsp;&lt;' + subtag + '&gt;...&lt;/' + subtag + '&gt;'
245      html += '<br />'
246    else:
247      html += '...'
248    # Close tag.
249    if len(self.getTag()) > 0:
250      html += '&lt;/'
251      html += self.getTag()
252      html += '&gt;'
253      html += '<br />'
254    # Return.
255    return html
256
257  # ----------------------------------------------------------------------------
258  #  Render text.
259  # ----------------------------------------------------------------------------
260  renderText__roles__ = None
261  def renderText(self, text, REQUEST, id=None, clazz=None):
262    html = ''
263    # Open tag.
264    html += self.getStartTag( id, clazz)
265    # Sub tag.
266    text = br_quote( text, self.getSubTag(), REQUEST)
267    # Value.
268    try:
269      html += str( text)
270    except:
271      html += text
272    # Close tag.
273    html += self.getEndTag()
274    # Return.
275    return html
276
277################################################################################
Note: See TracBrowser for help on using the repository browser.