Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Link patterns - customizing link text #71

Open
wants to merge 1 commit into from

1 participant

Pedro Ferreira
Pedro Ferreira

Hello,

I have added an extra parameter to link patterns, so that you can customize the text that comes inside the <a/> tag.
If you think it's useful, please feel free to pull.

Thanks!

Pedro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 27, 2011
  1. Pedro Ferreira
This page is out of date. Refresh to see the latest.
Showing with 23 additions and 4 deletions.
  1. +23 −4 lib/markdown2.py
27 lib/markdown2.py
View
@@ -1755,21 +1755,40 @@ def _do_link_patterns(self, text):
lookbehind assertion to attempt to guard against this.
"""
link_from_hash = {}
- for regex, repl in self.link_patterns:
+ for pattern in self.link_patterns:
replacements = []
+
+ if len(pattern) == 3:
+ regex, repl, repl_title = pattern
+ else:
+ regex, repl = pattern
+ repl_title = link_text = None
+
for match in regex.finditer(text):
+
if hasattr(repl, "__call__"):
href = repl(match)
else:
href = match.expand(repl)
- replacements.append((match.span(), href))
- for (start, end), href in reversed(replacements):
+
+ if repl_title != None:
+ if hasattr(repl_title, "__call__"):
+ link_text = repl_title(match)
+ else:
+ link_text = match.expand(repl_title)
+
+ replacements.append((match.span(), href, link_text))
+ for (start, end), href, new_text in reversed(replacements):
escaped_href = (
href.replace('"', '&quot;') # b/c of attr quote
# To avoid markdown <em> and <strong>:
.replace('*', self._escape_table['*'])
.replace('_', self._escape_table['_']))
- link = '<a href="%s">%s</a>' % (escaped_href, text[start:end])
+
+ if new_text == None:
+ new_text = text[start:end]
+
+ link = '<a href="%s">%s</a>' % (escaped_href, new_text)
hash = _hash_text(link)
link_from_hash[hash] = link
text = text[:start] + hash + text[end:]
Something went wrong with that request. Please try again.