Module: Dolt::View::Blob

Defined in:
lib/libdolt/view/blob.rb

Instance Method Summary collapse

Instance Method Details

#binary?(content) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/libdolt/view/blob.rb', line 23

def binary?(content)
  !content[0...(content.length-1)].index("\000").nil?
end

#format_binary_blob(path, content, repository = nil, ref = nil) ⇒ Object



32
33
34
# File 'lib/libdolt/view/blob.rb', line 32

def format_binary_blob(path, content, repository = nil, ref = nil)
  link_binary_blob(path, content, repository, ref)
end

#format_blob(path, content, repo = nil, ref = nil) ⇒ Object



27
28
29
30
# File 'lib/libdolt/view/blob.rb', line 27

def format_blob(path, content, repo = nil, ref = nil)
  return format_binary_blob(path, content, repo, ref) if binary?(content)
  format_text_blob(path, content, repo, ref)
end

#format_text_blob(path, content, repository = nil, ref = nil) ⇒ Object



45
46
47
# File 'lib/libdolt/view/blob.rb', line 45

def format_text_blob(path, content, repository = nil, ref = nil)
  multiline(HTMLEscape.entityfy(content))
end

#format_whitespace(text) ⇒ Object



49
50
51
# File 'lib/libdolt/view/blob.rb', line 49

def format_whitespace(text)
  text
end


36
37
38
39
40
41
42
43
# File 'lib/libdolt/view/blob.rb', line 36

def link_binary_blob(path, content, repository = nil, ref = nil)
  <<-HTML
<p class="prettyprint">
The content you're attempting to browse appears to be binary.
<a href="#{raw_url(repository, ref, path)}">Download #{File.basename(path)}</a>.
</p>
  HTML
end

#multiline(blob, options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/libdolt/view/blob.rb', line 53

def multiline(blob, options = {})
  class_names = options[:class_names] || []
  class_names << "prettyprint" << "linenums"

  num = 0
  lines = blob.split("\n").inject("") do |html, line|
    num += 1
    # Empty elements causes annoying rendering artefacts
    # Forcing one space on each line affects copy-paste negatively
    # TODO: Don't force one space, find CSS fix
    line = format_whitespace(line).sub(/^$/, " ")
    "#{html}<li class=\"L#{num}\"><span class=\"line\">#{line}</span></li>"
  end

  "<pre class=\"#{class_names.join(' ')}\">" +
    "<ol class=\"linenums gts-lines\">#{lines}</ol></pre>"
end

#safe_blob_text(blob) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/libdolt/view/blob.rb', line 71

def safe_blob_text(blob)
  text = blob.text(nil, defined?(Encoding) ? Encoding.default_external : nil)

  if text.respond_to?(:encode)
    text = text.encode('UTF-16', :invalid => :replace, :undef => :replace,
                                 :replace => "").encode('UTF-8')
  end

  text
end