Exemple #1
0
def _colorize_ansi_answer(topic, answer, color_style,       # pylint: disable=too-many-arguments
                          highlight_all=True, highlight_code=False,
                          unindent_code=False):

    color_style = color_style or "native"
    lexer_class = languages_data.LEXER['bash']
    if '/' in topic:
        section_name = topic.split('/', 1)[0].lower()
        section_name = languages_data.get_lexer_name(section_name)
        lexer_class = languages_data.LEXER.get(section_name, lexer_class)
        if section_name == 'php':
            answer = "<?\n%s?>\n" % answer

    if highlight_all:
        highlight = lambda answer: pygments_highlight(
            answer, lexer_class(), Terminal256Formatter(style=color_style)).strip('\n')+'\n'
    else:
        highlight = lambda x: x

    if highlight_code:
        blocks = fmt.comments.code_blocks(
            answer, wrap_lines=True, unindent_code=(4 if unindent_code else False))
        highlighted_blocks = []
        for block in blocks:
            if block[0] == 1:
                this_block = highlight(block[1])
            else:
                this_block = block[1].strip('\n')+'\n'
            highlighted_blocks.append(this_block)

        result = "\n".join(highlighted_blocks)
    else:
        result = highlight(answer).lstrip('\n')
    return result
Exemple #2
0
def highlight_html(code, language, linenos=False, linenostart=1, **kwargs):
    if not has_pygments:
        return str(code)

    return pygments_highlight(str(code),
                              get_lexer_by_name(language),
                              HtmlFormatter(wrapcode=True, linenos=linenos, linenostart=linenostart, **kwargs))
Exemple #3
0
def python_highlight(code, renderer, wrapper, hl_line=-1, python_console=False, **kw):
    """Syntax highlighting of Python code

    In:
      - ``code`` -- the Python code
      - ``renderer`` -- a HTML renderer
      - ``wrapper`` -- the wrapper HTML element
      - ``hl_line`` -- line to highlight
      - ``python_console`` -- is ``code`` a Python console capture or Python source ?

    Return:
      - a DOM tree
    """
    lexer = PythonConsoleLexer() if python_console else PythonLexer()
    source = pygments_highlight(code, lexer, HtmlFormatter(**kw))

    lines = []
    for (n, line) in enumerate(source.splitlines()):
        i = line.find('<')
        if i != -1:
            # Replace the starting spaces by NBSP characters
            line = NBSP * i + line[i:]

        if n == hl_line:
            line = '<span class="source-highlight">%s</span>' % line

        lines.append(line)

    html = renderer.parse_htmlstring('<br>'.join(lines))
    return wrapper(html[0][0][0][:], class_='source highlight')
Exemple #4
0
def convert2html(book_id, text):
    extra = {}

    def url2(*a, **b):
        b['args'] = [book_id] + b.get('args', [])
        return URL(*a, **b)

    def truncate(x):
        return x[:70] + '...' if len(x) > 70 else x

    extra['verbatim'] = lambda code: cgi.escape(code)
    extra['cite'] = lambda key: TAG.sup(
        '[', A(key, _href=URL('reference', args=(book_id, key)),
               _target='_blank'), ']').xml()
    extra['inxx'] = lambda code: '<div class="inxx">' + code + '</div>'
    extra['ref'] = lambda code: '[ref:' + code + ']'
    # extra['code'] = lambda code: CODE(code, language='web2py').xml()
    # The comment above line could be replaced by this
    from pygments import highlight as pygments_highlight
    from pygments.lexers import PythonLexer as pygments_PythonLexer
    from pygments.formatters import HtmlFormatter as pygments_HtmlFormatter

    extra['code'] = lambda code: '<div class="highlight_wrapper">' + \
                                 pygments_highlight(code,
                                                    pygments_PythonLexer(),
                                                    pygments_HtmlFormatter(style='friendly',
                                                                           linenos=True
                                                                           )).encode('utf8') + \
                                 '</div>'
    rtn = MARKMIN(text.replace('\r', ''), extra=extra, url=url2)
    return rtn
Exemple #5
0
def show_paste_html(pasteid=None):
	if pasteid == None:
		bottle.abort(404)
	else:
		# get paste from database
		# TODO, as in show_paste_plain(): make. this. f*****g. portable.
		db = MySQLdb.connect(
				host=CONFIG_MYSQL_HOST,
				user=CONFIG_MYSQL_USER,
				passwd=CONFIG_MYSQL_PASSWORD,
				db=CONFIG_MYSQL_DB
				)
		db_curs = db.cursor()
		db_curs.execute("SELECT title,description,author_name,author_email,code,language,privacy FROM pastes WHERE id=\""+MySQLdb.escape_string(pasteid)+"\"")
		db.close()
		paste = db_curs.fetchall()[0]
		paste_title = paste[0]
		paste_description = nl2br(htmlspecialchars(paste[1]))
		paste_author_name = paste[2]
		paste_author_email = paste[3]
		paste_code = paste[4]
		paste_language = languages[paste[5]]
		paste_privacy = paste[6]
		lexer = pygments_get_lexer_by_name(paste[5], stripall=True, encoding="UTF-8")
		formatter = pygments_HtmlFormatter(linenos=True, cssclass="source", encoding="UTF-8")
		paste_code = pygments_highlight(paste_code, lexer, formatter)
		bottle.response.content_type = 'text/html; charset=UTF-8'
		return bottle.template("show_paste", service_name=CONFIG_SERVICE_NAME, title=paste_title, description=paste_description, author_name=paste_author_name, author_email=paste_author_email, code=paste_code, language=paste_language, privacy=paste_privacy)
def _colorize_ansi_answer(topic, answer, color_style,               # pylint: disable=too-many-arguments
                          highlight_all=True, highlight_code=False,
                          unindent_code=False):

    color_style = color_style or "native"
    lexer_class = LEXER['bash']
    if '/' in topic:
        section_name = topic.split('/', 1)[0].lower()
        section_name = LANGUAGE_ALIAS.get(section_name, section_name)
        lexer_class = LEXER.get(section_name, lexer_class)
        if section_name == 'php':
            answer = "<?\n%s?>\n" % answer

    if highlight_all:
        highlight = lambda answer: pygments_highlight(
            answer, lexer_class(), Terminal256Formatter(style=color_style)).strip('\n')+'\n'
    else:
        highlight = lambda x: x

    if highlight_code:
        blocks = code_blocks(answer, wrap_lines=True, unindent_code=(4 if unindent_code else False))
        highlighted_blocks = []
        for block in blocks:
            if block[0] == 1:
                this_block = highlight(block[1])
            else:
                this_block = block[1].strip('\n')+'\n'
            highlighted_blocks.append(this_block)

        result = "\n".join(highlighted_blocks)
    else:
        result = highlight(answer).lstrip('\n')
    return result
Exemple #7
0
def convert2html(book_id, text):
    extra = {}

    def url2(*a, **b):
        b['args'] = [book_id] + b.get('args', [])
        return URL(*a, **b)

    def truncate(x):
        return x[:70] + '...' if len(x) > 70 else x

    extra['verbatim'] = lambda code: cgi.escape(code)
    extra['cite'] = lambda key: TAG.sup(
        '[',
        A(key, _href=URL('reference', args=(book_id, key)), _target='_blank'),
        ']').xml()
    extra['inxx'] = lambda code: '<div class="inxx">' + code + '</div>'
    extra['ref'] = lambda code: '[ref:' + code + ']'
    # extra['code'] = lambda code: CODE(code, language='web2py').xml()
    # The comment above line could be replaced by this
    from pygments import highlight as pygments_highlight
    from pygments.lexers import PythonLexer as pygments_PythonLexer
    from pygments.formatters import HtmlFormatter as pygments_HtmlFormatter

    extra['code'] = lambda code: '<div class="highlight_wrapper">' + \
                                 pygments_highlight(code,
                                                    pygments_PythonLexer(),
                                                    pygments_HtmlFormatter(style='friendly',
                                                                           linenos=True
                                                                           )).encode('utf8') + \
                                 '</div>'
    rtn = MARKMIN(text.replace('\r', ''), extra=extra, url=url2)
    return rtn
Exemple #8
0
 def highlight(html_code):
     """Highlights the given code (for use in the template)"""
     if isinstance(html_code, _Element):
         html_code = tostring(html_code)
     return html(
         pygments_highlight(html_code, HtmlLexer(),
                            HtmlFormatter(noclasses=True)))
Exemple #9
0
    def highlight(self, code=None, classes=None):
        """ returns highlighted code in html format
            styles are not included, you must have a
            css file on hand and reference it.
        """
        if not code:
            code = self.code
        if not classes:
            classes = self.classes
        # Default class for all highlighted code (for the div wrapper)
        classlist = ['highlight']
        if classes:
            # User defined classes, may override the default class.
            classlist.extend(classes)

        classtxt = ' '.join(classlist)

        try:
            codeh = pygments_highlight(code, self.lexer, self.formatter)
            highlighted = '\n'.join(
                ['<div class=\'{}\'>'.format(classtxt), codeh, '</div>\n'])

        except Exception as ex:
            log.error('WpHighlighter.highlight() error:\n{}'.format(ex))
            highlighted = ''
        return highlighted
Exemple #10
0
def highlight_terminal(code, language):
    if not has_pygments:
        return code

    if not _colorable_terminal():
        return code

    return pygments_highlight(code, get_lexer_by_name(language), TerminalFormatter())
 def new_codehilite_highlight(src, lexer, formatter):
     lang = lexer.name.lower().replace(' ', '-')
     result = pygments_highlight(src, lexer, formatter)
     if isinstance(formatter, HtmlFormatter):
         d = pq(result, parser='html')
         d.add_class('language-{}'.format(lang))
         result = d.outer_html()
     return result
 def new_codehilite_highlight(src, lexer, formatter):
     lang = lexer.name.lower().replace(' ', '-')
     result = pygments_highlight(src, lexer, formatter)
     if isinstance(formatter, HtmlFormatter):
         d = pq(result, parser='html')
         d.add_class('language-{}'.format(lang))
         result = d.outer_html()
     return result
Exemple #13
0
def highlight(code, lang='python'):
    """
    Highlights source in the given programming language.
    """

    formatter = get_formatter_by_name('html')
    try:
        lexer = get_lexer_by_name(lang)
    except ClassNotFound:
        lexer = get_lexer_by_name('python')
    return Markup(pygments_highlight(code, lexer, formatter))
Exemple #14
0
def highlight(code, lang='python'):
    """
    Highlights source in the given programming language.
    """

    formatter = get_formatter_by_name('html')
    try:
        lexer = get_lexer_by_name(lang)
    except ClassNotFound:
        lexer = get_lexer_by_name('python')
    return Markup(pygments_highlight(code, lexer, formatter))
Exemple #15
0
 def view_source(self, req, resp, url):
     """
     View the highlighted source (from `action_view`).
     """
     content_type = resp.content_type
     if content_type.startswith("application/xml"):
         lexer = XmlLexer()
     elif content_type == "text/html":
         lexer = HtmlLexer()
     else:
         ## FIXME: what then?
         lexer = HtmlLexer()
     text = pygments_highlight(resp.body, lexer, HtmlFormatter(full=True, linenos=True, lineanchors="code"))
     return Response(text)
Exemple #16
0
 def view_source(self, req, resp, url):
     """
     View the highlighted source (from `action_view`).
     """
     content_type = resp.content_type
     if content_type.startswith('application/xml'):
         lexer = XmlLexer()
     elif content_type == 'text/html':
         lexer = HtmlLexer()
     else:
         ## FIXME: what then?
         lexer = HtmlLexer()
     text = pygments_highlight(
         resp.body, lexer,
         HtmlFormatter(full=True, linenos=True, lineanchors='code'))
     return Response(text)
Exemple #17
0
def render(self, h, comp, *args):
    (request, (exc_type, exc_value, tb)) = self.get_exception()
    tb = ''.join(traceback.format_exception(exc_type, exc_value, tb))

    with h.div:
        # Exception informations
        with h.div(class_='tab_info'):
            h << h.span(u'⇝', style='color: #f00') << NBSP
            h << exc_type.__name__ << ': ' << str(exc_value)

        with h.div(style='padding: 10px', id='frames'):
            with h.ul:
                # Request informations (CGI and WSGI variables)
                h << component.Component(request, model='ide')

                # Textual traceback
                with h.li('Text Traceback'):
                    lexer = PythonTracebackLexer()
                    source = pygments_highlight(tb, lexer, HtmlFormatter())
                    h << h.ul(h.li(h.parse_htmlstring(source), yuiConfig='{ "not_expandable" : true }'))

                # Interactive traceback
                with h.li('Interactive Traceback', class_='expanded'):
                    h << h.ul(self.frames)

        h << h.script('''
        var frames = new YAHOO.widget.TreeView("frames");

        frames.subscribe("clickEvent", function(e) { return !e.node.data.not_expandable; });
        frames.render();

        // Don't let the TreeView widget catch the keydown events for our <input> or <textarea> fields
        var fn = YAHOO.util.Event.getListeners(frames.getEl(), "keydown")[0].fn;
        YAHOO.util.Event.removeListener(frames.getEl(), "keydown");
        YAHOO.util.Event.addListener(frames.getEl(), "keydown", function(e) {
            if(e.target) {
                if(e.target.tagName != "INPUT" && e.target.tagName != "TEXTAREA") { fn.call(frames, e); }
                return false;
            }
        });

        ''')

    return h.root
Exemple #18
0
def highlight(filename, code, linenos=False, lineanchors=None,
              cssclass='highlight'):
    if lineanchors is None and linenos:
        lineanchors = 'code'
    lexer = None
    if filename:
        if filename.endswith('.py'):
            # XXX: Pygments gives back NumPyLexer for some reason, which
            # we don't need
            lexer = PythonLexer()
        else:
            try:
                lexer = get_lexer_for_filename(filename)
            except ClassNotFound:
                pass
    if not lexer:
        lexer = TextLexer()
    formatter = HtmlFormatter(linenos=linenos, lineanchors=lineanchors,
                              cssclass=cssclass)
    return pygments_highlight(code, lexer, formatter)
Exemple #19
0
def _colorize_ansi_answer(
        topic,
        answer,
        color_style,  # pylint: disable=too-many-arguments
        highlight_all=True,
        highlight_code=False,
        unindent_code=False):

    color_style = color_style or "native"
    lexer_class = LEXER['bash']
    for lexer_name, lexer_value in LEXER.items():
        if topic.startswith("%s/" % lexer_name):
            # color_style = color_style or "monokai"
            if lexer_name == 'php':
                answer = "<?\n%s?>\n" % answer
            lexer_class = lexer_value
            break

    if highlight_all:
        highlight = lambda answer: pygments_highlight(
            answer, lexer_class(), Terminal256Formatter(style=color_style)
        ).strip('\n') + '\n'
    else:
        highlight = lambda x: x

    if highlight_code:
        blocks = code_blocks(answer,
                             wrap_lines=True,
                             unindent_code=(4 if unindent_code else False))
        highlighted_blocks = []
        for block in blocks:
            if block[0] == 1:
                this_block = highlight(block[1])
            else:
                this_block = block[1].strip('\n') + '\n'
            highlighted_blocks.append(this_block)

        result = "\n".join(highlighted_blocks)
    else:
        result = highlight(answer).lstrip('\n')
    return result
Exemple #20
0
def try_highlight(code, langname, formatter=None):
    """ Try highlighting a line of text.
        Return the highlighted code on success,
        return unhighlighted code on failure.
    """
    if formatter is None:
        # Formatter was not preloaded by the user,
        # Use the default preloaded html formatter.
        formatter = DEFAULT_FORMATTER

    try:
        lexer = lexers.get_lexer_by_name(langname)
        if not lexer:
            log.debug('try_highlight: No lexer found for  {}'.format(langname))
            return code

        highlighted = pygments_highlight(code, lexer, formatter)
        # log.debug('highlight: {}, {}'.format(langname, highlighted))
        return ''.join(
            ('<div class="highlighted-embedded">', highlighted, '</div>'))
    except Exception as ex:
        log.debug('try_highlight: Error highlighting.\n{}'.format(ex))
        return code
Exemple #21
0
 def highlight(html_code):
     """Highlights the given code (for use in the template)"""
     if isinstance(html_code, _Element):
         html_code = tostring(html_code)
     return html(pygments_highlight(html_code, HtmlLexer(),
                                    HtmlFormatter(noclasses=True)))
Exemple #22
0
def cheat_wrapper(query, request_options=None, html=False):

    #
    # at the moment, we just remove trailing slashes
    # so queries python/ and python are equal
    #
    query = query.rstrip('/')

    query = rewrite_aliases(query)

    highlight = not bool(request_options
                         and request_options.get('no-terminal'))
    color_style = request_options.get('style', '')
    if color_style not in COLOR_STYLES:
        color_style = ''

    keyword = None
    if '~' in query:
        topic = query
        pos = topic.index('~')
        keyword = topic[pos + 1:]
        topic = topic[:pos]

        options = ""
        if '/' in keyword:
            options = keyword[::-1]
            options = options[:options.index('/')]
            keyword = keyword[:-len(options) - 1]

        answers = find_answer_by_keyword(topic, keyword, options=options)
        search_mode = True
    else:
        answers = [(query, get_answer(query, keyword))]
        search_mode = False

    found = True  # if the page was found in the database
    editable = False  # can generated page be edited on github (only cheat.sheets pages can)
    result = ""
    for topic, answer in answers:

        if topic == 'LIMITED':
            result += colored.bg('dark_goldenrod') + colored.fg(
                'yellow_1') + ' ' + answer + ' ' + colored.attr('reset') + "\n"
            break

        if topic in [":list", ":bash_completion"]:
            highlight = False

        topic_type = get_topic_type(topic)
        if topic_type == 'unknown':
            found = False

        if highlight:
            #if topic_type.endswith(" dir"):
            #    pass

            if topic_type == "internal":
                answer = colorize_internal(topic, answer, html)
            else:
                color_style = color_style or "native"
                lexer = pygments.lexers.BashLexer
                for lexer_name, lexer_value in LEXER.items():
                    if topic.startswith("%s/" % lexer_name):
                        color_style = color_style or "monokai"
                        if lexer_name == 'php':
                            answer = "<?\n%s?>\n" % answer
                        lexer = lexer_value
                        break

                formatter = Terminal256Formatter(style=color_style)
                answer = pygments_highlight(answer, lexer(), formatter)

        if topic_type == "cheat.sheets":
            editable = True

        if search_mode:
            if highlight:
                result += "\n%s%s %s %s%s\n" % (
                    colored.bg('dark_gray'),
                    colored.attr("res_underlined"), topic,
                    colored.attr("res_underlined"), colored.attr('reset'))
            else:
                result += "\n[%s]\n" % topic

        result += answer

    if search_mode:
        result = result[1:]
        editable = False
        repository_button = ''
    else:
        repository_button = github_button(topic_type)

    if html:
        result = result + "\n$"
        result = html_wrapper(result)
        title = "<title>cheat.sh/%s</title>" % topic
        # title += '\n<link rel="stylesheet" href="/files/awesomplete.css" />script src="/files/awesomplete.min.js" async></script>'
        # submit button: thanks to http://stackoverflow.com/questions/477691/
        submit_button = '<input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;" tabindex="-1" />'
        topic_list = ('<datalist id="topics">%s</datalist>' %
                      ("\n".join("<option value='%s'></option>" % x
                                 for x in get_topics_list())))

        curl_line = "<span class='pre'>$ curl cheat.sh/</span>"
        if query == ':firstpage':
            query = ""
        form_html = '<form action="/" method="GET"/>%s%s<input type="text" value="%s" name="topic" list="topics" autofocus autocomplete="off"/>%s</form>' % (
            submit_button, curl_line, query, topic_list)

        edit_button = ''
        if editable:
            edit_page_link = 'https://github.com/chubin/cheat.sheets/edit/master/sheets/' + topic
            edit_button = '<pre style="position:absolute;padding-left:40em;overflow:visible;height:0;">[<a href="%s" style="color:cyan">edit</a>]</pre>' % edit_page_link
        result = re.sub("<pre>", edit_button + form_html + "<pre>", result)
        result = re.sub("<head>", "<head>" + title, result)
        if not request_options.get('quiet'):
            result = result.replace(
                '</body>', TWITTER_BUTTON + GITHUB_BUTTON + repository_button +
                GITHUB_BUTTON_FOOTER + '</body>')

    return result, found
Exemple #23
0
def highlight(sql):
    from pygments import highlight as pygments_highlight
    from pygments.lexers import SqlLexer
    from pygments.formatters import TerminalFormatter

    return pygments_highlight(sql, SqlLexer(), TerminalFormatter())
Exemple #24
0
def highlight(string, lang):
    lexer = get_lexer_by_name(lang)
    return pygments_highlight(string, lexer, _formatter)
Exemple #25
0
def highlight_pre_elems(elem):
    """ Highlights pre tag content in an HtmlElement according to the classes
        set on the pre tags and transforms them into pre-like divs.
        This is a processor for wrap_elem().
        Returns the element on success, or None on error.
    """
    disallow_styles = {
        # Highlighting a template means the tag gets highlighted twice.
        'codewrap-template',
    }
    # Styles that won't be highlighted, but wrapped in a div.
    basic_styles = {
        'codewrap', 'codewrap-noscript', 'sampwrap', 'sampwrap-noscript',
        'highlighted-inline'
    }

    # Go ahead and set the div's class, returning None means this element
    # isn't used anyway.
    elem.set('class', 'highlighted-inline')
    for preelem in elem.cssselect('pre'):
        preclass_str = preelem.get('class', '')
        classes = set(preclass_str.split(' '))
        if (not (preclass_str and classes)) or ('none' in classes):
            # No processing done, just set the class for the wrapping div.
            return elem
        elif classes.intersection(disallow_styles):
            # Cancel processing, contains a no-highlight-allowed class.
            return None
        elif classes.intersection(basic_styles):
            # No processing done, class already set for pre tag.
            elem.set(
                'class', ' '.join((
                    elem.get('class', default='highlighted-inline'),
                    preclass_str,
                )))
            return elem
        # Have a class set that is probably a language name for pygments.
        try:
            # Allow class names like '_c' to be transformed to 'c'.
            # If a language name interferes with actual css add the _ to it.
            lexer = get_lexer_bynames([s.lstrip('_') for s in classes])
        except ClassNotFound:
            # No valid lexer. Just leave it.
            log.error('No lexer found with: {!r}'.format(preclass_str))
            return elem

        # Highlight the pre tag's text using pygments.
        try:
            highlighted = pygments_highlight(preelem.text, lexer,
                                             DEFAULT_FORMATTER)
        except Exception as ex:
            log.error('\n'.join(
                ('Failed to highlight pre tag: <p class={clsstr}>',
                 '    Source line: {sourceline}', '    Error: {err}')).format(
                     clsstr=preelem.get('class', default=''),
                     sourceline=preelem.sourceline,
                     err=ex,
                 ))
            return elem

        # Highlight succeeded, replace the text with the highlight's html.
        preelem.text = ''
        preelem.append(lxml.html.fromstring(highlighted))

    return elem
Exemple #26
0
def highlight(sql):
    from pygments import highlight as pygments_highlight
    from pygments.lexers import SqlLexer
    from pygments.formatters import TerminalFormatter

    return pygments_highlight(sql, SqlLexer(), TerminalFormatter())
Exemple #27
0
def djit_auto_highlight(blob):
  """Prints the given blob in highlighted mode."""
  try: lexer = guess_lexer_for_filename(blob.basename, blob.data)
  except: lexer = get_lexer_by_name('text', enconding='UTF-8') 
  return pygments_highlight(blob.data, lexer, 
      HtmlFormatter(cssclass="highlight", linenos="table"))
Exemple #28
0
def djit_highlight(type, text):
  """Prints the given text in a selectable highlight mode."""
  return pygments_highlight(text, get_lexer_by_name(type, encoding='UTF-8'), 
      HtmlFormatter(cssclass="highlight", linenos="table"))