-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_html.py
90 lines (62 loc) · 2.13 KB
/
event_html.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def order_events(merge_events):
import heapq
event_heap = []
last_revision = None
for merge_event in merge_events:
for event in merge_event.events:
heapq.heappush(event_heap, (event.revision, event))
while event_heap:
revision, event = event_heap[0]
if last_revision and revision != last_revision + 1:
break
yield event
last_revision = revision
heapq.heappop(event_heap)
def sentinel_events(events, sentinel_event):
for event in events:
yield event
if event == sentinel_event:
break
def build_event_text(events, text = "\n"):
import text_events
for event in events:
event_data = text_events.text_apply_event(text, event.changeset_data)
text, event_changes, event_new_changes = event_data
return text
def format_text_sections(text):
import event_html_template as template
import re
import event_sections
def default_section(pattern, header, children = []):
return pattern, children
cases, sections = event_sections.sections
stack = list(sections)
while stack:
section = stack.pop()
pattern, children = default_section(*section)
stack.extend(children)
text = re.sub(pattern, template.section_template % "\g<0>", text)
return text
def format_event_text(text, merge_event):
import event_html_template as template
result_text = ""
p_start, p_end, p_text = merge_event.positive_changes
n_start, n_end, n_text = merge_event.negative_changes
positive_text = p_text and template.positive_changes_template % p_text
negative_text = n_text and template.negative_changes_template % n_text
if p_start < n_start:
result_text += text[:p_start]
result_text += positive_text
result_text += text[p_end:max(p_end, n_start)]
result_text += negative_text
result_text += text[max(p_end, n_start):]
else:
result_text += text[:n_start]
result_text += negative_text
result_text += text[n_start:p_start]
result_text += positive_text
result_text += text[p_end:]
for text_replace, text_template in template.text_templates:
result_text = result_text.replace(text_replace, text_template)
result_text = format_text_sections(result_text)
return template.content_template % result_text