2014-06-07 08:46:58 -04:00
|
|
|
module BlobHelper
|
2015-05-23 23:34:03 -04:00
|
|
|
def highlight(blob_name, blob_content, nowrap: false, continue: false)
|
2015-06-28 14:45:40 -04:00
|
|
|
@formatter ||= Rouge::Formatters::HTMLGitlab.new(
|
2014-12-03 09:27:31 -05:00
|
|
|
nowrap: nowrap,
|
|
|
|
cssclass: 'code highlight',
|
|
|
|
lineanchors: true,
|
|
|
|
lineanchorsid: 'LC'
|
|
|
|
)
|
|
|
|
|
|
|
|
begin
|
2015-06-28 14:42:41 -04:00
|
|
|
@lexer ||= Rouge::Lexer.guess(filename: blob_name, source: blob_content).new
|
2015-05-23 23:34:03 -04:00
|
|
|
result = @formatter.format(@lexer.lex(blob_content, continue: continue)).html_safe
|
2015-05-20 09:46:40 -04:00
|
|
|
rescue
|
2015-06-28 14:45:40 -04:00
|
|
|
@lexer = Rouge::Lexers::PlainText
|
|
|
|
result = @formatter.format(@lexer.lex(blob_content)).html_safe
|
2014-06-07 08:46:58 -04:00
|
|
|
end
|
2014-12-03 09:27:31 -05:00
|
|
|
|
2015-05-20 09:46:40 -04:00
|
|
|
result
|
2014-06-07 08:46:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def no_highlight_files
|
2014-12-03 06:50:00 -05:00
|
|
|
%w(credits changelog news copying copyright license authors)
|
2014-06-07 08:46:58 -04:00
|
|
|
end
|
2015-01-26 18:03:14 -05:00
|
|
|
|
|
|
|
def edit_blob_link(project, ref, path, options = {})
|
|
|
|
blob =
|
|
|
|
begin
|
|
|
|
project.repository.blob_at(ref, path)
|
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if blob && blob.text?
|
|
|
|
text = 'Edit'
|
|
|
|
after = options[:after] || ''
|
|
|
|
from_mr = options[:from_merge_request_id]
|
|
|
|
link_opts = {}
|
|
|
|
link_opts[:from_merge_request_id] = from_mr if from_mr
|
|
|
|
cls = 'btn btn-small'
|
|
|
|
if allowed_tree_edit?(project, ref)
|
2015-01-24 13:02:58 -05:00
|
|
|
link_to(text,
|
|
|
|
namespace_project_edit_blob_path(project.namespace, project,
|
|
|
|
tree_join(ref, path),
|
|
|
|
link_opts),
|
|
|
|
class: cls
|
|
|
|
)
|
2015-01-26 18:03:14 -05:00
|
|
|
else
|
|
|
|
content_tag :span, text, class: cls + ' disabled'
|
|
|
|
end + after.html_safe
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def leave_edit_message
|
|
|
|
"Leave edit mode?\nAll unsaved changes will be lost."
|
|
|
|
end
|
|
|
|
|
|
|
|
def editing_preview_title(filename)
|
2015-05-12 19:40:11 -04:00
|
|
|
if Gitlab::MarkupHelper.previewable?(filename)
|
2015-01-26 18:03:14 -05:00
|
|
|
'Preview'
|
|
|
|
else
|
2015-12-02 08:53:11 -05:00
|
|
|
'Preview Changes'
|
2015-01-26 18:03:14 -05:00
|
|
|
end
|
|
|
|
end
|
2014-10-04 06:29:18 -04:00
|
|
|
|
|
|
|
# Return an image icon depending on the file mode and extension
|
|
|
|
#
|
|
|
|
# mode - File unix mode
|
|
|
|
# mode - File name
|
|
|
|
def blob_icon(mode, name)
|
|
|
|
icon("#{file_type_icon_class('file', mode, name)} fw")
|
|
|
|
end
|
2014-06-07 08:46:58 -04:00
|
|
|
end
|