Merge pull request #170 from taigaio/bug/1668/fix-wiki-links-in-markdown
Fix wikilink extension: use absolute urlsremotes/origin/enhancement/email-actions
commit
5130b32b0f
|
@ -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
|
||||
|
|
|
@ -63,13 +63,13 @@ bleach.ALLOWED_ATTRIBUTES["img"] = ["alt", "src"]
|
|||
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),
|
||||
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue