UI/CodeDocumentationViewerTemplate.py

Sat, 21 Oct 2017 15:18:15 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 21 Oct 2017 15:18:15 +0200
changeset 5919
d0de2b378b24
parent 5914
e44c04a89dbc
child 5942
8b083e33c51f
permissions
-rw-r--r--

Improved and beautified the rich text display of the documentation viewer some more.

# -*- coding: utf-8 -*-

# Copyright (c) 2017 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing functions to prepare an HTML documentation view.
"""

from __future__ import unicode_literals

from PyQt5.QtCore import QCoreApplication

import Utilities


def prepareDocumentationViewerHtmlDocument(documentationInfo):
    """
    Public function to prepare the HTML document.
    
    @param documentationInfo dictionary containing the various documentation
        parts
    @type dict
    @return prepared HTML document
    @rtype str
    """
    mainTemplate = """
        <!DOCTYPE html>
        <html>
        <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <link rel="stylesheet" href="qrc:documentViewerStyle.css"
            type="text/css" />
        </head>
        <body>
            @HEADER@
            @DOCSTRING@
        </body>
        </html>
    """
    
    headerTemplate = """
        @TITLE@
        @METADATA@
    """
    
    titleTemplate = """
        <div class="title"><h1>@NAME@</h1></div>
    """
    
    metadataTemplate = """
        <div class="metadata">
        @ARGSPEC@
        @NOTE@
        </div>
    """
    
    argspecTemplate = """
        <p><b>Definition:</b> <span class="def">@NAME@@ARGSPEC@</span></p>
    """
    
    noteTemplate = """
        <p><b>Type:</b> @NOTE@</p>
    """
    
    docstringTemplate = """
        <div class="docstring">
        @DOCSTRING@
        </div>
    """
    
    name = documentationInfo["name"]
    if name:
        title = titleTemplate.replace("@NAME@", name)
        if documentationInfo["argspec"] or documentationInfo["note"]:
            if documentationInfo["argspec"]:
                argspec = Utilities.html_encode(documentationInfo["argspec"])
                for char in ['=', ',', '(', ')', '*', '**']:
                    argspec = argspec.replace(
                        char,
                        '<span class="argspec-highlight">{0}</span>'.format(
                            char))
                argspec = argspecTemplate\
                    .replace("@NAME@", name)\
                    .replace("@ARGSPEC@", argspec)
            else:
                argspec = ""
            if documentationInfo["note"]:
                note = noteTemplate.replace("@NOTE@",
                                            documentationInfo["note"])
            else:
                note = ""
            metaData = metadataTemplate\
                .replace("@ARGSPEC@", argspec)\
                .replace("@NOTE@", note)
        else:
            metaData = ""
        
        header = headerTemplate\
            .replace("@TITLE@", title)\
            .replace("@METADATA@", metaData)
    else:
        header = ""
    
    if documentationInfo["docstring"]:
        docstring = documentationInfo["docstring"]\
            .replace("\r\n", "<br/>")\
            .replace("\n", "<br/>")\
            .replace("\r", "<br/>")
        docstring = docstringTemplate.replace("@DOCSTRING@", docstring)
    else:
        docstring = \
            """<div class="hr"></div><div id="doc-warning">{0}</div>"""\
            .format(QCoreApplication.translate(
                "CodeDocumentationViewer",
                "No further documentation available"))
    
    return mainTemplate\
        .replace("@HEADER@", header)\
        .replace("@DOCSTRING@", docstring)


def prepareDocumentationViewerHtmlDocWarningDocument(text):
    """
    Public function to prepare a HTML warning document.
    
    @param text warning text to be shown
    @type str
    @return prepared HTML document
    @rtype str
    """
    mainTemplate = """
        <!DOCTYPE html>
        <html>
        <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <link rel="stylesheet" href="qrc:documentViewerStyle.css"
            type="text/css" />
        </head>
        <body>
            <div id="doc-warning">@TEXT@</div>
        </body>
        </html>
    """
    
    return mainTemplate.replace("@TEXT@", text)


def prepareDocumentationViewerHtmlWarningDocument(text):
    """
    Public function to prepare a HTML warning document.
    
    @param text warning text to be shown
    @type str
    @return prepared HTML document
    @rtype str
    """
    mainTemplate = """
        <!DOCTYPE html>
        <html>
        <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <link rel="stylesheet" href="qrc:documentViewerStyle.css"
            type="text/css" />
        </head>
        <body>
            <div id="warning">@TEXT@</div>
        </body>
        </html>
    """
    
    return mainTemplate.replace("@TEXT@", text)

eric ide

mercurial