Class: Tufte::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/tufte/post.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date:, title:, slug:, raw_body:) ⇒ Post



7
8
9
10
11
12
# File 'lib/tufte/post.rb', line 7

def initialize(date:, title:, slug:, raw_body:)
  @date = date
  @slug = slug
  @title = title
  @raw_body = raw_body
end

Instance Attribute Details

#slugObject (readonly)

Returns the value of attribute slug.



5
6
7
# File 'lib/tufte/post.rb', line 5

def slug
  @slug
end

#titleObject (readonly)

Returns the value of attribute title.



5
6
7
# File 'lib/tufte/post.rb', line 5

def title
  @title
end

Class Method Details

.load(filename) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tufte/post.rb', line 30

def self.load(filename)
  file_contents = File.read(filename)
   = YAML.load(file_contents)

  new(
    date: .fetch("date"),
    title: .fetch("title"),
    slug: File.basename(filename, ".yml"),
    raw_body: file_contents.split("---\n", 3).last,
  )
end

Instance Method Details

#bodyObject



14
15
16
# File 'lib/tufte/post.rb', line 14

def body
  Markdown.new(@raw_body).to_html
end

#dateObject



18
19
20
# File 'lib/tufte/post.rb', line 18

def date
  @date.strftime("%Y %b %-d")
end

#output_directoryObject



22
23
24
# File 'lib/tufte/post.rb', line 22

def output_directory
  File.join(@date.year.to_s, @date.month.to_s, @date.day.to_s, slug)
end

#output_filenameObject



26
27
28
# File 'lib/tufte/post.rb', line 26

def output_filename
  File.join(output_directory, "index.html")
end