Class: Eksa::MarkdownPost
- Inherits:
-
Object
- Object
- Eksa::MarkdownPost
- Defined in:
- lib/eksa/markdown_post.rb
Constant Summary collapse
- POSTS_DIR =
"_posts"
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#slug ⇒ Object
readonly
Returns the value of attribute slug.
Class Method Summary collapse
Instance Method Summary collapse
- #author ⇒ Object
- #body_html ⇒ Object
- #category ⇒ Object
- #date ⇒ Object
- #image ⇒ Object
-
#initialize(file_path) ⇒ MarkdownPost
constructor
A new instance of MarkdownPost.
- #published? ⇒ Boolean
- #title ⇒ Object
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
#content ⇒ Object (readonly)
Returns the value of attribute content.
9 10 11 |
# File 'lib/eksa/markdown_post.rb', line 9 def content @content end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
9 10 11 |
# File 'lib/eksa/markdown_post.rb', line 9 def filename @filename end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
9 10 11 |
# File 'lib/eksa/markdown_post.rb', line 9 def @metadata end |
#slug ⇒ Object (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
#author ⇒ Object
42 43 44 |
# File 'lib/eksa/markdown_post.rb', line 42 def @metadata['author'] || "" end |
#body_html ⇒ Object
58 59 60 |
# File 'lib/eksa/markdown_post.rb', line 58 def body_html Kramdown::Document.new(@content, input: 'GFM').to_html end |
#category ⇒ Object
46 47 48 |
# File 'lib/eksa/markdown_post.rb', line 46 def category @metadata['category'] || "Uncategorized" end |
#date ⇒ Object
38 39 40 |
# File 'lib/eksa/markdown_post.rb', line 38 def date @metadata['date'] || File.mtime(File.join(POSTS_DIR, @filename)) end |
#image ⇒ Object
50 51 52 |
# File 'lib/eksa/markdown_post.rb', line 50 def image @metadata['image'] || "" end |
#published? ⇒ Boolean
54 55 56 |
# File 'lib/eksa/markdown_post.rb', line 54 def published? @metadata.key?('published') ? @metadata['published'] : true end |
#title ⇒ Object
34 35 36 |
# File 'lib/eksa/markdown_post.rb', line 34 def title @metadata['title'] || "Untitled Post" end |