# Path: brigitte/repositories/decorators.py
# def repository_view(can_admin=False):
#     def inner_repository_view(f):
#         def wrapped(request, user, slug, *args, **kwargs):
#             qs = Repository.objects.none()
# 
#             if request.user.is_authenticated():
#                 if can_admin:
#                     qs = Repository.objects.manageable_repositories(request.user)
#                 else:
#                     qs = Repository.objects.available_repositories(request.user)
#             else:
#                 if not can_admin:
#                     qs = Repository.objects.public_repositories()
# 
#             repository = get_object_or_404(qs, user__username=user, slug=slug)
# 
#             return f(request, repository, *args, **kwargs)
#         return wraps(f)(wrapped)
#     return inner_repository_view
# 
# Path: brigitte/trees/utils.py
# def build_path_breadcrumb(path):
#     if not path:
#         return []
# 
#     links = []
#     cur_path = None
# 
#     for part in path.split('/'):
#         if cur_path:
#             cur_path += '/' + part
#         else:
#             cur_path = part
# 
#         links.append({
#             'path': cur_path,
#             'name': part,
#         })
# 
#     return links
# 
# Path: brigitte/utils/highlight.py
# 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)


from django.core.serializers.json import DjangoJSONEncoder
from django.http import Http404, HttpResponse
from django.shortcuts import render
from django.template.defaultfilters import timesince
from django.utils import simplejson
from brigitte.repositories.decorators import repository_view
from brigitte.trees.utils import build_path_breadcrumb
from brigitte.utils.highlight import pygmentize
                cls=DjangoJSONEncoder), mimetype='application/json')

        try:
            tree = commit.get_tree(path)
        except KeyError:
            raise Http404

        return render(request, 'trees/tree.html', {
            'repository': repo,
            'commit': commit,
            'tree': tree,
            'breadcrumb': build_path_breadcrumb(path)
        })

    else:
        try:
            file_obj = commit.get_file(path)
        except KeyError:
            raise Http404

        file_blob_pygmentized = pygmentize(
            path.rsplit('.', 1)[-1], file_obj.content)

        return render(request, 'trees/file.html', {
            'repository': repo,
            'commit': commit,
            'file_path': path,
            'file_obj': file_obj,
            'file_lines': range(1, file_obj.content.count('\n') + 1),
            'file_blob_pygmentized': file_blob_pygmentized,

# Path: brigitte/repositories/decorators.py
# def repository_view(can_admin=False):
#     def inner_repository_view(f):
#         def wrapped(request, user, slug, *args, **kwargs):
#             qs = Repository.objects.none()
# 
#             if request.user.is_authenticated():
#                 if can_admin:
#                     qs = Repository.objects.manageable_repositories(request.user)
#                 else:
#                     qs = Repository.objects.available_repositories(request.user)
#             else:
#                 if not can_admin:
#                     qs = Repository.objects.public_repositories()
# 
#             repository = get_object_or_404(qs, user__username=user, slug=slug)
# 
#             return f(request, repository, *args, **kwargs)
#         return wraps(f)(wrapped)
#     return inner_repository_view
# 
# Path: brigitte/trees/utils.py
# def build_path_breadcrumb(path):
#     if not path:
#         return []
# 
#     links = []
#     cur_path = None
# 
#     for part in path.split('/'):
#         if cur_path:
#             cur_path += '/' + part
#         else:
#             cur_path = part
# 
#         links.append({
#             'path': cur_path,
#             'name': part,
#         })
# 
#     return links
# 
# Path: brigitte/utils/highlight.py
# 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)


# Path: brigitte/trees/views.py
from django.core.serializers.json import DjangoJSONEncoder
from django.http import Http404, HttpResponse
from django.shortcuts import render
from django.template.defaultfilters import timesince
from django.utils import simplejson
from brigitte.repositories.decorators import repository_view
from brigitte.trees.utils import build_path_breadcrumb
from brigitte.utils.highlight import pygmentize

                cls=DjangoJSONEncoder), mimetype='application/json')

        try:
            tree = commit.get_tree(path)
        except KeyError:
            raise Http404

        return render(request, 'trees/tree.html', {
            'repository': repo,
            'commit': commit,
            'tree': tree,
            'breadcrumb': build_path_breadcrumb(path)
        })

    else:
        try:
            file_obj = commit.get_file(path)
        except KeyError:
            raise Http404

        file_blob_pygmentized = pygmentize(
            path.rsplit('.', 1)[-1], file_obj.content)

        return render(request, 'trees/file.html', {
            'repository': repo,
            'commit': commit,
            'file_path': path,
            'file_obj': file_obj,
            'file_lines': range(1, file_obj.content.count('\n') + 1),
            'file_blob_pygmentized': file_blob_pygmentized,

            'breadcrumb': build_path_breadcrumb(path)