Class: Musako::Renderers::Post

Inherits:
Musako::Renderer show all
Defined in:
lib/musako/renderers/post.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Musako::Renderer

#file_extname, #file_name, #file_path, #file_updated_at, #original_file_source

Constructor Details

#initialize(file) ⇒ Post

Returns a new instance of Post.



10
11
12
13
# File 'lib/musako/renderers/post.rb', line 10

def initialize(file)
  super file
  @file_extname = ".html"
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



8
9
10
# File 'lib/musako/renderers/post.rb', line 8

def date
  @date
end

#htmlObject (readonly)

Returns the value of attribute html.



8
9
10
# File 'lib/musako/renderers/post.rb', line 8

def html
  @html
end

#iso_dateObject (readonly)

Returns the value of attribute iso_date.



8
9
10
# File 'lib/musako/renderers/post.rb', line 8

def iso_date
  @iso_date
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/musako/renderers/post.rb', line 8

def path
  @path
end

#titleObject (readonly)

Returns the value of attribute title.



8
9
10
# File 'lib/musako/renderers/post.rb', line 8

def title
  @title
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/musako/renderers/post.rb', line 8

def url
  @url
end

Instance Method Details

#output_pathObject

create directory from file’s updated_at



53
54
55
# File 'lib/musako/renderers/post.rb', line 53

def output_path
  File.join(Musako.destination_path, @url)
end

#renderObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/musako/renderers/post.rb', line 15

def render
  markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML,
    :autolink => true,
    :space_after_headers => true,
    :fenced_code_blocks => true
  )
  @html = markdown.render(self.original_file_source)

  # use first header tag as title
  title = if @html =~ /<h1>(.*?)<\/h1>/
    $1
  else
    Musako.configuration[:title]
  end
  self.(title)

  post = Slim::Template.new(
    File.join(Musako.views_path, "post.slim")
  ).render(self, {post: self, config: Musako.configuration})

  layout = Slim::Template.new(
    File.join(Musako.views_path, "layouts", "application.slim")
  ).render(self, {
    title: Musako.configuration[:title],
    page_title: "#{title} - #{Musako.configuration[:title]}"
  }) { post }

  dir = File.dirname(self.output_path)
  unless File.directory? dir
    FileUtils.mkdir_p dir
  end

  File.open(self.output_path, "w") do |file|
    file.write layout
  end
end

#set_meta_data(title) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/musako/renderers/post.rb', line 57

def (title)
  buf  = self.file_name.split("-")

  name = (buf.size > 1) ? buf[1] : buf[0]
  date = DateTime.parse(buf[0]) rescue self.file_updated_at

  @title = title
  @date = date
  @iso_date = date.strftime("%FT%T%z")
  @url = File.join("#{date.year}/#{date.month}/#{date.day}", name)
  @path = self.file_path
end