blob: 54bd9aaa75d5618343100ded435fdcfaf89e0aca [file] [log] [blame]
Avi Drissman7ff27242022-09-15 20:11:09 +00001# Copyright 2017 The Chromium Authors
vapierd6d67462017-03-08 23:05:13 -08002# 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 Fischer2a89b082019-06-01 01:07:33 +00009https://gerrit.googlesource.com/gitiles/+/master/java/com/google/gitiles/Linkifier.java
vapierd6d67462017-03-08 23:05:13 -080010"""
11
Yu-Ping Wub61dc902021-12-01 04:33:21 +000012from markdown.inlinepatterns import (AutolinkInlineProcessor, Pattern)
vapierd6d67462017-03-08 23:05:13 -080013from markdown.extensions import Extension
14
15
Nate Fischer2a89b082019-06-01 01:07:33 +000016# 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$_+*\'%@=#/~<-])')
vapierd6d67462017-03-08 23:05:13 -080023
24
25class _GitilesSmartQuotesExtension(Extension):
26 """Add Gitiles' simpler linkifier to Markdown."""
Yu-Ping Wub61dc902021-12-01 04:33:21 +000027
28 def extendMarkdown(self, md):
vapierd6d67462017-03-08 23:05:13 -080029 md.inlinePatterns.add('gitilesautolink',
Yu-Ping Wub61dc902021-12-01 04:33:21 +000030 AutolinkInlineProcessor(AUTOLINK_RE, md), '<autolink')
vapierd6d67462017-03-08 23:05:13 -080031
32
33def makeExtension(*args, **kwargs):
34 return _GitilesSmartQuotesExtension(*args, **kwargs)