| author | Simon Descarpentries <sid@sopinspace.com> |
| Tue, 06 May 2014 13:52:01 +0200 | |
| changeset 651 | 9bbc657f6837 |
| parent 516 | c6105d922ac6 |
| 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 |
||
|
502
8ec189cc214d
do not skip span for newline textnodes otherwise compute_new_comment_positions() will return bad results for pandoc texts.
gibus
parents:
473
diff
changeset
|
10 |
def is_real_text_node(textNode, nolinefeed=True): |
|
8ec189cc214d
do not skip span for newline textnodes otherwise compute_new_comment_positions() will return bad results for pandoc texts.
gibus
parents:
473
diff
changeset
|
11 |
if nolinefeed and textNode.string == "\n": |
|
473
cefe588b2a2b
Do not spannify empty text nodes, prevents abiword crash.
gibus
parents:
464
diff
changeset
|
12 |
return False |
| 0 | 13 |
return not textNode.findParent('style') |
14 |
||
15 |
def get_the_soup(input): |
|
|
516
c6105d922ac6
For some reason, BeautifulSoup wants now fromEncoding='UTF-8' in some unidentified cases.
gibus
parents:
502
diff
changeset
|
16 |
return BeautifulSoup(input, convertEntities=BeautifulSoup.ALL_ENTITIES, fromEncoding='UTF-8') |
| 270 | 17 |
|
18 |
from cm.utils.cache import memoize, dj_memoize |
|
19 |
@dj_memoize |
|
|
502
8ec189cc214d
do not skip span for newline textnodes otherwise compute_new_comment_positions() will return bad results for pandoc texts.
gibus
parents:
473
diff
changeset
|
20 |
def spannify(input, nolinefeed=True): |
| 0 | 21 |
""" |
22 |
wrap textNodes in spans |
|
23 |
""" |
|
24 |
||
25 |
input = re.sub("\s*$","",input) |
|
26 |
||
27 |
soup = get_the_soup(input) |
|
28 |
||
29 |
textNodes = get_text_nodes(soup) |
|
30 |
textNodes_content = [] |
|
31 |
||
32 |
span_starts = {} |
|
33 |
for i in xrange(len(textNodes)): |
|
34 |
textNode = textNodes[i] |
|
|
502
8ec189cc214d
do not skip span for newline textnodes otherwise compute_new_comment_positions() will return bad results for pandoc texts.
gibus
parents:
473
diff
changeset
|
35 |
if is_real_text_node(textNode, nolinefeed) : |
| 0 | 36 |
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>') |
37 |
span_starts[i] = len(''.join(textNodes_content)) |
|
38 |
textNodes_content.append(textNode.string) |
|
39 |
output = unicode(soup) |
|
| 450 | 40 |
# Soup has introduced HTML entities, which should be expanded |
41 |
output =re.sub(r""", '"', output) |
|
42 |
output =re.sub(r"&", '&', output) |
|
43 |
output =re.sub(r">", '>', output) |
|
44 |
output =re.sub(r"<", '<', output) |
|
| 0 | 45 |
|
46 |
textualized = ''.join(textNodes_content) |
|
47 |
return output, textualized, span_starts |