Skip to content

Differ

hypha.apply.funds.differ

wrap_deleted

wrap_deleted(text)
Source code in hypha/apply/funds/differ.py
def wrap_deleted(text):
    return format_html(
        '<span class="bg-red-200 line-through">{}</span>', mark_safe(text)
    )

wrap_added

wrap_added(text)
Source code in hypha/apply/funds/differ.py
def wrap_added(text):
    return format_html('<span class="bg-green-200">{}</span>', mark_safe(text))

compare

compare(answer_a, answer_b, should_bleach=True)
Source code in hypha/apply/funds/differ.py
def compare(answer_a, answer_b, should_bleach=True):
    if should_bleach:
        cleaner = Cleaner(tags=["h4"], attributes={}, strip=True)
        answer_a = re.sub("(<li[^>]*>)", r"\1◦ ", answer_a)
        answer_b = re.sub("(<li[^>]*>)", r"\1◦ ", answer_b)
        answer_a = cleaner.clean(answer_a)
        answer_b = cleaner.clean(answer_b)

    diff = SequenceMatcher(None, answer_a, answer_b)
    from_diff = []
    to_diff = []
    for opcode, a0, a1, b0, b1 in diff.get_opcodes():
        if opcode == "equal":
            from_diff.append(mark_safe(diff.a[a0:a1]))
            to_diff.append(mark_safe(diff.b[b0:b1]))
        elif opcode == "insert":
            from_diff.append(mark_safe(diff.a[a0:a1]))
            to_diff.append(wrap_added(diff.b[b0:b1]))
        elif opcode == "delete":
            from_diff.append(wrap_deleted(diff.a[a0:a1]))
            to_diff.append(mark_safe(diff.b[b0:b1]))
        elif opcode == "replace":
            from_diff.append(wrap_deleted(diff.a[a0:a1]))
            to_diff.append(wrap_added(diff.b[b0:b1]))

    from_display = "".join(from_diff)
    to_display = "".join(to_diff)
    from_display = re.sub("(\\.\n)", r"\1<br><br>", from_display)
    to_display = re.sub("(\\.\n)", r"\1<br><br>", to_display)
    from_display = re.sub(r"([◦])", r"<br>\1", from_display)
    to_display = re.sub(r"([◦])", r"<br>\1", to_display)
    from_display = mark_safe(from_display)
    to_display = mark_safe(to_display)

    return (from_display, to_display)