Find an empty row and remove it

6 views
Skip to first unread message

techtonik

unread,
Sep 13, 2010, 2:44:24 PM9/13/10
to Genshi
Hi,

I wonder how using Genshi I can remove an empty table row from the
stream. That means transform:

<table>
<tr><th></th><td><td/><td></td></tr>
<tr><th>not empty</th><td><td/><td></td></tr>
</table>

into

<table>
<tr><th>not empty</th><td><td/><td></td></tr>
</table>

?

techtonik

unread,
Sep 14, 2010, 10:14:13 AM9/14/10
to Genshi
Here is that I ended with. I wonder if there is a better way?


---
from genshi.input import HTML
from genshi.filters.transform import Transformer, StreamBuffer
import re

html2 = HTML('''
<table>
<tr><th></th><td></td><td></td></tr>
<tr><th>not empty</th><td></td><td></td></tr>
</table>
''')

buffer = StreamBuffer()

def rowfilter(): # attention, closure
text = buffer.render()
text = re.sub(r'(?s)<tr>(\s*<t[hd](/>|>\s*</t[hd]>))+\s*</tr>', '',
text)
#print(text)
return HTML(text)

transtream = html2 | Transformer().select('.')\
.copy(buffer).replace(rowfilter)
print transtream
Reply all
Reply to author
Forward
0 new messages