forked from Ghostkeeper/SettingsGuide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQtMarkdownRenderer.py
82 lines (71 loc) · 4.02 KB
/
QtMarkdownRenderer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#Copyright (C) 2020 Ghostkeeper
#This plug-in is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
#This plug-in is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for details.
#You should have received a copy of the GNU Affero General Public License along with this plug-in. If not, see <https://gnu.org/licenses/>.
from .Mistune import mistune #Extending from this library's renderer.
import os.path #To fix the source paths for images.
import PyQt5.QtCore #To fix the source paths for images using QUrl.
import UM.Qt.Bindings.Theme #To get the correct hyperlink colour from the theme.
class QtMarkdownRenderer(mistune.Renderer):
"""
Specialises the Mistune renderer in order to be better compatible with Qt's
rich text.
Mistune converts Markdown into HTML. However its choice for the HTML
elements to use is not always supported by the limited HTML subset that Qt
can display with its "rich text" rendering. This class makes sure that the
supported subset of HTML is used.
"""
def __init__(self, images_path):
"""
Creates a renderer with a certain relative path for resources.
:param images_path: The path to which relative paths should be
dereferenced.
"""
super().__init__()
self._images_path = images_path
def link(self, link, title, text):
"""
Renders a given link with content and title.
This gives the link the correct colour, since Qt's linkColor property
seems to be completely broken. This wraps the link text with a <font>
tag that adjusts its colour.
:param link: The URL to link towards.
:param title: The description text for the link. This is not shown by Qt
though, so in the end the argument will not be used.
:param text: The text to display (in the correct colour).
:return: HTML for Qt's Rich Text to display the link.
"""
link = mistune.escape_link(link)
link_colour = UM.Qt.Bindings.Theme.Theme.getInstance().getColor("text_link").name()
if "://" not in link and link.endswith(".md"): # Link to a different article.
link = os.path.join(self._images_path, link)
if not title:
return "<a href=\"{link}\"><font color=\"{colour}\">{text}</font></a>".format(colour=link_colour, link=link, text=text)
title = mistune.escape(title, quote=True)
return "<a href=\"{link}\" title=\"{title}\"><font color=\"{colour}\">{text}</font></a>".format(colour=link_colour, title=title, link=link, text=text)
def emphasis(self, text: str) -> str:
"""
Rendering *emphasis* text.
By default, Mistune emphasises text using the <em> tag. Even though the
QML documentation claims that <em> behaves the same as <i>,
experimentally it seems that <em> doesn't work while <i> does.
:param text: The emphasised text.
:return: That text in italics.
"""
return "<i>{text}</i>".format(text=text)
def image(self, src, title, alt_text):
"""
Renders an image with a title (normally displayed on hover) and an alt-
text that gets displayed if the image is not available.
:param src: The path to the image, relative to the "articles" directory.
:param title: A text to display with the image. This gets ignored
because Qt's Rich Text doesn't support it.
:param alt_text: A text to display in case the image can't get loaded.
This gets ignored because Qt's Rich Text doesn't support it.
:return: HTML for Qt's Rich Text to display the image.
"""
image_full_path = os.path.join(self._images_path, src)
image_url = PyQt5.QtCore.QUrl.fromLocalFile(image_full_path).url()
margin = UM.Qt.Bindings.Theme.Theme.getInstance().getSize("default_margin").width()
width = UM.Qt.Bindings.Theme.Theme.getInstance().getSize("tooltip").width() / 3 - margin * 2 # Fit 3 images in the width.
return "<img src=\"{image_url}\" width=\"{width}\" />".format(image_url=image_url, width=width)