@register.filter
def highlightregion(value, regions):
    if not regions:
        return value

    s = ''

    # We need to insert span tags into a string already consisting
    # of span tags. We have a list of ranges that our span tags should
    # go into, but those ranges are in the markup-less string.
    #
    # We go through the string and keep track of the location in the
    # markup and in the markup-less string. We make sure to insert our
    # span tag any time that we're within the current region, so long
    # as we haven't already created one. We close the span tag whenever
    # we're done with the region or when we're about to enter a tag in
    # the markup string.
    #
    # This code makes the assumption that the list of regions is sorted.
    # This is safe to assume in practice, but if we ever at some point
    # had reason to doubt it, we could always sort the regions up-front.
    in_hl = False
    i = j = r = 0
    region_start, region_end = regions[r]

    while i < len(value):
        c = value[i]

        if c == '<':
            if in_hl:
                s += '</span>'
                in_hl = False

            k = value.find('>', i)
            assert k != -1

            s += value[i:k + 1]
            i = k
        else:
            if not in_hl and region_start <= j < region_end:
                s += '<span class="hl">'
                in_hl = True

            if c == '&':
                k = value.find(';', i)
                assert k != -1

                s += value[i:k + 1]
                i = k
                j += 1
            else:
                j += 1
                s += c

        if j == region_end:
            if in_hl:
                s += '</span>'
                in_hl = False

            r += 1

            if r == len(regions):
                break

            region_start, region_end = regions[r]

        i += 1

    if i + 1 < len(value):
        s += value[i + 1:]

    return s
A new project can be created by users with staff privilege (is_staff=True) or customer owners.
dataset/ETHPy150Open reviewboard/reviewboard/reviewboard/diffviewer/templatetags/difftags.py/highlightregion--crossover with--dataset/ETHPy150Open opennode/nodeconductor/nodeconductor/structure/views.py/ProjectViewSet.create