src/cm/utils/comment_positioning.py
changeset 454 b7a092a52eae
parent 365 a478cb9786fd
child 496 c3df46754007
equal deleted inserted replaced
453:1d314f629611 454:b7a092a52eae
    80             
    80             
    81     # returns to_modify, to_remove
    81     # returns to_modify, to_remove
    82     return [c for c in commentList if c.valid], \
    82     return [c for c in commentList if c.valid], \
    83            [c for c in commentList if not c.valid]
    83            [c for c in commentList if not c.valid]
    84 
    84 
    85 ## no colors, just markers           
       
    86 #def insert_comment_markers_and_nocolors(htmlcontent, comments):
       
    87 #    
       
    88 #    parser = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder("beautifulsoup"))
       
    89 #    html = parser.parse(htmlcontent.encode("utf8"), encoding="utf8")
       
    90 #    
       
    91 #    cpt = 1
       
    92 #    
       
    93 #    # key : node id, value : indexes of added markers
       
    94 #    # to remember index of inserted markers
       
    95 #    rememberMarkerOffsets = {}
       
    96 #    
       
    97 #    #O(n²) ?
       
    98 #    for comment in comments :
       
    99 #        for i in [0,1] :
       
   100 #            wrapper = comment.start_wrapper if i == 0 else comment.end_wrapper
       
   101 #            offset =  comment.start_offset if i == 0 else comment.end_offset
       
   102 #            marker = "[%d>"%cpt if i == 0 else "<%d]"%cpt
       
   103 #            marker_length = len(marker)
       
   104 #            content = html.find(id = "sv-%d"%wrapper).contents[0]
       
   105 ##            import pdb;pdb.set_trace()
       
   106 #            smallerIndexes = rememberMarkerOffsets.get(wrapper, [])
       
   107 #            original_offset =  offset
       
   108 #            offset += marker_length * len([index for index in smallerIndexes if index <= offset])
       
   109 #        
       
   110 #            smallerIndexes.append(original_offset)
       
   111 #            rememberMarkerOffsets[wrapper] = smallerIndexes
       
   112 #        
       
   113 #            content.replaceWith(content[:offset]+marker+content[offset:])
       
   114 #                
       
   115 #        cpt = cpt + 1
       
   116 #    
       
   117 #    return unicode(html)
       
   118 
       
   119 def add_marker(text, color, start_ids, end_ids, with_markers, with_colors):
    85 def add_marker(text, color, start_ids, end_ids, with_markers, with_colors):
   120 # TODO
    86 # TODO
   121 # THESE 3 LINES ARE REALLY JUST FOR TESTING THIS IS COPIED FROM C-TEXT.CSS AND SHOULD BE DONE DIFFERENTLY
    87 # THESE 3 LINES ARE REALLY JUST FOR TESTING THIS IS COPIED FROM C-TEXT.CSS AND SHOULD BE DONE DIFFERENTLY
   122     BCKCOLORS = ['#FFF', '#FFF39A', '#FFDB9A', '#FFC39A', '#FFAB9A', '#FF879A', '#FF7B9A', '#FF6272']
    88     BCKCOLORS = ['#FFF', '#FFF39A', '#FFDB9A', '#FFC39A', '#FFAB9A', '#FF879A', '#FF7B9A', '#FF6272']
   123     for i in range(30) :
    89     for i in range(30) :
   140     return ret
   106     return ret
   141 
   107 
   142 # comments are comments and replies :
   108 # comments are comments and replies :
   143 def insert_comment_markers(htmlcontent, comments, with_markers, with_colors) :
   109 def insert_comment_markers(htmlcontent, comments, with_markers, with_colors) :
   144 
   110 
   145 #    parser = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder("beautifulsoup"))
       
   146 #    html = parser.parse(htmlcontent.encode("utf8"), encoding="utf8")
       
   147     html = get_the_soup(htmlcontent) ;
   111     html = get_the_soup(htmlcontent) ;
   148     
   112     
   149 #    import pdb;pdb.set_trace()
       
   150     if comments :
   113     if comments :
   151         max_wrapper = max([comment.end_wrapper for comment in comments])
   114         max_wrapper = max([comment.end_wrapper for comment in comments])
   152         min_wrapper = min([comment.start_wrapper for comment in comments])
   115         min_wrapper = min([comment.start_wrapper for comment in comments])
   153         
   116         
   154     datas = {} # { wrapper_id : {'start_color':nb_of_comments_unterminated_at_wrapper_start, 'offsets':{offset: [[ids of wrappers starting at offset], [ids of wrappers ending at offset]]}}
   117     datas = {} # { wrapper_id : {'start_color':nb_of_comments_unterminated_at_wrapper_start, 'offsets':{offset: [[ids of wrappers starting at offset], [ids of wrappers ending at offset]]}}
   156     cpt = 1 # starting numbered comment
   119     cpt = 1 # starting numbered comment
   157     for comment in comments :
   120     for comment in comments :
   158         if comment.is_reply() :
   121         if comment.is_reply() :
   159             continue ;
   122             continue ;
   160         
   123         
   161         #import pdb;pdb.set_trace()
       
   162         # start 
   124         # start 
   163         wrapper_data = datas.get(comment.start_wrapper, {'start_color':0, 'offsets':{}})
   125         wrapper_data = datas.get(comment.start_wrapper, {'start_color':0, 'offsets':{}})
   164         offset = wrapper_data.get('offsets').get(comment.start_offset, [[],[]])
   126         offset = wrapper_data.get('offsets').get(comment.start_offset, [[],[]])
   165         offset[0].append(cpt)
   127         offset[0].append(cpt)
   166         #offset[0].append(comment.id)
       
   167         wrapper_data['offsets'][comment.start_offset] = offset
   128         wrapper_data['offsets'][comment.start_offset] = offset
   168         datas[comment.start_wrapper] = wrapper_data
   129         datas[comment.start_wrapper] = wrapper_data
   169             
   130             
   170         # end 
   131         # end 
   171         wrapper_data = datas.get(comment.end_wrapper, {'start_color':0, 'offsets':{}})
   132         wrapper_data = datas.get(comment.end_wrapper, {'start_color':0, 'offsets':{}})
   172         offset = wrapper_data.get('offsets').get(comment.end_offset, [[],[]])
   133         offset = wrapper_data.get('offsets').get(comment.end_offset, [[],[]])
   173         offset[1].append(cpt)
   134         offset[1].append(cpt)
   174         #offset[1].append(comment.id)
       
   175         wrapper_data['offsets'][comment.end_offset] = offset
   135         wrapper_data['offsets'][comment.end_offset] = offset
   176         datas[comment.end_wrapper] = wrapper_data
   136         datas[comment.end_wrapper] = wrapper_data
   177             
   137             
   178         for cc in range(comment.start_wrapper + 1, comment.end_wrapper + 1) : 
   138         for cc in range(comment.start_wrapper + 1, comment.end_wrapper + 1) : 
   179             wrapper_data = datas.get(cc, {'start_color':0, 'offsets':{}})
   139             wrapper_data = datas.get(cc, {'start_color':0, 'offsets':{}})
   199             
   159             
   200             start = 0
   160             start = 0
   201             start_ids = []
   161             start_ids = []
   202             end_ids = []
   162             end_ids = []
   203             
   163             
   204 #            for offset, nbs in offsets :
       
   205             for offset, ids in offsets :
   164             for offset, ids in offsets :
   206                 
   165                 
   207                 end_ids = ids[1]
   166                 end_ids = ids[1]
   208                 end = offset
   167                 end = offset
   209                 
   168                 
   220             spans += add_marker(content, start_color, [], [], with_markers, with_colors)
   179             spans += add_marker(content, start_color, [], [], with_markers, with_colors)
   221 
   180 
   222         content.replaceWith(spans)
   181         content.replaceWith(spans)
   223 
   182 
   224     return unicode(html)
   183     return unicode(html)
   225 
       
   226 #def output_comment_line(comment) :
       
   227 #    ret = "<tr>"
       
   228 #    for i in range(comment.depth()) : 
       
   229 #        ret = ret + """<td width="1 em"></td>"""
       
   230 #    
       
   231 #    ret = ret + """<td width="1 em">[%d]</td><td>"""
       
   232 #    
       
   233 #
       
   234 #def output_comments(comments) :
       
   235 #
       
   236 #    max_depth = max([comment.depth() for comment in comments])
       
   237 #    top_comments = [comment for comment in comments if comment.reply_to_id == None]
       
   238 #    top_comment_cpt = 0
       
   239 #    html_comments = ""
       
   240 #    
       
   241 #    for top_comment in top_comments :
       
   242 #        html_comments = html_comments + """<table>"""
       
   243 #         
       
   244 #        html_comments = html_comments + "<table><tr>"
       
   245 #         
       
   246 #        html_comments = html_comments + "</table>" 
       
   247 #
       
   248 #        top_comment_cpt = top_comment_cpt + 1
       
   249 #    
       
   250 #    ret = "%s%s%s"%("""<div class="pagebreakhere">""", html_comments, """</div>""")
       
   251 #    return ret