Пример #1
0
def get_lexer(text, lang):
    """Tries to get lexer for the text whether the lang is provided or not"""
    if lang:
        try:
            return get_lexer_by_name(lang, stripall=False)
        except:
            pass
        return None

    # No language provided, try to guess
    mime = getMagicMimeFromBuffer(text.strip())
    if mime:
        try:
            return get_lexer_for_mimetype(mime, stripall=False)
        except:
            pass

        # The pygments data sometimes miss mime options provided by python magic
        # library
        if mime.startswith('text/'):
            try:
                return get_lexer_for_mimetype(mime.replace(
                    'text/', 'application/'),
                                              stripall=False)
            except:
                pass

    return None
Пример #2
0
    def set_lexer_from_filename(self, filename):
        """
        Change the lexer based on the filename (actually only the extension is
        needed)

        :param filename: Filename or extension
        """
        self._lexer = None
        if filename.endswith("~"):
            filename = filename[0:len(filename) - 1]
        try:
            self._lexer = get_lexer_for_filename(filename)
        except (ClassNotFound, ImportError):
            print(('class not found for url', filename))
            try:
                m = mimetypes.guess_type(filename)
                print(m)
                self._lexer = get_lexer_for_mimetype(m[0])
            except (ClassNotFound, IndexError, ImportError):
                self._lexer = get_lexer_for_mimetype('text/plain')
        if self._lexer is None:
            _logger().warning(
                'failed to get lexer from filename: %s, using '
                'plain text instead...', filename)
            self._lexer = TextLexer()
Пример #3
0
    def set_lexer_from_mime_type(self, mime, **options):
        """
        Sets the pygments lexer from mime type.

        :param mime: mime type
        :param options: optional addtional options.
        """

        try:
            self._lexer = get_lexer_for_mimetype(mime, **options)
        except (ClassNotFound, ImportError):
            print('class not found for mime', mime)
            self._lexer = get_lexer_for_mimetype('text/plain')
        else:
            _logger().debug('lexer for mimetype (%s): %r', mime, self._lexer)
def get_lexer(
    # The lexer itself, which will simply be returned.
    lexer=None,
    # The `short name <http://pygments.org/docs/lexers/>`_, or alias, of the
    # lexer to use.
    alias=None,
    # The filename of the source file to lex.
    filename=None,
    # The MIME type of the source file to lex.
    mimetype=None,
    # The code to be highlighted, used to guess a lexer.
    code=None,
    # _`options`: Specify the lexer (see `get_lexer` arguments), and provide it any other needed options.
    **options):

    # This sets the default tabsize to 4 spaces in
    # `Pygments' lexer <http://pygments.org/docs/api/#pygments.lexer.Lexer>`_,
    # and this link is a list  of
    # `all available lexers <http://pygments.org/docs/lexers/#available-lexers>`_
    options.setdefault("tabsize", 4)

    if lexer:
        return lexer
    if alias:
        return get_lexer_by_name(alias, **options)
    if filename:
        if code:
            return guess_lexer_for_filename(filename, code, **options)
        else:
            return get_lexer_for_filename(filename, **options)
    if mimetype:
        return get_lexer_for_mimetype(mimetype, **options)
    if code:
        return guess_lexer(code, **options)
Пример #5
0
def view_full_response(response, headers_only=False):
    def check_type(response, against):
        if 'Content-Type' in response.headers and against in response.headers[
                'Content-Type']:
            return True
        return False

    if headers_only:
        to_print = printable_data(response.raw_headers)
        to_print = pygments.highlight(to_print, HttpLexer(),
                                      TerminalFormatter())
        print to_print
    else:
        headers = printable_data(response.raw_headers)
        headers = pygments.highlight(headers, HttpLexer(), TerminalFormatter())
        print headers
        to_print = printable_data(response.raw_data)
        if 'Content-Type' in response.headers:
            try:
                lexer = get_lexer_for_mimetype(
                    response.headers['Content-Type'].split(';')[0])
                to_print = pygments.highlight(to_print, lexer,
                                              TerminalFormatter())
            except ClassNotFound:
                pass

        print to_print
Пример #6
0
def test_get_lexers():
    # test that the lexers functions work
    def verify(func, args):
        x = func(opt='val', *args)
        assert isinstance(x, lexers.PythonLexer)
        assert x.options["opt"] == "val"

    for func, args in [(lexers.get_lexer_by_name, ("python",)),
                       (lexers.get_lexer_for_filename, ("test.py",)),
                       (lexers.get_lexer_for_mimetype, ("text/x-python",)),
                       (lexers.guess_lexer, ("#!/usr/bin/python -O\nprint",)),
                       (lexers.guess_lexer_for_filename, ("a.py", "<%= @foo %>"))
                       ]:
        yield verify, func, args

    for cls, (_, lname, aliases, _, mimetypes) in lexers.LEXERS.items():
        assert cls == lexers.find_lexer_class(lname).__name__

        for alias in aliases:
            assert cls == lexers.get_lexer_by_name(alias).__class__.__name__

        for mimetype in mimetypes:
            assert cls == lexers.get_lexer_for_mimetype(mimetype).__class__.__name__

    try:
        lexers.get_lexer_by_name(None)
    except ClassNotFound:
        pass
    else:
        raise Exception
Пример #7
0
    def body(self, content, content_type):
        prettyfiers_by_lexer = {
            JSONLexer: lambda x: json.dumps(json.loads(x),
                                    sort_keys=True, indent=4),
            pygments.lexers.XmlLexer: xml_prettify,
        }

        content_type = content_type.split(';')[0]
        try:
            lexer = get_lexer_for_mimetype(content_type)
        except ClassNotFound:
            if 'json' in content_type:
                # JSON lexer not found, use internal
                lexer = JSONLexer()
            else:
                # no lexer for mimetype
                return content

        prettyfier = prettyfiers_by_lexer.get(lexer.__class__)
        if prettyfier is not None:
            try:
                # prettify the data.
                content = prettyfier(content)
            except Exception:
                pass
        return pygments.highlight(content, lexer, self.formatter)
Пример #8
0
    def _html_format(self, content, content_types):
        try:
            from pygments import highlight
            from pygments.lexers import get_lexer_for_mimetype
            from pygments.formatters import HtmlFormatter

            lexer = None
            for ct in content_types:
                try:
                    lexer = get_lexer_for_mimetype(ct)
                    break
                except Exception:
                    pass

            if lexer is None:
                raise ValueError("No lexer found")
            formatter = HtmlFormatter()
            return html_body % dict(
                css=formatter.get_style_defs(),
                content=highlight(content, lexer, formatter).encode('utf8'))
        except Exception as e:
            log.warning(
                "Could not pygment the content because of the following "
                "error :\n%s" % e)
            return html_body % dict(
                css='',
                content=u('<pre>%s</pre>') %
                content.replace(b('>'), b('&gt;')).replace(b('<'), b('&lt;')))
Пример #9
0
    def body(self, content, content_type):
        lexer = None
        content_type = content_type.split(';')[0]
        if 'json' in content_type:
            lexer = JSONLexer()
            try:
                # Indent the JSON data.
                content = json.dumps(json.loads(content),
                                    sort_keys=True, indent=4)
            except Exception:
                pass
        if not lexer:
            try:
                lexer = get_lexer_for_mimetype(content_type)
            except ClassNotFound:
                try:
                    lexer = guess_lexer(content)
                except ClassNotFound:
                    return content

        if lexer.name == 'XML':
            dom = parseString(content)
            content = dom.toprettyxml(indent='  ')

        return pygments.highlight(content, lexer, self.formatter)
Пример #10
0
def edit(db, id):
    """
    Edits the entry. If the entry is protected with a password it will display
    a simple password entry form until the password is a match in the database
    """

    paste = _get_paste(db, id)
    if not paste:
        return bottle.HTTPError(404, output='This paste does not exist')
    password = bottle.request.forms.password
    is_encrypted = bool(bottle.request.forms.is_encrypted)
    if not is_encrypted:
        match = hashlib.sha1(password).hexdigest()
    else:
        match = password
    util.log.debug(
        '%s == %s ? %s' % (
            match, paste.password,
            match == paste.password,
        )
    )
    kwargs = {
        'password': paste.password,
        'content': paste.content,
        'syntax': lexers.get_lexer_for_mimetype(paste.mimetype).aliases[0],
    }
    if paste.password:
        if not password:
            return _password_protect_form()
        if match == paste.password:
            return _edit_form('edit entry #%s' % (paste.id,), **kwargs)
        else:
            return bottle.HTTPError(401, output='Wrong password provided')
    else:
        return _edit_form('edit entry #%s' % (paste.id, ), **kwargs)
Пример #11
0
    def get_bodypart_tokens(self, text):
        # return if:
        #  * no content
        #  * no content type specific
        #  * content encoding is not readable
        #  * max recurrsion exceed
        if not text.strip() or not self.content_type:
            return [(0, Other, text)]

        cte = self.content_transfer_encoding
        if cte and cte not in {"8bit", "7bit", "quoted-printable"}:
            return [(0, Other, text)]

        if self.max_nested_level == 0:
            return [(0, Other, text)]

        # get lexer
        try:
            lexer = get_lexer_for_mimetype(self.content_type)
        except ClassNotFound:
            return [(0, Other, text)]

        if isinstance(lexer, type(self)):
            lexer.max_nested_level = self.max_nested_level - 1

        return lexer.get_tokens_unprocessed(text)
Пример #12
0
def test_get_lexers():
    # test that the lexers functions work
    for func, args in [
        (lexers.get_lexer_by_name, ("python", )),
        (lexers.get_lexer_for_filename, ("test.py", )),
        (lexers.get_lexer_for_mimetype, ("text/x-python", )),
        (lexers.guess_lexer, ("#!/usr/bin/python3 -O\nprint", )),
        (lexers.guess_lexer_for_filename, ("a.py", "<%= @foo %>"))
    ]:
        x = func(opt='val', *args)
        assert isinstance(x, lexers.PythonLexer)
        assert x.options["opt"] == "val"

    for cls, (_, lname, aliases, _, mimetypes) in lexers.LEXERS.items():
        assert cls == lexers.find_lexer_class(lname).__name__

        for alias in aliases:
            assert cls == lexers.get_lexer_by_name(alias).__class__.__name__

        for mimetype in mimetypes:
            assert cls == lexers.get_lexer_for_mimetype(
                mimetype).__class__.__name__

    try:
        lexers.get_lexer_by_name(None)
    except ClassNotFound:
        pass
    else:
        raise Exception
Пример #13
0
    def content_callback(self, match):
        content_type = getattr(self, 'content_type', None)
        content = match.group()
        offset = match.start()
        if content_type:
            from pygments.lexers import get_lexer_for_mimetype
            possible_lexer_mimetypes = [content_type]
            if '+' in content_type:
                # application/calendar+xml can be treated as application/xml
                # if there's not a better match.
                general_type = re.sub(r'^(.*)/.*\+(.*)$', r'\1/\2',
                                      content_type)
                possible_lexer_mimetypes.append(general_type)

            for i in possible_lexer_mimetypes:
                try:
                    lexer = get_lexer_for_mimetype(i)
                except ClassNotFound:
                    pass
                else:
                    for idx, token, value in lexer.get_tokens_unprocessed(
                            content):
                        yield offset + idx, token, value
                    return
        yield offset, Text, content
Пример #14
0
    def _html_format(self, content, content_types):
        try:
            from pygments import highlight
            from pygments.lexers import get_lexer_for_mimetype
            from pygments.formatters import HtmlFormatter

            lexer = None
            for ct in content_types:
                try:
                    lexer = get_lexer_for_mimetype(ct)
                    break
                except:
                    pass

            if lexer is None:
                raise ValueError("No lexer found")
            formatter = HtmlFormatter()
            return html_body % dict(
                css=formatter.get_style_defs(),
                content=highlight(content, lexer, formatter).encode('utf8'))
        except Exception as e:
            log.warning(
                "Could not pygment the content because of the following "
                "error :\n%s" % e)
            return html_body % dict(
                css='',
                content=u('<pre>%s</pre>') %
                    content.replace(b('>'), b('&gt;'))
                           .replace(b('<'), b('&lt;')))
def filedisplay(context):
    """
    Display a file at a given revision.
    According to the mimetype, the file may be displayed as source, colored
    thanks to Pygments, or, for pictures and PDF, directly in the browser
    """
    from pygments import highlight
    from pygments.formatters import HtmlFormatter
    from pygments.lexers import get_lexer_for_mimetype, guess_lexer_for_filename
    import mimetypes

    if 'raw' in context['request'].GET:
        mimetype = ('text/plain', None)
    else:
        mimetype = mimetypes.guess_type(context['file'])

    if mimetype != ('text/plain', None):
        lexer = None
        if mimetype[0] is not None:
            try:
                lexer = get_lexer_for_mimetype(mimetype)
            except:
                lexer = None
        if lexer is None:
            try:
                lexer = guess_lexer_for_filename(context['file'],
                                                 context['fctx'].data())
            except:
                lexer = None
    else:
        # a lexer can't be guess from plain text mimetype.
        # we force the file to be view as text
        try:
            lexer = guess_lexer_for_filename(context['file'] + '.txt',
                                             context['fctx'].data())
        except:
            lexer = None

    if lexer:
        formatter = HtmlFormatter(linenos=True, cssclass="source")
        content = highlight(context['fctx'].data(), lexer, formatter)
    else:
        lexer = None
        if mimetype[0] == 'image/png' or mimetype[
                0] == 'image/jpeg' or mimetype[0] == 'image/gif':
            content = 'image'
        elif mimetype[0] == 'application/pdf':
            content = 'pdf'
        else:
            content = None

    return {
        'content': content,
        'lexer': lexer,
        'mimetype': mimetype,
        'file': context['file'],
        'size': context['fctx'].size(),
        'name': context['repo'],
        'rev': context['rev']
    }
Пример #16
0
def _pygmentize(paste, lang):
    """
    Guess (or force if lang is given) highlight on a given paste via pygments
    """

    if lang:
        try:
            lexer = lexers.get_lexer_by_name(lang)
        except lexers.ClassNotFound:
            lexer = lexers.get_lexer_by_name('text')
    else:
        lexer = lexers.get_lexer_for_mimetype(paste.mimetype)
    a = '<small><a href="/edit/%s">edit</a></small>' % (paste.id,)
    if paste.ip:
        ip = IPy.IP(long(paste.ip, 2))
        util.log.debug('Originally pasted from %s' % (ip,))
    if paste.filename:
        title = u'%s, created on %s' % (paste.filename, paste.created, )
    else:
        title = u'created on %s' % (paste.created, )
    title = '%s %s (%s)' % (paste.mimetype, title, a,)
    util.log.debug(lexer)
    return pygments.highlight(
        paste.content, lexer, formatters.HtmlFormatter(
            full=True, linenos='table',
            encoding='utf-8', lineanchors='ln', title=title)
        )
Пример #17
0
def nice_body(body, content=None, cssclass=None, encoding='utf-8'):
    if not body:
        return None
    cssclasses = ['codehilite']
    if cssclass:
        cssclasses.append(cssclass)
    classes = ' '.join(cssclasses)
    content = get_body_content_type(body, content)
    if content is not None:
        if 'x-www-form-urlencoded' in content:
            lex = IniLexer()
        elif 'json' in content:
            lex = JsonLexer()
        else:
            try:
                lex = get_lexer_for_mimetype(content, encoding=encoding)
            except ClassNotFound as e:
                return body

        if isinstance(lex, IniLexer):
            parsedbody = urlparse.parse_qsl(body, keep_blank_values=True)
            if body and not parsedbody:
                return tornado.escape.xhtml_escape(body)
            parsedbody = [(x.strip(), y) for [x, y] in parsedbody]
            args = collections.OrderedDict(sorted(parsedbody))
            params = "\n".join([k.strip() + "=" + v for k, v in args.iteritems()])
            return highlight(params, IniLexer(), HtmlFormatter(cssclass=classes, encoding=encoding))
        elif isinstance(lex, JsonLexer):
            try:
                return highlight(json.dumps(json.loads(body), indent=4), JsonLexer(), HtmlFormatter(cssclass=classes))
            except ValueError as e:
                pass

    return highlight(body, lex, HtmlFormatter(cssclass=classes, encoding=encoding))
Пример #18
0
 def process_body(self, content, content_type):
     try:
         lexer = get_lexer_for_mimetype(content_type)
     except ClassNotFound:
         pass
     else:
         content = pygments.highlight(content, lexer, self.formatter)
     return content
Пример #19
0
 def print_data(self, data, mimetype):
     if data:
         try:
             lexer = get_lexer_for_mimetype(mimetype)
         except ClassNotFound:
             lexer = guess_lexer(data)
         print
         print highlight(data, lexer, TerminalFormatter())
Пример #20
0
 def print_data(self, data, mimetype):
     if data:
         try:
             lexer = get_lexer_for_mimetype(mimetype)
         except ClassNotFound:
             lexer = guess_lexer(data)
         print
         print highlight(data, lexer, TerminalFormatter())
Пример #21
0
def pygment(content, mime=None):
    if mime:
        lexer = get_lexer_for_mimetype(mime, stripall=True)
    else:
        lexer = guess_lexer(content)
    formatter = HtmlFormatter(linenos='table')
    code = highlight(content, lexer, formatter)
    return Markup(code)
Пример #22
0
 def render_example(self, example):
     renderer = self.renderers[self.media_type]
     rendered = renderer().render(example, renderer_context={'indent': 4})
     if self.media_type == 'application/xml':
         xml_doc = xml.dom.minidom.parseString(rendered)
         rendered = xml_doc.toprettyxml(indent='   ')
     lexer = get_lexer_for_mimetype(self.media_type)
     return mark_safe(highlight(rendered, lexer, formatter))
Пример #23
0
def do_request(request):
    # Parse http request
    request_line, headers_and_body = request.lstrip().split('\n', 1)
    message = email.message_from_file(StringIO(headers_and_body))
    method, request_uri = request_line.split(' ')
    headers = dict(message.items())
    message_body = message.get_payload()

    # Eval body
    expressions = re.findall(r'\$\((.*?)\)', message_body)
    for expression in expressions:
        value = subprocess.run(expression, capture_output=True, shell=True, encoding='UTF-8').stdout.rstrip()
        message_body = message_body.replace(f"$({expression})", value)

    # Perform http request
    parsed = urlparse(request_uri)
    hostname = parsed.netloc
    path = parsed.path
    if parsed.query:
        path = '%s?%s' % (path, parsed.query)
    connection = {}
    if parsed.scheme == 'http':
        connection = HTTPConnection(hostname)
    elif parsed.scheme == 'https':
        connection = HTTPSConnection(hostname, timeout=8, context=ssl._create_unverified_context())
    if 'Host' not in headers:
        headers['Host'] = parsed.netloc
    connection.request(method, path, message_body, headers=headers)
    response = connection.getresponse()

    # Parse response
    mimetype = ""
    charset = ""
    body = ""
    if 'Content-Type' in response.headers:
        content_type = response.headers['Content-Type']
        if "; charset=" in content_type:
            mimetype, charset = content_type.split('; charset=', 1)
        else:
            mimetype, charset = content_type, "ISO-8859-1"
        body = response.read().decode(charset)
    else:
        body = response.read().decode("ISO-8859-1")
    response.close()
    connection.close()

    # Print http response
    print("%s %s" % (response.status, response.reason))
    for key, val in response.headers.items():
        print(colored(key, "cyan") + ": " + colored(val, "yellow"))
    if mimetype:
        lexer = lexers.get_lexer_for_mimetype(mimetype)
        if 'application/json' in mimetype:
            body = json.dumps(json.loads(body), indent=4)
        colored_output = highlight(body, lexer, formatters.TerminalFormatter())
        print(colored_output.strip())
    else:
        print(body)
Пример #24
0
    def set_lexer_from_mime_type(self, mime, **options):
        """
        Sets the pygments lexer from mime type.

        :param mime: mime type
        :param options: optional addtional options.
        """
        self._lexer = get_lexer_for_mimetype(mime, **options)
        _logger().debug('lexer for mimetype (%s): %r', mime, self._lexer)
Пример #25
0
def example(body):
    if body:
        example = body.raw[body.mime_type].get('example', None)

        if example is not None:
            lexer = get_lexer_for_mimetype(body.mime_type, stripall=True)
            formatter = HtmlFormatter(linenos=False, cssclass="codehilite")
            return mark_safe(highlight(example, lexer, formatter))
    return ''
Пример #26
0
def _pygmentize(paste, lang):
    """
    Guess (or force if lang is given) highlight on a given paste via pygments
    """

    util.log.debug("{0} in {1} language".format(
        paste,
        lang,
    ))
    if lang:
        try:
            lexer = lexers.get_lexer_by_name(lang)
        except lexers.ClassNotFound:
            lexer = lexers.get_lexer_by_name('text')
    else:
        try:
            util.log.debug(paste.lexer)
            lexer = lexers.get_lexer_by_name(paste.lexer)
            util.log.debug(lexer)
        except lexers.ClassNotFound:
            lexer = lexers.get_lexer_for_mimetype(paste.mimetype)
    util.log.debug('Lexer is {0}'.format(lexer, ))
    if paste.ip:
        ip = IPy.IP(int(paste.ip, 2))
        util.log.debug('Originally pasted from {0}'.format(ip, ))
    if paste.filename:
        title = '{0}, created on {1}'.format(
            paste.filename,
            paste.created,
        )
    else:
        title = 'created on {0}'.format(paste.created, )
    title = '{0} {1}'.format(
        paste.mimetype,
        title,
    )
    util.log.debug(lexer)
    content = pygments.highlight(
        paste.content, lexer,
        formatters.HtmlFormatter(
            linenos='table',
            encoding='utf-8',
            lineanchors='ln',
            anchorlinenos=True,
        ))
    _add_header_metadata(paste)
    return bottle.template(
        'pygmentize.html',
        pygmentized=content,
        title=title,
        version=pasttle.__version__,
        current_year=CURRENT_YEAR,
        url=get_url(),
        id=paste.id,
        parent=paste.parent or u'',
        pygments_style=util.conf.get(util.cfg_section, 'pygments_style'),
    )
Пример #27
0
    def set_lexer_from_mime_type(self, mime, **options):
        """
        Sets the pygments lexer from mime type.

        :param mime: mime type
        :param options: optional addtional options.
        """
        self._lexer = get_lexer_for_mimetype(mime, **options)
        _logger().info('lexer for mimetype (%s): %r', mime, self._lexer)
def create_text_thumb(gio_file, size=None, threshold=2):
    """ tries to use pygments to get a thumbnail of a text file """
    if pygments is None:
        return None
    try:
        lexer = get_lexer_for_mimetype(gio_file.mime_type)
    except pygments.util.ClassNotFound:
        lexer = get_lexer_for_mimetype("text/plain")
    if chardet:
        lexer.encoding = "chardet"
    thumb = tempfile.NamedTemporaryFile()
    formatter = ImageFormatter(font_name="DejaVu Sans Mono",
                               line_numbers=False,
                               font_size=10)
    # to speed things up only highlight the first 20 lines
    content = "\n".join(gio_file.get_content().split("\n")[:20])
    try:
        content = highlight(content, lexer, formatter)
    except (UnicodeDecodeError, TypeError):
        # we can't create the pixbuf
        return None
    thumb.write(content)
    thumb.flush()
    thumb.seek(0)
    try:
        pixbuf = gtk.gdk.pixbuf_new_from_file(thumb.name)
    except glib.GError:
        return None  # (LP: #743125)
    thumb.close()
    if size is not None:
        new_height = None
        new_width = None
        height = pixbuf.get_height()
        width = pixbuf.get_width()
        if width > threshold * size[0]:
            new_width = threshold * size[0]
        if height > threshold * size[1]:
            new_height = threshold * size[1]
        if new_height is not None or new_width is not None:
            pixbuf = __crop_pixbuf(pixbuf, 0, 0,
                                   (new_width or width, new_height or height))
    return pixbuf
Пример #29
0
def pretty_body(msg):
    from .util import printable_data
    to_ret = printable_data(msg.body, colors=False)
    if 'content-type' in msg.headers:
        try:
            lexer = get_lexer_for_mimetype(
                msg.headers.get('content-type').split(';')[0])
            to_ret = highlight(to_ret, lexer, TerminalFormatter())
        except:
            pass
    return to_ret
Пример #30
0
def highlight_by_mime_type(content, mimetype):
    try:
        #if config.get('global', 'syntax') == "1":
        lexer = get_lexer_for_mimetype(mimetype, encoding='chardet')
        #else:
        #lexer = get_lexer_for_mimetype('text/plain')
        formatter = HtmlFormatter(linenos="inline", cssclass="source", outencoding="utf-8")
        content = highlight(content, lexer, formatter)
    except Exception, e:
        #log_package('hilight error in extract_code : ' + str(e), log_path)
        content = '<p>An error occurred while formatting content: '+str(e)+'</p>' + content
Пример #31
0
 def highlight_content(self, mime, data):
     if mime.startswith('application/json'):
         data = json.dumps(json.loads(data), sort_keys=True, indent=2)
     elif mime.startswith('application/octet-stream'):
         data = '<binary data>'
     try:
         lexer = lexers.get_lexer_for_mimetype(mime)
         data = highlight(data, lexer, TerminalFormatter())
     except LexerNotFound:
         pass
     return data.strip()
Пример #32
0
def _pygmentize(paste, lang):
    """
    Guess (or force if lang is given) highlight on a given paste via pygments
    """

    util.log.debug("%s in %s language" % (
        paste,
        lang,
    ))
    if lang:
        try:
            lexer = lexers.get_lexer_by_name(lang)
        except lexers.ClassNotFound:
            lexer = lexers.get_lexer_by_name('text')
    else:
        try:
            util.log.debug(paste.lexer)
            lexer = lexers.get_lexer_by_name(paste.lexer)
            util.log.debug(lexer)
        except lexers.ClassNotFound:
            lexer = lexers.get_lexer_for_mimetype(paste.mimetype)
    util.log.debug('Lexer is %s' % (lexer, ))
    if paste.ip:
        ip = IPy.IP(long(paste.ip, 2))
        util.log.debug('Originally pasted from %s' % (ip, ))
    if paste.filename:
        title = u'%s, created on %s' % (
            paste.filename,
            paste.created,
        )
    else:
        title = u'created on %s' % (paste.created, )
    title = '%s %s' % (
        paste.mimetype,
        title,
    )
    util.log.debug(lexer)
    content = pygments.highlight(
        paste.content, lexer,
        formatters.HtmlFormatter(
            linenos='table',
            encoding='utf-8',
            lineanchors='ln',
            anchorlinenos=True,
        ))
    return template(
        'pygmentize.html',
        pygmentized=content,
        title=title,
        version=pasttle.__version__,
        url=get_url(),
        id=paste.id,
        pygments_style=util.conf.get(util.cfg_section, 'pygments_style'),
    )
Пример #33
0
    def get(self, name):
        try:
            item = current_app.storage.open(name)
        except (OSError, IOError) as e:
            if e.errno == errno.ENOENT:
                return render_template('file_not_found.html'), 404

        with item as item:
            if not item.meta.get('unlocked'):
                error = 'File locked.'
            elif not item.meta.get('complete'):
                error = 'Upload incomplete. Try again later.'
            else:
                error = None
            if error:
                return render_template('display_error.html',
                                       name=name,
                                       item=item,
                                       error=error), 409
            ct = item.meta['type']
            if ct.startswith('text/'):
                code = item.data.read(item.data.size, 0)
                code = code.decode(
                    'utf-8')  # TODO we don't have the coding in metadata
                lexer = get_lexer_for_mimetype(ct)
                formatter = HtmlFormatter(linenos='table',
                                          lineanchors="L",
                                          anchorlinenos=True)
                rendered_content = Markup(highlight(code, lexer, formatter))
            elif ct.startswith('image/'):
                src = url_for('bepasty.download', name=name)
                rendered_content = Markup(u'<img src="%s" width="800">' % src)
            elif ct.startswith('audio/'):
                src = url_for('bepasty.download', name=name)
                alt_msg = u'html5 audio element not supported by your browser.'
                rendered_content = Markup(
                    u'<audio controls src="%s">%s</audio>' % (src, alt_msg))
            elif ct.startswith('video/'):
                src = url_for('bepasty.download', name=name)
                alt_msg = u'html5 video element not supported by your browser.'
                rendered_content = Markup(
                    u'<video controls src="%s">%s</video>' % (src, alt_msg))
            elif ct == 'application/pdf':
                src = url_for('bepasty.inline', name=name)
                link_txt = u'Click to see PDF'
                rendered_content = Markup(u'<a href="%s">%s</a>' %
                                          (src, link_txt))
            else:
                rendered_content = u"Can't render this content type."
            return render_template('display.html',
                                   name=name,
                                   item=item,
                                   rendered_content=rendered_content)
Пример #34
0
def get_language_for(filename, mimetype=None, default='text'):
    """Get language for filename and mimetype"""
    try:
        if mimetype is None:
            raise ClassNotFound()
        lexer = get_lexer_for_mimetype(mimetype)
    except ClassNotFound:
        try:
            lexer = get_lexer_for_filename(filename)
        except ClassNotFound:
            return default
    return get_known_alias(lexer, default)
Пример #35
0
def get_language_for(filename, mimetype=None, default='text'):
    """Get language for filename and mimetype"""
    try:
        if mimetype is None:
            raise ClassNotFound()
        lexer = get_lexer_for_mimetype(mimetype)
    except ClassNotFound:
        try:
            lexer = get_lexer_for_filename(filename)
        except ClassNotFound:
            return default
    return get_known_alias(lexer, default)
Пример #36
0
def edit(db, id):
    """
    Edits the entry. If the entry is protected with a password it will display
    a simple password entry form until the password is a match in the database
    """

    paste = _get_paste(db, id)
    if not paste:
        return bottle.HTTPError(404, 'This paste does not exist')
    post_args = dict(
        title='Create new entry based on #{0}'.format(paste.id),
        password=paste.password or u'',
        content=paste.content,
        checked='',
        syntax=lexers.get_lexer_for_mimetype(paste.mimetype).aliases[0],
        parent=id,
        url=get_url(),
        version=pasttle.__version__,
        current_year=CURRENT_YEAR,
    )

    form = bottle.request.forms
    if paste.password:
        password = form.get('password')

        if not password:
            return bottle.template(
                'password_protect',
                url=get_url(),
                title=util.conf.get(util.cfg_section, 'title'),
                version=pasttle.__version__,
                current_year=CURRENT_YEAR,
            )

        is_encrypted = bool(form.get('is_encrypted'))
        if not is_encrypted:
            match = hashlib.sha1(password.encode()).hexdigest()
        else:
            match = password
        util.log.debug('{0} == {1} ? {2}'.format(
            match,
            paste.password,
            match == paste.password,
        ))

        if match == paste.password:
            post_args['checked'] = 'checked="checked"'
            return bottle.template('post', post_args)
        else:
            return bottle.HTTPError(401, 'Wrong password provided')
    else:
        return bottle.template('post', post_args)
Пример #37
0
def get_blob_text(repopath, path, branchname='master'):
    repo = Repo(repopath)
    git = repo.git
    text = None
    text = git.show('%s:%s' % (branchname, path))
    mime_type = get_mime_type(repo, branchname, path)
    try:
        lexer = get_lexer_for_mimetype(mime_type)
    except ClassNotFound:
        lexer = get_lexer_by_name('text')
    formatter = HtmlFormatter(linenos=True, lineanchors='line', anchorlinenos=True)
    result = highlight(Markup(text).unescape(), lexer, formatter)
    return result
Пример #38
0
def pygments_highlight2(code, mime_type, filename=None, **kwargs):
	if not code:
		return ""

	lexer = None

	if mime_type == "text/plain" and filename:
		try:
			lexer = get_lexer_for_filename(filename)
		except ClassNotFound as cnf:
			logging.info("No lexer: %s" % cnf)

	if not lexer:
		try:
			lexer = get_lexer_for_mimetype(mime_type)
		except ClassNotFound as cnf:
			if filename:
				lexer = get_lexer_for_filename(filename)
			else:
				lexer = get_lexer_for_mimetype('text/plain')

	return highlight(code, lexer, HtmlFormatter(**kwargs))
Пример #39
0
def pygmentize(mime, blob):
    try:
        lexer = lexers.get_lexer_for_mimetype(mime)
    except ClassNotFound:
        try:
            lexer = lexers.get_lexer_by_name(mime)
        except:
            lexer = lexers.get_lexer_by_name('text')

    pygmented_string = pygments.highlight(blob, lexer, NakedHtmlFormatter())
    pygmented_string = unescape_amp(pygmented_string)

    return mark_safe(pygmented_string)
Пример #40
0
def edit(db, id):
    """
    Edits the entry. If the entry is protected with a password it will display
    a simple password entry form until the password is a match in the database
    """

    paste = _get_paste(db, id)
    if not paste:
        return bottle.HTTPError(404, 'This paste does not exist')
    post_args = dict(
        title='Create new entry based on #{0}'.format(paste.id),
        password=paste.password or u'',
        content=paste.content,
        checked='',
        syntax=lexers.get_lexer_for_mimetype(paste.mimetype).aliases[0],
        parent=id,
        url=get_url(),
        version=pasttle.__version__,
    )

    form = bottle.request.forms
    if paste.password:
        password = form.get('password')

        if not password:
            return template(
                'password_protect',
                url=get_url(),
                title=util.conf.get(util.cfg_section, 'title'),
                version=pasttle.__version__,
            )

        is_encrypted = bool(form.get('is_encrypted'))
        if not is_encrypted:
            match = hashlib.sha1(password.encode()).hexdigest()
        else:
            match = password
        util.log.debug(
            '{0} == {1} ? {2}'.format(
                match, paste.password,
                match == paste.password,
            )
        )

        if match == paste.password:
            post_args['checked'] = 'checked="checked"'
            return template('post', post_args)
        else:
            return bottle.HTTPError(401, 'Wrong password provided')
    else:
        return template('post', post_args)
Пример #41
0
def prettify(jsonobj, prettify=True):
    """ prettiffy JSON output """
    if not prettify:
        return json.dumps(jsonobj)

    json_str = json.dumps(jsonobj, indent=2, sort_keys=True)
    if pygments:
        try:
            lexer = get_lexer_for_mimetype("application/json")
            return pygments.highlight(json_str, lexer, TerminalFormatter())
        except:
            pass

    return json_str
Пример #42
0
 def process_body(self, content, content_type, subtype):
     try:
         lexer = self.lexers_by_type.get(content_type)
         if not lexer:
             try:
                 lexer = get_lexer_for_mimetype(content_type)
             except ClassNotFound:
                 lexer = get_lexer_by_name(subtype)
             self.lexers_by_type[content_type] = lexer
     except ClassNotFound:
         pass
     else:
         content = pygments.highlight(content, lexer, self.formatter)
     return content.strip()
Пример #43
0
 def fill_highlighted(self):
     with DisableUpdates(self.htmlxml_widg):
         self.highlighted_widg.setPlainText("")
         if not self.data:
             return
         ct = self.headers.get('Content-Type').lower()
         if ";" in ct:
             ct = ct.split(";")[0]
         try:
             lexer = get_lexer_for_mimetype(ct)
             highlighted = textedit_highlight(self.data, lexer)
         except:
             highlighted = printable_data(self.data)
         self.highlighted_widg.setHtml(highlighted)
Пример #44
0
def highlight_for_mimetype(
        text: str,
        mimetype: str,
        *,
        fallback_mimetype: Optional[str] = 'text/plain',
        formatter_cls: Type[Formatter] = TerminalFormatter) -> str:
    """Return ANSI-escaped highlighted text, as per the .

    If :param`mimetype` cannot be resolved, then :param`fallback_mimetype` will be used.
    If that cannot be resolved (or is ``None``), then the pygments ``ClassNotFound``
    exception will be raised.

    """
    try:
        lexer = get_lexer_for_mimetype(mimetype)
    except ClassNotFound as e:
        if fallback_mimetype is not None:
            lexer = get_lexer_for_mimetype(fallback_mimetype)
        else:
            raise e

    highlighted_text: str = highlight(text, lexer, formatter_cls())
    return highlighted_text
Пример #45
0
 def find_lexer(mimetype):
     from pygments.lexers import get_lexer_for_mimetype
     from pygments.util import ClassNotFound
     mime = str(mimetype)
     lexer = None
     try:
         lexer = _lexers_cache[mime]()
     except KeyError:
         try:
             lexer = get_lexer_for_mimetype(mime)
             _lexers_cache[mime] = lexer.__class__
         except ClassNotFound:
             pass
     return lexer
Пример #46
0
async def request(*, method: str, proto: str = 'https', **params: str):
    """Send an HTTP or HTTPS request."""
    if method not in HTTP_VERBS:
        app.io.error(f'Invalid HTTP verb {method}')

    path = str(app.current_path).lstrip('/')
    url = f'{proto}://{path}'
    app.io.info(f'Sending {method} request to {url}...')

    request_coro = getattr(app.bag.session, method.lower())
    resp = await request_coro(url=url, params=params)
    async with resp:
        try:
            lexer = get_lexer_for_mimetype(resp.content_type)
        except Exception:
            lexer = get_lexer_for_mimetype('text/plain')

        app.io.info(f'Status {resp.status} response from {resp.url}')
        app.io.info('Here\'s the content:')

        text = await resp.text()
        highlighted_text = highlight(text, lexer, TerminalFormatter())
        app.io.ansi(highlighted_text)
Пример #47
0
def prettify(jsonobj, prettify=True):
    """ prettiffy JSON output """
    if not prettify:
        return json.dumps(jsonobj)

    json_str = json.dumps(jsonobj, indent=2, sort_keys=True)
    if pygments:
        try:
            lexer = get_lexer_for_mimetype("application/json")
            return pygments.highlight(json_str, lexer, TerminalFormatter())
        except:
            pass

    return json_str
Пример #48
0
 def process_body(self, content, content_type, subtype):
     try:
         lexer = self.lexers_by_type.get(content_type)
         if not lexer:
             try:
                 lexer = get_lexer_for_mimetype(content_type)
             except ClassNotFound:
                 lexer = get_lexer_by_name(subtype)
             self.lexers_by_type[content_type] = lexer
     except ClassNotFound:
         pass
     else:
         content = pygments.highlight(content, lexer, self.formatter)
     return content.strip()
Пример #49
0
    def return_lexer(self, lexer, args, inputs, code=None):
        """
        Accepting a variety of possible inputs, return a Lexer object.

        The inputs argument should be a hash with at least one of the following
        keys:

            - 'lexer' ("python")
            - 'mimetype' ("text/x-ruby")
            - 'filename' ("yeaaah.py")

        The code argument should be a string, such as "import derp".

        The code guessing method is not especially great. It is advised that
        clients pass in a literal lexer name whenever possible, which provides
        the best probability of match (100 percent).
        """

        if lexer:
            if inputs:
                return lexers.get_lexer_by_name(lexer, **inputs)
            else:
                return lexers.get_lexer_by_name(lexer)

        if inputs:
            if 'lexer' in inputs:
                return lexers.get_lexer_by_name(inputs['lexer'], **inputs)

            elif 'mimetype' in inputs:
                return lexers.get_lexer_for_mimetype(inputs['mimetype'],
                                                     **inputs)

            elif 'filename' in inputs:
                name = inputs['filename']

                # If we have code and a filename, pygments allows us to guess
                # with both. This is better than just guessing with code.
                if code:
                    return lexers.guess_lexer_for_filename(
                        name, code, **inputs)
                else:
                    return lexers.get_lexer_for_filename(name, **inputs)

        # If all we got is code, try anyway.
        if code:
            return lexers.guess_lexer(code, **inputs)

        else:
            return None
Пример #50
0
    def set_lexer_from_filename(self, filename):
        """
        Change the lexer based on the filename (actually only the extension is
        needed)

        :param filename: Filename or extension
        """
        self._lexer = None
        if filename.endswith("~"):
            filename = filename[0:len(filename) - 1]
        try:
            self._lexer = get_lexer_for_filename(filename)
        except (ClassNotFound, ImportError):
            print('class not found for url', filename)
            try:
                m = mimetypes.guess_type(filename)
                print(m)
                self._lexer = get_lexer_for_mimetype(m[0])
            except (ClassNotFound, IndexError, ImportError):
                self._lexer = get_lexer_for_mimetype('text/plain')
        if self._lexer is None:
            _logger().warning('failed to get lexer from filename: %s, using '
                              'plain text instead...', filename)
            self._lexer = TextLexer()
Пример #51
0
    def generate_content(cls, attachment):
        mime_type = attachment.file.content_type

        lexer = cls.CUSTOM_LEXERS.get(mime_type)
        if lexer is None:
            lexer = get_lexer_for_mimetype(mime_type)

        with attachment.file.open() as f:
            html_formatter = HtmlFormatter(style='tango', linenos='inline', prestyles='mono')
            html_code = highlight(f.read(), lexer, html_formatter)

        css_code = html_formatter.get_style_defs('.highlight')

        return render_template('previewer_code:pygments_preview.html', attachment=attachment,
                               html_code=html_code, css_code=css_code)
Пример #52
0
 def content_callback(self, match):
     content_type = getattr(self, 'content_type', None)
     content = match.group()
     offset = match.start()
     if content_type:
         from pygments.lexers import get_lexer_for_mimetype
         try:
             lexer = get_lexer_for_mimetype(content_type)
         except ClassNotFound:
             pass
         else:
             for idx, token, value in lexer.get_tokens_unprocessed(content):
                 yield offset + idx, token, value
             return
     yield offset, Text, content
Пример #53
0
    def body(self, content, content_type):
        content_type = content_type.split(';')[0]
        try:
            lexer = get_lexer_for_mimetype(content_type)
        except ClassNotFound:
            return content

        if content_type == 'application/json':
            try:
                # Indent and sort the JSON data.
                content = json.dumps(json.loads(content),
                                     sort_keys=True, indent=4)
            except:
                pass

        return pygments.highlight(content, lexer, self.formatter)
Пример #54
0
 def body(self, content, content_type):
     lexer = None
     content_type = content_type.split(";")[0]
     if "json" in content_type:
         lexer = JSONLexer()
         try:
             # Indent the JSON data.
             content = json.dumps(json.loads(content), sort_keys=True, indent=4)
         except Exception:
             pass
     if not lexer:
         try:
             lexer = get_lexer_for_mimetype(content_type)
         except ClassNotFound:
             return content
     return pygments.highlight(content, lexer, self.formatter)
Пример #55
0
def syntaxhighlight_pygments(mimetype):
    from pygments import highlight
    from pygments.lexers import get_lexer_for_mimetype
    from pygments.formatters import TerminalFormatter

    lexer = get_lexer_for_mimetype(mimetype, encoding='utf-8')
    formatter = TerminalFormatter(encoding='utf-8')

    @contextmanager
    def filter(output):
        with make_temp_file() as f:
            yield f
            f.seek(0)
            code = f.read()
        highlight(code, lexer, formatter, output)
    return filter
Пример #56
0
 def body(self, content, content_type):
     content_type = content_type.split(';')[0]
     if 'json' in content_type:
         content_type = TYPE_JS
         try:
             # Indent JSON
             content = json.dumps(json.loads(content),
                                 sort_keys=True, indent=4)
         except Exception:
             pass
     try:
         lexer = get_lexer_for_mimetype(content_type)
     except ClassNotFound:
         return content
     content = pygments.highlight(content, lexer, self.formatter)
     return content
Пример #57
0
    def return_lexer(self, lexer, args, inputs, code=None):
        """
        Accepting a variety of possible inputs, return a Lexer object.

        The inputs argument should be a hash with at least one of the following
        keys:

            - 'lexer' ("python")
            - 'mimetype' ("text/x-ruby")
            - 'filename' ("yeaaah.py")

        The code argument should be a string, such as "import derp".

        The code guessing method is not especially great. It is advised that
        clients pass in a literal lexer name whenever possible, which provides
        the best probability of match (100 percent).
        """

        if lexer:
            if inputs:
                return lexers.get_lexer_by_name(lexer, **inputs)
            else:
                return lexers.get_lexer_by_name(lexer)

        if inputs:
            if "lexer" in inputs:
                return lexers.get_lexer_by_name(inputs["lexer"], **inputs)

            elif "mimetype" in inputs:
                return lexers.get_lexer_for_mimetype(inputs["mimetype"], **inputs)

            elif "filename" in inputs:
                name = inputs["filename"]

                # If we have code and a filename, pygments allows us to guess
                # with both. This is better than just guessing with code.
                if code:
                    return lexers.guess_lexer_for_filename(name, code, **inputs)
                else:
                    return lexers.get_lexer_for_filename(name, **inputs)

        # If all we got is code, try anyway.
        if code:
            return lexers.guess_lexer(code, **inputs)

        else:
            return None
Пример #58
0
def edit(db, id):
    """
    Edits the entry. If the entry is protected with a password it will display
    a simple password entry form until the password is a match in the database
    """

    paste = _get_paste(db, id)
    if not paste:
        return bottle.HTTPError(404, output='This paste does not exist')
    password = bottle.request.forms.password
    is_encrypted = bool(bottle.request.forms.is_encrypted)
    if not is_encrypted:
        match = hashlib.sha1(password).hexdigest()
    else:
        match = password
    util.log.debug(
        '%s == %s ? %s' % (
            match, paste.password,
            match == paste.password,
        )
    )

    post_args = dict(
        title='Edit entry #%s' % (paste.id),
        password=paste.password or u'',
        content=paste.content,
        checked=u'',
        syntax=lexers.get_lexer_for_mimetype(paste.mimetype).aliases[0],
        url=get_url(),
        version=pasttle.__version__,
    )

    if paste.password:
        if not password:
            return template(
                'password_protect',
                url=get_url(),
                title=util.conf.get(util.cfg_section, 'title'),
                version=pasttle.__version__,
            )
        if match == paste.password:
            post_args['checked'] = 'checked'
            return template('post', post_args)
        else:
            return bottle.HTTPError(401, output='Wrong password provided')
    else:
        return template('post', post_args)