bannedwords_marker_stripper.py (2861B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\bannedwords\bannedwords_marker_stripper.py 2 import re 3 import logging 4 5 def compile_pattern(tag): 6 return re.compile('(?i)<%s(?P<attrs>[^>]*)>(?P<inner>.*?)</%s>' % (tag, tag)) 7 8 9 def compile_attribute_pattern(attribute_name, valid_value_regex): 10 return re.compile('(?i)%s\\="?%s"?' % (attribute_name, valid_value_regex)) 11 12 13 STRIPPER_MAP = {'font': {'pattern': compile_pattern('font'), 14 'attributes': {'size': compile_attribute_pattern('size', '\\d{1,2}'), 15 'color': compile_attribute_pattern('color', '(#|0x)[\\dabcdef]{6,8}')}}, 16 'a': {'pattern': compile_pattern('a'), 17 'attributes': {'href': compile_attribute_pattern('href', '(fitting|joinchannel|overviewpreset|showinfo|killreport)\\:[\\dabcdef/;:-]+')}}, 18 'color': {'pattern': compile_pattern('color'), 19 'attributes': {'__none__': compile_attribute_pattern('', '(#|0x)[\\dabcdef]{6,8}')}}, 20 'fontsize': {'pattern': compile_pattern('fontsize'), 21 'attributes': {'__none__': compile_attribute_pattern('', '\\d{1,2}')}}, 22 'u': {'pattern': compile_pattern('u'), 23 'attributes': {}}, 24 'i': {'pattern': compile_pattern('i'), 25 'attributes': {}}, 26 'b': {'pattern': compile_pattern('b'), 27 'attributes': {}}, 28 'loc': {'pattern': compile_pattern('loc'), 29 'attributes': {}}, 30 'url': {'pattern': compile_pattern('url'), 31 'attributes': {'__none__': compile_attribute_pattern('', '(fitting|joinchannel|overviewpreset|showinfo|killreport)\\:[\\dabcdef/;:-]+')}}} 32 33 def format_stripper(markup): 34 try: 35 working_text = markup.replace('<br>', '') 36 for tag, data in STRIPPER_MAP.iteritems(): 37 pattern = data.get('pattern', None) 38 buff = [] 39 i = 0 40 m = pattern.search(working_text) 41 while m: 42 buff.append(working_text[i:m.start()]) 43 attrs = m.group('attrs') or '' 44 if attrs: 45 attribute_patterns = data.get('attributes', None) 46 if attribute_patterns: 47 for attr_name, attr_pattern in attribute_patterns.iteritems(): 48 attrs = attr_pattern.sub('', attrs) 49 50 attrs = attrs.strip() 51 if attrs: 52 buff.append(attrs) 53 inner = m.group('inner') or '' 54 if inner: 55 buff.append(inner) 56 i = m.end() 57 m = pattern.search(working_text, i) 58 59 if i: 60 buff.append(working_text[i:]) 61 if buff: 62 working_text = u''.join(buff) 63 64 return working_text 65 except Exception as ex: 66 logging.warn('Failed to strip text marker, error: {!r}'.format(str(ex))) 67 return markup