Class: DownloadLink

Inherits:
JekyllSupport::JekyllTagNoArgParsing
  • Object
show all
Includes:
JekyllDownloadLinkVersion
Defined in:
lib/jekyll_download_link.rb

Overview

Generates an href to a file for the user to download from the site. Also shows the file size in a human-readable format.

Constant Summary

Constants included from JekyllDownloadLinkVersion

JekyllDownloadLinkVersion::VERSION

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.as_size(string) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/jekyll_download_link.rb', line 13

def self.as_size(string)
  units = %w[B KB MB GB TB]

  size, unit = units.reduce(string.to_f) do |(fsize, _), utype|
    fsize > 512 ? [fsize / 1024, utype] : (break [fsize, utype])
  end

  fmt_str = "#{size > 9 || size.modulo(1) < 0.1 ? '%d' : '%.1f'} %s"
  format(fmt_str, size, unit)
end

Instance Method Details

#render_implObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/jekyll_download_link.rb', line 24

def render_impl
  @filename = @argument_string.delete('"').delete("'").strip

  source = @config['source']
  file_fq = File.join(source, @filename)
  abort "Error: '#{file_fq}' not found. See the link tag in" unless File.exist?(file_fq)

  size_str = self.class.as_size(File.size(file_fq))
  "<a href='/#{@filename}'><code>#{@filename}</code></a> (#{size_str})"
end