2018-09-06 05:48:57 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-06-10 04:05:16 -04:00
|
|
|
class BlobPresenter < Gitlab::View::Presenter::Delegated
|
2018-09-06 05:48:57 -04:00
|
|
|
presents :blob
|
|
|
|
|
2019-09-01 23:19:32 -04:00
|
|
|
def highlight(to: nil, plain: nil)
|
2019-07-22 01:25:24 -04:00
|
|
|
load_all_blob_data
|
2018-10-04 00:04:13 -04:00
|
|
|
|
2018-09-06 05:48:57 -04:00
|
|
|
Gitlab::Highlight.highlight(
|
|
|
|
blob.path,
|
2019-09-01 23:19:32 -04:00
|
|
|
limited_blob_data(to: to),
|
2020-01-22 07:08:40 -05:00
|
|
|
language: language,
|
2018-09-11 00:37:44 -04:00
|
|
|
plain: plain
|
2018-09-06 05:48:57 -04:00
|
|
|
)
|
|
|
|
end
|
2019-06-04 09:38:18 -04:00
|
|
|
|
|
|
|
def web_url
|
|
|
|
Gitlab::Routing.url_helpers.project_blob_url(blob.repository.project, File.join(blob.commit_id, blob.path))
|
|
|
|
end
|
2019-07-22 01:25:24 -04:00
|
|
|
|
2020-07-23 11:09:28 -04:00
|
|
|
def web_path
|
|
|
|
Gitlab::Routing.url_helpers.project_blob_path(blob.repository.project, File.join(blob.commit_id, blob.path))
|
|
|
|
end
|
|
|
|
|
2019-07-22 01:25:24 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def load_all_blob_data
|
|
|
|
blob.load_all_data! if blob.respond_to?(:load_all_data!)
|
|
|
|
end
|
2019-09-01 23:19:32 -04:00
|
|
|
|
|
|
|
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
|
2020-01-22 07:08:40 -05:00
|
|
|
|
|
|
|
def language
|
|
|
|
blob.language_from_gitattributes
|
|
|
|
end
|
2018-09-06 05:48:57 -04:00
|
|
|
end
|