Class: Eksa::MarkdownPost

Inherits:
Object
  • Object
show all
Defined in:
lib/eksa/markdown_post.rb

Constant Summary collapse

POSTS_DIR =
"_posts"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ MarkdownPost

Returns a new instance of MarkdownPost.



22
23
24
25
26
# File 'lib/eksa/markdown_post.rb', line 22

def initialize(file_path)
  @filename = File.basename(file_path)
  @slug = @filename.sub(/\.md$/, '')
  load_file(file_path)
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



6
7
8
# File 'lib/eksa/markdown_post.rb', line 6

def content
  @content
end

#filenameObject (readonly)

Returns the value of attribute filename.



6
7
8
# File 'lib/eksa/markdown_post.rb', line 6

def filename
  @filename
end

#metadataObject (readonly)

Returns the value of attribute metadata.



6
7
8
# File 'lib/eksa/markdown_post.rb', line 6

def 
  @metadata
end

#slugObject (readonly)

Returns the value of attribute slug.



6
7
8
# File 'lib/eksa/markdown_post.rb', line 6

def slug
  @slug
end

Class Method Details

.allObject



10
11
12
13
14
15
16
# File 'lib/eksa/markdown_post.rb', line 10

def self.all
  return [] unless Dir.exist?(POSTS_DIR)
  
  Dir.glob(File.join(POSTS_DIR, "*.md")).map do |file|
    new(file)
  end.sort_by { |p| p.date }.reverse
end

.find(slug) ⇒ Object



18
19
20
# File 'lib/eksa/markdown_post.rb', line 18

def self.find(slug)
  all.find { |p| p.slug == slug }
end

Instance Method Details

#body_htmlObject



36
37
38
# File 'lib/eksa/markdown_post.rb', line 36

def body_html
  Kramdown::Document.new(@content, input: 'GFM').to_html
end

#dateObject



32
33
34
# File 'lib/eksa/markdown_post.rb', line 32

def date
  @metadata['date'] || File.mtime(File.join(POSTS_DIR, @filename))
end

#titleObject



28
29
30
# File 'lib/eksa/markdown_post.rb', line 28

def title
  @metadata['title'] || "Untitled Post"
end