, , , replace()
: , 4 .
IMO, :
def fixStyle(self, text):
t = text.replace('<p>', '<p style="%s">' % (STYLEDEF,))
t = t.replace('class="wide"', 'style="width: 125px; %s"' % (STYLEDEF,))
t = t.replace('<td>', '<td style="%s">' % STYLEDEF)
t = t.replace('<a ', '<a style="%s" ' % (LINKSTYLE,))
return t
import re
STYLEDEF = 'stuff-from-styledef'
LINKSTYLE = 'VVVV'
def aux(m, dic = {'<p':('<p style="',STYLEDEF),
'<td':('<td style="',STYLEDEF),
'class="wide"':('style="width: 125px; ',STYLEDEF),
'<a':('<a style="',LINKSTYLE)} ):
return '%s%s"' % dic[m.group()]
pat = re.compile('<p(?=>)>|class="wide"|<td(?=>)|<a(?= )')
ch = '<table><tr><td>Test!</td></tr></table><a type="brown" >'
print ch
print fixStyle(None, ch)
print pat.sub(aux,ch)
<table><tr><td>Test!</td></tr></table><a type="brown" >
<table><tr><td style="stuff-from-styledef">Test!</td></tr></table><a style="VVVV" type="brown" >
<table><tr><td style="stuff-from-styledef">Test!</td></tr></table><a style="VVVV" type="brown" >
, re.sub()
.
dic = > dic aux(), doesn ' . dic : .
, aux() STYLEDEF LINKSTYLE .
, .
.
EDIT:
' style="'
STYLEDEF , , ,
def aux(m, dic = {'<p' :'<p style="%s"',
'<td' :'<td style="%s"',
'class="wide"':'style="width: 125px; %s"'} ):
if m.group(1):
return '<a style="%s"' % LINKSTYLE
else:
return dic[m.group()] % STYLEDEF
pat = re.compile('<p(?=>)|class="wide"|<td(?=>)|(<a)(?= )')
, , , . , , .
:
def aux(m, STY = STYLEDEF,LIN = LINKSTYLE ):
return ( 'style="width: 125px; ' if m.group(3) else m.group(1)+' style="' ) + \
( LIN if m.group(2) else STY) + '"'
pat = re.compile('(<p(?=>)|<td(?=>)|(<a(?= )))|(class="wide")')
, :
def aux(m, dic = {'<p' :'<p style="%s"' % STYLEDEF,
'<td':'<td style="%s"' % STYLEDEF,
'<a' :'<a style="%s"' % LINKSTYLE,
'class="wide"':'style="%s"' % ('width: 125px; '+STYLEDEF) } ):
return dic[m.group()]
pat = re.compile('<p(?=>)|class="wide"|<td(?=>)|<a(?= )')
dic aux().
replace()
.