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
|
# 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/>.
|
# 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 import Extension
|
||||||
from markdown.inlinepatterns import Pattern
|
from markdown.inlinepatterns import Pattern
|
||||||
from markdown.util import etree
|
from markdown.util import etree
|
||||||
import re
|
|
||||||
|
|
||||||
|
from taiga.front import resolve
|
||||||
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(Extension):
|
class WikiLinkExtension(Extension):
|
||||||
def __init__(self, configs):
|
def __init__(self, project, *args, **kwargs):
|
||||||
# set extension defaults
|
self.project = project
|
||||||
self.config = {
|
return super().__init__(*args, **kwargs)
|
||||||
'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 extendMarkdown(self, md, md_globals):
|
def extendMarkdown(self, md, md_globals):
|
||||||
self.md = md
|
WIKILINK_RE = r"\[\[([\w0-9_ -]+)(\|[\w0-9_ -]+)?\]\]"
|
||||||
|
wikilinkPattern = WikiLinksPattern(WIKILINK_RE, self.project)
|
||||||
# append to end of inline patterns
|
|
||||||
WIKILINK_RE = r'\[\[([\w0-9_ -]+)(\|[\w0-9_ -]+)?\]\]'
|
|
||||||
wikilinkPattern = WikiLinks(WIKILINK_RE, self.getConfigs())
|
|
||||||
wikilinkPattern.md = md
|
wikilinkPattern.md = md
|
||||||
md.inlinePatterns.add('wikilink', wikilinkPattern, "<not_strong")
|
md.inlinePatterns.add("wikilink", wikilinkPattern, "<not_strong")
|
||||||
|
|
||||||
|
|
||||||
class WikiLinks(Pattern):
|
class WikiLinksPattern(Pattern):
|
||||||
def __init__(self, pattern, config):
|
def __init__(self, pattern, project):
|
||||||
super(WikiLinks, self).__init__(pattern)
|
self.project = project
|
||||||
self.config = config
|
super().__init__(pattern)
|
||||||
|
|
||||||
def handleMatch(self, m):
|
def handleMatch(self, m):
|
||||||
base_url, end_url, html_class = self._getMeta()
|
|
||||||
label = m.group(2).strip()
|
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):
|
if m.group(3):
|
||||||
title = m.group(3).strip()[1:]
|
title = m.group(3).strip()[1:]
|
||||||
else:
|
else:
|
||||||
title = label
|
title = label
|
||||||
|
|
||||||
a = etree.Element('a')
|
a = etree.Element("a")
|
||||||
a.text = title
|
a.text = title
|
||||||
a.set('href', url)
|
a.set("href", url)
|
||||||
if html_class:
|
a.set("title", title)
|
||||||
a.set('class', html_class)
|
a.set("class", "reference wiki")
|
||||||
return a
|
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"]
|
bleach.ALLOWED_ATTRIBUTES["*"] = ["class", "style"]
|
||||||
|
|
||||||
|
|
||||||
def _make_extensions_list(wikilinks_config=None, project=None):
|
def _make_extensions_list(project=None):
|
||||||
return [AutolinkExtension(),
|
return [AutolinkExtension(),
|
||||||
AutomailExtension(),
|
AutomailExtension(),
|
||||||
SemiSaneListExtension(),
|
SemiSaneListExtension(),
|
||||||
SpacedLinkExtension(),
|
SpacedLinkExtension(),
|
||||||
StrikethroughExtension(),
|
StrikethroughExtension(),
|
||||||
WikiLinkExtension(wikilinks_config),
|
WikiLinkExtension(project),
|
||||||
EmojifyExtension(),
|
EmojifyExtension(),
|
||||||
MentionsExtension(),
|
MentionsExtension(),
|
||||||
TaigaReferencesExtension(project),
|
TaigaReferencesExtension(project),
|
||||||
|
@ -103,11 +103,7 @@ def _get_markdown(project):
|
||||||
def build_url(*args, **kwargs):
|
def build_url(*args, **kwargs):
|
||||||
return args[1] + slugify(args[0])
|
return args[1] + slugify(args[0])
|
||||||
|
|
||||||
wikilinks_config = {"base_url": "/project/{}/wiki/".format(project.slug),
|
extensions = _make_extensions_list(project=project)
|
||||||
"end_url": "",
|
|
||||||
"build_url": build_url}
|
|
||||||
extensions = _make_extensions_list(wikilinks_config=wikilinks_config,
|
|
||||||
project=project)
|
|
||||||
md = Markdown(extensions=extensions)
|
md = Markdown(extensions=extensions)
|
||||||
md.extracted_data = {"mentions": [], "references": []}
|
md.extracted_data = {"mentions": [], "references": []}
|
||||||
return md
|
return md
|
||||||
|
|
|
@ -106,12 +106,12 @@ def test_render_relative_link():
|
||||||
|
|
||||||
|
|
||||||
def test_render_wikilink():
|
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
|
assert render(dummy_project, "[[test]]") == expected_result
|
||||||
|
|
||||||
|
|
||||||
def test_render_wikilink_with_custom_title():
|
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
|
assert render(dummy_project, "[[test|custom]]") == expected_result
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue