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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
#!/usr/bin/env python3
"""
Fix changelog markdown by wrapping "special links" in brackets,
and by ensuring that lists of special links at the end of a paragraph
are parenthesized and comma-separated.
Ensures that each of these parenthesized links is preceded with
appropriate punctuation, but not followed by a period.
A special link is an MR link (like "!1234"), an issue link (like "#1234"),
or a commit link (like "1234ABCD").
"""
# TODO: When you find yourself inclined to modify this, add more tests!
import re
import sys
from collections.abc import Generator
# Regular expression: Matches a single "special link",
# possibly wrapped in brackets.
#
# There are 3 forms of special link:
# MR (!1234), issue (#1245), and commit (1234ABCDE)
#
# (Note that in practice, insisting on 7 characters for a commit link
# is enough to ensure that we don't get false positives.)
LINK_RE = re.compile(
r"""
# Negative lookbehind assertion: Not preceded by a nonspace.
(?<!\S)
(\[?)
( \! \d+ | \# \d+ | [\da-fA-F]{7,40} )
(\]?)
""",
re.VERBOSE | re.DOTALL,
)
# Regular expression: matches zero or more "special links" at the end of a
# string, possibly wrapped in parentheses.
TRAILING_LINKS_RE = re.compile(
r"""
# Pre-parenthesis punctuation, followed by spaces.
# (We accept any sentence-ending punctuation, followed optionally
# by a `)` or `"`, since sentence-ending punctuation can go inside
# a parenthetical or quotation.)
([\.\?\!][\)\"]?)?
(\s*)
# Optional opening parenthesis.
\(?
((?:
\[
[^\]]+
\]
,? \s*
)+)
# Optionally: trailing parenthesis, maybe followed by a bogus period.
(?: \) [\ ]* \.? )?
# Trailing space - typically newlines.
(\s*)
$
""",
re.VERBOSE | re.DOTALL,
)
# Regular expression: Used to split a list of "special links"
DIVIDER_RE = re.compile(
r"""
\[ [^\]]* \]
""",
re.VERBOSE | re.DOTALL,
)
# Regular expression: used to match spaces only.
ANY_SPACES = re.compile(
r"""
\s+
""",
re.VERBOSE | re.DOTALL,
)
# Things that look like links, but are not.
NONLINKS = set(item.lower() for item in ["ed25519"])
def bracket_links(s: str) -> str:
"""
Wrap all of our the special link expressions expressions found in `s`
with [brackets].
>>> bracket_links("Hello world. !123 #456 abcdef12345")
'Hello world. [!123] [#456] [abcdef12345]'
>>> bracket_links("https://gitlab.torproject.org/abcdef12345")
'https://gitlab.torproject.org/abcdef12345'
"""
def add_brackets(m: re.Match):
in_brackets = m.group(1) and m.group(3)
text = m.group(2)
if not in_brackets and text.lower() in NONLINKS:
return text
else:
return "[{}]".format(text)
return LINK_RE.sub(add_brackets, s)
def really_changed(s1: str, s2: str) -> bool:
"""
Return true if s1 and s2 have changes other than in nonempty
sequences of spaces.
"""
s1 = ANY_SPACES.subn(" ", s1)[0]
s2 = ANY_SPACES.subn(" ", s2)[0]
return s1 != s2
def parenthesize_links(s: str) -> str:
"""
Ensure that the trailing bracketed special links in `s` are
wrapped with (parens) and (separated, with, commas).
Also removes any period-after-parentheses,
and ensures that there is punctuation before the parentheses.
>>> parenthesize_links("Hello [!234] world. [!123] [#456] [abcdef12345]")
'Hello [!234] world. ([!123], [#456], [abcdef12345])'
>>> parenthesize_links("Hello [!234] world. ([!123] [#456] [abcdef12345])")
'Hello [!234] world. ([!123], [#456], [abcdef12345])'
"""
def add_parens_and_commas(m: re.Match):
punc = m.group(1)
sp1 = m.group(2)
links = m.group(3)
sp2 = m.group(4)
links = [link.group(0) for link in DIVIDER_RE.finditer(links)]
if links:
if not punc:
punc = "."
links = "({})".format(", ".join(links))
return f"{punc}{sp1}{links}{sp2}"
else:
return m.group(0)
outcome = TRAILING_LINKS_RE.subn(add_parens_and_commas, s, 1)[0]
if really_changed(s, outcome):
return outcome
else:
return s
def paragraphs(s: str) -> Generator[str]:
r"""
Divide `s` into "paragraphs". A paragraph can be indicated by
a markdown bullet item starting with `- `, or with a blank line
separating it from other paragraphs.
>>> s = "a\nb\nc\n\nd\ne\nf"
>>> list(paragraphs(s))
['a\nb\nc\n\n', 'd\ne\nf\n']
>>> s = "a\n- b.\n- c\nd\n"
>>> list(paragraphs(s))
['a\n', '- b.\n', '- c\n', 'd\n\n']
"""
cur = []
graf_type = "none"
for line in s.split("\n"):
if line.strip() == "":
cur.append(line + "\n")
yield "".join(cur)
cur = []
graf_type = "none"
elif line.startswith("- "):
if cur:
yield "".join(cur)
cur = [line + "\n"]
graf_type = "list_item"
elif line.startswith(" "):
cur.append(line + "\n")
else:
if graf_type == "list_item":
if cur:
yield "".join(cur)
cur = []
graf_type = "other"
cur.append(line + "\n")
if cur:
yield "".join(cur)
def process(s: str) -> str:
"""
Perform all of this script's operations (q.v. toplevel docstring)
on a given string, and return the result.
"""
output = []
# for item in paragraphs(s):
# print(parenthesize_links(bracket_links(item)))
for item in paragraphs(s):
output.append(parenthesize_links(bracket_links(item)))
while output[-1] == "\n":
output = output[:-1]
return "".join(output)
def run():
import argparse
parser = argparse.ArgumentParser(
prog="format_md_links", description="format markdown links in our changelog"
)
parser.add_argument("filename")
parser.add_argument(
"--check",
action="store_true",
help="check that everything is up-to-date; make no changes",
)
args = parser.parse_args()
text = open(args.filename).read()
output = process(text)
if args.check:
if text == output:
print("No changes")
else:
print("Output would change; re-run without --check to make the changes")
sys.exit(1)
else:
with open(args.filename, "w") as out:
out.write(output)
if __name__ == "__main__":
run()
|