| author | gibus |
| Mon, 24 Sep 2012 01:12:12 -0700 | |
| changeset 464 | 5a02bfc8aae8 |
| parent 450 | 81fa74c112b8 |
| child 473 | cefe588b2a2b |
| permissions | -rw-r--r-- |
| 0 | 1 |
import uuid |
2 |
import xml.dom.minidom |
|
3 |
import re |
|
4 |
from BeautifulSoup import BeautifulSoup, Comment |
|
5 |
||
6 |
||
7 |
def get_text_nodes(soup): |
|
8 |
return soup(text=lambda text:not isinstance(text, Comment)) |
|
9 |
||
10 |
def is_real_text_node(textNode): |
|
11 |
return not textNode.findParent('style') |
|
12 |
||
13 |
def get_the_soup(input): |
|
|
464
5a02bfc8aae8
For some reasons BeautifulSouf does not convert entities with convertEntities=["xml","html"], use convertEntities=BeautifulSoup.ALL_ENTITIES instead.
gibus
parents:
450
diff
changeset
|
14 |
return BeautifulSoup(input, convertEntities=BeautifulSoup.ALL_ENTITIES) |
| 270 | 15 |
|
16 |
from cm.utils.cache import memoize, dj_memoize |
|
17 |
@dj_memoize |
|
| 0 | 18 |
def spannify(input): |
19 |
""" |
|
20 |
wrap textNodes in spans |
|
21 |
""" |
|
22 |
||
23 |
input = re.sub("\s*$","",input) |
|
24 |
||
25 |
soup = get_the_soup(input) |
|
26 |
||
27 |
textNodes = get_text_nodes(soup) |
|
28 |
textNodes_content = [] |
|
29 |
||
30 |
span_starts = {} |
|
31 |
for i in xrange(len(textNodes)): |
|
32 |
textNode = textNodes[i] |
|
33 |
if is_real_text_node(textNode) : |
|
34 |
textNode.replaceWith('<span id="sv_' + str(i) + '" class="c-s"><span id="sv-' + str(i) + '" class="c-count-0 c-c">' + textNode.string + '</span></span>') |
|
35 |
span_starts[i] = len(''.join(textNodes_content)) |
|
36 |
textNodes_content.append(textNode.string) |
|
37 |
output = unicode(soup) |
|
| 450 | 38 |
# Soup has introduced HTML entities, which should be expanded |
39 |
output =re.sub(r""", '"', output) |
|
40 |
output =re.sub(r"&", '&', output) |
|
41 |
output =re.sub(r">", '>', output) |
|
42 |
output =re.sub(r"<", '<', output) |
|
| 0 | 43 |
|
44 |
textualized = ''.join(textNodes_content) |
|
45 |
return output, textualized, span_starts |