Avi Drissman | dfd88085 | 2022-09-15 20:11:09 | [diff] [blame] | 1 | # Copyright 2017 The Chromium Authors |
vapier | fceea8e | 2017-03-09 07:05:13 | [diff] [blame] | 2 | # 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 | |
| 7 | This extention auto links basic URLs that aren't bracketed by <...>. |
| 8 | |
Nate Fischer | 0cc74af5 | 2019-06-01 01:07:33 | [diff] [blame] | 9 | https://gerrit.googlesource.com/gitiles/+/master/java/com/google/gitiles/Linkifier.java |
vapier | fceea8e | 2017-03-09 07:05:13 | [diff] [blame] | 10 | """ |
| 11 | |
Yu-Ping Wu | 4f924c90 | 2021-12-01 04:33:21 | [diff] [blame] | 12 | from markdown.inlinepatterns import (AutolinkInlineProcessor, Pattern) |
vapier | fceea8e | 2017-03-09 07:05:13 | [diff] [blame] | 13 | from markdown.extensions import Extension |
| 14 | |
| 15 | |
Nate Fischer | 0cc74af5 | 2019-06-01 01:07:33 | [diff] [blame] | 16 | # 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. |
| 21 | AUTOLINK_RE = (r'(https?://[a-zA-Z0-9$_.+!*\',%;:@=?#/~<>-]+' |
| 22 | r'[a-zA-Z0-9$_+*\'%@=#/~<-])') |
vapier | fceea8e | 2017-03-09 07:05:13 | [diff] [blame] | 23 | |
| 24 | |
| 25 | class _GitilesSmartQuotesExtension(Extension): |
Peter Kasting | 3f58d92bd | 2022-12-16 21:07:11 | [diff] [blame] | 26 | """Add Gitiles' simpler linkifier to Markdown, with a priority just higher |
| 27 | than that of the builtin ''autolink''.""" |
Yu-Ping Wu | 4f924c90 | 2021-12-01 04:33:21 | [diff] [blame] | 28 | |
| 29 | def extendMarkdown(self, md): |
Peter Kasting | 3f58d92bd | 2022-12-16 21:07:11 | [diff] [blame] | 30 | md.inlinePatterns.register(AutolinkInlineProcessor(AUTOLINK_RE, md), |
| 31 | 'gitilesautolink', 122) |
vapier | fceea8e | 2017-03-09 07:05:13 | [diff] [blame] | 32 | |
| 33 | |
| 34 | def makeExtension(*args, **kwargs): |
| 35 | return _GitilesSmartQuotesExtension(*args, **kwargs) |