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
- #body_html ⇒ Object
- #date ⇒ Object
-
#initialize(file_path) ⇒ MarkdownPost
constructor
A new instance of MarkdownPost.
- #title ⇒ Object
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
#content ⇒ Object (readonly)
Returns the value of attribute content.
6 7 8 |
# File 'lib/eksa/markdown_post.rb', line 6 def content @content end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
6 7 8 |
# File 'lib/eksa/markdown_post.rb', line 6 def filename @filename end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
6 7 8 |
# File 'lib/eksa/markdown_post.rb', line 6 def @metadata end |
#slug ⇒ Object (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
.all ⇒ Object
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_html ⇒ Object
36 37 38 |
# File 'lib/eksa/markdown_post.rb', line 36 def body_html Kramdown::Document.new(@content, input: 'GFM').to_html end |
#date ⇒ Object
32 33 34 |
# File 'lib/eksa/markdown_post.rb', line 32 def date @metadata['date'] || File.mtime(File.join(POSTS_DIR, @filename)) end |
#title ⇒ Object
28 29 30 |
# File 'lib/eksa/markdown_post.rb', line 28 def title @metadata['title'] || "Untitled Post" end |