gitiles_autolink.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. # Copyright 2017 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. """Implements Gitiles' simpler auto linking.
  5. This extention auto links basic URLs that aren't bracketed by <...>.
  6. https://gerrit.googlesource.com/gitiles/+/master/java/com/google/gitiles/Linkifier.java
  7. """
  8. from markdown.inlinepatterns import (AutolinkInlineProcessor, Pattern)
  9. from markdown.extensions import Extension
  10. # Best effort attempt to match URLs without matching past the end of the URL.
  11. # The first "[]" is copied from Linkifier.java (safe, reserved, and unsafe
  12. # characters). The second "[]" is similar to the first, but with English
  13. # punctuation removed, since the gitiles parser treats these as punction in the
  14. # sentence, rather than the final character of the URL.
  15. AUTOLINK_RE = (r'(https?://[a-zA-Z0-9$_.+!*\',%;:@=?#/~<>-]+'
  16. r'[a-zA-Z0-9$_+*\'%@=#/~<-])')
  17. class _GitilesSmartQuotesExtension(Extension):
  18. """Add Gitiles' simpler linkifier to Markdown."""
  19. def extendMarkdown(self, md):
  20. md.inlinePatterns.add('gitilesautolink',
  21. AutolinkInlineProcessor(AUTOLINK_RE, md), '<autolink')
  22. def makeExtension(*args, **kwargs):
  23. return _GitilesSmartQuotesExtension(*args, **kwargs)