Esempio n. 1
0
def highlight(value):
    """ Highlight the python code with pygments"""
    soup = BeautifulSoup(value)
    prespy = soup.findAll('pre', lang="python")
    prespyco = soup.findAll('pre', lang="python_console")
    presbs = soup.findAll('pre', lang="bash")
    presht = soup.findAll('pre', lang="html")

    for pre in prespy:
        pre.replaceWith(hl(unescape(pre.string), PythonLexer(), HtmlFormatter()))
    for pre in prespyco:
        pre.replaceWith(hl(unescape(pre.string), PythonConsoleLexer(), HtmlFormatter()))
    for pre in presbs:
        pre.replaceWith(hl(unescape(pre.string), BashSessionLexer(), HtmlFormatter()))
    for pre in presht:
        pre.replaceWith(hl(unescape(pre.string), HtmlDjangoLexer(), HtmlFormatter()))

    return soup
Esempio n. 2
0
def highlight(filepath, linenos=False, mode=None):
    """Simply highlight source files.

    Sometimes it might be useful to comfortably read source codes on targets...

    Arguments:
        filepath (string)          : The path to the file to be highlighted.
        linenos  (bool, optional)  : Whether to show line numbers or not.
        mode     (string, optional): The display mode ('more' | 'less').
                                     If mode is not set, it prints all the
                                     formatted content to the screen.

    Returns:
        Nothing.
    """
    from os import environ
    fname = filepath.split('/')[-1]
    with open(filepath, 'rb') as f:
        src = f.read()
        if src:
            try:
                lexer = guess_lexer(src)
            except pygmentsClassNotFound:
                print 'No lexer found... Print without formatting.'
                print src
            if 'xterm' in environ['TERM']:
                formatter = Terminal256Formatter(linenos=linenos)
            else:
                formatter = TerminalFormatter(linenos=linenos)
            highlighted = hl(src, lexer, formatter)
            if mode:
                if mode == 'more':
                    more(highlighted)
                elif mode == 'less':
                    less(highlighted, filename=fname)
                else:
                    print highlighted
            else:
                print highlighted
Esempio n. 3
0
 def highlight(source):
     return hl(source, PythonLexer(), HtmlFormatter(noclasses=True))
Esempio n. 4
0
 def highlight(source):
     return hl(source, PythonLexer(), HtmlFormatter(noclasses=True))