src/cm/utils/html.py
author rbernard
Fri, 05 Feb 2010 18:43:58 +0100
changeset 154 8cb89c153140
parent 0 40c8f766c9b8
child 175 4f072edc51a1
permissions -rw-r--r--
bug fix: removed \r in textversion's content because of a comment position bug in pre_edit. Added a save function to TextVersionManager to do so. Also removed cleaning \r before computing diffs because it should not be necessary anymore.

"""
Package to manipulage html chunks
"""

from BeautifulSoup import BeautifulSoup, Comment

def surrond_text_node(html_chunk, start_html, end_html):
    """
    Surround text nodes in html_chunk
    """
    soup = BeautifulSoup(html_chunk)
    text_nodes = get_text_nodes(soup)
    for text_node in text_nodes:        
        if text_node.string.strip():
            text_node.replaceWith(start_html + text_node.string + end_html)
    return unicode(soup)
    

# utilities    
def get_text_nodes(soup):
    return soup(text=lambda text:not isinstance(text, Comment))