Class: LinkTag::Linker
- Inherits:
-
Liquid::Tag
- Object
- Liquid::Tag
- LinkTag::Linker
- Defined in:
- 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.
Instance Method Summary collapse
-
#as_size(s) ⇒ Object
-
#initialize(tag_name, text, tokens) ⇒ void
constructor
Constructor.
-
#render(context) ⇒ String
Method prescribed by the Jekyll plugin lifecycle.
Constructor Details
#initialize(tag_name, text, tokens) ⇒ void
Constructor.
14 15 16 17 |
# File 'link.rb', line 14 def initialize(tag_name, text, tokens) super(tag_name, text, tokens) @filename = text.delete('"').delete("'").strip end |
Instance Method Details
#as_size(s) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'link.rb', line 29 def as_size(s) units = %w[B KB MB GB TB] size, unit = units.reduce(s.to_f) do |(fsize, _), utype| fsize > 512 ? [fsize / 1024, utype] : (break [fsize, utype]) end "#{size > 9 || size.modulo(1) < 0.1 ? '%d' : '%.1f'} %s" % [size, unit] end |
#render(context) ⇒ String
Method prescribed by the Jekyll plugin lifecycle.
21 22 23 24 25 26 27 |
# File 'link.rb', line 21 def render(context) source = context.registers[:site].config['source'] file_fq = File.join(source, @filename) abort("Error: '#{file_fq}' not found. See the link tag in") unless File.exist?(file_fq) "<a href='/#{@filename}'><code>#{@filename}</code></a> (#{as_size(File.size(file_fq))})" end |