Skip to content

Commit

Permalink
Fix wikilink extension: use absolute urls
Browse files Browse the repository at this point in the history
  • Loading branch information
bameda committed Nov 26, 2014
1 parent 72730fd commit 30ca702
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 52 deletions.
59 changes: 16 additions & 43 deletions taiga/mdrender/extensions/wikilinks.py
Expand Up @@ -15,69 +15,42 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from __future__ import absolute_import
from __future__ import unicode_literals
from markdown import Extension
from markdown.inlinepatterns import Pattern
from markdown.util import etree
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)
from taiga.front import resolve


class WikiLinkExtension(Extension):
def __init__(self, configs):
# set extension defaults
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.'],
'build_url': [build_url, 'Callable formats URL from label.'],
}
configs = dict(configs) or {}
# Override defaults with user settings
for key, value in configs.items():
self.setConfig(key, value)
def __init__(self, project, *args, **kwargs):
self.project = project
return super().__init__(*args, **kwargs)

def extendMarkdown(self, md, md_globals):
self.md = md

# append to end of inline patterns
WIKILINK_RE = r'\[\[([\w0-9_ -]+)(\|[\w0-9_ -]+)?\]\]'
wikilinkPattern = WikiLinks(WIKILINK_RE, self.getConfigs())
WIKILINK_RE = r"\[\[([\w0-9_ -]+)(\|[\w0-9_ -]+)?\]\]"
wikilinkPattern = WikiLinksPattern(WIKILINK_RE, self.project)
wikilinkPattern.md = md
md.inlinePatterns.add('wikilink', wikilinkPattern, "<not_strong")
md.inlinePatterns.add("wikilink", wikilinkPattern, "<not_strong")


class WikiLinks(Pattern):
def __init__(self, pattern, config):
super(WikiLinks, self).__init__(pattern)
self.config = config
class WikiLinksPattern(Pattern):
def __init__(self, pattern, project):
self.project = project
super().__init__(pattern)

def handleMatch(self, m):
base_url, end_url, html_class = self._getMeta()
label = m.group(2).strip()
url = self.config['build_url'](label, base_url, end_url)
url = resolve("wiki", self.project.slug, label)

if m.group(3):
title = m.group(3).strip()[1:]
else:
title = label

a = etree.Element('a')
a = etree.Element("a")
a.text = title
a.set('href', url)
if html_class:
a.set('class', html_class)
a.set("href", url)
a.set("title", title)
a.set("class", "reference wiki")
return a

def _getMeta(self):
""" Return meta data or config data. """
base_url = self.config['base_url']
end_url = self.config['end_url']
html_class = self.config['html_class']
return base_url, end_url, html_class
10 changes: 3 additions & 7 deletions taiga/mdrender/service.py
Expand Up @@ -63,13 +63,13 @@ def _serialize(domtree):
bleach.ALLOWED_ATTRIBUTES["*"] = ["class", "style"]


def _make_extensions_list(wikilinks_config=None, project=None):
def _make_extensions_list(project=None):
return [AutolinkExtension(),
AutomailExtension(),
SemiSaneListExtension(),
SpacedLinkExtension(),
StrikethroughExtension(),
WikiLinkExtension(wikilinks_config),
WikiLinkExtension(project),
EmojifyExtension(),
MentionsExtension(),
TaigaReferencesExtension(project),
Expand Down Expand Up @@ -103,11 +103,7 @@ def _get_markdown(project):
def build_url(*args, **kwargs):
return args[1] + slugify(args[0])

wikilinks_config = {"base_url": "/project/{}/wiki/".format(project.slug),
"end_url": "",
"build_url": build_url}
extensions = _make_extensions_list(wikilinks_config=wikilinks_config,
project=project)
extensions = _make_extensions_list(project=project)
md = Markdown(extensions=extensions)
md.extracted_data = {"mentions": [], "references": []}
return md
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_mdrender.py
Expand Up @@ -106,12 +106,12 @@ def test_render_relative_link():


def test_render_wikilink():
expected_result = "<p><a class=\"wikilink\" href=\"/project/test/wiki/test\">test</a></p>"
expected_result = "<p><a class=\"reference wiki\" href=\"http://localhost:9001/project/test/wiki/test\" title=\"test\">test</a></p>"
assert render(dummy_project, "[[test]]") == expected_result


def test_render_wikilink_with_custom_title():
expected_result = "<p><a class=\"wikilink\" href=\"/project/test/wiki/test\">custom</a></p>"
expected_result = "<p><a class=\"reference wiki\" href=\"http://localhost:9001/project/test/wiki/test\" title=\"custom\">custom</a></p>"
assert render(dummy_project, "[[test|custom]]") == expected_result


Expand Down

0 comments on commit 30ca702

Please sign in to comment.