You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An interesting idea from @aehlke in pull req #43 is to pass links through a callback or callbacks that accept and return (possibly among other things) a dict of attributes.
I think this has a lot of potential to simplify the linkify API and address the issues that delinkify aimed to, but in a better way.
Total straw man API might be:
deflinkify(text, callback):
# ... snip ...defsome_callback(text, attributes=None, new=False): # This signature would be specified in docs.# Do processing, including things like url and text munging that are currently callbacks.# Create a link.return {'href': href, ...} # Return a dict, everything's OK.# Or don't create a link/remove the link.returnNone# Existing links removed, new links not created.
The obvious question that arises is what to do with multiple callbacks. It'd be great to make these callback functions small, atomic, etc, so something along the lines of Django context processors seems obvious:
deflinkify(text, callbacks=None):
ifcallbacksisNone:
callbacks= []
elsifcallable(callbacks):
callbacks= [callbacks]
# ... snip ...# Found a link!attrs= {'some': 'dict', ...}
forcbincallbacks:
a=cb(link_text, attrs, new)
ifaisNone:
# Break out of this somehow, just replace everything with the text.attrs.update(a)
# Render the link with the attributes in attrs
Pros of this approach:
Generalizes everything linkify currently does. filter_url and filter_text are already callbacks, so they could easily be updated to return dicts. Adding or setting attributes like rel and target is trivial, etc.
Is extremely flexible.
It's easy to create a bunch of default callbacks for people to use.
It can handle what delinkify was meant to do--just write a callback that only allows relative links or links from certain domains.
Adding new callbacks requires repeating all the old ones. Hopefully this is solvable (e.g. linkify(text, DEFAULT_CALLBACKS + [my_callback]) or something).
The order of the callbacks is critical and might introduce subtle bugs for people? (They should have tests.)
It's flexible but more complicated for developers.
How to deal with the link text? (Maybe add it to the dict as _text? Something that can never be an HTML attribute.)
As described above, wouldn't ever remove attributes unless their was some sort of whitelist/blacklist filter callback. (Maybe allow setting 'some_attr': None to remove it? Or make sure you return the whole attr dict, unlike Django context processors.)
The text was updated successfully, but these errors were encountered:
The AMO filter_url callback would need to be rewritten to set the 'href' value in the attributes dict and return the dict. And calls to linkify would need to be modified.
I realize that my last con is just as much an issue in the current implementation. While rel and target can get set, and href can get munged, any other attributes won't be touched (on existing links). So, this actually adds the ability to add an attribute filter if you want, assuming we make each callback return the whole dict.
An interesting idea from @aehlke in pull req #43 is to pass links through a callback or callbacks that accept and return (possibly among other things) a dict of attributes.
I think this has a lot of potential to simplify the
linkify
API and address the issues thatdelinkify
aimed to, but in a better way.Total straw man API might be:
The obvious question that arises is what to do with multiple callbacks. It'd be great to make these callback functions small, atomic, etc, so something along the lines of Django context processors seems obvious:
Pros of this approach:
linkify
currently does.filter_url
andfilter_text
are already callbacks, so they could easily be updated to return dicts. Adding or setting attributes likerel
andtarget
is trivial, etc.delinkify
was meant to do--just write a callback that only allows relative links or links from certain domains.skip_existing_links
idea from pull req Added a flag to linkify to skip existing links #43.Cons, and some suggestions:
linkify(text, DEFAULT_CALLBACKS + [my_callback])
or something)._text
? Something that can never be an HTML attribute.)'some_attr': None
to remove it? Or make sure you return the wholeattr
dict, unlike Django context processors.)The text was updated successfully, but these errors were encountered: