Skip to content

Commit

Permalink
Add tooltips to markdown help texts
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenklar committed Mar 26, 2021
1 parent 2528998 commit 13fef16
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 deletions.
8 changes: 2 additions & 6 deletions rdmo/core/serializers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
from markdown import markdown as markdown_function

from django.contrib.auth.models import Group
from django.contrib.sites.models import Site
from django.utils.encoding import force_text

from rest_framework import serializers

from rdmo.core.utils import get_languages
from rdmo.core.utils import get_languages, markdown2html


class RecursiveField(serializers.Serializer):
Expand Down Expand Up @@ -36,7 +32,7 @@ def to_representation(self, instance):

for markdown_field in self.markdown_fields:
if markdown_field in response and response[markdown_field]:
response[markdown_field] = markdown_function(force_text(response[markdown_field]))
response[markdown_field] = markdown2html(response[markdown_field])

return response

Expand Down
6 changes: 6 additions & 0 deletions rdmo/core/static/core/css/base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ code {
}
}

u {
cursor: help;
text-decoration: underline;
text-decoration-style: dashed;
}

table {
p {
margin-bottom: 5px;
Expand Down
5 changes: 2 additions & 3 deletions rdmo/core/templatetags/core_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
from django.urls import resolve, reverse
from django.urls.resolvers import Resolver404
from django.utils import translation
from django.utils.encoding import force_text
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.utils.translation import get_language, to_locale
from markdown import markdown as markdown_function

from rdmo import __version__
from rdmo.core.utils import markdown2html

register = template.Library()

Expand Down Expand Up @@ -160,7 +159,7 @@ def back_to_project_link(context):
@register.filter(is_safe=True)
@stringfilter
def markdown(value):
return mark_safe(markdown_function(force_text(value)))
return mark_safe(markdown2html(value))


@register.simple_tag
Expand Down
13 changes: 13 additions & 0 deletions rdmo/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
from django.conf import settings
from django.http import Http404, HttpResponse, HttpResponseBadRequest
from django.template.loader import get_template
from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _
from markdown import markdown

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -300,3 +302,14 @@ def human2bytes(string):
return number * 1024**4
elif unit == 'pib':
return number * 1024**5


def markdown2html(markdown_string):
# adoption of the normal markdown function which also converts
# `[<string>]{<title>}` to <u title="<title>"><string></u> to
# allow for underlined tooltips
html = markdown(force_text(markdown_string))
html = re.sub(r'\[(.*?)\]\{(.*?)\}',
r'<u data-toggle="tooltip" data-placement="bottom" data-html="true" title="\2">\1</u>',
html)
return html
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ angular.module('project_questions')
$window.scrollTo(0, 0);
back = false;

$timeout(function() {
$('[data-toggle="tooltip"]').tooltip();
});
}, function (result) {
if (result === false) {
// checkConditions returned $q.reject
Expand Down
6 changes: 6 additions & 0 deletions rdmo/projects/templates/projects/project_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
{% compress css %}
<link rel="stylesheet" href="{% static 'projects/css/project.scss' %}" type="text/x-scss" />
{% endcompress %}

<script>
$(function () {
$('[data-toggle="tooltip"]').tooltip()
})
</script>
{% endblock %}

{% block sidebar %}
Expand Down

0 comments on commit 13fef16

Please sign in to comment.