Beispiel #1
0
    def getHtml(self, highlight=True):
        """
        Returns the comment text converted to HTML.

        :param highlight: Whether to highlight the code
        :type highlight: bool

        """

        if not Text.supportsMarkdown:
            raise UserError(
                "Markdown is not supported by the system. Documentation comments could converted to HTML."
            )

        if highlight:

            if self.__highlightedText is None:

                highlightedText = ""

                for block in self.__blocks:

                    if block["type"] == "comment":
                        highlightedText += Text.highlightCodeBlocks(
                            Text.markdownToHtml(block["processed"]))
                    else:
                        highlightedText += "\n%s" % Text.highlightCodeBlocks(
                            Text.markdownToHtml(block["text"]))

                self.__highlightedText = highlightedText

            return self.__highlightedText

        else:

            if self.__processedText is None:

                processedText = ""

                for block in self.__blocks:

                    if block["type"] == "comment":
                        processedText += Text.markdownToHtml(
                            block["processed"])
                    else:
                        processedText += "\n%s\n\n" % block["text"]

                self.__processedText = processedText.strip()

            return self.__processedText
Beispiel #2
0
    def getHtml(self, highlight=True):
        """
        Returns the comment text converted to HTML.

        :param highlight: Whether to highlight the code
        :type highlight: bool

        """

        if not Text.supportsMarkdown:
            raise UserError("Markdown is not supported by the system. Documentation comments could converted to HTML.")

        if highlight:

            if self.__highlightedText is None:

                highlightedText = ""

                for block in self.__blocks:

                    if block["type"] == "comment":
                        highlightedText += Text.highlightCodeBlocks(Text.markdownToHtml(block["processed"]))
                    else:
                        highlightedText += "\n%s" % Text.highlightCodeBlocks(Text.markdownToHtml(block["text"]))

                self.__highlightedText = highlightedText

            return self.__highlightedText

        else:

            if self.__processedText is None:

                processedText = ""

                for block in self.__blocks:

                    if block["type"] == "comment":
                        processedText += Text.markdownToHtml(block["processed"])
                    else:
                        processedText += "\n%s\n\n" % block["text"]

                self.__processedText = processedText.strip()

            return self.__processedText
Beispiel #3
0
    def getApi(self):
        field = "api[%s]" % self.id
        apidata = self.project.getCache().read(field, self.getModificationTime())

        if not Text.supportsMarkdown:
            raise UserError("Missing Markdown feature to convert package docs into HTML.")

        if apidata is None:
            apidata = Data.ApiData(self.id)
            apidata.main["type"] = "Package"
            apidata.main["doc"] = Text.highlightCodeBlocks(Text.markdownToHtml(self.getText()))

            self.project.getCache().store(field, apidata, self.getModificationTime())

        return apidata
Beispiel #4
0
    def getApi(self):
        field = "api[%s]" % self.id
        apidata = self.project.getCache().read(field,
                                               self.getModificationTime())

        if not Text.supportsMarkdown:
            raise UserError(
                "Missing Markdown feature to convert package docs into HTML.")

        if apidata is None:
            apidata = Data.ApiData(self.id)
            apidata.main["type"] = "Package"
            apidata.main["doc"] = Text.highlightCodeBlocks(
                Text.markdownToHtml(self.getText()))

            self.project.getCache().store(field, apidata,
                                          self.getModificationTime())

        return apidata
Beispiel #5
0
    def __splitBlocks(self, text):
        """
        Splits up text and code blocks in comments.

        This will try to use Misaka for Markdown parsing if available and will fallback to a simpler implementation in
        order to allow processing of doc parameters and links without Misaka being installed.

        """

        if not Text.supportsMarkdown:
            return self.__splitSimple(text)

        marked = Text.markdownToHtml(text)

        def unescape(html):
            html = html.replace('&lt;', '<')
            html = html.replace('&gt;', '>')
            html = html.replace('&amp;', '&')
            html = html.replace('&quot;', '"')
            return html.replace('&#39;', "'")

        parts = []

        lineNo = 0
        lines = text.split("\n")
        markedLines = marked.split("\n")

        i = 0
        while i < len(markedLines):

            l = markedLines[i]

            # the original text of the line
            parsed = unescape(stripMarkup.sub("", l))

            # start of a code block, grab all text before it and move it into a block
            if l.startswith('<pre><code>'):

                # everything since the last code block and before this one must be text
                comment = []
                for s in range(lineNo, len(lines)):

                    source = lines[s]
                    if source.strip() == parsed.strip():
                        lineNo = s
                        break

                    comment.append(source)

                parts.append({"type": "comment", "text": "\n".join(comment)})

                # Find the end of the code block
                e = i
                while i < len(markedLines):
                    l = markedLines[i]
                    i += 1

                    if l.startswith('</code></pre>'):
                        break

                lineCount = (i - e) - 1

                # add the code block
                parts.append({
                    "type":
                    "code",
                    "text":
                    "\n".join(lines[lineNo:lineNo + lineCount])
                })

                lineNo += lineCount

            else:
                i += 1

        # append the rest of the comment as text
        parts.append({"type": "comment", "text": "\n".join(lines[lineNo:])})

        return parts
Beispiel #6
0
    def __splitBlocks(self, text):
        """
        Splits up text and code blocks in comments.

        This will try to use Misaka for Markdown parsing if available and will fallback to a simpler implementation in
        order to allow processing of doc parameters and links without Misaka being installed.

        """

        if not Text.supportsMarkdown:
            return self.__splitSimple(text)

        marked = Text.markdownToHtml(text)

        def unescape(html):
            html = html.replace('&lt;', '<')
            html = html.replace('&gt;', '>')
            html = html.replace('&amp;', '&')
            html = html.replace('&quot;', '"')
            return html.replace('&#39;', "'")

        parts = []

        lineNo = 0
        lines = text.split("\n")
        markedLines = marked.split("\n")

        i = 0
        while i < len(markedLines):

            l = markedLines[i]

            # the original text of the line
            parsed = unescape(stripMarkup.sub("", l))

            # start of a code block, grab all text before it and move it into a block
            if l.startswith('<pre><code>'):

                # everything since the last code block and before this one must be text
                comment = []
                for s in range(lineNo, len(lines)):

                    source = lines[s]
                    if source.strip() == parsed.strip():
                        lineNo = s
                        break

                    comment.append(source)

                parts.append({
                    "type": "comment",
                    "text": "\n".join(comment)
                })

                # Find the end of the code block
                e = i
                while i < len(markedLines):
                    l = markedLines[i]
                    i += 1

                    if l.startswith('</code></pre>'):
                        break

                lineCount = (i - e) - 1

                # add the code block
                parts.append({
                    "type": "code",
                    "text": "\n".join(lines[lineNo:lineNo + lineCount])
                })

                lineNo += lineCount

            else:
                i += 1

        # append the rest of the comment as text
        parts.append({
            "type": "comment",
            "text": "\n".join(lines[lineNo:])
        })

        return parts
Beispiel #7
0
    def test_markdown(self):
        
        self.assertEqual(Text.markdownToHtml("*emphased*"), "<p><em>emphased</em></p>\n")
        self.assertEqual(Text.markdownToHtml("**bold**"), "<p><strong>bold</strong></p>\n")

        self.assertEqual(Text.markdownToHtml("# Header 1"), "<h1>Header 1</h1>\n")
        self.assertEqual(Text.markdownToHtml("## Header 2"), "<h2>Header 2</h2>\n")
        self.assertEqual(Text.markdownToHtml("### Header 3"), "<h3>Header 3</h3>\n")
        self.assertEqual(Text.markdownToHtml("#### Header 4"), "<h4>Header 4</h4>\n")
        self.assertEqual(Text.markdownToHtml("##### Header 5"), "<h5>Header 5</h5>\n")
        self.assertEqual(Text.markdownToHtml("###### Header 6"), "<h6>Header 6</h6>\n")

        self.assertEqual(Text.markdownToHtml("""
Paragraph 1

Paragraph 2
        """), "<p>Paragraph 1</p>\n\n<p>Paragraph 2</p>\n")

        self.assertEqual(Text.markdownToHtml("""
- Item 1
- Item 2
- Item 3
        """), "<ul>\n<li>Item 1</li>\n<li>Item 2</li>\n<li>Item 3</li>\n</ul>\n")        

        self.assertEqual(Text.markdownToHtml("""
1. Item 1
2. Item 2
3. Item 3
        """), "<ol>\n<li>Item 1</li>\n<li>Item 2</li>\n<li>Item 3</li>\n</ol>\n")


        self.assertEqual(Text.highlightCodeBlocks(Text.markdownToHtml("""
```js
alert("hello");
```
        """)), 
'''<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1</pre></div></td><td class="code"><div class="highlight"><pre><span class="nx">alert</span><span class="p">(</span><span class="s2">"hello"</span><span class="p">);</span>
</pre></div>
</td></tr></table>
''')