-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_building.py
More file actions
222 lines (177 loc) · 8.09 KB
/
message_building.py
File metadata and controls
222 lines (177 loc) · 8.09 KB
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
from __future__ import annotations
from typing import Optional
from io import StringIO
from string import Template
from urllib.parse import urlencode
from enum import Enum, auto
from redditwarp.util.base_conversion import to_base36
from .feature_extraction import FeatureFlags
class MessagePartsStaticNamepace:
code_outside_of_code_block_message = '''\
Looks like your PowerShell code isn’t wrapped in a code block.
To properly style code on **[new Reddit][new.reddit.com]**, highlight the code and
choose ‘Code Block’ from the editing toolbar.
If you’re on **[old Reddit][old.reddit.com]**, separate the code from your text with
a blank line gap and precede each line of code with **4 spaces** or a **tab**.
[old.reddit.com]: https://old.reddit.com${permalink_path}
[new.reddit.com]: https://new.reddit.com${permalink_path}
'''
code_outside_of_code_block_template = Template(code_outside_of_code_block_message)
some_code_outside_of_code_block_message = '''\
Some of your PowerShell code isn’t enclosed in a code block.
To properly style code on **[new Reddit][new.reddit.com]**, highlight the code and
choose ‘Code Block’ from the editing toolbar.
If you’re on **[old Reddit][old.reddit.com]**, separate the code from your text with
a blank line gap and precede each line of code with **4 spaces** or a **tab**.
[old.reddit.com]: https://old.reddit.com${permalink_path}
[new.reddit.com]: https://new.reddit.com${permalink_path}
'''
some_code_outside_of_code_block_template = Template(some_code_outside_of_code_block_message)
multiline_inline_code_message = '''\
It appears that you have used *inline code* formatting when a **code block**
should have been used.
Consider using a code block for longer sequences of code.
To correct the formatting, highlight your code then click the ‘Code Block’
button in the editing toolbar.
'''
multiline_inline_code_template = Template(multiline_inline_code_message)
very_long_inline_code_message = '''\
That’s a really long line of inline code.
On **[old Reddit][old.reddit.com]** inline code blocks do not word wrap, making it
difficult for many of us to see all your code.
To ensure your code is readable by everyone, on **[new Reddit][new.reddit.com]**,
highlight your code and select ‘Code Block’ in the editing toolbar.
If you’re on **[old Reddit][old.reddit.com]**, separate the code from your text with
a blank line gap and precede each line of code with **4 spaces** or a **tab**.
[old.reddit.com]: https://old.reddit.com${permalink_path}
[new.reddit.com]: https://new.reddit.com${permalink_path}
'''
very_long_inline_code_template = Template(very_long_inline_code_message)
code_fences_message = '''\
Code fences are a **[new Reddit][new.reddit.com]** feature and won’t render for
those viewing your post on **[old Reddit][old.reddit.com]**.
If you want those viewing from old Reddit to see your PowerShell code formatted
correctly then consider using a regular space-indented **code block**. This can
be easily be done on **[new Reddit][new.reddit.com]** by highlighting your code
and selecting ‘Code Block’ in the editing toolbar.
[old.reddit.com]: https://old.reddit.com${permalink_path}
[new.reddit.com]: https://new.reddit.com${permalink_path}
'''
code_fences_template = Template(code_fences_message)
thematic_break = '\n-----\n\n'
def build_body_message_part(template: Template, permalink_path: str) -> str:
return template.substitute({
'permalink_path': permalink_path,
})
def build_pester_message_part(
determiner: MessageDeterminer,
enlightened: bool,
thing: str,
completed_in: int,
) -> str:
sign = '-'
symbol = '\N{CROSS MARK}'
if enlightened:
sign = '+'
symbol = '\N{WHITE HEAVY CHECK MARK}'
elif determiner in {
MessageDeterminer.CODE_FENCES,
MessageDeterminer.SOME_CODE_OUTSIDE_OF_CODE_BLOCK,
}:
sign = '~'
symbol = '\N{WARNING SIGN}\N{VARIATION SELECTOR-16}'
return f'''\
Describing {thing}
[{sign}] Well formatted
Tests completed in {completed_in}ms
Tests Passed: {symbol}
'''
def build_enlightenment_message_part(fraction: tuple[int, int]) -> str:
length = 20
fill_length = int(length * (fraction[0]/fraction[1]))
bar = fill_length*'\N{FULL BLOCK}' + (length - fill_length)*'-'
symbol = '\N{CROSS MARK}'
if fraction[0] == fraction[1]:
symbol = '\N{WHITE HEAVY CHECK MARK}'
elif fraction[0] != 0:
symbol = '\N{WARNING SIGN}\N{VARIATION SELECTOR-16}'
return '''\
You examine the path beneath your feet...
[AboutRedditFormatting]: [{}] {} {}
'''.format(bar, '%d/%d' % fraction, symbol)
def build_footer_message_part(submission_id: int, username: str) -> str:
submission_id36 = to_base36(submission_id)
message = '''\
Click ‘send’ to immediately delete the bot’s comment.
The comment will not be deleted if:
* You are not the submitter of the submission.
* There are any replies on the comment.
'''
query_params = {
'to': username,
'subject': f'!delete {submission_id36}',
'message': message,
}
deletion_form_url = 'https://www.reddit.com/message/compose?' + urlencode(query_params)
return f'''\
 ^(*Beep-boop, I am a bot.* | [Remove-Item])
[Remove-Item]: {deletion_form_url}
'''
class MessageDeterminer(Enum):
CODE_FENCES = auto()
SOME_CODE_OUTSIDE_OF_CODE_BLOCK = auto()
CODE_OUTSIDE_OF_CODE_BLOCK = auto()
MULTILINE_INLINE_CODE = auto()
VERY_LONG_INLINE_CODE = auto()
def get_message_determiner(feature_flags: int) -> Optional[MessageDeterminer]:
if (feature_flags & (mask := FeatureFlags.CODE_OUTSIDE_OF_CODE_BLOCK | FeatureFlags.CODE_FENCE)) == mask:
return MessageDeterminer.CODE_FENCES
elif (feature_flags & (mask := FeatureFlags.CODE_OUTSIDE_OF_CODE_BLOCK | FeatureFlags.CODE_BLOCK)) == mask:
return MessageDeterminer.SOME_CODE_OUTSIDE_OF_CODE_BLOCK
elif feature_flags & FeatureFlags.CODE_OUTSIDE_OF_CODE_BLOCK:
return MessageDeterminer.CODE_OUTSIDE_OF_CODE_BLOCK
elif feature_flags & FeatureFlags.MULTILINE_INLINE_CODE:
return MessageDeterminer.MULTILINE_INLINE_CODE
elif feature_flags & FeatureFlags.VERY_LONG_INLINE_CODE:
return MessageDeterminer.VERY_LONG_INLINE_CODE
return None
def build_message(
*,
determiner: MessageDeterminer,
enlightened: bool,
submission_id: int,
permalink_path: str,
username: str,
submission_body_len: int,
) -> str:
sio = StringIO()
template = {
MessageDeterminer.CODE_FENCES: MessagePartsStaticNamepace.code_fences_template,
MessageDeterminer.SOME_CODE_OUTSIDE_OF_CODE_BLOCK: MessagePartsStaticNamepace.some_code_outside_of_code_block_template,
MessageDeterminer.CODE_OUTSIDE_OF_CODE_BLOCK: MessagePartsStaticNamepace.code_outside_of_code_block_template,
MessageDeterminer.MULTILINE_INLINE_CODE: MessagePartsStaticNamepace.multiline_inline_code_template,
MessageDeterminer.VERY_LONG_INLINE_CODE: MessagePartsStaticNamepace.very_long_inline_code_template,
}[determiner]
sio.write(build_body_message_part(template, permalink_path))
# '''
sio.write(MessagePartsStaticNamepace.thematic_break)
sio.write(build_pester_message_part(
determiner=determiner,
enlightened=enlightened,
thing=permalink_path.strip('/').rpartition('/')[-1],
completed_in=submission_body_len,
))
'''
fraction = {
MessageDeterminer.CODE_FENCES: (2, 2) if enlightened else (1, 2),
MessageDeterminer.SOME_CODE_OUTSIDE_OF_CODE_BLOCK: (3, 3) if enlightened else (2, 3),
MessageDeterminer.CODE_OUTSIDE_OF_CODE_BLOCK: (1, 1) if enlightened else (0, 1),
MessageDeterminer.MULTILINE_INLINE_CODE: (1, 1) if enlightened else (0, 1),
MessageDeterminer.VERY_LONG_INLINE_CODE: (1, 1) if enlightened else (0, 1),
}[determiner]
sio.write(MessagePartsStaticNamepace.thematic_break)
sio.write(build_enlightenment_message_part(fraction))
'''#'''
sio.write(MessagePartsStaticNamepace.thematic_break)
sio.write(build_footer_message_part(submission_id, username))
return sio.getvalue()