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 - 2008 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):
35 if cfg
.Read('/Message/Format') == 'yes':
39 if re
.match('^([a-z]+[^ ]*)?[A-Z].*[a-z]{2,}[A-Z]{2,}.*$', a
) != None:
41 if UnicodeConv(string
.lowercase
).find(a
[0]) != -1:
43 elif UnicodeConv(string
.uppercase
).find(a
[0]) != -1:
45 elif UnicodeConv(string
.digits
).find(a
[0]) != -1:
53 if UnicodeConv(string
.lowercase
).find(x
) != -1:
55 elif UnicodeConv(string
.uppercase
).find(x
) != -1:
57 elif UnicodeConv(string
.digits
).find(x
) != -1:
62 if curtype
== nexttype
:
65 if curtype
== 'u' and nexttype
== 'l' and prevtype
== 'p' and len(s
) == 1:
71 ret
= ret
.rstrip() + s
+ ' '
73 ret
+= s
.lower() + ' '
81 ret
= ret
.rstrip() + s
+ ' '
83 ret
+= s
.lower() + ' '
92 xmlsafe
= xml
.sax
.saxutils
.escape(ret
)
93 return xmlsafe
.replace('\n', '<br>')
95 return ret
.replace('\n', ' ')
97 def SmsToHtml(cfg
, v
):
98 if v
.has_key('SMSInfo'):
101 Wammu
.Ringtone
.ringtones
= {}
102 for i
in v
['SMSInfo']['Entries']:
103 if i
['ID'] in Wammu
.Data
.SMSIDs
['PredefinedAnimation']:
104 if i
['Number'] > len(Wammu
.Data
.PredefinedAnimations
):
106 '<wxp module="Wammu.Image" class="Bitmap">' + \
107 '<param name="tooltip" value="' + (_('Predefined animation number %d') % i
['Number']) + '">' + \
108 '<param name="image" value="' + "['" + string
.join(Wammu
.Data
.UnknownPredefined
, "', '") + "']" + '">' + \
112 '<wxp module="Wammu.Image" class="Bitmap">' + \
113 '<param name="tooltip" value="' + Wammu
.Data
.PredefinedAnimations
[i
['Number']][0]+ '">' + \
114 '<param name="image" value="' + "['" + string
.join(Wammu
.Data
.PredefinedAnimations
[i
['Number']][1], "', '") + "']" + '">' + \
117 if i
['ID'] in Wammu
.Data
.SMSIDs
['PredefinedSound']:
118 if i
['Number'] >= len(Wammu
.Data
.PredefinedSounds
):
119 desc
= _('Unknown predefined sound #%d') % i
['Number']
121 desc
= Wammu
.Data
.PredefinedSounds
[i
['Number']][0]
123 '[<wxp module="Wammu.Image" class="Bitmap">' + \
124 '<param name="image" value="' + "['" + string
.join(Wammu
.Data
.Note
, "', '") + "']" + '">' + \
125 '</wxp>' + desc
+ ']'
127 if i
['ID'] in Wammu
.Data
.SMSIDs
['Sound']:
128 Wammu
.Ringtone
.ringtones
[ringno
] = i
['Ringtone']
130 '<wxp module="Wammu.Ringtone" class="Ringtone">' + \
131 '<param name="tooltip" value="' + i
['Ringtone']['Name'] + '">' + \
132 '<param name="ringno" value="' + str(ringno
) + '">' + \
136 if i
['ID'] in Wammu
.Data
.SMSIDs
['Text']:
138 for x
in Wammu
.Data
.TextFormats
:
139 for name
, txt
, style
in x
[1:]:
140 if i
.has_key(name
) and i
[name
]:
142 text
= text
+ (fmt
% SmsTextFormat(cfg
, i
['Buffer']))
144 if i
['ID'] in Wammu
.Data
.SMSIDs
['Bitmap']:
147 '<wxp module="Wammu.Image" class="Bitmap">' + \
148 '<param name="scale" value="(' + str(cfg
.ReadInt('/Message/ScaleImage')) + ')">' + \
149 '<param name="image" value="' + "['" + string
.join(x
['XPM'], "', '") + "']" + '">' + \
152 if i
['ID'] in Wammu
.Data
.SMSIDs
['Animation']:
154 for x
in i
['Bitmap']:
155 data
.append("['" + string
.join(x
['XPM'], "', '") + "']")
157 '<wxp module="Wammu.Image" class="Throbber">' + \
158 '<param name="scale" value="(' + str(cfg
.ReadInt('/Message/ScaleImage')) + ')">' + \
159 '<param name="images" value="' + "['" + string
.join(data
, "', '") + "']" + '">' + \
161 if v
['SMSInfo'].has_key('Unknown') and v
['SMSInfo']['Unknown']:
162 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
164 text
= SmsTextFormat(cfg
, v
['Text'])
166 return HtmlStrConv(text
)