rb/noat.rb
# Returns a string containing the markup of the text content, with
# annotations inserted.
Returns a string containing the markup of the text content, with
annotations inserted.
Returns a string containing the markup of the text content with annotations inserted .
def _addTextAnnotations(text, annotations)
    annotation_index_by_start = {}
    annotation_index_by_end = {}
    for a in annotations do
        if not annotation_index_by_start.has_key?(a[:start])
            annotation_index_by_start[a[:start]] = []
        end
        annotation_index_by_start[a[:start]].push(a)
        if a[:start] != a[:end]
            if not annotation_index_by_end.has_key?(a[:end])
                annotation_index_by_end[a[:end]] = []
            end
            annotation_index_by_end[a[:end]].push(a)
        end
    end
    segment_boundaries = annotation_index_by_end.keys() + annotation_index_by_start.keys()
    segment_boundaries.sort!()
    if segment_boundaries.length == 0 or segment_boundaries[0] != 0
        segment_boundaries.insert(0,0)
    end
    if segment_boundaries[-1] != text.length
        segment_boundaries.push(text.length)
    end
    segments = []
    segment_boundaries[0...segment_boundaries.length-1].each_index do |i|
        s_start = segment_boundaries[i]
        s_end = segment_boundaries[i+1]
        if s_start != s_end
            segments.push(text[s_start...s_end])
        end
    end
    if segments.length == 0
        segments.push('')
    end
    output = ''
    open_tags = []
    i = 0
    for seg_text in segments
        tags_to_open = annotation_index_by_start.fetch(i, [])
        tags_to_close = annotation_index_by_end.fetch(i, [])
        tags_to_reopen = []
        for t in tags_to_close
            while open_tags.length > 0 and t[:tag] != open_tags[-1][:tag]
                o_tag = open_tags.pop()
                output += _closeTag(o_tag)
                tags_to_reopen.push(o_tag)
            end
            output += _closeTag(t)
            open_tags.pop()
            while tags_to_reopen.length > 0
                o_tag = tags_to_reopen.pop()
                output += _openTag(o_tag)
                open_tags.push(o_tag)
            end
        end
        for t in tags_to_open
            output += _openTag(t)
            if t[:start] != t[:end]
                open_tags.push(t)
            else
                output += _closeTag(t)
            end
        end
        output += CGI::escapeHTML(seg_text)
        i += seg_text.length
    end
    while open_tags.length > 0
        o_tag = open_tags.pop()
        output += _closeTag(o_tag)
    end
    return output
end
def _addTextAnnotations ( text , annotations ) annotation_index_by_start = { } annotation_index_by_end = { } for a in annotations do if not annotation_index_by_start . has_key? ( a [ :start ] ) annotation_index_by_start [ a [ :start ] ] = [ ] end annotation_index_by_start [ a [ :start ] ] . push ( a ) if a [ :start ] != a [ :end ] if not annotation_index_by_end . has_key? ( a [ :end ] ) annotation_index_by_end [ a [ :end ] ] = [ ] end annotation_index_by_end [ a [ :end ] ] . push ( a ) end end segment_boundaries = annotation_index_by_end . keys ( ) + annotation_index_by_start . keys ( ) segment_boundaries . sort! ( ) if segment_boundaries . length == 0 or segment_boundaries [ 0 ] != 0 segment_boundaries . insert ( 0 , 0 ) end if segment_boundaries [ - 1 ] != text . length segment_boundaries . push ( text . length ) end segments = [ ] segment_boundaries [ 0 ... segment_boundaries . length - 1 ] . each_index do | i | s_start = segment_boundaries [ i ] s_end = segment_boundaries [ i + 1 ] if s_start != s_end segments . push ( text [ s_start ... s_end ] ) end end if segments . length == 0 segments . push ( '' ) end output = '' open_tags = [ ] i = 0 for seg_text in segments tags_to_open = annotation_index_by_start . fetch ( i , [ ] ) tags_to_close = annotation_index_by_end . fetch ( i , [ ] ) tags_to_reopen = [ ] for t in tags_to_close while open_tags . length > 0 and t [ :tag ] != open_tags [ - 1 ] [ :tag ] o_tag = open_tags . pop ( ) output += _closeTag ( o_tag ) tags_to_reopen . push ( o_tag ) end output += _closeTag ( t ) open_tags . pop ( ) while tags_to_reopen . length > 0 o_tag = tags_to_reopen . pop ( ) output += _openTag ( o_tag ) open_tags . push ( o_tag ) end end for t in tags_to_open output += _openTag ( t ) if t [ :start ] != t [ :end ] open_tags . push ( t ) else output += _closeTag ( t ) end end output += CGI :: escapeHTML ( seg_text ) i += seg_text . length end while open_tags . length > 0 o_tag = open_tags . pop ( ) output += _closeTag ( o_tag ) end return output end
Returns a string containing the markup of the text content, with
annotations inserted.
Returns a string containing the markup of the text content with annotations inserted .
# Index annotations by their start and end positions. # Find the segment boundaries of text, as bounded by opening and closing # tags (equivalent to the text nodes in the HTML DOM). # Make sure the segments include the beginning and end of the text. # Always have at least one segment, even if empty, to allow for adding # annotations to empty strings. # Work back up the stack of open tags until the annotation to be # closed is found, closing the open tags in order and saving them # for reopening. (This should raise an IndexError if there aren't # any open tags, which should not be true at this point.) # Close the annotation. # Reopen annotations that were closed to prevent overlap. # Open the tags that start at this point. # Unless the tag also closes at this point, add it to the stack of # open tags. Otherwise, close it. # Add the escaped segment text content, but track the length of the # original text to preserve annotation positions. # Close any tags that are still open (should only be any that are set to # end at the end of the target string).
text
<not_specific>
annotations
<not_specific>