blob: 7077e2265057a283d5b75391b707cfcfafd5d37b [file] [log] [blame]
Avi Drissmandfd880852022-09-15 20:11:091# Copyright 2017 The Chromium Authors
vapierfceea8e2017-03-09 07:05:132# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Implements Gitiles' simpler auto linking.
6
7This extention auto links basic URLs that aren't bracketed by <...>.
8
Nate Fischer0cc74af52019-06-01 01:07:339https://gerrit.googlesource.com/gitiles/+/master/java/com/google/gitiles/Linkifier.java
vapierfceea8e2017-03-09 07:05:1310"""
11
Yu-Ping Wu4f924c902021-12-01 04:33:2112from markdown.inlinepatterns import (AutolinkInlineProcessor, Pattern)
vapierfceea8e2017-03-09 07:05:1313from markdown.extensions import Extension
14
15
Nate Fischer0cc74af52019-06-01 01:07:3316# Best effort attempt to match URLs without matching past the end of the URL.
17# The first "[]" is copied from Linkifier.java (safe, reserved, and unsafe
18# characters). The second "[]" is similar to the first, but with English
19# punctuation removed, since the gitiles parser treats these as punction in the
20# sentence, rather than the final character of the URL.
21AUTOLINK_RE = (r'(https?://[a-zA-Z0-9$_.+!*\',%;:@=?#/~<>-]+'
22 r'[a-zA-Z0-9$_+*\'%@=#/~<-])')
vapierfceea8e2017-03-09 07:05:1323
24
25class _GitilesSmartQuotesExtension(Extension):
Peter Kasting3f58d92bd2022-12-16 21:07:1126 """Add Gitiles' simpler linkifier to Markdown, with a priority just higher
27 than that of the builtin ''autolink''."""
Yu-Ping Wu4f924c902021-12-01 04:33:2128
29 def extendMarkdown(self, md):
Peter Kasting3f58d92bd2022-12-16 21:07:1130 md.inlinePatterns.register(AutolinkInlineProcessor(AUTOLINK_RE, md),
31 'gitilesautolink', 122)
vapierfceea8e2017-03-09 07:05:1332
33
34def makeExtension(*args, **kwargs):
35 return _GitilesSmartQuotesExtension(*args, **kwargs)