-
Notifications
You must be signed in to change notification settings - Fork 9
/
render.py
496 lines (384 loc) · 16.2 KB
/
render.py
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
from cachetools.func import *
from cachy import stale_cache
import threading
import re
from commons_static import *
from colors import *
# extract non-markdown urls
url_regex = r'((((http|https|ftp):(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+(:\[0-9]+)?|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)(?![^<]*?(?:<\/\w+>|\/?>))(?![^\(]*?\))'
# dont use for now
# username rule
legal_chars = r'0-9a-zA-Z\u4e00-\u9fbb\-\_\.'
username_regex_proto = f'[{legal_chars}]' + r'{2,16}'
username_regex=r'^' + username_regex_proto + r'$'
username_regex_pgp = r'2047login#(' + username_regex_proto + r')#(.{19})'
username_regex_pgp_new = r'2047login##(.*?)##(.{19})'
username_regex_string = str(username_regex).replace('\\\\','\\')
tagname_regex_long = username_regex_proto.replace('2,16','1,40')
tagname_regex = username_regex_proto.replace('2,16','1,14')
at_extractor_regex = (r'(?:(?<=^)|(?<=[(),。,() *]))@([a-z]{2,16}?)(?=[^a-z]|$)').replace('a-z', legal_chars)
# @lru_cache(maxsize=4096)
def _extract_ats(s): # extract @usernames out of text
groups = re.findall(at_extractor_regex, s, flags=re.MULTILINE)
print(groups)
return [g[1] for g in groups]
def extract_ats(s):
html, collected = frend(s)
return collected['at_user_list']
# @lru_cache(maxsize=4096)
def replace_ats(s): # replace occurence
if s.startswith('<p>'):
return s
def f(match):
uname = match.group(2)
return match.group(1) + f'[@{uname}](/member/{uname})'
return re.sub(at_extractor_regex, f, s, flags=re.MULTILINE)
post_autolink_regex = r'</?[#p]/?([0-9]{1,16})>'
thread_autolink_regex = r'</?t/?([0-9]{1,16})>'
replace_autolink_regex = r'</?([pt])([0-9]{1,16})>'
def replace_tal_form(s):
def f(match):
type = match.group(1)
id = match.group(2)
return f'</{type}/{id}>'
return re.sub(replace_autolink_regex, f, s, flags=re.M)
# @lru_cache(maxsize=4096)
def replace_pal(s):
def f(match):
pid = match.group(1)
return '[#{}](/p/{})'.format(pid,pid)
return re.sub(post_autolink_regex, f, s, flags=re.MULTILINE)
# @lru_cache(maxsize=4096)
def replace_tal(s):
def f(match):
pid = match.group(1)
return '[t{}](/t/{})'.format(pid,pid)
return re.sub(thread_autolink_regex, f, s, flags=re.MULTILINE)
def replace_polls(s):
def f(match):
pollid = match.group(1)
return f'<div class="poll-instance-unprocessed" data-id="{pollid}"></div>'
return re.sub(r'(?=^)#poll(\d{1,16})(?=\s|\t|$)', f, s, flags=re.MULTILINE)
# match only youtube links that occupy a single line
youtube_extractor_regex = r'(?=\n|\r|^)(?:http|https|)(?::\/\/|)(?:www.|)(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[A-Za-z0-9;:@#?&%=+\/\$_.-]*(?=\n|$)'
# match those from 2049bbs
old_youtube_extractor_regex = r'<div class="videowrapper"><iframe src="https://www\.youtube\.com/embed/([\w\-]{11})" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div>'
youtube_extractor_regex_bugfix = r'<div class="youtube-player".*?data-id="(.*?)">.*?播放</a>'
# combined together
combined_youtube_extractor_regex = \
'(?:'+youtube_extractor_regex+'|'+old_youtube_extractor_regex+'|'\
+youtube_extractor_regex_bugfix + ')'
# @lru_cache(maxsize=4096)
def replace_ytb_f(match):
vid = match.group(1) or match.group(2) or match.group(3)
ts = None
if vid==match.group(1):
url = match.group(0)
# print('urlmatch', url)
timestamp_found = re.search(r'\?t=([0-9]{1,})', url)
if timestamp_found:
ts = timestamp_found.group(1)
ts = int(ts)
# print('timestamp', ts)
ts = ('?t='+str(ts)) if ts else ''
return f'''<a target="_blank" href="https://youtu.be/{vid}{ts}">https://youtu.be/{vid}{ts}</a><div class="youtube-player-unprocessed" data-id="{vid}" data-ts="{ts}"></div>'''.format(vid)
twitter_extractor_regex = r"^(https://twitter.com/[a-zA-Z0-9_]*?/status/\d+).*?$"
def replace_twitter_f(match):
turl = match.group(1)
k = f'''<a href="{turl}">{turl}</a><blockquote class="twitter-tweet" data-conversation="true" data-dnt="true"><a href="{turl}">{turl}</a></blockquote>'''
return k
def replace_soundcloud_f(match):
return f'''<iframe width="100%" height="166" scrolling="no" frameborder="no" allow="autoplay" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/993308041&color=%23ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true"></iframe><div style="font-size: 10px; color: #cccccc;line-break: anywhere;word-break: normal;overflow: hidden;white-space: nowrap;text-overflow: ellipsis; font-family: Interstate,Lucida Grande,Lucida Sans Unicode,Lucida Sans,Garuda,Verdana,Tahoma,sans-serif;font-weight: 100;"><a href="https://soundcloud.com/trinhcongdan" title="Trịnh Công Đan" target="_blank" style="color: #cccccc; text-decoration: none;">Trịnh Công Đan</a> · <a href="https://soundcloud.com/trinhcongdan/cafe-khong-duong-jombie-x-tkan-bean-official-music-audio" title="CAFE KHÔNG ĐƯỜNG || JOMBIE x TKAN & BEAN || OFFICIAL MUSIC AUDIO" target="_blank" style="color: #cccccc; text-decoration: none;">CAFE KHÔNG ĐƯỜNG || JOMBIE x TKAN & BEAN || OFFICIAL MUSIC AUDIO</a></div>'''
def replace_pincong(s):
def f(match):
return f'<mark class="parody">{match.group(1)}</mark>'
s = re.sub(r'(?<![>@/])((?:姨|桂|支|偽|伪|张献|張獻|韭|鹿)(?:葱|蔥)|(?:品|葱|蔥)韭)', f, s)
return s
# @lru_cache(maxsize=4096)
def replace_ytb(s):
s = re.sub(combined_youtube_extractor_regex,
replace_ytb_f, s, flags=re.MULTILINE)
return s
def replace_twitter(s):
s = re.sub(twitter_extractor_regex, replace_twitter_f, s, flags=re.M)
return s
# @lru_cache(maxsize=4096)
# def extract_ytb(s):
# groups = re.findall(combined_youtube_extractor_regex, s, flags=re.MULTILINE)
# return [g[0] or g[1] for g in groups]
@lru_cache(maxsize=4096)
def extract_ytb(s):
res, collected = frend(s)
return collected['youtube_players']
youtube_extraction_test_string="""
youtu.be/DFjD8iOUx0I
https://youtu.be/DFjD8iOUx0I
自动
youtu.be/DFjD8iOUx0I
http://youtu.be/dQw4w9WgXcQ
// https://www.youtube.com/embed/dQw4w9WgXcQ
// http://www.youtube.com/watch?v=dQw4w9WgXcQ
// http://www.youtube.com/?v=dQw4w9WgXcQ
// http://www.youtube.com/v/dQw4w9WgXcQ
// http://www.youtube.com/e/dQw4w9WgXcQ
// http://www.youtube.com/user/username#p/u/11/dQw4w9WgXcQ
// http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/dQw4w9WgXcQ
// http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ
//
http://www.youtube.com/?feature=player_embedded&v=dQw4w9WgXcQ
" https://youtu.be/yVpbFMhOAwE ",
"https://www.youtube.com/embed/yVpbFMhOAwE",
"youtu.be/yVpbFMhOAwE",
"youtube.com/watch?v=yVpbFMhOAwE",
"http://youtu.be/yVpbFMhOAwE",
"http://www.youtube.com/embed/yVpbFMhOAwE",
"http://www.youtube.com/watch?v=yVpbFMhOAwE",
"http://www.youtube.com/watch?v=yVpbFMhOAwE&feature=g-vrec",
"http://www.youtube.com/watch?v=yVpbFMhOAwE&feature=player_embedded",
好的" http://www.youtube.com/v/yVpbFMhOAwE?fs=1&hl=en_US ",
" http://www.youtube.com/ytscreeningroom?v=yVpbFMhOAwE ",
"http://www.youtube.com/watch?NR=1&feature=endscreen&v=yVpbFMhOAwE",
"http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo",
" http://www.youtube.com/watch?v=6zUVS4kJtrA&feature=c4-overview-vl&list=PLbzoR-pLrL6qucl8-lOnzvhFc2UM1tcZA ",
"
#真人献唱#
**《没有倒车档就没有庆丰国》**
<div class="videowrapper"><iframe src="https://www.youtube.com/embed/kfUx7Lv-az8" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div>
https://www.youtube.com/watch?v=FZu097wb8wU&list=RDFZu097wb8wU
"
youtu.be/DFjD8iOUx0I
"""
# markdown renderer
if 0:
import markdown
def convert_markdown(s):
return markdown.markdown(s)
elif 1:
import mistletoe
from html_stuff import parse_html, sanitize_html
# @lru_cache(maxsize=2048)
def just_markdown(s):
# s = replace_pal(s)
# s = replace_tal(s)
s = replace_tal_form(s)
# s = replace_ats(s)
# s = replace_pincong(s)
# s = replace_ytb(s)
# s = replace_twitter(s)
# s = replace_polls(s)
# html = mistletoe.markdown(s)
html, _collected = frend(s)
return html
@lru_cache(maxsize=4096)
def convert_markdown(s):
out = just_markdown(s)
try:
soup = parse_html(out)
sanitize_html(soup)
out = soup.decode()
except Exception as e:
print_err('failed to parse with bs4')
out = '(parse failure: check your HTML)'
return out
else:
import re
pattern = (
r'((((http|https|ftp):(?:\/\/)?)' # scheme
r'(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+(:\[0-9]+)?' # user@hostname:port
r'|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)' # www.|user@hostname
r'((?:\/[\+~%\/\.\w\-_]*)?' # path
r'\??(?:[\-\+=&;%@\.\w_]*)' # query parameters
r'#?(?:[\.\!\/\\\w]*))?)' # fragment
r'(?![^<]*?(?:<\/\w+>|\/?>))' # ignore anchor HTML tags
r'(?![^\(]*?\))' # ignore links in brackets (Markdown links and images)
)
link_patterns = [(re.compile(pattern),r'\1')]
import markdown2 as markdown
from markdown2 import Markdown
md = Markdown(
extras=[
'link-patterns',
'fenced-code-blocks',
'nofollow',
'tag-friendly',
'strike',
],
link_patterns = link_patterns,
)
def convert_markdown(s):
return md.convert(s)
###
import mistletoe
from html_stuff import parse_html, sanitize_html
from mistletoe import Document
from mistletoe.span_token import SpanToken,RawText,AutoLink
from mistletoe.block_token import BlockToken
from mistletoe.html_renderer import HTMLRenderer
from mistletoe.ast_renderer import ASTRenderer
OldAutoLink = AutoLink
class AutoLink(OldAutoLink):
def __init__(self, match):
content = match.group(self.parse_group)
self.children = (RawText(content),)
self.target = content
self.mailto = (0 and '@' in self.target) and 'mailto' not in self.target.casefold()
mistletoe.span_token.AutoLink = AutoLink
class AtUser(SpanToken):
pattern = re.compile(at_extractor_regex)
parse_inner = False
parse_group = 1 # useful only when parse_inner = 1
precedence = 1 # default 5
def __init__(self, match):
self.username = match.group(1)
# self.children = (RawText(self.username),)
class PostMention(SpanToken):
pattern = re.compile(post_autolink_regex)
parse_inner = False
parse_group = 1 # useful only when parse_inner = 1
precedence = 6 # default 5
def __init__(self, match):
self.id = match.group(1)
class LjhReference(SpanToken):
pattern = re.compile(':liangjiahe\-?(\d{1,4}):')
parse_inner = False
parse_group = 1 # useful only when parse_inner = 1
precedence = 1 # default 5
def __init__(self, match):
self.id = match.group(1)
class ModMessage(SpanToken):
pattern = re.compile('\[mod\](.*?)\[/mod\]')
parse_inner = True
parse_group = 1 # useful only when parse_inner = 1
precedence = 1 # default 5
def __init__(self, match):
self.inner = match.group(1)
class ThreadMention(SpanToken):
pattern = re.compile(thread_autolink_regex)
parse_inner = False
parse_group = 1 # useful only when parse_inner = 1
precedence = 6 # default 5
def __init__(self, match):
self.id = match.group(1)
class Cnmb(BlockToken):
pattern = re.compile(r'cnm(.+?)b$')
# pattern = re.compile(r' {0,3}(?:([-_*])\s*?)(?:\1\s*?){2,}$')
def __init__(self, matches):
self.content = matches[0]
@classmethod
def start(cls, line):
return cls.pattern.match(line)
@staticmethod
def read(lines):
return [next(lines)]
class YoutubePlayer(Cnmb):
pattern = re.compile(combined_youtube_extractor_regex)
def __init__(self, matches):
line = matches[0].strip()
g = self.pattern.match(line).group
url = g(0)
vid = g(1) or g(2) or g(3)
ts = None
if vid==g(1):
timestamp_found = re.search(r'\?t=([0-9]{1,})', url)
if timestamp_found:
ts = timestamp_found.group(1)
ts = int(ts)
ts = ('?t='+str(ts)) if ts else ''
self.url = url
self.replacement = f'''<a target="_blank" href="https://youtu.be/{vid}{ts}">youtu.be/{vid}{ts}</a><div class="youtube-player-unprocessed" data-id="{vid}" data-ts="{ts}"></div>'''
class TwitterFrame(Cnmb):
pattern = re.compile(twitter_extractor_regex)
def __init__(self, matches):
line = matches[0].strip()
g = self.pattern.match(line).group
url = g(0)
twid = g(1)
self.url = url
turl = url
self.replacement = f'''<a href="{turl}">{turl}</a><blockquote class="twitter-tweet" data-conversation="true" data-dnt="true"><a href="{turl}">{turl}</a></blockquote>'''
class PollInstance(Cnmb):
pattern = re.compile(r'#{0,1}poll(\d{1,16})(?=\s|\t|$)')
def __init__(self, matches):
line = matches[0].strip()
g = self.pattern.match(line).group
self.pollid = g(1)
self.replacement = f'<div class="poll-instance-unprocessed" data-id="{self.pollid}"></div>'
class FlavoredRenderer(HTMLRenderer):
def __init__(self):
# bad interface design ?
super().__init__(PollInstance, YoutubePlayer, TwitterFrame, Cnmb,
AtUser, PostMention, ThreadMention, LjhReference, ModMessage)
self.init_collection()
def init_collection(self):
self.collected = {
'at_user_list':[],
'youtube_players':[],
'twitter_frames':[],
}
def render_at_user(self, token):
# implicit PascalCase to snake_case conversion
# no documentation at all
un = token.username
self.collected['at_user_list'].append(un)
# un = self.escape_html(un)
# un = flask.escape(un)
return f'<a href="/member/{un}">@{un}</a>'
def render_post_mention(self, token):
tid = token.id
return f'<a href="/p/{tid}">#{tid}</a>'
def render_thread_mention(self, token):
tid = token.id
return f'<a href="/t/{tid}">/t/{tid}</a>'
def render_cnmb(self, token):
return f'<a href="#">{token.content}</a>'
def render_youtube_player(self, token):
self.collected['youtube_players'].append(token.url)
return token.replacement
def render_twitter_frame(self, token):
self.collected['twitter_frames'].append(token.url)
return token.replacement
def render_poll_instance(self, token):
return token.replacement
def render_ljh_reference(self, token):
from api import render_ljh_one
return render_ljh_one(token.id)
def render_auto_link(self, token): # overridden
template = '<a href="{target}">{inner}</a>'
if token.mailto:
target = 'mailto:{}'.format(token.target)
else:
target = self.escape_url(token.target)
inner = self.render_inner(token)
# unescape inner
inner = decodeURI(inner)
return template.format(target=target, inner=inner)
def render_mod_message(self, token):
inner = self.render_inner(token)
return f'<span data-class="mod_message">{inner}</span>'
import urllib.parse
unquote_ = decodeURI = urllib.parse.unquote
# flavored_renderer = FlavoredRenderer()
renderlock = threading.Lock()
@lru_cache(maxsize=16384)
def frend(md):
with renderlock:
# ren = FlavoredRenderer()
with FlavoredRenderer() as ren:
# again very bad interface design
rend = ren.render(Document(md))
coll = ren.collected
return rend, coll
if __name__ == '__main__':
xamplemd = '''
# heading1
@yesno
- @yesno
- totally @yesno yes!
```python
@yesno
@yesno
```
'''*10
print(frend(xamplemd))
print(just_markdown('<https://baidu.com/>'))
print(convert_markdown('<https://baidu.com/>'))