I'm writing an extension for linkifying URLs like http://example.com so that users don't even need to know that they could use <http://example.com> instead. Matching URLs isn't too hard (it doesn't need to be perfect), but the challenge is not modifying URLs in cases like [example](http://example.com). My understanding is that extension priority affects this, but the priority concept isn't really documented in the API docs.
The docs first say that
You may register instances of your processors and patterns with an appropriate priority
but as a user it's hard for me to tell what would be an "appropriate priority" in my case. The examples then later use 175 as the priority without explaining why that number was chosen. Finally, Register.register documents priority argument like
An integer or float used to sort against all items.
which isn't very helpful.
Thinks I'd expect to find from the docs:
- Does a small number mean a higher or smaller priority? In other words, is a plugin with priority 1 run before a plugin with priority 1000?
- Are negative priorities allowed? How are they interpreted?
- If a content is processed with an extension with a higher priority, is it anymore processed with other extensions? Does this behavior vary between different kind of extensions? I'm mostly interested in inline processors.
- What are priorities of built-in extensions? Can they be considered to be stable?
Good news is that I was able to make my prototype work by using a low priority like 10. When I used 175 that the examples had, the plugin messed up the [example](http://example.com) syntax. Based on that my assumption is that a higher number means higher priority and that extensions with lower priority aren't run if a higher priority extension already matched something.
Below is my current prototype code in case someone is interested. I still need to test it more thoroughly and the somewhat hairy regex should probably get some comments.
import xml.etree.ElementTree as ET
from markdown.inlinepatterns import InlineProcessor
from markdown.extensions import Extension
class LinkifyInlineProcessor(InlineProcessor):
def handleMatch(self, match, data):
url, tail = match.groups()
el = ET.Element("a", href=url)
el.text = url
el.tail = tail
return el, match.start(0), match.end(0)
class LinkifyExtension(Extension):
def extendMarkdown(self, md):
pattern = r"([a-z][\w+-.]*://\S+?)([)\]}\"'.,!?:;|]*(?=\s|$))"
md.inlinePatterns.register(LinkifyInlineProcessor(pattern, md), 'linkify', 10)
I'm writing an extension for linkifying URLs like
http://example.comso that users don't even need to know that they could use<http://example.com>instead. Matching URLs isn't too hard (it doesn't need to be perfect), but the challenge is not modifying URLs in cases like[example](http://example.com). My understanding is that extension priority affects this, but the priority concept isn't really documented in the API docs.The docs first say that
but as a user it's hard for me to tell what would be an "appropriate priority" in my case. The examples then later use 175 as the priority without explaining why that number was chosen. Finally,
Register.registerdocumentspriorityargument likewhich isn't very helpful.
Thinks I'd expect to find from the docs:
Good news is that I was able to make my prototype work by using a low priority like 10. When I used 175 that the examples had, the plugin messed up the
[example](http://example.com)syntax. Based on that my assumption is that a higher number means higher priority and that extensions with lower priority aren't run if a higher priority extension already matched something.Below is my current prototype code in case someone is interested. I still need to test it more thoroughly and the somewhat hairy regex should probably get some comments.