--- a/UI/CodeDocumentationViewer.py Sat Oct 21 19:22:16 2017 +0200 +++ b/UI/CodeDocumentationViewer.py Sun Oct 22 14:51:02 2017 +0200 @@ -211,10 +211,6 @@ self.__disabledString = self.tr( "No source code documentation provider has been registered or" " this function has been disabled.") - - self.__processingThread = DocumentProcessingThread() - self.__processingThread.htmlReady.connect(self.__setHtml) - self.__processingThread.warning.connect(self.__setHtmlWarning) def __setupUi(self): """ @@ -312,6 +308,8 @@ provider = self.__disabledProvider self.providerComboBox.setCurrentIndex(index) self.__selectedProvider = provider + if index == 0: + self.documentationReady(self.__disabledString, isWarning=True) # TODO: document this hook in the plug-in document def registerProvider(self, providerName, providerDisplay, provider, @@ -433,18 +431,17 @@ """ self.__ui.activateCodeDocumentationViewer(switchFocus=False) - self.__lastDocumentation = documentationInfo + if not isWarning and not isDocWarning: + self.__lastDocumentation = documentationInfo - if documentationInfo is not None: - if not documentationInfo: - if self.__selectedProvider == self.__disabledProvider: - self.documentationReady(self.__disabledString, - isWarning=True) - else: - self.documentationReady(self.__noDocumentationString, - isDocWarning=True) - return - + if not documentationInfo: + if self.__selectedProvider == self.__disabledProvider: + self.documentationReady(self.__disabledString, + isWarning=True) + else: + self.documentationReady(self.__noDocumentationString, + isDocWarning=True) + else: if self.__showMarkdown: if isWarning: html = prepareDocumentationViewerHtmlWarningDocument( @@ -456,43 +453,38 @@ html = prepareDocumentationViewerHtmlDocument( documentationInfo) else: - html = "" - if html: - self.__setHtml(html) - return - - if isinstance(documentationInfo, str): - fullText = documentationInfo - elif isinstance(documentationInfo, dict): - name = documentationInfo["name"] - if name: - title = "".join([name, "\n", - "=" * len(name), "\n\n"]) - else: - title = "" + html = documentationInfo + self.__setHtml(html) + else: + if isinstance(documentationInfo, str): + fullText = documentationInfo + elif isinstance(documentationInfo, dict): + name = documentationInfo["name"] + if name: + title = "".join([name, "\n", + "=" * len(name), "\n\n"]) + else: + title = "" - if documentationInfo["argspec"]: - definition = self.tr("Definition: {0}{1}\n").format( - name, documentationInfo["argspec"]) - else: - definition = '' + if documentationInfo["argspec"]: + definition = self.tr("Definition: {0}{1}\n").format( + name, documentationInfo["argspec"]) + else: + definition = '' - if documentationInfo["note"]: - note = self.tr("Info: {0}\n\n----\n\n").format( - documentationInfo["note"]) - else: - note = "" + if documentationInfo["note"]: + note = self.tr("Info: {0}\n\n----\n\n").format( + documentationInfo["note"]) + else: + note = "" + + if documentationInfo["docstring"] is None: + docString = "" + else: + docString = documentationInfo["docstring"] + + fullText = "".join([title, definition, note, docString]) - if documentationInfo["docstring"] is None: - docString = "" - else: - docString = documentationInfo["docstring"] - - fullText = "".join([title, definition, note, docString]) - - if self.__showMarkdown: - self.__processingThread.process("markdown", fullText) - else: self.__plainTextViewer.setText(fullText) def __setHtml(self, html): @@ -579,122 +571,3 @@ self.documentationReady(self.__lastDocumentation) Preferences.setDocuViewer("ShowInfoAsRichText", richText) - - -class DocumentProcessingThread(QThread): - """ - Class implementing a thread to process some text into HTML usable by the - viewer. - - @signal htmlReady(str) emitted with the processed HTML to signal the - availability of the processed HTML - @signal warning(str) emitted with an error or warning message - """ - htmlReady = pyqtSignal(str) - warning = pyqtSignal(str) - - def __init__(self, parent=None): - """ - Constructor - - @param parent reference to the parent object (QObject) - """ - super(DocumentProcessingThread, self).__init__() - - def process(self, language, text): - """ - Public method to convert the given text to HTML. - - @param language language of the text - @type str - @param text text to be processed - @type str - """ - if self.wait(): - self.__language = language - self.__text = text - self.start() - - def run(self): - """ - Public thread method to convert the stored data. - """ - language = self.__language - text = self.__text - - if language == "markdown": - html = self.__convertMarkdown(text, True, "html5") - self.htmlReady.emit(html) - else: - html = self.tr("Format <b>{0}</b> is not supported.")\ - .format(language) - self.warning.emit(html) - - def __convertMarkdown(self, text, convertNewLineToBreak, htmlFormat): - """ - Private method to convert Markdown text into HTML. - - @param text text to be processed - @type str - @param convertNewLineToBreak flag indicating to convert new lines - to HTML break - @type bool - @param htmlFormat HTML format to be generated by markdown - @type str - @return processed HTML - @rtype str - """ - try: - import markdown # __IGNORE_EXCEPTION__ - except ImportError: - return self.tr( - """<p>Markdown view requires the <b>Markdown</b> """ - """package.<br/>Install it with your package manager,""" - """ 'pip install Markdown' or see """ - """<a href="http://pythonhosted.org/Markdown/install.html">""" - """installation instructions.</a></p>""") - - try: - import mdx_mathjax # __IGNORE_EXCEPTION__ __IGNORE_WARNING__ - except ImportError: - # mathjax doesn't require import statement if installed - # as extension - pass - - if convertNewLineToBreak: - extensions = ['fenced_code', 'nl2br', 'extra'] - else: - extensions = ['fenced_code', 'extra'] - - # version 2.0 supports only extension names, not instances - if markdown.version_info[0] > 2 or \ - (markdown.version_info[0] == 2 and - markdown.version_info[1] > 0): - class _StrikeThroughExtension(markdown.Extension): - """ - Class is placed here, because it depends on imported markdown, - and markdown import is lazy. - - (see https://pythonhosted.org/Markdown/extensions/api.html - this page for details) - """ - DEL_RE = r'(~~)(.*?)~~' - - def extendMarkdown(self, md, md_globals): - # Create the del pattern - del_tag = markdown.inlinepatterns.SimpleTagPattern( - self.DEL_RE, 'del') - # Insert del pattern into markdown parser - md.inlinePatterns.add('del', del_tag, '>not_strong') - - extensions.append(_StrikeThroughExtension()) - - try: - return markdown.markdown(text, extensions=extensions + ['mathjax'], - output_format=htmlFormat.lower()) - except (ImportError, ValueError): - # markdown raises ValueError or ImportError, depends on version - # It is not clear, how to distinguish missing mathjax from other - # errors. So keep going without mathjax. - return markdown.markdown(text, extensions=extensions, - output_format=htmlFormat.lower())