Example #1
0
def code(macro, environ, data, *args, **kwargs):
    """Displays a block of text with syntax highlighting in a HTML <pre>.
    
    This macro uses the pygments syntax highlighter to render a block of
    text given a specific language. For a list of available languages
    that are supported by pygments see:
    * [[http://pygments.org/languages/]]

    If an invalid language is specified or a lexer cannot be found for the
    given language (//given by the lang argument//) then the block of text
    will be rendered unhighlighted.

    **Arguments:**
    * lang=None (//the language to highlight//)

    **Example(s):**
    {{{
    <<code lang="python">>
    def main():
        print "Hello World!"

    if __name__ == "__main__":
        main()
    <</code>>
    }}}

    <<code lang="python">>
    def main():
        print "Hello World!"

    if __name__ == "__main__":
        main()
    <</code>>
    """
    
    if not macro.body:
        return None

    lang = kwargs.get("lang", None)
    linenos = kwargs.get("linenos", False)
    title = kwargs.get("title", "")

    return highlight(macro.body, lang=lang, linenos=linenos, title=title)
Example #2
0
def code(macro, environ, data, *args, **kwargs):
    """Displays a block of text with syntax highlighting in a HTML <pre>.
    
    This macro uses the pygments syntax highlighter to render a block of
    text given a specific language. For a list of available languages
    that are supported by pygments see:
    * [[http://pygments.org/languages/]]

    If an invalid language is specified or a lexer cannot be found for the
    given language (//given by the lang argument//) then the block of text
    will be rendered unhighlighted.

    **Arguments:**
    * lang=None (//the language to highlight//)

    **Example(s):**
    {{{
    <<code lang="python">>
    def main():
        print "Hello World!"

    if __name__ == "__main__":
        main()
    <</code>>
    }}}

    <<code lang="python">>
    def main():
        print "Hello World!"

    if __name__ == "__main__":
        main()
    <</code>>
    """

    if not macro.body:
        return None

    lang = kwargs.get("lang", None)
    linenos = kwargs.get("linenos", False)
    title = kwargs.get("title", "")

    return highlight(macro.body, lang=lang, linenos=linenos, title=title)
Example #3
0
        self.body = body
        self.isblock = isblock

def dispatcher(name, arg_string, body, isblock, (environ, data)):
    if name in environ.macros:
        macro = Macro(name, arg_string, body, isblock)
        args, kwargs = parse_args(arg_string)
        try:
            return environ.macros[name](macro, environ, data,
                *args, **kwargs)
        except Exception, e:
            error = "ERROR: Error while executing macro %s (%s)" % (name, e)
            traceback = format_exc()
            return tag.div(
                tag.p(error),
                highlight(traceback, lang="pytb"),
                class_="error"
            )

    else:
        return tag.div(tag.p("Macro %s Not Found!" % name), class_="error")

def loadMacros():
    path = os.path.abspath(os.path.dirname(__file__))
    p = lambda x: os.path.splitext(x)[1] == ".py"
    modules = [x for x in os.listdir(path) if p(x) and not x == "__init__.py"]

    macros = {}

    for module in modules:
        name, _ = os.path.splitext(module)
Example #4
0
        self.arg_string = arg_string
        self.body = body
        self.isblock = isblock


def dispatcher(name, arg_string, body, isblock, (environ, data)):
    if name in environ.macros:
        macro = Macro(name, arg_string, body, isblock)
        args, kwargs = parse_args(arg_string)
        try:
            return environ.macros[name](macro, environ, data, *args, **kwargs)
        except Exception, e:
            error = "ERROR: Error while executing macro %s (%s)" % (name, e)
            traceback = format_exc()
            return tag.div(tag.p(error),
                           highlight(traceback, lang="pytb"),
                           class_="error")

    else:
        return tag.div(tag.p("Macro %s Not Found!" % name), class_="error")


def loadMacros():
    path = os.path.abspath(os.path.dirname(__file__))
    p = lambda x: os.path.splitext(x)[1] == ".py"
    modules = [x for x in os.listdir(path) if p(x) and not x == "__init__.py"]

    macros = {}

    for module in modules:
        name, _ = os.path.splitext(module)