~sircmpwn/lists.sr.ht

ref: 15f7d0772ced4f8c7c3712bc4b7c7822b26be2b1 lists.sr.ht/listssrht/filters.py -rw-r--r-- 5.5 KiB View raw
15f7d077Drew DeVault filters.py: format_body: parse patch 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from jinja2 import Markup, escape
from jinja2.filters import urlize
from srht.config import cfg

def post_address(ml, suffix=""):
    if ml.mirror_id:
        if suffix == "+subscribe":
            return ml.mirror.list_subscribe
        elif suffix == "+unsubscribe":
            return ml.mirror.list_unsubscribe
        elif suffix == "":
            return ml.mirror.list_post
        else:
            return None

    domain = cfg("lists.sr.ht", "posting-domain")
    return "{}/{}{}@{}".format(
            ml.owner.canonical_name, ml.name, suffix, domain)

def _format_patch(msg, limit=None):
    text = Markup("")
    is_diff = False

    # Predict the starting lines of each file name
    patch = msg.patch()
    old_files = {delta.old_file.path for delta in patch.deltas}
    new_files = {delta.new_file.path for delta in patch.deltas}
    file_lines = {
        f" {p} ": p for p in old_files | new_files
    }
    line_no = 0

    for line in msg.body.replace("\r", "").split("\n"):
        line_no += 1
        if line_no == limit:
            text = text.rstrip()
            text += Markup(
                "\n<span class='text-muted'>[message trimmed]</span>"
            )
            break
        if not is_diff:
            f = next((
                key for key in file_lines.keys() if line.startswith(key)
            ), None)
            if f != None:
                f = file_lines[f]
                text += Markup(" <a href='#{}'>{}</a>".format(
                    escape(msg.message_id) + "+" + escape(f), escape(f)))
                try:
                    stat = line[line.rindex(" ") + 1:]
                    line = line[:line.rindex(" ") + 1]
                    if "+" in stat and "-" in stat:
                        removed = stat[stat.index("-"):]
                        added = stat[:stat.index("-")]
                        stat = Markup(("<span class='text-success'>{}</span>" +
                            "<span class='text-danger'>{}</span>"
                        ).format(escape(added), escape(removed)))
                    elif "-" in stat:
                        stat = Markup(
                                "<span class='text-danger'>{}</span>".format(
                                    escape(stat)))
                    elif "+" in stat:
                        stat = Markup(
                                "<span class='text-success'>{}</span>".format(
                                    escape(stat)))
                    else:
                        stat = escape(stat)
                except ValueError:
                    stat = Markup("")
                text += escape(line[len(f) + 1:])
                text += escape(stat)
                text += Markup("\n")
            else:
                text += escape(line + "\n")
            if line.startswith("diff"):
                is_diff = True
        else:
            if line.strip() == "--":
                text += escape(line + "\n")
            elif line.startswith("+++"):
                path = line[4:].lstrip("b/")
                if f" {path} " in file_lines:
                    text += (
                        Markup("<a href='#{0}' id='{0}' class='text-info'>".format(
                            escape(msg.message_id) + "+" + escape(path)
                        ))
                        + escape(line)
                        + Markup("</a>\n"))
                    continue
            elif line.startswith("---"):
                text += (
                    Markup("<span class='text-info'>")
                    + escape(line)
                    + Markup("</span>\n"))
                continue
            elif line.startswith("+"):
                text += (
                    Markup("<span class='text-success'>")
                    + Markup(
                        ("<a class='text-success' href='#{0}-{1}' " +
                        "id='{0}-{1}'>+</a>").format(
                            escape(msg.message_id), line_no))
                    + escape(line[1:])
                    + Markup("</span>\n"))
            elif line.startswith("-"):
                text += (
                    Markup("<span class='text-danger'>")
                    + Markup(
                        ("<a class='text-danger' href='#{0}-{1}' " +
                        "id='{0}-{1}'>-</a>").format(
                            escape(msg.message_id), line_no))
                    + escape(line[1:])
                    + Markup("</span>\n"))
            elif line.startswith(" "):
                text += (
                    Markup("<a href='#{0}-{1}' id='{0}-{1}'> </a>".format(
                            escape(msg.message_id), line_no))
                    + escape(line[1:] + "\n"))
            else:
                text += escape(line + "\n")
    return text.rstrip()

def format_body(msg, limit=None):
    if msg.patch != None:
        return _format_patch(msg, limit)
    text = Markup("")
    line_no = 0
    body = urlize(msg.body, rel="noopener nofollow")
    for line in msg.body.replace("\r", "").split("\n"):
        line_no += 1
        if line_no == limit:
            break
        if line.startswith(">"):
            text += (
                Markup("<span class='text-muted'>")
                    + Markup(urlize(escape(line), rel="noopener nofollow"))
                + Markup("</span>\n"))
        else:
            text += Markup(urlize(escape(line), rel="noopener nofollow")) + "\n"
    return text.rstrip()

def diffstat(patch_email):
    stats = patch_email.patch().stats
    return type("diffstat", tuple(), {
        "added": stats.insertions,
        "removed": stats.deletions,
    })