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.



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

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.



9
10
11
# File 'lib/eksa/markdown_post.rb', line 9

def content
  @content
end

#filenameObject (readonly)

Returns the value of attribute filename.



9
10
11
# File 'lib/eksa/markdown_post.rb', line 9

def filename
  @filename
end

#metadataObject (readonly)

Returns the value of attribute metadata.



9
10
11
# File 'lib/eksa/markdown_post.rb', line 9

def 
  @metadata
end

#slugObject (readonly)

Returns the value of attribute slug.



9
10
11
# File 'lib/eksa/markdown_post.rb', line 9

def slug
  @slug
end

Class Method Details

.all(include_unpublished: false) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/eksa/markdown_post.rb', line 13

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

.find(slug) ⇒ Object



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

def self.find(slug)
  all(include_unpublished: true).find { |p| p.slug == slug && p.published? }
end

Instance Method Details

#authorObject



42
43
44
# File 'lib/eksa/markdown_post.rb', line 42

def author
  @metadata['author'] || ""
end

#body_htmlObject



58
59
60
# File 'lib/eksa/markdown_post.rb', line 58

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

#categoryObject



46
47
48
# File 'lib/eksa/markdown_post.rb', line 46

def category
  @metadata['category'] || "Uncategorized"
end

#dateObject



38
39
40
# File 'lib/eksa/markdown_post.rb', line 38

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

#imageObject



50
51
52
# File 'lib/eksa/markdown_post.rb', line 50

def image
  @metadata['image'] || ""
end

#published?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/eksa/markdown_post.rb', line 54

def published?
  @metadata.key?('published') ? @metadata['published'] : true
end

#titleObject



34
35
36
# File 'lib/eksa/markdown_post.rb', line 34

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