[062892]: / Allura / allura / lib / markdown_extensions.py  Maximize  Restore  History

Download this file

588 lines (447 with data), 20.0 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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations
import re
import logging
from typing import List
import xml.etree.ElementTree as etree
from urllib.parse import urljoin
from tg import config
from bs4 import BeautifulSoup
import html5lib
import html5lib.serializer
import html5lib.filters.alphabeticalattributes
import markdown
import emoji
from markupsafe import Markup
from . import macro
from . import helpers as h
from allura import model as M
from allura.lib.utils import ForgeHTMLSanitizerFilter, is_nofollow_url
import six
log = logging.getLogger(__name__)
MACRO_PATTERN = r'\[\[([^\]\[]+)\]\]'
# SHORT_REF_RE copied from markdown pre 3.0
SHORT_REF_RE = markdown.inlinepatterns.NOIMG + r'\[([^\]]+)\]'
# FORGE_LINK_RE copied from markdown pre 3.0's LINK_RE
# TODO: replace these with newer approach, see ForgeLinkPattern
NOBRACKET = r'[^\]\[]*' # if not using regex-as-re-globally, must change "*" to {0,50} for performance mitigation
BRK = (
r'\[(' +
(NOBRACKET + r'(\[')*6 +
(NOBRACKET + r'\])*')*6 +
NOBRACKET + r')\]'
)
FORGE_LINK_RE = markdown.inlinepatterns.NOIMG + BRK + \
r'''\(\s*(<.*?>|((?:(?:\(.*?\))|[^\(\)]))*?)\s*((['"])(.*?)\12\s*)?\)'''
def clear_markdown_registry(reg: markdown.util.Registry, keep: List[str] = []):
keep_items = {}
for name in keep:
keep_items[name] = reg[name]
# this resets Registry's internal data structures to be empty
reg.__init__()
for name, item in keep_items.items():
reg.register(item, name, 50) # arbitrary priority :(
class CommitMessageExtension(markdown.Extension):
"""Markdown extension for processing commit messages.
People don't expect their commit messages to be parsed as Markdown. This
extension is therefore intentionally minimal in what it does. It knows how
to handle Trac-style short refs, will replace short refs with links, and
will create paragraphs around double-line breaks. That is *all* it does.
To make it do more, re-add some inlinePatterns and/or blockprocessors.
Some examples of the Trac-style refs this extension can parse::
#100
r123
ticket:100
comment:13:ticket:100
source:path/to/file.c@123#L456 (rev 123, lineno 456)
Trac-style refs will be converted to links to the appropriate artifact by
the :class:`PatternReplacingProcessor` preprocessor.
"""
def __init__(self, app):
super().__init__()
self.app = app
self._use_wiki = False
def extendMarkdown(self, md):
md.registerExtension(self)
# remove default preprocessors and add our own
clear_markdown_registry(md.preprocessors)
# The last param of .register() is priority. Higher vals go first.
md.preprocessors.register(PatternReplacingProcessor(TracRef1(), TracRef2(), TracRef3(self.app)), 'trac_refs', 0)
# remove all inlinepattern processors except short refs and links
clear_markdown_registry(md.inlinePatterns, keep=['link'])
md.inlinePatterns.register(ForgeLinkPattern(SHORT_REF_RE, md, ext=self), 'short_reference', 0)
# remove all default block processors except for paragraph
clear_markdown_registry(md.parser.blockprocessors, keep=['paragraph'])
# wrap artifact link text in square brackets
self.forge_link_tree_processor = ForgeLinkTreeProcessor(md)
md.treeprocessors.register(self.forge_link_tree_processor, 'links', 0)
# Sanitize HTML
md.postprocessors.register(HTMLSanitizer(), 'sanitize_html', 3)
# Put a class around markdown content for custom css
md.postprocessors.register(AddCustomClass(), 'add_custom_class', 2)
md.postprocessors.register(MarkAsSafe(), 'mark_safe', 1)
def reset(self):
self.forge_link_tree_processor.reset()
class Pattern:
"""Base class for regex patterns used by the :class:`PatternReplacingProcessor`.
Subclasses must define :attr:`pattern` (a compiled regex), and
:meth:`repl`.
"""
BEGIN, END = r'(^|\b|\s)', r'($|\b|\s)'
def sub(self, line):
return self.pattern.sub(self.repl, line)
def repl(self, match):
"""Return a string to replace ``match`` in the source string (the
string in which the match was found).
"""
return match.group()
class TracRef1(Pattern):
"""Replaces Trac-style short refs with links. Example patterns::
#100 (ticket 100)
r123 (revision 123)
"""
pattern = re.compile(r'(?<!\[|\w)([#r]\d+)(?!\]|\w)')
def repl(self, match):
shortlink = M.Shortlink.lookup(match.group(1))
if shortlink and not getattr(shortlink.ref.artifact, 'deleted', False):
return '[{ref}]({url})'.format(
ref=match.group(1),
url=shortlink.url)
return match.group()
class TracRef2(Pattern):
"""Replaces Trac-style short refs with links. Example patterns::
ticket:100
comment:13:ticket:400
"""
pattern = re.compile(
Pattern.BEGIN + r'((comment:(\d+):)?(ticket:)(\d+))' + Pattern.END)
def repl(self, match):
shortlink = M.Shortlink.lookup('#' + match.group(6))
if shortlink and not getattr(shortlink.ref.artifact, 'deleted', False):
url = shortlink.url
if match.group(4):
slug = self.get_comment_slug(
shortlink.ref.artifact, match.group(4))
slug = '#' + slug if slug else ''
url = url + slug
return '{front}[{ref}]({url}){back}'.format(
front=match.group(1),
ref=match.group(2),
url=url,
back=match.group(7))
return match.group()
def get_comment_slug(self, ticket, comment_num):
"""Given the id of an imported Trac comment, return it's Allura slug.
"""
if not ticket:
return None
comment_num = int(comment_num)
comments = ticket.discussion_thread.post_class().query.find(dict(
discussion_id=ticket.discussion_thread.discussion_id,
thread_id=ticket.discussion_thread._id,
status={'$in': ['ok', 'pending']},
deleted=False)).sort('timestamp')
if comment_num <= comments.count():
return comments.all()[comment_num - 1].slug
class TracRef3(Pattern):
"""Replaces Trac-style short refs with links. Example patterns::
source:trunk/server/file.c@123#L456 (rev 123, lineno 456)
Creates a link to a specific line of a source file at a specific revision.
"""
pattern = re.compile(
Pattern.BEGIN + r'((source:)([^@#\s]+)(@(\w+))?(#L(\d+))?)' + Pattern.END)
def __init__(self, app):
super(Pattern, self).__init__()
self.app = app
def repl(self, match):
if not self.app:
return match.group()
file, rev, lineno = (
match.group(4),
match.group(6) or 'HEAD',
'#l' + match.group(8) if match.group(8) else '')
url = '{app_url}{rev}/tree/{file}{lineno}'.format(
app_url=self.app.url,
rev=rev,
file=file,
lineno=lineno)
return '{front}[{ref}]({url}){back}'.format(
front=match.group(1),
ref=match.group(2),
url=url,
back=match.group(9))
class PatternReplacingProcessor(markdown.preprocessors.Preprocessor):
"""A Markdown preprocessor that searches the source lines for patterns and
replaces matches with alternate text.
"""
def __init__(self, *patterns):
self.patterns = patterns or []
def run(self, lines):
new_lines = []
for line in lines:
for pattern in self.patterns:
line = pattern.sub(line)
new_lines.append(line)
return new_lines
class ForgeExtension(markdown.Extension):
def __init__(self, wiki=False, email=False, macro_context=None):
super().__init__()
self._use_wiki = wiki
self._is_email = email
self._macro_context = macro_context
def extendMarkdown(self, md):
md.registerExtension(self)
md.preprocessors.register(ForgeMacroIncludePreprocessor(md), 'macro_include', -99)
# The last param of .register() is priority. Higher vals go first.
# this has to be before the 'escape' processor, otherwise weird
# placeholders are inserted for escaped chars within urls, and then the
# autolink can't match the whole url
md.inlinePatterns.register(AutolinkPattern(r'(http(?:s?)://[a-zA-Z0-9./\-\\_%?&=+#;~:!@]+)', md),
'autolink_without_brackets',
185) # was '<escape' and 'escape' is priority 180; great num runs first, so: 185
# replace the link pattern with our extended version
md.inlinePatterns.register(ForgeLinkPattern(FORGE_LINK_RE, md, ext=self), 'link', 160)
md.inlinePatterns.register(ForgeLinkPattern(SHORT_REF_RE, md, ext=self), 'short_reference', 130)
# macro must be processed before links
md.inlinePatterns.register(ForgeMacroPattern(MACRO_PATTERN, md, ext=self), 'macro', 165) # similar to above
self.forge_link_tree_processor = ForgeLinkTreeProcessor(md)
md.treeprocessors.register(self.forge_link_tree_processor, 'links', 0)
# Sanitize HTML
md.postprocessors.register(HTMLSanitizer(), 'sanitize_html', 5)
# Rewrite all relative links that don't start with . to have a '../' prefix
md.postprocessors.register(RelativeLinkRewriter(make_absolute=self._is_email), 'rewrite_relative_links', 4)
# Put a class around markdown content for custom css
md.postprocessors.register(AddCustomClass(), 'add_custom_class', 3)
md.postprocessors.register(MarkAsSafe(), 'mark_safe', 2)
def reset(self):
self.forge_link_tree_processor.reset()
class EmojiExtension(markdown.Extension):
EMOJI_RE = r'(:[a-zA-Z0-9\+\-_&.ô’Åéãíç()!#\*]+:)'
def extendMarkdown(self, md):
md.registerExtension(self)
md.inlinePatterns.register(EmojiInlinePattern(self.EMOJI_RE), 'emoji', 0)
class EmojiInlinePattern(markdown.inlinepatterns.Pattern):
def handleMatch(self, m):
emoji_code = m.group(2)
return emoji.emojize(emoji_code, language="alias")
class UserMentionExtension(markdown.Extension):
UM_RE = r'\B(@(?![0-9]+$)(?!-)[a-z0-9_-]{2,14}[a-z0-9_])'
def extendMarkdown(self, md):
md.registerExtension(self)
md.inlinePatterns.register(UserMentionInlinePattern(self.UM_RE), 'user_mentions', 0)
class UserMentionInlinePattern(markdown.inlinepatterns.Pattern):
def handleMatch(self, m):
user_name = m.group(2).replace("@", "")
user = M.User.by_username(user_name)
result = None
if user and not user.pending and not user.disabled:
result = etree.Element('a')
result.text = "@%s" % user_name
result.set('href', h.username_project_url(user))
result.set('class', 'user-mention')
else:
result = "@%s" % user_name
return result
class ForgeLinkPattern(markdown.inlinepatterns.Pattern):
# TODO: convert from extending Pattern to extending InlineProcessor
# which is how core Markdown library in 3.0 made its base link parsing much faster.
# https://github.com/Python-Markdown/markdown/commit/d18c3d0acab0e7469c3284c897afcb61f9dd1fea
artifact_re = re.compile(r'((.*?):)?((.*?):)?(.+)')
def __init__(self, *args, **kwargs):
self.ext = kwargs.pop('ext')
super().__init__(*args, **kwargs)
def handleMatch(self, m):
el = etree.Element('a')
el.text = m.group(2)
is_link_with_brackets = False
try:
href = m.group(9)
except IndexError:
href = m.group(2)
is_link_with_brackets = True
if el.text == 'x' or el.text == ' ': # skip [ ] and [x] for markdown checklist
return '[' + el.text + ']'
try:
title = m.group(13)
except IndexError:
title = None
classes = ''
if href:
if href == 'TOC':
return '[TOC]' # skip TOC
if self.artifact_re.match(href):
href, classes = self._expand_alink(href, is_link_with_brackets)
el.set('href', self.unescape(href.strip()))
el.set('class', classes)
else:
el.set('href', '')
if title:
title = markdown.inlinepatterns.dequote(self.unescape(title))
el.set('title', title)
if 'notfound' in classes and not self.ext._use_wiki:
text = el.text
el = etree.Element('span')
el.text = '[%s]' % text
return el
def _expand_alink(self, link, is_link_with_brackets):
'''Return (href, classes) for an artifact link'''
classes = ''
if is_link_with_brackets:
classes = 'alink'
href = link
shortlink = M.Shortlink.lookup(link)
if shortlink and shortlink.ref and not getattr(shortlink.ref.artifact, 'deleted', False):
href = shortlink.url
if getattr(shortlink.ref.artifact, 'is_closed', False):
classes += ' strikethrough'
self.ext.forge_link_tree_processor.alinks.append(shortlink)
elif is_link_with_brackets:
href = h.urlquote(link)
classes += ' notfound'
attach_link = link.split('/attachment/')
if len(attach_link) == 2 and self.ext._use_wiki:
shortlink = M.Shortlink.lookup(attach_link[0])
if shortlink:
attach_status = ' notfound'
for attach in shortlink.ref.artifact.attachments:
if attach.filename == attach_link[1]:
attach_status = ''
classes += attach_status
return href, classes
class ForgeMacroPattern(markdown.inlinepatterns.Pattern):
def __init__(self, *args, **kwargs):
self.ext = kwargs.pop('ext')
self.macro = macro.parse(self.ext._macro_context)
super().__init__(*args, **kwargs)
def handleMatch(self, m):
html = self.macro(m.group(2))
placeholder = self.md.htmlStash.store(html)
return placeholder
class ForgeLinkTreeProcessor(markdown.treeprocessors.Treeprocessor):
'''Wraps artifact links with []'''
def __init__(self, parent):
self.parent = parent
self.alinks = []
def run(self, root):
for node in root.iter('a'):
if 'alink' in node.get('class', '').split() and node.text:
node.text = '[' + node.text + ']'
return root
def reset(self):
self.alinks = []
class MarkAsSafe(markdown.postprocessors.Postprocessor):
def run(self, text):
return Markup(text)
class AddCustomClass(markdown.postprocessors.Postprocessor):
def run(self, text):
return '<div class="markdown_content">%s</div>' % text
class RelativeLinkRewriter(markdown.postprocessors.Postprocessor):
def __init__(self, make_absolute=False):
self._make_absolute = make_absolute
def run(self, text):
soup = BeautifulSoup(text,
'html5lib') # 'html.parser' parser gives weird </li> behaviour with test_macro_members
if self._make_absolute:
rewrite = self._rewrite_abs
else:
rewrite = self._rewrite
for link in soup.findAll('a'):
rewrite(link, 'href')
for link in soup.findAll('img'):
rewrite(link, 'src')
# html5lib parser adds html/head/body tags, so output <body> without its own tags
return str(soup.body)[len('<body>'):-len('</body>')]
def _rewrite(self, tag, attr):
val = tag.get(attr)
if val is None:
return
if ' ' in val:
# Don't urllib.quote to avoid possible double-quoting
# just make sure no spaces
val = val.replace(' ', '%20')
tag[attr] = val
if 'markdown_syntax' in val:
tag['rel'] = 'nofollow'
if '://' in val:
if is_nofollow_url(val):
tag['rel'] = 'nofollow'
return
if val.startswith('/'):
return
if val.startswith('.'):
return
if val.startswith('mailto:'):
return
if val.startswith('#'):
return
tag[attr] = '../' + val
def _rewrite_abs(self, tag, attr):
self._rewrite(tag, attr)
val = tag.get(attr)
val = urljoin(config['base_url'], val)
tag[attr] = val
class HTMLSanitizer(markdown.postprocessors.Postprocessor):
def run(self, text):
parsed = html5lib.parseFragment(text)
# if we didn't have to customize our sanitization, could just do:
# return html5lib.serialize(parsed, sanitize=True)
# instead we do the same steps as that function,
# but add our ForgeHTMLSanitizerFilter instead of sanitize=True which would use the standard one
TreeWalker = html5lib.treewalkers.getTreeWalker("etree")
walker = TreeWalker(parsed)
walker = ForgeHTMLSanitizerFilter(walker) # this is our custom step
s = html5lib.serializer.HTMLSerializer()
return s.render(walker)
class AutolinkPattern(markdown.inlinepatterns.Pattern):
def __init__(self, pattern, markdown_instance=None):
super().__init__(pattern, markdown_instance)
# override the complete regex, requiring the preceding text (.*?) to end
# with whitespace or beginning of line "\s|^"
self.compiled_re = re.compile(r"^(.*?\s|^)%s(.*?)$" % pattern,
re.DOTALL | re.UNICODE)
def handleMatch(self, mo):
old_link = mo.group(2)
result = etree.Element('a')
result.text = old_link
# since this is run before the builtin 'escape' processor, we have to
# do our own unescaping
for char in self.md.ESCAPED_CHARS:
old_link = old_link.replace('\\' + char, char)
result.set('href', old_link)
return result
class ForgeMacroIncludePreprocessor(markdown.preprocessors.Preprocessor):
'''Join include statements to prevent extra <br>'s inserted by nl2br extension.
Converts:
[[include ref=some_ref]]
[[include ref=some_other_ref]]
To:
[[include ref=some_ref]][[include ref=some_other_ref]]
'''
pattern = re.compile(r'^\s*\[\[include ref=[^\]]*\]\]\s*$', re.IGNORECASE)
def run(self, lines):
buf = []
result = []
for line in lines:
if self.pattern.match(line):
buf.append(line)
else:
if buf:
result.append(''.join(buf))
buf = []
result.append(line)
return result