Class: Ottogen::Permalink

Inherits:
Object
  • Object
show all
Defined in:
lib/ottogen/permalink.rb

Constant Summary collapse

TOKEN_PATTERN =
/:(year|month|day|slug|title)/
TOKEN_RESOLVERS =
{
  ':year' => ->(doc) { doc.respond_to?(:date) && doc.date ? doc.date.strftime('%Y') : '' },
  ':month' => ->(doc) { doc.respond_to?(:date) && doc.date ? doc.date.strftime('%m') : '' },
  ':day' => ->(doc) { doc.respond_to?(:date) && doc.date ? doc.date.strftime('%d') : '' },
  ':slug' => ->(doc) { doc.respond_to?(:slug) ? doc.slug.to_s : '' },
  ':title' => ->(doc) { doc.respond_to?(:title) ? slugify(doc.title) : '' }
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Permalink

Returns a new instance of Permalink.



27
28
29
# File 'lib/ottogen/permalink.rb', line 27

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly) Also known as: url

Returns the value of attribute path.



24
25
26
# File 'lib/ottogen/permalink.rb', line 24

def path
  @path
end

Class Method Details

.expand(template, doc) ⇒ Object



15
16
17
18
# File 'lib/ottogen/permalink.rb', line 15

def self.expand(template, doc)
  expanded = template.gsub(TOKEN_PATTERN) { |token| TOKEN_RESOLVERS.fetch(token).call(doc) }
  new(expanded)
end

.slugify(str) ⇒ Object



20
21
22
# File 'lib/ottogen/permalink.rb', line 20

def self.slugify(str)
  str.to_s.downcase.gsub(/[^a-z0-9]+/, '-').sub(/\A-/, '').sub(/-\z/, '')
end

Instance Method Details

#output_path(build_dir) ⇒ Object



31
32
33
# File 'lib/ottogen/permalink.rb', line 31

def output_path(build_dir)
  File.join(build_dir, @path.end_with?('/') ? "#{@path}index.html" : @path)
end