-
Notifications
You must be signed in to change notification settings - Fork 509
/
htmlgen.py
154 lines (132 loc) · 5.1 KB
/
htmlgen.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
# -*- Mode: Python; tab-width: 4; indent-tabs-mode: nil; coding: utf-8; -*-
# vim:set ft=python ts=4 sw=4 sts=4 autoindent:
'''
Server-side HTML generation-related functionality for
Brat Rapid Annotation Tool (brat)
'''
from cgi import escape as cgi_escape
from projectconfig import get_entity_type_hierarchy, get_event_type_hierarchy
def escape(s):
return cgi_escape(s).replace('"', '"');
def __generate_input_and_label(t, keymap, indent, disabled):
l = []
nst = t.replace(" ","_")
if not disabled:
dstr = ""
else:
dstr = ' disabled="disabled"'
s = indent+' <input id="span_%s" name="span_type" type="radio" value="%s" %s/>' % (escape(nst.lower()),escape(nst),dstr)
s += '<label for="span_%s">' % escape(nst.lower())
# TODO: saner case/space-vs-underscore-insensitive processing
kmt = t.lower().replace(" ", "_")
if kmt not in keymap or kmt.find(keymap[kmt].lower()) == -1:
s += '%s</label>' % escape(t)
else:
accesskey = keymap[kmt].lower()
key_offset= kmt.find(accesskey)
s += '%s<span class="accesskey">%s</span>%s</label>' % (escape(t[:key_offset]), escape(t[key_offset:key_offset+1]), escape(t[key_offset+1:]))
l.append(s)
return l
def __generate_node_html_lines(node, keymap, depth=0):
# TODO: make this less exceptional, avoid magic values
if node == "SEPARATOR":
return ["<hr/>"]
t = node.interface_term()
# for debugging
indent = " "*6*depth
lines = []
if len(node.children) == 0:
# simple item
lines.append(indent+'<div class="item">')
lines.append(indent+' <div class="item_content">')
lines += __generate_input_and_label(t, keymap, indent, node.unused)
lines.append(indent+' </div>')
lines.append(indent+'</div>')
else:
# collapsible item with children
lines.append(indent+'<div class="item">')
lines.append(indent+' <div class="collapser open"></div>')
lines.append(indent+' <div class="item_content">')
lines += __generate_input_and_label(t, keymap, indent, node.unused)
lines.append(indent+' <div class="collapsible open">')
for n in node.children:
lines += __generate_node_html_lines(n, keymap, depth+1)
lines.append(indent+' </div>')
lines.append(indent+' </div>')
lines.append(indent+'</div>')
return lines
def __generate_term_hierarchy_html(root_nodes, type_key_map):
all_lines = []
for n in root_nodes:
all_lines += __generate_node_html_lines(n, type_key_map)
return "\n".join(all_lines)
def generate_entity_type_html(directory, type_key_map):
hierarchy = get_entity_type_hierarchy(directory)
return __generate_term_hierarchy_html(hierarchy, type_key_map)
def generate_event_type_html(directory, type_key_map):
hierarchy = get_event_type_hierarchy(directory)
return __generate_term_hierarchy_html(hierarchy, type_key_map)
def generate_client_keymap(keyboard_shortcuts):
client_keymap = {}
for k in keyboard_shortcuts:
# Note: all keymap processing is case-insensitive and treats space
# and underscore ("_") interchangeably
client_keymap[k] = 'span_'+keyboard_shortcuts[k].lower().replace(" ", "_")
return client_keymap
def generate_textbound_type_html(directory, keyboard_shortcuts):
# Note: all keymap processing is case-insensitive and treats space
# and underscore ("_") interchangeably
type_to_key_map = {}
for k in keyboard_shortcuts:
type_to_key_map[keyboard_shortcuts[k].lower().replace(" ", "_")] = k.lower()
return """<fieldset>
<legend>Entities</legend>
<fieldset>
<legend>Type</legend>
<div class="type_scroller">
""" + generate_entity_type_html(directory, type_to_key_map) + """
</div>
</fieldset>
</fieldset>
<fieldset>
<legend>Events</legend>
<fieldset>
<legend>Type</legend>
<div class="type_scroller">
""" + generate_event_type_html(directory, type_to_key_map) + """</div>
</fieldset>
<fieldset id="span_mod_fset">
<legend>Modifications</legend>
<input id="span_mod_negation" type="checkbox" value="Negation"/>
<label for="span_mod_negation"><span class="accesskey">N</span>egation</label>
<input id="span_mod_speculation" type="checkbox" value="Speculation"/>
<label for="span_mod_speculation"><span class="accesskey">S</span>peculation</label>
</fieldset>
</fieldset>
"""
if __name__ == '__main__':
# debugging
keymap = {
'P': 'Protein',
'E': 'Entity',
'H': 'Hydroxylation',
'R': 'Dehydroxylation',
'O': 'Phosphorylation',
'S': 'Dephosphorylation',
'U': 'Ubiquitination',
'B': 'Deubiquitination',
'G': 'Glycosylation',
'L': 'Deglycosylation',
'A': 'Acetylation',
'T': 'Deacetylation',
'M': 'Methylation',
'Y': 'Demethylation',
'D': 'DNA_methylation',
'N': 'DNA_demethylation',
'C': 'Catalysis',
}
reverse_keymap = {}
for k in keymap:
reverse_keymap[keymap[k]] = k
print generate_event_type_html(".", reverse_keymap)