Esempio n. 1
0
def pygmentize(code, filename, render_markup, ctags=None, ctags_baseurl=None):
    """Render code using Pygments, markup (markdown, rst, ...) using the
    corresponding renderer, if available.

    :param code: the program code to highlight, str
    :param filename: name of the source file the code is taken from, str
    :param render_markup: whether to render markup if possible, bool
    :param ctags: tagsfile obj used for source code hyperlinks, ``ctags.CTags``
    :param ctags_baseurl: base url used for source code hyperlinks, str
    """
    if render_markup and markup.can_render(filename):
        return markup.render(filename, code)

    try:
        lexer = get_lexer_for_filename(filename, code)
    except ClassNotFound:
        try:
            lexer = guess_lexer(code)
        except ClassNotFound:
            lexer = TextLexer()

    formatter_cls = {
        'Python': KlausPythonFormatter,
    }.get(lexer.name, KlausDefaultFormatter)
    if ctags:
        ctags_urlscheme = ctags_baseurl + "%(path)s%(fname)s%(fext)s"
    else:
        ctags_urlscheme = None
    formatter = formatter_cls(
        language=PYGMENTS_CTAGS_LANGUAGE_MAP.get(lexer.name),
        ctags=ctags,
        tagurlformat=ctags_urlscheme,
    )

    return highlight(code, lexer, formatter)
Esempio n. 2
0
    def make_context(self, *args):
        super(BlobView, self).make_context(*args)

        if guess_is_binary(self.context['blob_or_tree']):
            self.context.update({
                'is_markup': False,
                'is_binary': True,
                'is_image': False,
            })
            if guess_is_image(self.context['filename']):
                self.context.update({
                    'is_image': True,
                })
        else:
            render_markup = 'markup' not in request.args
            rendered_code = pygmentize(
                force_unicode(self.context['blob_or_tree'].data),
                self.context['filename'],
                render_markup
            )
            self.context.update({
                'too_large': sum(map(len, self.context['blob_or_tree'].chunked)) > 100*1024,
                'is_markup': markup.can_render(self.context['filename']),
                'render_markup': render_markup,
                'rendered_code': rendered_code,
                'is_binary': False,
            })
Esempio n. 3
0
def highlight_or_render(code, filename, render_markup=True, ctags=None, ctags_baseurl=None):
    """Render code using Pygments, markup (markdown, rst, ...) using the
    corresponding renderer, if available.

    :param code: the program code to highlight, str
    :param filename: name of the source file the code is taken from, str
    :param render_markup: whether to render markup if possible, bool
    :param ctags: tagsfile obj used for source code hyperlinks, ``ctags.CTags``
    :param ctags_baseurl: base url used for source code hyperlinks, str
    """
    if render_markup and markup.can_render(filename):
        return markup.render(filename, code)

    try:
        lexer = get_lexer_for_filename(filename, code)
    except ClassNotFound:
        try:
            lexer = guess_lexer(code)
        except ClassNotFound:
            lexer = TextLexer()

    formatter_cls = {
        'Python': KlausPythonFormatter,
    }.get(lexer.name, KlausDefaultFormatter)
    if ctags:
        ctags_urlscheme = ctags_baseurl + "%(path)s%(fname)s%(fext)s"
    else:
        ctags_urlscheme = None
    formatter = formatter_cls(
        language=PYGMENTS_CTAGS_LANGUAGE_MAP.get(lexer.name),
        ctags=ctags,
        tagurlformat=ctags_urlscheme,
    )

    return highlight(code, lexer, formatter)
Esempio n. 4
0
 def make_template_context(self, *args):
     super(FileView, self).make_template_context(*args)
     if self.context['can_render']:
         render_markup = 'markup' not in request.args
         self.context.update({
             'is_markup': markup.can_render(self.context['filename']),
             'render_markup': render_markup,
             'rendered_code': self.render_code(render_markup),
         })
Esempio n. 5
0
def pygmentize(code, filename=None, render_markup=True):
    """
    Renders code using Pygments, markup (markdown, rst, ...) using the
    corresponding renderer, if available.
    """
    if render_markup and markup.can_render(filename):
        return markup.render(filename, code)

    lexer = get_lexer_for_filename(filename, code)
    return highlight(code, lexer, KlausFormatter())
Esempio n. 6
0
def pygmentize(code, filename=None, render_markup=True):
    """
    Renders code using Pygments, markup (markdown, rst, ...) using the
    corresponding renderer, if available.
    """
    if render_markup and markup.can_render(filename):
        return markup.render(filename, code)

    lexer = get_lexer_for_filename(filename, code)
    return highlight(code, lexer, KlausFormatter())
Esempio n. 7
0
 def make_template_context(self, *args):
     super(FileView, self).make_template_context(*args)
     if self.context["can_render"]:
         render_markup = "markup" not in request.args
         self.context.update(
             {
                 "is_markup": markup.can_render(self.context["filename"]),
                 "render_markup": render_markup,
                 "rendered_code": self.render_code(render_markup),
             }
         )
Esempio n. 8
0
 def make_template_context(self, *args):
     super(FileView, self).make_template_context(*args)
     if self.context["can_render"]:
         render_markup = "markup" not in request.args
         self.context.update(
             {
                 "is_markup": markup.can_render(self.context["filename"]),
                 "render_markup": render_markup,
                 "rendered_code": self.render_code(render_markup),
             }
         )
Esempio n. 9
0
 def make_template_context(self, *args):
     super(FileView, self).make_template_context(*args)
     if self.context['can_render']:
         render_markup = 'markup' not in request.args
         self.context.update({
             'is_markup':
             markup.can_render(self.context['filename']),
             'render_markup':
             render_markup,
             'rendered_code':
             self.render_code(render_markup),
         })
Esempio n. 10
0
    def make_template_context(self, *args):
        super(IndexView, self).make_template_context(*args)

        self.context['base_href'] = url_for('blob',
                                            repo=self.context['repo'].name,
                                            rev=self.context['rev'],
                                            path='')

        self.context['page'] = 0
        history_length = 10
        history = self.context['repo'].history(
            self.context['commit'],
            self.context['path'],
            history_length + 1,
            skip=0,
        )
        if len(history) == history_length + 1:
            # At least one more commit for next page left
            more_commits = True
            # We don't want show the additional commit on this page
            history.pop()
        else:
            more_commits = False

        self.context.update({
            'history': history,
            'more_commits': more_commits,
        })
        try:
            (readme_filename, readme_data) = self._get_readme()
        except KeyError:
            self.context.update({
                'is_markup': None,
                'rendered_code': None,
            })
        else:
            readme_base_url = url_for(
                'raw',
                repo=self.context['repo'].name,
                rev=self.context['rev'],
                path=os.path.dirname(self.context['path']),
            )
            readme_filename = force_unicode(readme_filename)
            readme_data = force_unicode(readme_data)
            self.context.update({
                'is_markup':
                markup.can_render(readme_filename),
                'rendered_code':
                highlight_or_render(readme_data, readme_base_url,
                                    readme_filename)
            })
Esempio n. 11
0
    def make_template_context(self, *args):
        super(IndexView, self).make_template_context(*args)

        self.context["base_href"] = url_for(
            "blob",
            repo=self.context["repo"].namespaced_name,
            rev=self.context["rev"],
            path="",
        )

        self.context["page"] = 0
        history_length = 10
        history = self.context["repo"].history(
            self.context["commit"],
            self.context["path"],
            history_length + 1,
            skip=0,
        )
        if len(history) == history_length + 1:
            # At least one more commit for next page left
            more_commits = True
            # We don't want show the additional commit on this page
            history.pop()
        else:
            more_commits = False

        self.context.update(
            {
                "history": history,
                "more_commits": more_commits,
            }
        )
        try:
            (readme_filename, readme_data) = self._get_readme()
        except KeyError:
            self.context.update(
                {
                    "is_markup": None,
                    "rendered_code": None,
                }
            )
        else:
            readme_filename = force_unicode(readme_filename)
            readme_data = force_unicode(readme_data)
            self.context.update(
                {
                    "is_markup": markup.can_render(readme_filename),
                    "rendered_code": highlight_or_render(readme_data, readme_filename),
                }
            )
Esempio n. 12
0
    def get_readme(self):
        rev = self.get_default_branch()
        commit = self.get_commit(rev)
        tree = self.get_blob_or_tree(commit, "/")

        for item in tree.items():
            if item.path.startswith("README."):
                content = self[item.sha].data
                if can_render(item.path):
                    return {"rendered": True, "content": render(item.path, content)}
                else:
                    return {"rendered": False, "content": force_unicode(content)}

        return None
Esempio n. 13
0
 def make_template_context(self, *args):
     super(FileView, self).make_template_context(*args)
     if self.context['can_render']:
         render_markup = 'markup' not in request.args
         rendered_code = pygmentize(
             force_unicode(self.context['blob_or_tree'].data),
             self.context['filename'],
             render_markup
         )
         self.context.update({
             'is_markup': markup.can_render(self.context['filename']),
             'render_markup': render_markup,
             'rendered_code': rendered_code,
         })
Esempio n. 14
0
def pygmentize(code, filename=None, render_markup=True):
    """Render code using Pygments, markup (markdown, rst, ...) using the
    corresponding renderer, if available.
    """
    if render_markup and markup.can_render(filename):
        return markup.render(filename, code)

    try:
        lexer = get_lexer_for_filename(filename, code)
    except ClassNotFound:
        try:
            lexer = guess_lexer(code)
        except ClassNotFound:
            lexer = TextLexer()
    return highlight(code, lexer, KlausFormatter())
Esempio n. 15
0
def pygmentize(code, filename=None, render_markup=True):
    """Render code using Pygments, markup (markdown, rst, ...) using the
    corresponding renderer, if available.
    """
    if render_markup and markup.can_render(filename):
        return markup.render(filename, code)

    try:
        lexer = get_lexer_for_filename(filename, code)
    except ClassNotFound:
        try:
            lexer = guess_lexer(code)
        except ClassNotFound:
            lexer = TextLexer()
    return highlight(code, lexer, KlausFormatter())
Esempio n. 16
0
File: views.py Progetto: jahir/klaus
    def make_template_context(self, *args):
        super(IndexView, self).make_template_context(*args)

        self.context['base_href'] = url_for(
            'blob',
            repo=self.context['repo'].name,
            rev=self.context['rev'],
            path=''
        )

        self.context['page'] = 0
        history_length = 10
        history = self.context['repo'].history(
            self.context['commit'],
            self.context['path'],
            history_length + 1,
            skip=0,
        )
        if len(history) == history_length + 1:
            # At least one more commit for next page left
            more_commits = True
            # We don't want show the additional commit on this page
            history.pop()
        else:
            more_commits = False

        self.context.update({
            'history': history,
            'more_commits': more_commits,
        })
        try:
            (readme_filename, readme_data) = self._get_readme()
        except KeyError:
            self.context.update({
                'is_markup': None,
                'rendered_code': None,
            })
        else:
            self.context.update({
                'is_markup': markup.can_render(readme_filename),
                'rendered_code': highlight_or_render(
                    force_unicode(readme_data),
                    force_unicode(readme_filename),
                ),
            })
Esempio n. 17
0
 def make_context(self, *args):
     super(BlobView, self).make_context(*args)
     render_markup = 'markup' not in request.args
     rendered_code = pygmentize(force_unicode(self.context['blob'].data),
                                self.context['filename'], render_markup)
     self.context.update({
         'too_large':
         sum(map(len, self.context['blob'].chunked)) > 100 * 1024,
         'is_markup':
         markup.can_render(self.context['filename']),
         'render_markup':
         render_markup,
         'rendered_code':
         rendered_code,
         'is_binary':
         guess_is_binary(self.context['blob']),
         'is_image':
         guess_is_image(self.context['filename']),
     })
Esempio n. 18
0
    def make_template_context(self, *args):
        super(BlobView, self).make_template_context(*args)

        if not isinstance(self.context['blob_or_tree'], Blob):
            raise NotFound("Not a blob")

        binary = guess_is_binary(self.context['blob_or_tree'])
        too_large = sum(map(len,
                            self.context['blob_or_tree'].chunked)) > 100 * 1024

        if binary:
            self.context.update({
                'is_markup': False,
                'is_binary': True,
                'is_image': False,
            })
            if guess_is_image(self.context['filename']):
                self.context.update({
                    'is_image': True,
                })
        elif too_large:
            self.context.update({
                'too_large': True,
                'is_markup': False,
                'is_binary': False,
            })
        else:
            render_markup = 'markup' not in request.args
            rendered_code = pygmentize(
                force_unicode(self.context['blob_or_tree'].data),
                self.context['filename'], render_markup)
            self.context.update({
                'too_large':
                False,
                'is_markup':
                markup.can_render(self.context['filename']),
                'render_markup':
                render_markup,
                'rendered_code':
                rendered_code,
                'is_binary':
                False,
            })
Esempio n. 19
0
    def make_context(self, *args):
        super(BlobView, self).make_context(*args)

        if self.context['blob'] == None or not hasattr(self.context['blob'], 'chunked'):
            raise RedirectException(url_for('history', repo=self.context['repo'].name, commit_id='master', path=self.context['path']))

        render_markup = 'markup' not in request.args
        rendered_code = pygmentize(
            force_unicode(self.context['blob'].data),
            self.context['filename'],
            render_markup
        )
        self.context.update({
            'too_large': sum(map(len, self.context['blob'].chunked)) > 100*1024,
            'is_markup': markup.can_render(self.context['filename']),
            'render_markup': render_markup,
            'rendered_code': rendered_code,
            'is_binary': guess_is_binary(self.context['blob']),
            'is_image': guess_is_image(self.context['filename']),
        })
Esempio n. 20
0
    def get_context_data(self, **ctx):
        context = super(BlobView, self).get_context_data(**ctx)

        if not isinstance(context['blob_or_tree'], Blob):
            raise RepoException("Not a blob")

        binary = guess_is_binary(context['blob_or_tree'])
        too_large = sum(map(len, context['blob_or_tree'].chunked)) > 100 * 1024

        if binary:
            context.update({
                'is_markup': False,
                'is_binary': True,
                'is_image': False,
            })
            if guess_is_image(context['filename']):
                context.update({
                    'is_image': True,
                })
        elif too_large:
            context.update({
                'too_large': True,
                'is_markup': False,
                'is_binary': False,
            })
        else:
            render_markup = 'markup' not in self.request.GET
            rendered_code = pygmentize(
                force_unicode(context['blob_or_tree'].data),
                context['filename'],
                render_markup
            )
            context.update({
                'too_large': False,
                'is_markup': markup.can_render(context['filename']),
                'render_markup': render_markup,
                'rendered_code': rendered_code,
                'is_binary': False,
            })

        return context
Esempio n. 21
0
    def make_template_context(self, *args):
        super(BlobView, self).make_template_context(*args)

        if not isinstance(self.context['blob_or_tree'], Blob):
            raise NotFound("Not a blob")

        binary = guess_is_binary(self.context['blob_or_tree'])
        too_large = sum(map(len, self.context['blob_or_tree'].chunked)) > 100*1024

        if binary:
            self.context.update({
                'is_markup': False,
                'is_binary': True,
                'is_image': False,
            })
            if guess_is_image(self.context['filename']):
                self.context.update({
                    'is_image': True,
                })
        elif too_large:
            self.context.update({
                'too_large': True,
                'is_markup': False,
                'is_binary': False,
            })
        else:
            render_markup = 'markup' not in request.args
            rendered_code = pygmentize(
                force_unicode(self.context['blob_or_tree'].data),
                self.context['filename'],
                render_markup
            )
            self.context.update({
                'too_large': False,
                'is_markup': markup.can_render(self.context['filename']),
                'render_markup': render_markup,
                'rendered_code': rendered_code,
                'is_binary': False,
            })
Esempio n. 22
0
    def get_context_data(self, **ctx):
        context = super(BlobView, self).get_context_data(**ctx)

        if not isinstance(context['blob_or_tree'], Blob):
            raise RepoException("Not a blob")

        binary = guess_is_binary(context['blob_or_tree'])
        too_large = sum(map(len, context['blob_or_tree'].chunked)) > 100 * 1024

        if binary:
            context.update({
                'is_markup': False,
                'is_binary': True,
                'is_image': False,
            })
            if guess_is_image(context['filename']):
                context.update({
                    'is_image': True,
                })
        elif too_large:
            context.update({
                'too_large': True,
                'is_markup': False,
                'is_binary': False,
            })
        else:
            render_markup = 'markup' not in self.request.GET
            rendered_code = pygmentize(
                force_unicode(context['blob_or_tree'].data),
                context['filename'], render_markup)
            context.update({
                'too_large': False,
                'is_markup': markup.can_render(context['filename']),
                'render_markup': render_markup,
                'rendered_code': rendered_code,
                'is_binary': False,
            })

        return context
Esempio n. 23
0
    def make_template_context(self, *args):
        super(BlameView, self).make_template_context(*args)

        if not isinstance(self.context['blob_or_tree'], Blob):
            raise NotFound("Not a blob")

        binary = guess_is_binary(self.context['blob_or_tree'])
        too_large = sum(map(len, self.context['blob_or_tree'].chunked)) > 100*1024

        if binary:
            self.context.update({
                'is_markup': False,
                'is_binary': True,
                'is_image': False,
            })
            if guess_is_image(self.context['filename']):
                self.context.update({
                    'is_image': True,
                })
        elif too_large:
            self.context.update({
                'too_large': True,
                'is_markup': False,
                'is_binary': False,
            })
        else:
            self.context.update({
                'too_large': False,
                'is_markup': markup.can_render(self.context['filename']),
                'is_binary': False,
                'rendered_code': pygmentize(
                    force_unicode(self.context['blob_or_tree'].data),
                    self.context['filename'],
                    render_markup=False , linenos=False),
                'authors': list(self.context["repo"].blame(self.context["commit"], self.context["path"]))
            })