annotate orpg/chat/chat_util.py @ 226:b29454610f36 beta

Traipse Beta 'OpenRPG' {100503-00} Traipse is a distribution of OpenRPG that is designed to be easy to setup and go. Traipse also makes it easy for developers to work on code without fear of sacrifice. 'Ornery-Orc' continues the trend of 'Grumpy' and adds fixes to the code. 'Ornery-Orc's main goal is to offer more advanced features and enhance the productivity of the user. Update Summary (Patch-2) Moved to Stable! New Features: New Namespace method with two new syntaxes New Namespace Internal is context sensitive, always! New Namespace External is 'as narrow as you make it' New Namespace FutureCheck helps ensure you don't receive an incorrect node New Namespace 2.0 documentation in the User Manual New Namespace plugin, Allows Traipse users to use the Standard syntax !@ :: @! New Mini Library with minis from Devin Knight New PluginDB access for URL2Link plugin New to Forms, they now show their content in Design Mode New to Update Manager, checks Repo for updates on software start New to Mini Lib node, change title in design mode New to Game Tree, never lose a node, appends a number to the end of corrupted trees New to Server GUI, Traipse Suite's Debug Console New Warhammer PC Sheet Updates: Update to White Board layer, uses a pencil image for color button Update to Grid Layer, uses a grid image for color button Update to Chat Window, size of drop down menus Update to default lobby message Update to template Text node Update to 4e PC Sheet node Update to how display names are acquired Update to Server, added some 'Pious' technology Update to features node Fixes: Fix to Server GUI startup errors Fix to Server GUI Rooms tab updating Fix to Chat and Settings if non existant die roller is picked Fix to Dieroller and .open() used with .vs(). Successes are correctly calculated Fix to Alias Lib's Export to Tree, Open, Save features Fix to alias node, now works properly Fix to Splitter node, minor GUI cleanup Fix to Backgrounds not loading through remote loader Fix to Node name errors Fix to rolling dice in chat Whispers Fix to Splitters Sizing issues Fix to URL2Link plugin, modified regex compilation should remove memory leak Fix to mapy.py, a roll back due to zoomed grid issues Fix to whiteboard_handler, Circles work by you clicking the center of the circle Fix to Servers parse_incoming_dom which was outdated and did not respect XML Fix to a broken link in the server welcome message Fix to InterParse and logger requiring traceback Fix to Update Manager Status Bar Fix to failed image and erroneous pop up Fix to Mini Lib node that was preventing use Fix to plugins that parce dice but did not call InterParse Fix to nodes for name changing by double click Fix to Game Tree, node ordering on drag and drop corrected Fix to Game Tree, corrupted error message was not showing Fix to Update Manager, checks for internet connection Fix to Update Manager, Auto Update corrections Fix to Server GUI's broadcast, room, player messaging
author sirebral
date Mon, 03 May 2010 03:29:14 -0500
parents 3b6888bb53b5
children
rev   line source
156
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
1 # utility function; see Post() in chatwnd.py
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
2
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
3 import re
71
449a8900f9ac Code refining almost completed, for this round. Some included files are still in need of some clean up, but this is test worthy.
sirebral
parents: 0
diff changeset
4 import string
449a8900f9ac Code refining almost completed, for this round. Some included files are still in need of some clean up, but this is test worthy.
sirebral
parents: 0
diff changeset
5 from orpg.orpgCore import *
140
e842a5f1b775 Traipse Beta 'OpenRPG' {091123-00}
sirebral
parents: 71
diff changeset
6 from orpg.tools.orpg_log import logger, debug
156
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
7 from orpg.tools.decorators import debugging
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
8
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
9 #============================================
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
10 # simple_html_repair(string)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
11 #
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
12 # Crude html/xml parser/verifier.
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
13 # Catches many mistyped and/or malformed
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
14 # html tags and prevents them from causing
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
15 # issues with the chat display (see chatwnd.py)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
16 # DOES NOT catch misused but properly formated
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
17 # html like <script> or <li> which are known
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
18 # to cause issues with the chat display
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
19 #
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
20 # Created 04-25-2005 by Snowdog
71
449a8900f9ac Code refining almost completed, for this round. Some included files are still in need of some clean up, but this is test worthy.
sirebral
parents: 0
diff changeset
21 #=============================================
156
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
22
71
449a8900f9ac Code refining almost completed, for this round. Some included files are still in need of some clean up, but this is test worthy.
sirebral
parents: 0
diff changeset
23 def simple_html_repair(string):
156
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
24 "Returns string with extra > symbols to isolate badly formated HTML"
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
25 #walk though string checking positions of < and > tags.
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
26 first_instance = string.find('<')
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
27 if first_instance == -1: return string #no html, bail out.
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
28
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
29 #strip string of an instances of ">>" and "<<" recursively
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
30 #while (string.find(">>") != -1):string = string.replace(">>",">")
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
31 while (string.find("<<") != -1):string = string.replace("<<","<")
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
32
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
33 last_start = first_instance
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
34 in_tag_flag = 1
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
35 a = first_instance + 1
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
36 while a < len(string):
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
37 if string[a] == '<':
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
38 if in_tag_flag == 1:
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
39 #attempt to figure out best place to put missing >
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
40 #search from last_start to current position
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
41 at_front = 1
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
42 for best_pos in range(last_start,a):
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
43 if (str(string[best_pos]).isspace())and (at_front == 0):
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
44 break
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
45 else:
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
46 at_front = 0
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
47 best_pos = best_pos + 1
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
48 a = best_pos
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
49 string = string[:a]+">"+string[a:]
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
50 in_tag_flag = 0
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
51 #jump back up one character to catch the last > and reset the in_tag_flag
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
52 a = a - 1
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
53 else:
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
54 in_tag_flag = 1
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
55 last_start = a
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
56
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
57 if string[a] == '>':
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
58 last_start = a #found a closing tag, move start of scan block up.
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
59 in_tag_flag = 0
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
60 if (a >= (len(string)-1))and(in_tag_flag == 1):
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
61 #at end of string and need a closing tag marker
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
62 string = string +">"
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
63 a = a+1
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
64
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
65 #strip string of an instances of "<>"
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
66 string = string.replace("<>","")
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
67
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
68 #sanity check. Count the < and > characters, if there arn't enough > chars
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
69 #tack them on the end to avoid open-tag conditions
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
70 diff = string.count('<') - string.count('>')
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
71 if diff > 0:
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
72 for d in range(1,diff):
140
e842a5f1b775 Traipse Beta 'OpenRPG' {091123-00}
sirebral
parents: 71
diff changeset
73 string = string+">"
71
449a8900f9ac Code refining almost completed, for this round. Some included files are still in need of some clean up, but this is test worthy.
sirebral
parents: 0
diff changeset
74 return string
156
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
75
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
76 # Depricated! Might as well use the already made component.get('xml')
71
449a8900f9ac Code refining almost completed, for this round. Some included files are still in need of some clean up, but this is test worthy.
sirebral
parents: 0
diff changeset
77 def strip_unicode(txt):
156
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
78 for i in xrange(len(txt)):
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
79 if txt[i] not in string.printable:
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
80 try:
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
81 txt = txt.replace(txt[i], '&#' + str(ord(txt[i])) + ';')
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
82 except:
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
83 txt = txt.replace(txt[i], '{?}')
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
84 return txt
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
85
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
86 #================================================
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
87 # strip_script_tags(string)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
88 #
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
89 # removes all script tags (start and end)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
90 # 04-26-2005 Snowdog
71
449a8900f9ac Code refining almost completed, for this round. Some included files are still in need of some clean up, but this is test worthy.
sirebral
parents: 0
diff changeset
91 #================================================
156
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
92
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
93 def strip_script_tags(string):
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
94 #kill the <script> issue
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
95 p = re.compile( '<(\s*)(/*)[Ss][Cc][Rr][Ii][Pp][Tt](.*?)>')
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
96 string = p.sub( "<!-- script tag removed //-->", string)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
97 return string
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
98
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
99 #================================================
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
100 # strip_li_tags(string)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
101 #
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
102 # removes all li tags (start and end)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
103 # 05-13-2005
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
104 #================================================
140
e842a5f1b775 Traipse Beta 'OpenRPG' {091123-00}
sirebral
parents: 71
diff changeset
105
156
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
106 def strip_li_tags(string):
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
107 #kill the <li> issue
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
108 string = re.sub( r'<(\s*)[Ll][Ii](.*?)>', r'<b><font color="#000000" size=+1>*</font></b> ', string)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
109 string = re.sub( r'<(/*)[Ll][Ii](.*?)>', r'<br />', string)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
110 return string
140
e842a5f1b775 Traipse Beta 'OpenRPG' {091123-00}
sirebral
parents: 71
diff changeset
111
156
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
112 #================================================
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
113 # strip_body_tags(string)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
114 #
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
115 # removes all body tags (start and end) from messages
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
116 # should not break the setting of custom background colors
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
117 # through legitimate means such as the OpenRPG settings.
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
118 # 07-27-2005 by mDuo13
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
119 #================================================
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
120
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
121 def strip_body_tags(string):
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
122 bodytag_regex = re.compile(r"""<\/?body.*?>""", re.I)
140
e842a5f1b775 Traipse Beta 'OpenRPG' {091123-00}
sirebral
parents: 71
diff changeset
123 string = re.sub(bodytag_regex, "", string)
156
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
124 return string
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
125
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
126 #================================================
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
127 # strip_misalignment_tags(string)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
128 #
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
129 # removes the alignment aspect of <p> tags, since
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
130 # simply closing one doesn't actually fix the text
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
131 # alignment. (I'm assuming this is a bug in wxWindows'
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
132 # html parser.)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
133 # However, closing <center> tags does
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
134 # return the text to its normal alignment, so this
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
135 # algorithm simply closes them, allowing them to be
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
136 # used legitimately without causing much annoyance.
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
137 # 07-27-2005 mDuo13
71
449a8900f9ac Code refining almost completed, for this round. Some included files are still in need of some clean up, but this is test worthy.
sirebral
parents: 0
diff changeset
138 #================================================
156
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
139
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
140 def strip_misalignment_tags(string):
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
141 alignment_regex = re.compile(r"""<p([^>]*?)align\s*=\s*('.*?'|".*?"|[^\s>]*)(.*?)>""", re.I)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
142 string = re.sub(alignment_regex, "<p\\1\\3>", string)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
143
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
144 center_regex = re.compile(r"""<center.*?>""", re.I)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
145 endcenter_regex = re.compile(r"""</center.*?>""", re.I)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
146 num_centertags = center_regex.findall(string)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
147 num_endcentertags = endcenter_regex.findall(string)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
148 if num_centertags > num_endcentertags:
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
149 missing_tags = len(num_centertags) - len(num_endcentertags)
140
e842a5f1b775 Traipse Beta 'OpenRPG' {091123-00}
sirebral
parents: 71
diff changeset
150 string = string + missing_tags*"</center>"#yes, you can do this.
156
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
151 return string
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
152
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
153 #================================================
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
154 # strip_img_tags(string)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
155 #
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
156 # removes all img tags (start and end)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
157 # 05-13-2005
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
158 # redone 07-11-2005 by mDuo13
71
449a8900f9ac Code refining almost completed, for this round. Some included files are still in need of some clean up, but this is test worthy.
sirebral
parents: 0
diff changeset
159 #================================================
156
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
160
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
161 def strip_img_tags(string):
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
162 #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.
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
163 #p = re.sub( r'<(\s*)(/*)[Ii][Mm][Gg][ ][Ss][Rr][Cc][=](.*?)>', r'<!-- img tag removed //--> <a href=\3>\3</a>', string)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
164
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
165 #this regex is substantially more powerful than the one above
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
166 img_tag_regex = re.compile(r"""<img.*?src\s*?=\s*('.*?'|".*?"|[^\s>]*).*?>""", re.I)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
167 #this is what replaces the regex match. the \\1 refers to the URL from the previous string
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
168 img_repl_str = "<a href=\\1>[img]</a>"
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
169
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
170 #replaces all instances of images in the string with links
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
171 p = re.sub(img_tag_regex, img_repl_str, string)
3b6888bb53b5 Traipse Beta 'OpenRPG' {091125-02}
sirebral
parents: 140
diff changeset
172 return p