Skip to content

Loading…

optional link labels #53

Closed
wants to merge 1 commit into from

2 participants

@FND

cf. issue trentm#50

NB: I opted not to escape link labels, since they weren't being escaped before - would be happy to fix this on request.

FND add support for optional link labels
This allows link_patterns callables to return a tuple of href and label
instead of just the href, thus enabling custom link labels.
cef7d38
@FND

Note that I haven't included tests - this is partly because I'm getting 18 failures (cd test; python test.py), which might be because I'm branching off an older state (v1.0.1.17 / trentm@ad03ffe).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 14, 2011
  1. add support for optional link labels

    FND committed
    This allows link_patterns callables to return a tuple of href and label
    instead of just the href, thus enabling custom link labels.
Showing with 11 additions and 4 deletions.
  1. +11 −4 lib/markdown2.py
View
15 lib/markdown2.py 100755 → 100644
@@ -1598,18 +1598,25 @@ def _do_link_patterns(self, text):
for regex, repl in self.link_patterns:
replacements = []
for match in regex.finditer(text):
+ title = None # XXX: rename variable (ambiguous/misleading)
if hasattr(repl, "__call__"):
- href = repl(match)
+ components = repl(match) # XXX: rename variable
+ try:
+ href, title = components
+ except ValueError:
+ href = components
else:
href = match.expand(repl)
- replacements.append((match.span(), href))
- for (start, end), href in reversed(replacements):
+ replacements.append((match.span(), href, title))
+ for (start, end), href, title in reversed(replacements):
escaped_href = (
href.replace('"', '"') # b/c of attr quote
# To avoid markdown <em> and <strong>:
.replace('*', g_escape_table['*'])
.replace('_', g_escape_table['_']))
- link = '<a href="%s">%s</a>' % (escaped_href, text[start:end])
+ if not title:
+ title = text[start:end]
+ link = '<a href="%s">%s</a>' % (escaped_href, title)
hash = _hash_text(link)
link_from_hash[hash] = link
text = text[:start] + hash + text[end:]
Something went wrong with that request. Please try again.