Class: Lifer::Entry
- Inherits:
-
Object
- Object
- Lifer::Entry
- 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
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Instance Method Summary collapse
- #body ⇒ Object
- #date ⇒ Object
- #frontmatter ⇒ Object
- #full_text ⇒ Object
-
#initialize(file:) ⇒ Entry
constructor
A new instance of Entry.
- #to_html ⇒ Object
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
#file ⇒ Object (readonly)
Returns the value of attribute file.
11 12 13 |
# File 'lib/lifer/entry.rb', line 11 def file @file end |
Instance Method Details
#body ⇒ Object
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 |
#date ⇒ Object
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 |
#frontmatter ⇒ Object
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_text ⇒ Object
46 47 48 |
# File 'lib/lifer/entry.rb', line 46 def full_text File.readlines(file).join if file end |
#to_html ⇒ Object
50 51 52 |
# File 'lib/lifer/entry.rb', line 50 def to_html Kramdown::Document.new(body).to_html end |