Mercurial > traipse_dev
comparison orpg/chat/chat_util.py @ 71:449a8900f9ac ornery-dev
Code refining almost completed, for this round. Some included files are still in need of some clean up, but this is test worthy.
author | sirebral |
---|---|
date | Thu, 20 Aug 2009 03:00:39 -0500 |
parents | 4385a7d0efd1 |
children | 1ed2feab0db9 |
comparison
equal
deleted
inserted
replaced
70:52a5fa913008 | 71:449a8900f9ac |
---|---|
1 # utility function; see Post() in chatwnd.py | 1 # utility function; see Post() in chatwnd.py |
2 | 2 |
3 import re | 3 import re |
4 import string | 4 import string |
5 from orpg.orpgCore import * | |
6 from orpg.tools.orpg_log import logger | |
7 from orpg.tools.decorators import debugging | |
5 | 8 |
6 #============================================ | 9 #============================================ |
7 # simple_html_repair(string) | 10 # simple_html_repair(string) |
8 # | 11 # |
9 # Crude html/xml parser/verifier. | 12 # Crude html/xml parser/verifier. |
14 # html like <script> or <li> which are known | 17 # html like <script> or <li> which are known |
15 # to cause issues with the chat display | 18 # to cause issues with the chat display |
16 # | 19 # |
17 # Created 04-25-2005 by Snowdog | 20 # Created 04-25-2005 by Snowdog |
18 #============================================= | 21 #============================================= |
22 @debugging | |
19 def simple_html_repair(string): | 23 def simple_html_repair(string): |
20 "Returns string with extra > symbols to isolate badly formated HTML" | 24 "Returns string with extra > symbols to isolate badly formated HTML" |
21 #walk though string checking positions of < and > tags. | 25 #walk though string checking positions of < and > tags. |
22 first_instance = string.find('<') | 26 first_instance = string.find('<') |
23 if first_instance == -1: return string #no html, bail out. | 27 if first_instance == -1: return string #no html, bail out. |
65 #tack them on the end to avoid open-tag conditions | 69 #tack them on the end to avoid open-tag conditions |
66 diff = string.count('<') - string.count('>') | 70 diff = string.count('<') - string.count('>') |
67 if diff > 0: | 71 if diff > 0: |
68 for d in range(1,diff): | 72 for d in range(1,diff): |
69 string = string+">" | 73 string = string+">" |
70 | |
71 return string | 74 return string |
72 | 75 |
76 """ Depricated! Might as well use the already made component.get('xml') | |
73 def strip_unicode(txt): | 77 def strip_unicode(txt): |
74 for i in xrange(len(txt)): | 78 for i in xrange(len(txt)): |
75 if txt[i] not in string.printable: | 79 if txt[i] not in string.printable: |
76 try: | 80 try: |
77 txt = txt.replace(txt[i], '&#' + str(ord(txt[i])) + ';') | 81 txt = txt.replace(txt[i], '&#' + str(ord(txt[i])) + ';') |
78 except: | 82 except: |
79 txt = txt.replace(txt[i], '{?}') | 83 txt = txt.replace(txt[i], '{?}') |
80 return txt | 84 return txt |
85 """ | |
81 | 86 |
82 #================================================ | 87 #================================================ |
83 # strip_script_tags(string) | 88 # strip_script_tags(string) |
84 # | 89 # |
85 # removes all script tags (start and end) | 90 # removes all script tags (start and end) |
86 # 04-26-2005 Snowdog | 91 # 04-26-2005 Snowdog |
87 #================================================ | 92 #================================================ |
93 @debugging | |
88 def strip_script_tags(string): | 94 def strip_script_tags(string): |
89 #kill the <script> issue | 95 #kill the <script> issue |
90 p = re.compile( '<(\s*)(/*)[Ss][Cc][Rr][Ii][Pp][Tt](.*?)>') | 96 p = re.compile( '<(\s*)(/*)[Ss][Cc][Rr][Ii][Pp][Tt](.*?)>') |
91 string = p.sub( "<!-- script tag removed //-->", string) | 97 string = p.sub( "<!-- script tag removed //-->", string) |
92 return string | 98 return string |
95 # strip_li_tags(string) | 101 # strip_li_tags(string) |
96 # | 102 # |
97 # removes all li tags (start and end) | 103 # removes all li tags (start and end) |
98 # 05-13-2005 | 104 # 05-13-2005 |
99 #================================================ | 105 #================================================ |
106 @debugging | |
100 def strip_li_tags(string): | 107 def strip_li_tags(string): |
101 #kill the <li> issue | 108 #kill the <li> issue |
102 string = re.sub( r'<(\s*)[Ll][Ii](.*?)>', r'<b><font color="#000000" size=+1>*</font></b> ', string) | 109 string = re.sub( r'<(\s*)[Ll][Ii](.*?)>', r'<b><font color="#000000" size=+1>*</font></b> ', string) |
103 string = re.sub( r'<(/*)[Ll][Ii](.*?)>', r'<br />', string) | 110 string = re.sub( r'<(/*)[Ll][Ii](.*?)>', r'<br />', string) |
104 return string | 111 return string |
109 # removes all body tags (start and end) from messages | 116 # removes all body tags (start and end) from messages |
110 # should not break the setting of custom background colors | 117 # should not break the setting of custom background colors |
111 # through legitimate means such as the OpenRPG settings. | 118 # through legitimate means such as the OpenRPG settings. |
112 # 07-27-2005 by mDuo13 | 119 # 07-27-2005 by mDuo13 |
113 #================================================ | 120 #================================================ |
121 @debugging | |
114 def strip_body_tags(string): | 122 def strip_body_tags(string): |
115 bodytag_regex = re.compile(r"""<\/?body.*?>""", re.I) | 123 bodytag_regex = re.compile(r"""<\/?body.*?>""", re.I) |
116 string = re.sub(bodytag_regex, "", string) | 124 string = re.sub(bodytag_regex, "", string) |
117 return string | 125 return string |
118 | 126 |
127 # return the text to its normal alignment, so this | 135 # return the text to its normal alignment, so this |
128 # algorithm simply closes them, allowing them to be | 136 # algorithm simply closes them, allowing them to be |
129 # used legitimately without causing much annoyance. | 137 # used legitimately without causing much annoyance. |
130 # 07-27-2005 mDuo13 | 138 # 07-27-2005 mDuo13 |
131 #================================================ | 139 #================================================ |
140 @debugging | |
132 def strip_misalignment_tags(string): | 141 def strip_misalignment_tags(string): |
133 alignment_regex = re.compile(r"""<p([^>]*?)align\s*=\s*('.*?'|".*?"|[^\s>]*)(.*?)>""", re.I) | 142 alignment_regex = re.compile(r"""<p([^>]*?)align\s*=\s*('.*?'|".*?"|[^\s>]*)(.*?)>""", re.I) |
134 string = re.sub(alignment_regex, "<p\\1\\3>", string) | 143 string = re.sub(alignment_regex, "<p\\1\\3>", string) |
135 | 144 |
136 center_regex = re.compile(r"""<center.*?>""", re.I) | 145 center_regex = re.compile(r"""<center.*?>""", re.I) |
147 # | 156 # |
148 # removes all img tags (start and end) | 157 # removes all img tags (start and end) |
149 # 05-13-2005 | 158 # 05-13-2005 |
150 # redone 07-11-2005 by mDuo13 | 159 # redone 07-11-2005 by mDuo13 |
151 #================================================ | 160 #================================================ |
161 @debugging | |
152 def strip_img_tags(string): | 162 def strip_img_tags(string): |
153 #This is a Settings definable feature, Allowing users to enable or disable image display to fix the client crash due to large img posted to chat. | 163 #This is a Settings definable feature, Allowing users to enable or disable image display to fix the client crash due to large img posted to chat. |
154 #p = re.sub( r'<(\s*)(/*)[Ii][Mm][Gg][ ][Ss][Rr][Cc][=](.*?)>', r'<!-- img tag removed //--> <a href=\3>\3</a>', string) | 164 #p = re.sub( r'<(\s*)(/*)[Ii][Mm][Gg][ ][Ss][Rr][Cc][=](.*?)>', r'<!-- img tag removed //--> <a href=\3>\3</a>', string) |
155 | 165 |
156 #this regex is substantially more powerful than the one above | 166 #this regex is substantially more powerful than the one above |