Ejemplo n.º 1
0
    def executeMacroAdd2Page(self, macrostr, page, doc=None, requestContext=None, paramsExtra="", markdown=False):
        """
        @param macrostr full string like {{test something more}}
        @param page is htmlpage, rstpage, confluencepage, ...
        find macro's in a page & execute the macro
        a doc is a document in final phase whichere the final result is generated
        """
        if not isinstance(page, Page):
            raise RuntimeError("Page was no page object. Was for macro:%s on doc:%s" % (macrostr, doc.name))

        macrospace, macro, tags, cmdstr = self.parseMacroStr(macrostr)

        # print "execute macro %s on page %s" % (macrostr,page.name)
        # for ease of use add the requestContext params to the main params
        taskletgroup = self._getTaskletGroup(doc, macrospace, macro)

        if taskletgroup:
            if markdown == True:
                from markdown2 import markdown2

                page.body = markdown2.markdown(
                    page.body, extras=["tables", "nofollow", "cuddled-lists", "markdown-in-html"]
                )
                # markdown format & style fixes
                if "<table>" in page.body:
                    page.body = page.body.replace(
                        "<table>", '<table class="table table-striped table-bordered table-hover">'
                    )

            try:
                page = taskletgroup.executeV2(
                    macro,
                    doc=doc,
                    tags=tags,
                    macro=macro,
                    macrostr=macrostr,
                    paramsExtra=paramsExtra,
                    cmdstr=cmdstr,
                    page=page,
                    requestContext=requestContext,
                )
            except:
                e = traceback.format_exc()
                result = "***ERROR***: Could not execute macro %s on %s, error in macro." % (macro, doc.name)
                if j.application.debug:
                    result += " Error was:\n%s " % (e)
                page.addMessage(j.portal.tools.html.escape(result))
        else:
            page.addMessage("***error***: could not find macro %s" % macro)

        if not isinstance(page, Page):
            raise RuntimeError(
                "params.page object which came back from macro's does not represent a page. Was for macro:%s on doc:%s"
                % (macro, doc.name)
            )

        page.body = page.body.replace("\{", "{")

        return page
Ejemplo n.º 2
0
def markdown_escaped_extras(text):
    # safe version of markdown
    return markdown2.markdown(text,
                              safe_mode='escape',
                              extras={
                                  'nofollow': {},
                                  'fenced-code-blocks': {
                                      'linenos': True
                                  },
                                  'html-classes': {
                                      'img': 'img-responsive'
                                  }
                              })
Ejemplo n.º 3
0
    def execMacrosOnContent(self, content, doc, paramsExtra={}, ctx=None, page=None, markdown=False):

        recursivedepth = 0
        page = j.portal.server.active.pageprocessor.getpage()
        page.body = ""

        def process(content):
            if ctx is not None:
                content = doc.applyParams(ctx.params, findfresh=True, content=content)
            if paramsExtra != {}:
                content = doc.applyParams(paramsExtra, findfresh=True, content=content)
            return content, self.findMacros(doc, content)

        content, macros = process(content)
        while macros:
            recursivedepth += 1
            if recursivedepth > 20:
                content += "ERROR: recursive error in executing macros"
                return content, doc

            for macroitem in macros:
                macrostr, macrospace, macro, tags, cmdstr = macroitem
                page.body = page.body.replace(macrostr, "", 1)

                if markdown == True:
                    doc.preprocessor.macroexecutorPage.executeMacroAdd2Page(
                        macrostr, page, doc, ctx, paramsExtra, markdown
                    )
                    page.body = page.body.replace("\n", "")
                    from markdown2 import markdown2

                    content = markdown2.markdown(
                        content, extras=["tables", "nofollow", "cuddled-lists", "markdown-in-html"]
                    )
                    # markdown format & style fixes
                    if "<p></p>" in content:
                        content = content.replace("<p></p>", "")
                    if "<table>" in content:
                        content = content.replace(
                            "<table>", '<table class="table table-striped table-bordered table-hover">'
                        )

                    content = content.replace(macrostr, page.body, 1)
                    page.body = ""
                else:
                    doc.preprocessor.macroexecutorPage.executeMacroAdd2Page(macrostr, page, doc, ctx, paramsExtra)

            content, macros = process(content)
        content = page.head + content
        return content, doc
Ejemplo n.º 4
0
 def convertText_(self, text):
     return markdown2.markdown(text)
Ejemplo n.º 5
0
    def execMacrosOnContent(self,
                            content,
                            doc,
                            paramsExtra={},
                            ctx=None,
                            page=None,
                            markdown=False):

        recursivedepth = 0
        page = j.core.portal.active.getpage()
        page.body = ""

        def process(content):
            if ctx != None:
                content = doc.applyParams(ctx.params,
                                          findfresh=True,
                                          content=content)
            if paramsExtra != {}:
                content = doc.applyParams(paramsExtra,
                                          findfresh=True,
                                          content=content)
            return content, self.findMacros(doc, content)

        content, macros = process(content)
        while macros:
            recursivedepth += 1
            if recursivedepth > 20:
                content += 'ERROR: recursive error in executing macros'
                return content, doc

            for macroitem in macros:
                macrostr, macrospace, macro, tags, cmdstr = macroitem
                page.body = page.body.replace(macrostr, "", 1)

                if markdown == True:
                    doc.preprocessor.macroexecutorPage.executeMacroAdd2Page(
                        macrostr, page, doc, ctx, paramsExtra, markdown)
                    page.body = page.body.replace('\n', '')
                    from markdown2 import markdown2
                    content = markdown2.markdown(content,
                                                 extras=[
                                                     "tables", "nofollow",
                                                     "cuddled-lists",
                                                     "markdown-in-html"
                                                 ])
                    # markdown format & style fixes
                    if '<p></p>' in content:
                        content = content.replace('<p></p>', '')
                    if '<table>' in content:
                        content = content.replace(
                            '<table>',
                            '<table class="table table-striped table-bordered table-hover">'
                        )

                    content = content.replace(macrostr, page.body, 1)
                    page.body = ""
                else:
                    doc.preprocessor.macroexecutorPage.executeMacroAdd2Page(
                        macrostr, page, doc, ctx, paramsExtra)

            content, macros = process(content)
        content = page.head + content
        return content, doc
Ejemplo n.º 6
0
    def executeMacroAdd2Page(self,
                             macrostr,
                             page,
                             doc=None,
                             requestContext=None,
                             paramsExtra="",
                             markdown=False):
        """
        @param macrostr full string like {{test something more}}
        @param page is htmlpage, rstpage, confluencepage, ...
        find macro's in a page & execute the macro
        a doc is a document in final phase whichere the final result is generated
        """
        if not isinstance(page, Page):
            raise RuntimeError(
                "Page was no page object. Was for macro:%s on doc:%s" %
                (macrostr, doc.name))

        macrospace, macro, tags, cmdstr = self.parseMacroStr(macrostr)

        # print "execute macro %s on page %s" % (macrostr,page.name)
        # for ease of use add the requestContext params to the main params
        taskletgroup = self._getTaskletGroup(doc, macrospace, macro)

        if taskletgroup:
            if markdown == True:
                from markdown2 import markdown2
                page.body = markdown2.markdown(page.body,
                                               extras=[
                                                   "tables", "nofollow",
                                                   "cuddled-lists",
                                                   "markdown-in-html"
                                               ])
                # markdown format & style fixes
                if '<table>' in page.body:
                    page.body = page.body.replace(
                        '<table>',
                        '<table class="table table-striped table-bordered table-hover">'
                    )

            try:
                page = taskletgroup.executeV2(macro,
                                              doc=doc,
                                              tags=tags,
                                              macro=macro,
                                              macrostr=macrostr,
                                              paramsExtra=paramsExtra,
                                              cmdstr=cmdstr,
                                              page=page,
                                              requestContext=requestContext)
            except exceptions.BaseError:
                raise
            except:
                e = traceback.format_exc()
                result = "***ERROR***: Could not execute macro %s on %s, error in macro." % (
                    macro, doc.name)
                if j.application.debug:
                    result += " Error was:\n%s " % (e)
                page.addMessage(j.html.escape(result))
        else:
            page.addMessage("***error***: could not find macro %s" % macro)

        if not isinstance(page, Page):
            raise RuntimeError(
                "params.page object which came back from macro's does not represent a page. Was for macro:%s on doc:%s"
                % (macro, doc.name))

        page.body = page.body.replace("\{", "{")

        return page
Ejemplo n.º 7
0
def markdown(text):
    if text:
        return markdown2.markdown(text)