debian-mirror-gitlab/app/presenters/blob_presenter.rb

41 lines
990 B
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2019-09-04 21:01:54 +05:30
class BlobPresenter < Gitlab::View::Presenter::Delegated
2018-12-13 13:39:08 +05:30
presents :blob
2019-12-04 20:38:33 +05:30
def highlight(to: nil, plain: nil)
2019-10-12 21:52:04 +05:30
load_all_blob_data
2018-12-13 13:39:08 +05:30
Gitlab::Highlight.highlight(
blob.path,
2019-12-04 20:38:33 +05:30
limited_blob_data(to: to),
2018-12-13 13:39:08 +05:30
language: blob.language_from_gitattributes,
plain: plain
)
end
2019-09-04 21:01:54 +05:30
def web_url
Gitlab::Routing.url_helpers.project_blob_url(blob.repository.project, File.join(blob.commit_id, blob.path))
end
2019-10-12 21:52:04 +05:30
private
def load_all_blob_data
blob.load_all_data! if blob.respond_to?(:load_all_data!)
end
2019-12-04 20:38:33 +05:30
def limited_blob_data(to: nil)
return blob.data if to.blank?
# Even though we don't need all the lines at the start of the file (e.g
# viewing the middle part of a file), they still need to be highlighted
# to ensure that the succeeding lines can be formatted correctly (e.g.
# multi-line comments).
all_lines[0..to - 1].join
end
def all_lines
@all_lines ||= blob.data.lines
end
2018-12-13 13:39:08 +05:30
end