From 54ba90aa3d4830b6321ce6b2f1efe65bc6d3caf8 Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Fri, 19 Feb 2016 13:36:12 +0000 Subject: syntax-highlighting: Escape HTML special characters correctly This fixes an issue where some .morph files wouldn't display correctly, because they'd contain a < character and the browser would think that this was a tag. I've added some docstrings as well. Change-Id: I3c7252319a06cac04880f8b20596003fde531609 --- share/baserock_definitions_cgit_filter.py | 33 ++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/share/baserock_definitions_cgit_filter.py b/share/baserock_definitions_cgit_filter.py index 03d3020..ff2ead9 100755 --- a/share/baserock_definitions_cgit_filter.py +++ b/share/baserock_definitions_cgit_filter.py @@ -156,7 +156,16 @@ class YAMLAnnotatedLoader(yaml.reader.Reader, yaml.scanner.Scanner, class TagAnnotation(): - '''Generic class for adding markup to plain text.''' + '''Generic class for adding markup to plain text. + + For example, if you have the string 'Hello world' and you want to + add HTML tags to 'world' to make it bold, you could do: + + text = 'Hello world' + annotation = TagAnnotation(6, 10, '', '') + result = apply_tag_annotations(text, [annotation]) + + ''' def __init__(self, start_index, end_index, start_text, end_text): self.start_index = start_index self.end_index = end_index @@ -164,7 +173,16 @@ class TagAnnotation(): self.end_text = end_text -def apply_tag_annotations(input_text, annotations, index_offset=0): +identity = lambda arg: arg +def apply_tag_annotations(input_text, annotations, escape_fn=identity, + index_offset=0): + '''Apply a list of TagAnnotation objects to some input text. + + This function can be used to add HTML annotations to text. If you are + doing this, you should ensure that the input text is quoted, by + passing `escape_fn=html.escape`. + + ''' def annotations_with_range(annotations_to_filter, start_index, end_index): return [a for a in annotations_to_filter if a.start_index >= start_index and a.end_index <= end_index] @@ -185,13 +203,13 @@ def apply_tag_annotations(input_text, annotations, index_offset=0): for a in sub_annotations: annotations.remove(a) - start_text = input_text[previous_pos: - top_annotation.start_index - index_offset] + start_text = escape_fn( + input_text[previous_pos:top_annotation.start_index - index_offset]) sub_input = input_text[top_annotation.start_index - index_offset: top_annotation.end_index - index_offset] sub_output = apply_tag_annotations( - sub_input, sub_annotations, + sub_input, sub_annotations, escape_fn=escape_fn, index_offset=top_annotation.start_index) segments.extend([start_text, top_annotation.start_text, sub_output, @@ -199,7 +217,7 @@ def apply_tag_annotations(input_text, annotations, index_offset=0): previous_pos = top_annotation.end_index - index_offset - end_text = input_text[previous_pos:] + end_text = escape_fn(input_text[previous_pos:]) return ''.join(segments + [end_text]) @@ -287,7 +305,8 @@ class HyperlinkAddition(): for system in morph.get('systems', []): html_annotations.extend(self.format_systemref(system)) - return apply_tag_annotations(input_text, html_annotations) + return apply_tag_annotations( + input_text, html_annotations, escape_fn=html.escape) def annotate_link(self, yaml_annotated_object, link_target): return TagAnnotation(yaml_annotated_object.start_mark.index, -- cgit v1.2.1