class Silly::PageModel

Constants

DateMatcher
Matcher

Attributes

after_content[RW]
after_data[RW]
before_content[RW]
before_data[RW]

Public Instance Methods

content() click to toggle source
# File lib/silly/page_model.rb, line 30
def content
  result = Silly::Parse.page_file(realpath)["content"]

  if self.class.before_content.respond_to?(:call)
    result = self.class.before_content.call(result)
  end

  self.class.before_content.respond_to?(:call) ?
    self.class.before_content.call(result) :
    result
end
data() click to toggle source
# File lib/silly/page_model.rb, line 10
def data
  return {} unless file?

  data = Silly::Parse.page_file(realpath)["data"]
  data['id'] = id

  if self.class.before_data.respond_to?(:call)
    data = self.class.before_data.call(data)
  end

  filename_data = parse_page_filename(id)
  data['title'] ||= filename_data['title']
  data['date'] = parse_date(data['date'] || filename_data['date'])
  data['_url'] = make_url(data)

  self.class.after_data.respond_to?(:call) ?
    self.class.after_data.call(data) :
    data
end

Private Instance Methods

file?() click to toggle source

Is the item backed by a physical file in the filesystem? @return

# File lib/silly/page_model.rb, line 64
def file?
  !!realpath
end
make_url(data) click to toggle source
# File lib/silly/page_model.rb, line 44
def make_url(data)
  page_data = data.dup
  format = page_data['permalink'] || "/:path/:filename"
  slug = Silly::UrlSlug.new(item: self, data: page_data, format: format)
  slug.generate
end
parse_date(date) click to toggle source

Parse and store date as an object

# File lib/silly/page_model.rb, line 52
def parse_date(date)
  return date if (date.nil? || date.is_a?(Time))

  Time.parse(date)
rescue
  raise(
    "ArgumentError: The date '#{ date }' specified in '#{ id }' is unparsable."
  )
end
parse_page_filename(filename) click to toggle source
# File lib/silly/page_model.rb, line 68
def parse_page_filename(filename)
  data = *filename.match(DateMatcher)
  data = *filename.match(Matcher) if data.empty?
  return {} if data.empty?

  if filename =~ DateMatcher
    {
      "path" => data[1],
      "date" => data[2],
      "slug" => data[3],
      "title" => to_title(data[3]),
      "extension" => data[4]
    }
  else
    {
      "path" => data[1],
      "slug" => data[2],
      "title" => to_title(data[2]),
      "extension" => data[3]
    }
  end
end
to_title(file_slug) click to toggle source

my-post-title ===> My Post Title

# File lib/silly/page_model.rb, line 92
def to_title(file_slug)
  if file_slug == 'index' && !id.index('/').nil?
    file_slug = id.split('/')[-2]
  end

  file_slug
end