Permalink
Browse files

Merge pull request #733 from benjaoming/feature/links-with-anchors

Support fragments in wiki links.
  • Loading branch information...
benjaoming committed Dec 10, 2017
2 parents 697b66e + 7e18cc0 commit 189f7c10663af63d1e4999760266d4708158ba1b
Showing with 59 additions and 11 deletions.
  1. +6 −0 docs/release_notes.rst
  2. +13 −11 src/wiki/plugins/links/mdx/djangowikilinks.py
  3. +40 −0 tests/plugins/links/test_links.py
View
@@ -12,6 +12,12 @@ the last series with Python 2 support.
django-wiki 0.3dev (unreleased master)
--------------------------------------
**Added**
* Markdown tag with wiki paths now support fragments like
``[Click Here](wiki:/path#header)`` (Frank Loemker) #701
**Changed**
* Removed exception catch all in ``URLPath.delete_subtree`` which silenced errors while delete articles with descendents
@@ -57,7 +57,7 @@ def extendMarkdown(self, md, md_globals):
self.md = md
# append to end of inline patterns
WIKI_RE = r'\[(?P<linkTitle>[^\]]+?)\]\(wiki:(?P<wikiTitle>[a-zA-Z0-9\./_-]*?)\)'
WIKI_RE = r'\[(?P<label>[^\]]+?)\]\(wiki:(?P<wikipath>[a-zA-Z0-9\./_-]*?)(?P<fragment>#[a-zA-Z0-9\./_-]*)?\)'
wikiPathPattern = WikiPath(WIKI_RE, self.config, markdown_instance=md)
wikiPathPattern.md = md
md.inlinePatterns.add('djangowikipath', wikiPathPattern, "<reference")
@@ -70,11 +70,11 @@ def __init__(self, pattern, config, **kwargs):
self.config = config
def handleMatch(self, m):
article_title = m.group('wikiTitle')
wiki_path = m.group('wikipath')
absolute = False
if article_title.startswith("/"):
if wiki_path.startswith("/"):
absolute = True
article_title = article_title.strip("/")
wiki_path = wiki_path.strip("/")
# Use this to calculate some kind of meaningful path
# from the link, regardless of whether or not something can be
@@ -83,12 +83,12 @@ def handleMatch(self, m):
if absolute:
base_path = self.config['base_url'][0]
path_from_link = os_path.join(str(base_path), article_title)
path_from_link = os_path.join(str(base_path), wiki_path)
urlpath = None
path = path_from_link
try:
urlpath = models.URLPath.get_by_path(article_title)
urlpath = models.URLPath.get_by_path(wiki_path)
path = urlpath.get_absolute_url()
except models.URLPath.DoesNotExist:
pass
@@ -101,14 +101,14 @@ def handleMatch(self, m):
starting_level = max(0, self.config['default_level'][0] - 1)
starting_path = "/".join(source_components[: starting_level])
path_from_link = os_path.join(starting_path, article_title)
path_from_link = os_path.join(starting_path, wiki_path)
lookup = models.URLPath.objects.none()
if urlpath.parent:
lookup = urlpath.parent.get_descendants().filter(
slug=article_title)
slug=wiki_path)
else:
lookup = urlpath.get_descendants().filter(slug=article_title)
lookup = urlpath.get_descendants().filter(slug=wiki_path)
if lookup.count() > 0:
urlpath = lookup[0]
@@ -117,9 +117,11 @@ def handleMatch(self, m):
urlpath = None
path = self.config['base_url'][0] + path_from_link
label = m.group('linkTitle')
label = m.group('label')
fragment = m.group('fragment') or ""
a = etree.Element('a')
a.set('href', path)
a.set('href', path + fragment)
if not urlpath:
a.set('class', self.config['html_class'][0] + " linknotfound")
else:
@@ -22,3 +22,43 @@ def test_works_with_lazy_functions(self):
md.convert(text),
'<p><a class="wikipath linknotfound" href="/fr">Français</a></p>',
)
URLPath.create_urlpath(URLPath.root(), "linktest",
title="LinkTest",
content="A page\n#A section\nA line",
user_message="Comment1")
# Link to an existing page
text = '[Test link](wiki:/linktest)'
self.assertEqual(
md.convert(text),
'<p><a class="wikipath" href="/linktest/">Test link</a></p>',
)
# Link with an empty fragment
text = '[Test link](wiki:/linktest#)'
self.assertEqual(
md.convert(text),
'<p><a class="wikipath" href="/linktest/#">Test link</a></p>',
)
# Link to a header in an existing page
text = '[Test head](wiki:/linktest#wiki-toc-a-section)'
self.assertEqual(
md.convert(text),
'<p><a class="wikipath" href="/linktest/#wiki-toc-a-section">Test head</a></p>',
)
# Link to a header in a non existing page
text = '[Test head nonExist](wiki:/linktesterr#wiki-toc-a-section)'
self.assertEqual(
md.convert(text),
'<p><a class="wikipath linknotfound" href="/linktesterr#wiki-toc-a-section">Test head nonExist</a></p>',
)
# Invalid Wiki link: The default markdown link parser takes over
text = '[Test head err](wiki:/linktest#wiki-toc-a-section#err)'
self.assertEqual(
md.convert(text),
'<p><a href="wiki:/linktest#wiki-toc-a-section#err">Test head err</a></p>',
)

0 comments on commit 189f7c1

Please sign in to comment.