blob_helper.rb 1.94 KB
Newer Older
1
module BlobHelper
2
  def highlight(blob_name, blob_content, nowrap: false, continue: false)
3
    @formatter ||= Rouge::Formatters::HTMLGitlab.new(
4 5 6 7 8 9 10
      nowrap: nowrap,
      cssclass: 'code highlight',
      lineanchors: true,
      lineanchorsid: 'LC'
    )

    begin
11
      @lexer ||= Rouge::Lexer.guess(filename: blob_name, source: blob_content).new
12
      result = @formatter.format(@lexer.lex(blob_content, continue: continue)).html_safe
13
    rescue
14 15
      @lexer = Rouge::Lexers::PlainText
      result = @formatter.format(@lexer.lex(blob_content)).html_safe
16
    end
17

18
    result
19 20 21
  end

  def no_highlight_files
22
    %w(credits changelog news copying copyright license authors)
23
  end
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

  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)
Vinnie Okada's avatar
Vinnie Okada committed
41 42 43 44 45 46
        link_to(text,
                namespace_project_edit_blob_path(project.namespace, project,
                                                 tree_join(ref, path),
                                                 link_opts),
                class: cls
               )
47 48 49 50 51 52 53 54 55 56 57 58 59
      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)
60
    if Gitlab::MarkupHelper.previewable?(filename)
61 62
      'Preview'
    else
Douwe Maan's avatar
Douwe Maan committed
63
      'Preview Changes'
64 65
    end
  end
66 67 68 69 70 71 72 73

  # 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
74
end