aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--docs/extensions/WikiLinks.txt16
-rw-r--r--markdown/extensions/wikilinks.py18
-rw-r--r--tests/extensions-x-wikilinks/wikilinks.txt4
3 files changed, 33 insertions, 5 deletions
diff --git a/docs/extensions/WikiLinks.txt b/docs/extensions/WikiLinks.txt
index 73991cf..36045cb 100644
--- a/docs/extensions/WikiLinks.txt
+++ b/docs/extensions/WikiLinks.txt
@@ -63,6 +63,8 @@ change the default behavior:
Default: `'wikilink'`
+4. **build_url**: Callable which formats the URL from it's parts.
+
For an example, let us suppose links should always point to the subdirectory
`/wiki/` and end with `.html`
@@ -74,6 +76,20 @@ The above would result in the following link for `[[WikiLink]]`.
<a href="/wiki/WikiLink.html" class="wikilink">WikiLink</a>
+If you want to do more that just alter the base and/or end of the URL, you
+could also pass in a callable which must accept three arguments (``label``,
+``base``, and ``end``). The callable must return the URL in it's entirety.
+
+ def my_url_builder(label, base, end):
+ # do stuff
+ return url
+
+ md = markdown.Markdown(
+ extensions=['wikilinks],
+ extension_configs={'wikilinks' : ('build_url', my_url_builder)}
+ )
+
+
The option is also provided to change or remove the class attribute.
>>> html = markdown.markdown(text,
diff --git a/markdown/extensions/wikilinks.py b/markdown/extensions/wikilinks.py
index 147ab62..36a00e8 100644
--- a/markdown/extensions/wikilinks.py
+++ b/markdown/extensions/wikilinks.py
@@ -70,6 +70,13 @@ Dependencies:
'''
import markdown
+import re
+
+def build_url(label, base, end):
+ """ Build a url from the label, a base, and an end. """
+ clean_label = re.sub(r'([ ]+_)|(_[ ]+)|([ ]+)', '_', label)
+ return '%s%s%s'% (base, clean_label, end)
+
class WikiLinkExtension(markdown.Extension):
def __init__(self, configs):
@@ -77,7 +84,8 @@ class WikiLinkExtension(markdown.Extension):
self.config = {
'base_url' : ['/', 'String to append to beginning or URL.'],
'end_url' : ['/', 'String to append to end of URL.'],
- 'html_class' : ['wikilink', 'CSS hook. Leave blank for none.']
+ 'html_class' : ['wikilink', 'CSS hook. Leave blank for none.'],
+ 'build_url' : [build_url, 'Callable formats URL from label.'],
}
# Override defaults with user settings
@@ -91,8 +99,8 @@ class WikiLinkExtension(markdown.Extension):
WIKILINK_RE = r'\[\[([A-Za-z0-9_ -]+)\]\]'
wikilinkPattern = WikiLinks(WIKILINK_RE, self.config)
wikilinkPattern.md = md
- md.inlinePatterns.add('wikilink', wikilinkPattern, "_end")
-
+ md.inlinePatterns.add('wikilink', wikilinkPattern, "<not_strong")
+
class WikiLinks(markdown.inlinepatterns.Pattern):
def __init__(self, pattern, config):
@@ -103,9 +111,9 @@ class WikiLinks(markdown.inlinepatterns.Pattern):
if m.group(2).strip():
base_url, end_url, html_class = self._getMeta()
label = m.group(2).strip()
- url = '%s%s%s'% (base_url, label.replace(' ', '_'), end_url)
+ url = self.config['build_url'][0](label, base_url, end_url)
a = markdown.etree.Element('a')
- a.text = markdown.AtomicString(label)
+ a.text = label #markdown.AtomicString(label)
a.set('href', url)
if html_class:
a.set('class', html_class)
diff --git a/tests/extensions-x-wikilinks/wikilinks.txt b/tests/extensions-x-wikilinks/wikilinks.txt
index 46d1279..8e6911b 100644
--- a/tests/extensions-x-wikilinks/wikilinks.txt
+++ b/tests/extensions-x-wikilinks/wikilinks.txt
@@ -2,6 +2,10 @@ Some text with a [[WikiLink]].
A link with [[ white space and_underscores ]] and a empty [[ ]] one.
+Another with [[double spaces]] and [[double__underscores]] and
+one that [[has _emphasis_ inside]] and one [[with_multiple_underscores]]
+and one that is _[[emphasised]]_.
+
And a <a href="http://example.com/RealLink">RealLink</a>.
<http://example.com/And_A_AutoLink>