Class: Jekyll::Itafroma::ArchiveSubstitution

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/itafroma/archive_substitution.rb

Instance Method Summary collapse

Constructor Details

#initialize(post) ⇒ ArchiveSubstitution

Initialize a new ArchiveSubstitution.

post - The post to generate a URL for.

Returns nothing.



22
23
24
# File 'lib/jekyll/itafroma/archive_substitution.rb', line 22

def initialize(post)
  @post = post
end

Instance Method Details

#placeholdersObject

Returns a hash of placeholder names (as symbols) mapping to the desired placeholder replacements.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/jekyll/itafroma/archive_substitution.rb', line 48

def placeholders
  {
    :year        => @post.date.strftime('%Y'),
    :short_year  => @post.date.strftime('%y'),
    :month       => @post.date.strftime('%m'),
    :i_month     => @post.date.strftime('%-m'),
    :short_month => @post.date.strftime('%b'),
    :long_month  => @post.date.strftime('%B'),
    :day         => @post.date.strftime('%d'),
    :i_day       => @post.date.strftime('%-d'),
    :y_day       => @post.date.strftime('%j')
  }
end

#translate(template) ⇒ Object

Substitute placeholders within a string with data from the post.

Adapted from Jekyll::URL::generate_url.

Returns a string containing the substituted post data.



31
32
33
34
35
36
# File 'lib/jekyll/itafroma/archive_substitution.rb', line 31

def translate(template)
  placeholders.inject(template) do |result, token|
    break result if result.index(':').nil?
    result.gsub(/:#{token.first}/, token.last)
  end
end

#url(template) ⇒ Object

Returns a Jekyll URL object for the given template.



39
40
41
42
43
44
# File 'lib/jekyll/itafroma/archive_substitution.rb', line 39

def url(template)
  URL.new({
    :template => template,
    :placeholders => placeholders
  })
end