Class: LinkTag::Linker

Inherits:
Liquid::Tag
  • Object
show all
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

Constructor Details

#initialize(tag_name, text, tokens) ⇒ void

Constructor.

Parameters:

  • tag_name (String)

    is the name of the tag, which we already know. Contains the name of the file, relative to the website top level directory

  • text (Hash, String, Liquid::Tag::Parser)

    the arguments from the web page.

  • tokens (Liquid::ParseContext)

    tokenized command line



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.

Returns:

  • (String)


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