10 import html5lib |
10 import html5lib |
11 from html5lib import treebuilders |
11 from html5lib import treebuilders |
12 |
12 |
13 def compute_new_comment_positions(old_content, old_format, new_content, new_format, commentList): |
13 def compute_new_comment_positions(old_content, old_format, new_content, new_format, commentList): |
14 |
14 |
15 if old_format!='html': |
15 # cf. TextVersion.get_content |
16 previousVersionContent = pandoc_convert(old_content, old_format, 'html') |
16 previousVersionContent = pandoc_convert(old_content, old_format, 'html') |
17 else: |
17 newVersionContent = pandoc_convert(new_content, new_format, 'html') |
18 previousVersionContent = old_content |
18 |
19 |
|
20 if new_format != 'html': |
|
21 newVersionContent = pandoc_convert(new_content, new_format, 'html') |
|
22 else: |
|
23 newVersionContent = new_content |
|
24 |
|
25 _, previous_char_list, span_starts_previous = spannify(previousVersionContent) |
19 _, previous_char_list, span_starts_previous = spannify(previousVersionContent) |
26 _, new_char_list, span_starts_new = spannify(newVersionContent) |
20 _, new_char_list, span_starts_new = spannify(newVersionContent) |
27 |
21 |
28 sm = SequenceMatcher(None, previous_char_list, new_char_list) |
22 sm = SequenceMatcher(None, previous_char_list, new_char_list) |
29 |
23 |
66 elif comment.initial_end_offset > i1: |
60 elif comment.initial_end_offset > i1: |
67 comment.valid = False |
61 comment.valid = False |
68 |
62 |
69 # id, initial_start, initial_end, computed_start, computed_end, valid = self.computationResults[i] |
63 # id, initial_start, initial_end, computed_start, computed_end, valid = self.computationResults[i] |
70 |
64 |
71 for c in commentList: |
65 for cc in commentList: |
72 if c.valid: |
66 if cc.valid: |
73 for id in xrange(len(span_starts_new.keys())): |
67 for id in xrange(len(span_starts_new.keys())): |
74 start = span_starts_new.get(id) |
68 start = span_starts_new.get(id) |
75 end = span_starts_new.get(id+1, sys.maxint) |
69 end = span_starts_new.get(id+1, sys.maxint) |
76 |
70 |
77 # adjust start |
71 # adjust start |
78 if c.computed_start_offset >= start and c.computed_start_offset < end: |
72 if cc.computed_start_offset >= start and cc.computed_start_offset < end: |
79 c.start_wrapper = id |
73 cc.start_wrapper = id |
80 c.start_offset = c.computed_start_offset - start |
74 cc.start_offset = cc.computed_start_offset - start |
81 |
75 |
82 # adjust end |
76 # adjust end |
83 if c.computed_end_offset >= start and c.computed_end_offset < end: |
77 if cc.computed_end_offset >= start and cc.computed_end_offset < end: |
84 c.end_wrapper = id |
78 cc.end_wrapper = id |
85 c.end_offset = c.computed_end_offset - start |
79 cc.end_offset = cc.computed_end_offset - start |
86 |
80 |
87 # returns to_modify, to_remove |
81 # returns to_modify, to_remove |
88 return [c for c in commentList if c.valid], \ |
82 return [c for c in commentList if c.valid], \ |
89 [c for c in commentList if not c.valid] |
83 [c for c in commentList if not c.valid] |
90 |
84 |