Module: Linguist::BlobHelper
- Included in:
- FileBlob
- Defined in:
- lib/linguist/blob_helper.rb
Overview
DEPRECATED Avoid mixing into Blob classes. Prefer functional interfaces like ‘Language.detect` over `Blob#language`. Functions are much easier to cache and compose.
Avoid adding additional bloat to this module.
BlobHelper is a mixin for Blobish classes that respond to “name”, “data” and “size” such as Grit::Blob.
Instance Method Summary collapse
-
#_mime_type ⇒ Object
Internal: Lookup mime type for extension.
-
#binary? ⇒ Boolean
Public: Is the blob binary?.
-
#binary_mime_type? ⇒ Boolean
Internal: Is the blob binary according to its mime type.
-
#colorize(options = {}) ⇒ Object
Public: Highlight syntax of blob.
-
#content_type ⇒ Object
Public: Get the Content-Type header value.
-
#csv? ⇒ Boolean
Public: Is this blob a CSV file?.
-
#detect_encoding ⇒ Object
Try to guess the encoding.
-
#disposition ⇒ Object
Public: Get the Content-Disposition header value.
- #encoding ⇒ Object
-
#extname ⇒ Object
Public: Get the extname of the path.
-
#generated? ⇒ Boolean
Public: Is the blob a generated file?.
-
#high_ratio_of_long_lines? ⇒ Boolean
Internal: Does the blob have a ratio of long lines?.
-
#image? ⇒ Boolean
Public: Is the blob a supported image format?.
-
#language ⇒ Object
Public: Detects the Language of the blob.
-
#large? ⇒ Boolean
Public: Is the blob too big to load?.
-
#lexer ⇒ Object
Internal: Get the lexer of the blob.
-
#likely_binary? ⇒ Boolean
Internal: Is the blob binary according to its mime type, overriding it if we have better data from the languages.yml database.
-
#lines ⇒ Object
Public: Get each line of data.
-
#loc ⇒ Object
Public: Get number of lines of code.
-
#mime_type ⇒ Object
Public: Get the actual blob mime type.
-
#pdf? ⇒ Boolean
Public: Is the blob a PDF?.
-
#safe_to_colorize? ⇒ Boolean
Public: Is the blob safe to colorize?.
-
#sloc ⇒ Object
Public: Get number of source lines of code.
-
#solid? ⇒ Boolean
Public: Is the blob a supported 3D model format?.
-
#text? ⇒ Boolean
Public: Is the blob text?.
-
#vendored? ⇒ Boolean
Public: Is the blob in a vendored directory?.
-
#viewable? ⇒ Boolean
Public: Is the blob viewable?.
Instance Method Details
#_mime_type ⇒ Object
Internal: Lookup mime type for extension.
Returns a MIME::Type
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/linguist/blob_helper.rb', line 35 def _mime_type 'text/plain' # if defined? @_mime_type # @_mime_type # else # guesses = ::MIME::Types.type_for(extname.to_s) # # # Prefer text mime types over binary # @_mime_type = guesses.detect { |type| type.ascii? } || # # Otherwise use the first guess # guesses.first # end end |
#binary? ⇒ Boolean
Public: Is the blob binary?
Return true or false
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/linguist/blob_helper.rb', line 132 def binary? # Large blobs aren't even loaded into memory if data.nil? true else false # end # Treat blank files as text # elsif data == "" # false # Charlock doesn't know what to think # elsif encoding.nil? # true # If Charlock says its binary # else # detect_encoding[:type] == :binary end end |
#binary_mime_type? ⇒ Boolean
Internal: Is the blob binary according to its mime type
Return true or false
64 65 66 67 |
# File 'lib/linguist/blob_helper.rb', line 64 def binary_mime_type? false # _mime_type ? _mime_type.binary? : false end |
#colorize(options = {}) ⇒ Object
Public: Highlight syntax of blob
options - A Hash of options (defaults to {})
Returns html String
326 327 328 329 330 331 |
# File 'lib/linguist/blob_helper.rb', line 326 def colorize( = {}) return unless safe_to_colorize? [:options] ||= {} [:options][:encoding] ||= encoding lexer.highlight(data, ) end |
#content_type ⇒ Object
Public: Get the Content-Type header value
This value is used when serving raw blobs.
Examples
# => 'text/plain; charset=utf-8'
# => 'application/octet-stream'
Returns a content type String.
89 90 91 92 93 |
# File 'lib/linguist/blob_helper.rb', line 89 def content_type "text/plain" # @content_type ||= (binary_mime_type? || binary?) ? mime_type : # (encoding ? "text/plain; charset=#{encoding.downcase}" : "text/plain") end |
#csv? ⇒ Boolean
Public: Is this blob a CSV file?
Return true or false
179 180 181 182 |
# File 'lib/linguist/blob_helper.rb', line 179 def csv? false # text? && extname.downcase == '.csv' end |
#detect_encoding ⇒ Object
Try to guess the encoding
Returns: a Hash, with :encoding, :confidence, :type
this will return nil if an error occurred during detection or
no valid encoding could be found
125 126 127 |
# File 'lib/linguist/blob_helper.rb', line 125 def detect_encoding {:encoding => 'UTF-8', :confidence => 100, :type => :text} end |
#disposition ⇒ Object
Public: Get the Content-Disposition header value
This value is used when serving raw blobs.
# => "attachment; filename=file.tar"
# => "inline"
Returns a content disposition String.
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/linguist/blob_helper.rb', line 103 def disposition if text? || image? 'inline' elsif name.nil? "attachment" else 'attachment' # "attachment; filename=#{EscapeUtils.escape_url(File.basename(name))}" end end |
#encoding ⇒ Object
114 115 116 117 118 |
# File 'lib/linguist/blob_helper.rb', line 114 def encoding # if hash = detect_encoding 'UTF-8' # end end |
#extname ⇒ Object
Public: Get the extname of the path
Examples
blob(name='foo.rb').extname
# => '.rb'
Returns a String
28 29 30 |
# File 'lib/linguist/blob_helper.rb', line 28 def extname File.extname(name.to_s) end |
#generated? ⇒ Boolean
Public: Is the blob a generated file?
Generated source code is suppressed in diffs and is ignored by language statistics.
May load Blob#data
Return true or false
292 293 294 295 |
# File 'lib/linguist/blob_helper.rb', line 292 def generated? false # @_generated ||= Generated.generated?(name, lambda { data }) end |
#high_ratio_of_long_lines? ⇒ Boolean
Internal: Does the blob have a ratio of long lines?
These types of files are usually going to make Pygments.rb angry if we try to colorize them.
Return true or false
220 221 222 223 224 |
# File 'lib/linguist/blob_helper.rb', line 220 def high_ratio_of_long_lines? false # return false if loc == 0 # size / loc > 5000 end |
#image? ⇒ Boolean
Public: Is the blob a supported image format?
Return true or false
163 164 165 166 |
# File 'lib/linguist/blob_helper.rb', line 163 def image? false # ['.png', '.jpg', '.jpeg', '.gif'].include?(extname.downcase) end |
#language ⇒ Object
Public: Detects the Language of the blob.
May load Blob#data
Returns a Language or nil if none is detected
302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/linguist/blob_helper.rb', line 302 def language return @language if defined? @language if defined?(@data) && @data.is_a?(String) && !data == '' && !data.nil? data = @data else data = lambda { self.data } end @language = Language.detect(name.to_s, data, mode) end |
#large? ⇒ Boolean
Public: Is the blob too big to load?
Return true or false
197 198 199 200 |
# File 'lib/linguist/blob_helper.rb', line 197 def large? false # size.to_i > MEGABYTE end |
#lexer ⇒ Object
Internal: Get the lexer of the blob.
Returns a Lexer.
317 318 319 |
# File 'lib/linguist/blob_helper.rb', line 317 def lexer language ? language.lexer : Pygments::Lexer.find_by_name('Text only') end |
#likely_binary? ⇒ Boolean
Internal: Is the blob binary according to its mime type, overriding it if we have better data from the languages.yml database.
Return true or false
74 75 76 77 |
# File 'lib/linguist/blob_helper.rb', line 74 def likely_binary? false # binary_mime_type? && !Language.find_by_filename(name) end |
#lines ⇒ Object
Public: Get each line of data
Requires Blob#data
Returns an Array of lines
257 258 259 260 261 262 263 264 |
# File 'lib/linguist/blob_helper.rb', line 257 def lines @lines ||= if viewable? && data && !data.nil? && !data == '' data.split(/\r\n|\r|\n/, -1) else [] end end |
#loc ⇒ Object
Public: Get number of lines of code
Requires Blob#data
Returns Integer
271 272 273 |
# File 'lib/linguist/blob_helper.rb', line 271 def loc lines.size end |
#mime_type ⇒ Object
Public: Get the actual blob mime type
Examples
# => 'text/plain'
# => 'text/html'
Returns a mime type String.
57 58 59 |
# File 'lib/linguist/blob_helper.rb', line 57 def mime_type 'text/plain' end |
#pdf? ⇒ Boolean
Public: Is the blob a PDF?
Return true or false
187 188 189 190 |
# File 'lib/linguist/blob_helper.rb', line 187 def pdf? false # extname.downcase == '.pdf' end |
#safe_to_colorize? ⇒ Boolean
Public: Is the blob safe to colorize?
We use Pygments for syntax highlighting blobs. Pygments can be too slow for very large blobs or for certain corner-case blobs.
Return true or false
209 210 211 212 |
# File 'lib/linguist/blob_helper.rb', line 209 def safe_to_colorize? true # !large? && text? && !high_ratio_of_long_lines? end |
#sloc ⇒ Object
Public: Get number of source lines of code
Requires Blob#data
Returns Integer
280 281 282 |
# File 'lib/linguist/blob_helper.rb', line 280 def sloc lines.grep(/\S/).size end |
#solid? ⇒ Boolean
Public: Is the blob a supported 3D model format?
Return true or false
171 172 173 174 |
# File 'lib/linguist/blob_helper.rb', line 171 def solid? false # extname.downcase == '.stl' end |
#text? ⇒ Boolean
Public: Is the blob text?
Return true or false
156 157 158 |
# File 'lib/linguist/blob_helper.rb', line 156 def text? true end |
#vendored? ⇒ Boolean
Public: Is the blob in a vendored directory?
Vendored files are ignored by language statistics.
See “vendor.yml” for a list of vendored conventions that match this pattern.
Return true or false
247 248 249 250 |
# File 'lib/linguist/blob_helper.rb', line 247 def vendored? false # name =~ VendoredRegexp ? true : false end |
#viewable? ⇒ Boolean
Public: Is the blob viewable?
Non-viewable blobs will just show a “View Raw” link
Return true or false
231 232 233 234 |
# File 'lib/linguist/blob_helper.rb', line 231 def viewable? true # !large? && text? end |