1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
5 Message to HTML conversion
7 __author__
= 'Michal Čihař'
8 __email__
= 'michal@cihar.com'
10 Copyright © 2003 - 2010 Michal Čihař
12 This program is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License version 2 as published by
14 the Free Software Foundation.
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 import xml
.sax
.saxutils
32 from Wammu
.Locales
import UnicodeConv
, HtmlStrConv
, hgettext
as _
34 def SmsTextFormat(cfg
, txt
, dohtml
= True, doxml
= False):
37 if cfg
.Read('/Message/Format') == 'yes':
41 if re
.match('^([a-z]+[^ ]*)?[A-Z].*[a-z]{2,}[A-Z]{2,}.*$', a
) != None:
43 if UnicodeConv(string
.lowercase
).find(a
[0]) != -1:
45 elif UnicodeConv(string
.uppercase
).find(a
[0]) != -1:
47 elif UnicodeConv(string
.digits
).find(a
[0]) != -1:
55 if UnicodeConv(string
.lowercase
).find(x
) != -1:
57 elif UnicodeConv(string
.uppercase
).find(x
) != -1:
59 elif UnicodeConv(string
.digits
).find(x
) != -1:
64 if curtype
== nexttype
:
67 if curtype
== 'u' and nexttype
== 'l' and prevtype
== 'p' and len(s
) == 1:
75 parts
.append(s
.lower())
85 parts
.append(s
.lower())
95 xmlsafe
= xml
.sax
.saxutils
.escape(ret
)
97 return xmlsafe
.replace('\n', '<br />')
99 return xmlsafe
.replace('\n', '<br>')
101 return ret
.replace('\n', ' ')
103 def SmsToHtml(cfg
, v
):
104 if v
.has_key('SMSInfo'):
107 Wammu
.Ringtone
.ringtones
= {}
108 for i
in v
['SMSInfo']['Entries']:
109 if i
['ID'] in Wammu
.Data
.SMSIDs
['PredefinedAnimation']:
110 if i
['Number'] > len(Wammu
.Data
.PredefinedAnimations
):
112 '<wxp module="Wammu.Image" class="Bitmap">' + \
113 '<param name="tooltip" value="' + (_('Predefined animation number %d') % i
['Number']) + '">' + \
114 '<param name="image" value="' + "['" + string
.join(Wammu
.Data
.UnknownPredefined
, "', '") + "']" + '">' + \
118 '<wxp module="Wammu.Image" class="Bitmap">' + \
119 '<param name="tooltip" value="' + Wammu
.Data
.PredefinedAnimations
[i
['Number']][0]+ '">' + \
120 '<param name="image" value="' + "['" + string
.join(Wammu
.Data
.PredefinedAnimations
[i
['Number']][1], "', '") + "']" + '">' + \
123 if i
['ID'] in Wammu
.Data
.SMSIDs
['PredefinedSound']:
124 if i
['Number'] >= len(Wammu
.Data
.PredefinedSounds
):
125 desc
= _('Unknown predefined sound #%d') % i
['Number']
127 desc
= Wammu
.Data
.PredefinedSounds
[i
['Number']][0]
129 '[<wxp module="Wammu.Image" class="Bitmap">' + \
130 '<param name="image" value="' + "['" + string
.join(Wammu
.Data
.Note
, "', '") + "']" + '">' + \
131 '</wxp>' + desc
+ ']'
133 if i
['ID'] in Wammu
.Data
.SMSIDs
['Sound']:
134 Wammu
.Ringtone
.ringtones
[ringno
] = i
['Ringtone']
136 '<wxp module="Wammu.Ringtone" class="Ringtone">' + \
137 '<param name="tooltip" value="' + i
['Ringtone']['Name'] + '">' + \
138 '<param name="ringno" value="' + str(ringno
) + '">' + \
142 if i
['ID'] in Wammu
.Data
.SMSIDs
['Text']:
144 for x
in Wammu
.Data
.TextFormats
:
145 for name
, txt
, style
in x
[1:]:
146 if i
.has_key(name
) and i
[name
]:
148 text
= text
+ (fmt
% SmsTextFormat(cfg
, i
['Buffer']))
150 if i
['ID'] in Wammu
.Data
.SMSIDs
['Bitmap']:
153 '<wxp module="Wammu.Image" class="Bitmap">' + \
154 '<param name="scale" value="(' + str(cfg
.ReadInt('/Message/ScaleImage')) + ')">' + \
155 '<param name="image" value="' + "['" + string
.join(x
['XPM'], "', '") + "']" + '">' + \
158 if i
['ID'] in Wammu
.Data
.SMSIDs
['Animation']:
160 for x
in i
['Bitmap']:
161 data
.append("['" + string
.join(x
['XPM'], "', '") + "']")
163 '<wxp module="Wammu.Image" class="Throbber">' + \
164 '<param name="scale" value="(' + str(cfg
.ReadInt('/Message/ScaleImage')) + ')">' + \
165 '<param name="images" value="' + "['" + string
.join(data
, "', '") + "']" + '">' + \
167 if v
['SMSInfo'].has_key('Unknown') and v
['SMSInfo']['Unknown']:
168 text
= ('<table border="1" bgcolor="#dd7777" color="#000000"><tr><td>%s</td></tr></table>' % _('Some parts of this message were not decoded correctly, probably due to missing support for it in Gammu.')) + text
170 text
= SmsTextFormat(cfg
, v
['Text'])
172 return HtmlStrConv(text
)