Suppose if I have below code. As you can see, I have some script or data wrapped with "%%[" and "]%%". And normally it is illegal. That's the original data I want to keep. Meanwhile I want to Add/Change/Remove the attributes in the <table>
. Then output the code after modified.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
%%[
Sever Language data here
]%%
<title>%%=v(@variable)=%%</title>
</head>
<body>
<div style="display:none;">
<custom name="opencounter" type="tracking">
<img width='0' height='0' src='%%=v(@adometry)=%%'>
</div>
<table width="100%" cellpadding="0" cellspacing="0" border="0" bgcolor="#ffffff">
<tr>
<td align="center">Something here
</td>
</tr>
</table>
</body>
I've tried lots of way to work on this. I've tried Beautifulsoup. But it will change some special character like "—" to "&mdash". I want to keep the special character if it's not coded as escaped character. Beautifulsoup also change the order of the attribute. For the <custom>
tag, it will convert it to <custom></custom>
. I think Beautifulsoup is a lib good at parsing data not manipulating data.
I also tried jsdom long ago, it was working fine I think. But it still have some trouble with <custom>
issue. It will have some trouble with changing <img>
to <img />
. Not sure if jsdom will keep the illegal data. And it's working very slow...
I've also tried to use jQuery in browser to output with .html()
function. But it will change the order of the attribute. And for the <table>
tag, it will insert <tbody>
in it which isn't what I want.
So suppose I want to change the cellpadding to 10. The code should looks like below. Maybe I can allow the different order of the attribute. Does anyone have any idea on what lib I can use or what kind of thing I can do to work on this requirement. Welcome any comments!!! BTW, I'm not sooo familiar with regular expression. I think it will makes me frustrating...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
%%[
Sever Language data here
]%%
<title>%%=v(@variable)=%%</title>
</head>
<body>
<div style="display:none;">
<custom name="opencounter" type="tracking">
<img width='0' height='0' src='%%=v(@adometry)=%%'>
</div>
<table width="100%" cellpadding="10" cellspacing="0" border="0" bgcolor="#ffffff">
<tr>
<td align="center">Something here
</td>
</tr>
</table>
</body>