Adding render_and_extract function to mdrender service
parent
6a48e28ca4
commit
5e95175654
|
@ -63,4 +63,7 @@ class MentionsPattern(Pattern):
|
||||||
a.set('alt', user.get_full_name())
|
a.set('alt', user.get_full_name())
|
||||||
a.set('title', user.get_full_name())
|
a.set('title', user.get_full_name())
|
||||||
a.set('class', "mention")
|
a.set('class', "mention")
|
||||||
|
|
||||||
|
self.md.extracted_data['mentions'].append(user)
|
||||||
|
|
||||||
return a
|
return a
|
||||||
|
|
|
@ -87,4 +87,7 @@ class TaigaReferencesPattern(Pattern):
|
||||||
a.set('alt', subject)
|
a.set('alt', subject)
|
||||||
a.set('title', subject)
|
a.set('title', subject)
|
||||||
a.set('class', html_classes)
|
a.set('class', html_classes)
|
||||||
|
|
||||||
|
self.md.extracted_data['references'].append(instance.content_object)
|
||||||
|
|
||||||
return a
|
return a
|
||||||
|
|
|
@ -4,8 +4,7 @@ import functools
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.utils.encoding import force_bytes
|
from django.utils.encoding import force_bytes
|
||||||
|
|
||||||
from markdown import markdown
|
from markdown import Markdown
|
||||||
from fn import F
|
|
||||||
|
|
||||||
from .extensions.autolink import AutolinkExtension
|
from .extensions.autolink import AutolinkExtension
|
||||||
from .extensions.automail import AutomailExtension
|
from .extensions.automail import AutomailExtension
|
||||||
|
@ -53,31 +52,46 @@ def cache_by_sha(func):
|
||||||
return _decorator
|
return _decorator
|
||||||
|
|
||||||
|
|
||||||
#@cache_by_sha
|
def _get_markdown(project):
|
||||||
def render(project, text):
|
|
||||||
wikilinks_config = {"base_url": "#/project/{}/wiki/".format(project.slug),
|
wikilinks_config = {"base_url": "#/project/{}/wiki/".format(project.slug),
|
||||||
"end_url": ""}
|
"end_url": ""}
|
||||||
extensions = _make_extensions_list(wikilinks_config=wikilinks_config, project=project)
|
extensions = _make_extensions_list(wikilinks_config=wikilinks_config,
|
||||||
return markdown(text, extensions=extensions)
|
project=project)
|
||||||
|
md = Markdown(extensions=extensions)
|
||||||
|
md.extracted_data = {"mentions": [], "references": []}
|
||||||
|
return md
|
||||||
|
|
||||||
|
|
||||||
|
@cache_by_sha
|
||||||
|
def render(project, text):
|
||||||
|
md = _get_markdown(project)
|
||||||
|
return md.convert(text)
|
||||||
|
|
||||||
|
|
||||||
|
def render_and_extract(project, text):
|
||||||
|
md = _get_markdown(project)
|
||||||
|
result = md.convert(text)
|
||||||
|
return (result, md.extracted_data)
|
||||||
|
|
||||||
|
|
||||||
class DiffMatchPatch(diff_match_patch.diff_match_patch):
|
class DiffMatchPatch(diff_match_patch.diff_match_patch):
|
||||||
def diff_pretty_html(self, diffs):
|
def diff_pretty_html(self, diffs):
|
||||||
html = []
|
html = []
|
||||||
for (op, data) in diffs:
|
for (op, data) in diffs:
|
||||||
text = (data.replace("&", "&").replace("<", "<")
|
text = (data.replace("&", "&").replace("<", "<")
|
||||||
.replace(">", ">").replace("\n", "<br />"))
|
.replace(">", ">").replace("\n", "<br />"))
|
||||||
if op == self.DIFF_INSERT:
|
if op == self.DIFF_INSERT:
|
||||||
html.append("<ins style=\"background:#e6ffe6;\">%s</ins>" % text)
|
html.append("<ins style=\"background:#e6ffe6;\">%s</ins>" % text)
|
||||||
elif op == self.DIFF_DELETE:
|
elif op == self.DIFF_DELETE:
|
||||||
html.append("<del style=\"background:#ffe6e6;\">%s</del>" % text)
|
html.append("<del style=\"background:#ffe6e6;\">%s</del>" % text)
|
||||||
elif op == self.DIFF_EQUAL:
|
elif op == self.DIFF_EQUAL:
|
||||||
html.append("<span>%s</span>" % text)
|
html.append("<span>%s</span>" % text)
|
||||||
return "".join(html)
|
return "".join(html)
|
||||||
|
|
||||||
|
|
||||||
def get_diff_of_htmls(html1, html2):
|
def get_diff_of_htmls(html1, html2):
|
||||||
diffutil = DiffMatchPatch()
|
diffutil = DiffMatchPatch()
|
||||||
diff = diffutil.diff_main(html1, html2)
|
diff = diffutil.diff_main(html1, html2)
|
||||||
return diffutil.diff_pretty_html(diff)
|
return diffutil.diff_pretty_html(diff)
|
||||||
|
|
||||||
__all__ = ['render', 'get_diff_of_htmls']
|
__all__ = ["render", "get_diff_of_htmls", "render_and_extract"]
|
||||||
|
|
|
@ -5,7 +5,7 @@ import pytest
|
||||||
import taiga.base
|
import taiga.base
|
||||||
from taiga.mdrender.extensions import mentions
|
from taiga.mdrender.extensions import mentions
|
||||||
from taiga.mdrender.extensions import emojify
|
from taiga.mdrender.extensions import emojify
|
||||||
from taiga.mdrender.service import render, cache_by_sha, get_diff_of_htmls
|
from taiga.mdrender.service import render, cache_by_sha, get_diff_of_htmls, render_and_extract
|
||||||
|
|
||||||
from taiga.projects.references import services
|
from taiga.projects.references import services
|
||||||
|
|
||||||
|
@ -162,3 +162,18 @@ def test_get_diff_of_htmls_deletions():
|
||||||
def test_get_diff_of_htmls_modifications():
|
def test_get_diff_of_htmls_modifications():
|
||||||
result = get_diff_of_htmls("<p>test1</p>", "<p>1test</p>")
|
result = get_diff_of_htmls("<p>test1</p>", "<p>1test</p>")
|
||||||
assert result == "<span><p></span><ins style=\"background:#e6ffe6;\">1</ins><span>test</span><del style=\"background:#ffe6e6;\">1</del><span></p></span>"
|
assert result == "<span><p></span><ins style=\"background:#e6ffe6;\">1</ins><span>test</span><del style=\"background:#ffe6e6;\">1</del><span></p></span>"
|
||||||
|
|
||||||
|
def test_render_and_extract_references():
|
||||||
|
with patch("taiga.mdrender.extensions.references.get_instance_by_ref") as mock:
|
||||||
|
instance = mock.return_value
|
||||||
|
instance.content_type.model = "issue"
|
||||||
|
instance.content_object.subject = "test"
|
||||||
|
(_, extracted) = render_and_extract(dummy_project, "**#1**")
|
||||||
|
assert extracted['references'] == [instance.content_object]
|
||||||
|
|
||||||
|
def test_render_and_extract_mentions():
|
||||||
|
with patch("taiga.mdrender.extensions.mentions.User") as mock:
|
||||||
|
instance = mock.objects.get.return_value
|
||||||
|
instance.get_full_name.return_value = "test name"
|
||||||
|
(_, extracted) = render_and_extract(dummy_project, "**@user1**")
|
||||||
|
assert extracted['mentions'] == [instance]
|
||||||
|
|
Loading…
Reference in New Issue