|
0
|
1 |
""" |
|
|
2 |
Package to manipulage html chunks |
|
|
3 |
""" |
|
|
4 |
|
|
|
5 |
from BeautifulSoup import BeautifulSoup, Comment |
|
|
6 |
|
|
|
7 |
def surrond_text_node(html_chunk, start_html, end_html): |
|
|
8 |
""" |
|
|
9 |
Surround text nodes in html_chunk |
|
|
10 |
""" |
|
|
11 |
soup = BeautifulSoup(html_chunk) |
|
|
12 |
text_nodes = get_text_nodes(soup) |
|
|
13 |
for text_node in text_nodes: |
|
|
14 |
if text_node.string.strip(): |
|
|
15 |
text_node.replaceWith(start_html + text_node.string + end_html) |
|
|
16 |
return unicode(soup) |
|
|
17 |
|
|
|
18 |
|
|
|
19 |
# utilities |
|
|
20 |
def get_text_nodes(soup): |
|
|
21 |
return soup(text=lambda text:not isinstance(text, Comment)) |