Module: Dolt::View::Blob
- Defined in:
- lib/libdolt/view/blob.rb
Instance Method Summary collapse
- #binary?(content) ⇒ Boolean
- #entityfy(content) ⇒ Object
- #format_binary_blob(path, content, repository = nil, ref = nil) ⇒ Object
- #format_blob(path, content, repo = nil, ref = nil) ⇒ Object
- #format_text_blob(path, content, repository = nil, ref = nil) ⇒ Object
- #format_whitespace(text) ⇒ Object
- #link_binary_blob(path, content, repository = nil, ref = nil) ⇒ Object
- #multiline(blob, options = {}) ⇒ Object
- #safe_blob_text(blob) ⇒ Object
Instance Method Details
#binary?(content) ⇒ Boolean
23 24 25 |
# File 'lib/libdolt/view/blob.rb', line 23 def binary?(content) !content[0...(content.length-1)].index("\000").nil? end |
#entityfy(content) ⇒ Object
27 28 29 30 |
# File 'lib/libdolt/view/blob.rb', line 27 def entityfy(content) @coder ||= HTMLEntities.new @coder.encode(content) end |
#format_binary_blob(path, content, repository = nil, ref = nil) ⇒ Object
37 38 39 |
# File 'lib/libdolt/view/blob.rb', line 37 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
32 33 34 35 |
# File 'lib/libdolt/view/blob.rb', line 32 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
50 51 52 |
# File 'lib/libdolt/view/blob.rb', line 50 def format_text_blob(path, content, repository = nil, ref = nil) multiline(entityfy(content)) end |
#format_whitespace(text) ⇒ Object
54 55 56 |
# File 'lib/libdolt/view/blob.rb', line 54 def format_whitespace(text) text end |
#link_binary_blob(path, content, repository = nil, ref = nil) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/libdolt/view/blob.rb', line 41 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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/libdolt/view/blob.rb', line 58 def multiline(blob, = {}) class_names = [: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
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/libdolt/view/blob.rb', line 76 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 |