Class: Lifer::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/lifer/entry.rb

Constant Summary collapse

FILENAME_DATE_FORMAT =
/^(\d{4}-\d{1,2}-\d{1,2})-/
FRONTMATTER_REGEX =
/^---\n(.*)---\n/m

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file:) ⇒ Entry

Returns a new instance of Entry.



13
14
15
# File 'lib/lifer/entry.rb', line 13

def initialize(file:)
  @file = File.exist?(file) ? Pathname(file) : nil
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



11
12
13
# File 'lib/lifer/entry.rb', line 11

def file
  @file
end

Instance Method Details

#bodyObject



17
18
19
20
21
# File 'lib/lifer/entry.rb', line 17

def body
  return full_text.strip unless frontmatter?

  full_text.gsub(FRONTMATTER_REGEX, "").strip
end

#dateObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/lifer/entry.rb', line 23

def date
  date_data = frontmatter[:date] || filename_date

  case date_data
  when Time then date_data
  when String then DateTime.parse(date_data).to_time
  else
    puts "[%s]: no date metadata" % [file]
    nil
  end
rescue ArgumentError => error
  puts "[%s]: %s" % [file, error]
  nil
end

#frontmatterObject



38
39
40
41
42
43
44
# File 'lib/lifer/entry.rb', line 38

def frontmatter
  return {} unless frontmatter?

  Lifer::Utilities.symbolize_keys(
    YAML.load(full_text[FRONTMATTER_REGEX, 1], permitted_classes: [Time])
  )
end

#full_textObject



46
47
48
# File 'lib/lifer/entry.rb', line 46

def full_text
  File.readlines(file).join if file
end

#to_htmlObject



50
51
52
# File 'lib/lifer/entry.rb', line 50

def to_html
  Kramdown::Document.new(body).to_html
end