Esempio n. 1
0
 def mark_tags_in_text(text, matches):
     '''
     produce a text with the tags written as markup
     :param text: text to tags
     :param matches: tags to encode
     :return:
     '''
     text_to_tag = text
     tagged_abstract = ''
     if isinstance(text, str):
         text_to_tag = text
     try:
         tagged_abstract = ChangeCollector(text_to_tag)
         for i, tag in enumerate(
                 sorted(matches, key=lambda x: (x['start'], -x['end']))):
             if isinstance(tag['reference'], (list, tuple)):
                 tag_reference = '|'.join(tag['reference'])
             else:
                 tag_reference = tag['reference']
             tagged_abstract.add_change(
                 tag['start'], tag['start'],
                 '<mark-%s data-entity="%s" reference-db="%s"  reference="%s">'
                 % (str(i), tag['category'], tag['reference_db'],
                    tag_reference))
             tagged_abstract.add_change(tag['end'], tag['end'],
                                        '</mark-%s>' % str(i))
         tagged_abstract = '<div  class="entities">%s</div></br>' % tagged_abstract.get_changed(
         )
     except UnicodeDecodeError:
         logging.error(
             'cannot generate maked text for unicode decode error')
     return tagged_abstract
Esempio n. 2
0
 def substitute(self, mapping):
     collector = ChangeCollector(self.template)
     for name, occurrences in self.names.items():
         for region in occurrences:
             original = mapping[name]
             result = self.twist(eval(original))
             collector.add_change(region[0], region[1], result)
     result = collector.get_changed()
     if result is None:
         return self.template
     return result
Esempio n. 3
0
 def substitute(self, mapping):
     collector = ChangeCollector(self.template)
     for name, occurrences in self.names.items():
         for region in occurrences:
             original = mapping[name]
             result = self.twist(eval(original))
             collector.add_change(region[0], region[1], result)
     result = collector.get_changed()
     if result is None:
         return self.template
     return result
	def print_code(self):
		region = self.node.region
		lines_min, lines_max = self.lines.get_line_number(region[0]), self.lines.get_line_number(region[1])
		
		lines_min -= 1
		if lines_min < 0:
			lines_min = 0

		lines_max += 2
		if lines_max > self.lines.length():
			lines_max = self.lines.length()

		min_offset = self.lines.get_line_start(lines_min)
		max_offset = self.lines.get_line_end(lines_max)

		code = self.lines.code[min_offset: max_offset]

		region_min = region[0] - min_offset
		region_max = region[1] - min_offset

		orig_offset = self.lines.get_line_start(self.node.lineno) + self.node.col_offset
		orig_offset = orig_offset - min_offset

		orig_f_offset = None
		if hasattr(self.node, 'f_lineno') and hasattr(self.node, 'f_offset'):
			try:
				orig_f_offset = self.lines.get_line_start(self.node.f_lineno) + self.node.f_offset
				orig_f_offset = orig_f_offset - min_offset
			except:
				pass

		rnd = lambda : ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(20))
		a1, a2, a3, a4, a5, a6 = tuple(rnd() for _ in range(6))

		t = {
			a1: u'<span style="background-color:#339999;">',
			a2: u'</span>',
			a3: u'<span style="background-color:#660000;">',
			a4: u'</span>',
			a5: u'<span style="background-color:#060f0f;">',
			a6: u'</span>',
			}

		c = ChangeCollector(code)
		c.add_change(region_min, region_min, a1)
		c.add_change(region_max, region_max, a2)
		c.add_change(orig_offset, orig_offset, a3)
		c.add_change(orig_offset+1, orig_offset+1, a4)

		if orig_f_offset:
			c.add_change(orig_f_offset, orig_f_offset, a5)
			c.add_change(orig_f_offset+1, orig_f_offset+1, a6)

		code = markupsafe.escape(code)

		rend_code = c.get_changed()

		for w, h in t.items():
			rend_code = rend_code.replace(w,h)			

		return rend_code