eric6/UI/CodeDocumentationViewerTemplate.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7229
53054eb5b15a
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2017 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing functions to prepare an HTML documentation view.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import QCoreApplication
13
14 import Utilities
15
16
17 def prepareDocumentationViewerHtmlDocument(documentationInfo):
18 """
19 Public function to prepare the HTML document.
20
21 @param documentationInfo dictionary containing the various documentation
22 parts
23 @type dict
24 @return prepared HTML document
25 @rtype str
26 """
27 mainTemplate = """
28 <!DOCTYPE html>
29 <html>
30 <head>
31 <meta http-equiv="content-type" content="text/html; charset=utf-8">
32 <link rel="stylesheet" href="qrc:documentViewerStyle.css"
33 type="text/css" />
34 </head>
35 <body>
36 @HEADER@
37 @DOCSTRING@
38 </body>
39 </html>
40 """
41
42 headerTemplate = """
43 @TITLE@
44 @METADATA@
45 """
46
47 titleTemplate = """
48 <div class="title"><h1>@NAME@</h1></div>
49 """
50
51 metadataTemplate = """
52 <div class="metadata">
53 @ARGSPEC@
54 @TYPE@
55 @NOTE@
56 </div>
57 """
58
59 argspecTemplate = QCoreApplication.translate(
60 "CodeDocumentationViewer",
61 '<p><b>Definition:</b> <span class="def">@NAME@@ARGSPEC@</span></p>',
62 "Just translate 'Definition:' and leave the rest intact.")
63
64 typeTemplate = QCoreApplication.translate(
65 "CodeDocumentationViewer",
66 "<p><b>Type:</b> @TYPE@</p>",
67 "Just translate 'Type:' and leave the rest intact.")
68
69 noteTemplate = QCoreApplication.translate(
70 "CodeDocumentationViewer",
71 "<p><b>Note:</b> @NOTE@</p>",
72 "Just translate 'Note:' and leave the rest intact.")
73
74 docstringTemplate = """
75 <div class="docstring">
76 @DOCSTRING@
77 </div>
78 """
79
80 name = documentationInfo["name"]
81 if name:
82 title = titleTemplate.replace("@NAME@", name)
83 if "argspec" in documentationInfo and documentationInfo["argspec"]:
84 argspec = Utilities.html_encode(documentationInfo["argspec"])
85 for char in ['=', ',', '(', ')', '*', '**']:
86 argspec = argspec.replace(
87 char,
88 '<span class="argspec-highlight">{0}</span>'.format(
89 char))
90 argspec = argspecTemplate\
91 .replace("@NAME@", name)\
92 .replace("@ARGSPEC@", argspec)
93 else:
94 argspec = argspecTemplate\
95 .replace("@NAME@", name)\
96 .replace("@ARGSPEC@", "")
97
98 if "typ" in documentationInfo and documentationInfo["typ"]:
99 typeInfo = typeTemplate.replace("@TYPE@",
100 documentationInfo["typ"])
101 else:
102 typeInfo = ""
103
104 if "note" in documentationInfo and documentationInfo["note"]:
105 note = noteTemplate.replace("@NOTE@",
106 documentationInfo["note"])
107 else:
108 note = ""
109
110 metaData = metadataTemplate\
111 .replace("@ARGSPEC@", argspec)\
112 .replace("@TYPE@", typeInfo)\
113 .replace("@NOTE@", note)
114
115 header = headerTemplate\
116 .replace("@TITLE@", title)\
117 .replace("@METADATA@", metaData)
118 else:
119 header = ""
120
121 if "docstring" in documentationInfo and documentationInfo["docstring"]:
122 docstring = documentationInfo["docstring"]\
123 .replace("\r\n", "<br/>")\
124 .replace("\n", "<br/>")\
125 .replace("\r", "<br/>")
126 docstring = docstringTemplate.replace("@DOCSTRING@", docstring)
127 else:
128 docstring = \
129 """<div class="hr"></div><div id="doc-warning">{0}</div>"""\
130 .format(QCoreApplication.translate(
131 "CodeDocumentationViewer",
132 "No further documentation available"))
133
134 return mainTemplate\
135 .replace("@HEADER@", header)\
136 .replace("@DOCSTRING@", docstring)
137
138
139 def prepareDocumentationViewerHtmlDocWarningDocument(text):
140 """
141 Public function to prepare a HTML warning document.
142
143 @param text warning text to be shown
144 @type str
145 @return prepared HTML document
146 @rtype str
147 """
148 mainTemplate = """
149 <!DOCTYPE html>
150 <html>
151 <head>
152 <meta http-equiv="content-type" content="text/html; charset=utf-8">
153 <link rel="stylesheet" href="qrc:documentViewerStyle.css"
154 type="text/css" />
155 </head>
156 <body>
157 <div id="doc-warning">@TEXT@</div>
158 </body>
159 </html>
160 """
161
162 return mainTemplate.replace("@TEXT@", text)
163
164
165 def prepareDocumentationViewerHtmlWarningDocument(text):
166 """
167 Public function to prepare a HTML warning document.
168
169 @param text warning text to be shown
170 @type str
171 @return prepared HTML document
172 @rtype str
173 """
174 mainTemplate = """
175 <!DOCTYPE html>
176 <html>
177 <head>
178 <meta http-equiv="content-type" content="text/html; charset=utf-8">
179 <link rel="stylesheet" href="qrc:documentViewerStyle.css"
180 type="text/css" />
181 </head>
182 <body>
183 <div id="warning">@TEXT@</div>
184 </body>
185 </html>
186 """
187
188 return mainTemplate.replace("@TEXT@", text)

eric ide

mercurial