Class: WriteDown::Model::Layout

Inherits:
Object
  • Object
show all
Defined in:
lib/write_down/model/layout.rb

Instance Method Summary collapse

Constructor Details

#initialize(page, target_file, option = {}) ⇒ Layout

Returns a new instance of Layout.

Parameters:

  • page

    被塞入的页面

  • target_file

    被写入的目标文件



13
14
15
16
17
# File 'lib/write_down/model/layout.rb', line 13

def initialize(page, target_file, option = {})
  @page        = page
  @target_file = target_file
  @option = option
end

Instance Method Details

#assets_versionObject

css 的版本号



41
42
43
# File 'lib/write_down/model/layout.rb', line 41

def assets_version
  "" #"?v=1"
end

友情链接集合



69
70
71
# File 'lib/write_down/model/layout.rb', line 69

def friend_links
  { "苏州八零年代": 'http://niandai80.com/' }
end

菜单



58
59
60
61
62
63
64
65
66
# File 'lib/write_down/model/layout.rb', line 58

def menus
  {
    "home": '/',
    "about": '/about/',
    "essays": '/essays/',
    "reading": '/reading/',
    "github": 'https://github.com/teddy-ma/'
  }
end

#renderObject

实际写入硬盘的是这个地方



20
21
22
23
24
25
# File 'lib/write_down/model/layout.rb', line 20

def render
  output = Pathname.new(@target_file)
  output.dirname.mkpath
  final_content = render_all_content(@page)
  output.write(final_content)
end

#render_all_content(page) ⇒ Object

page 相当于一个接口, 需要实现 render 方法



29
30
31
32
# File 'lib/write_down/model/layout.rb', line 29

def render_all_content(page)
  page_html = page.render # 先渲染 page, 这里使用了代理模式把职责分配给当前的 page
  render_layout { page_html } # 再渲染 layout
end

#render_layoutObject

可以调用到 erb 中的 yield



35
36
37
38
# File 'lib/write_down/model/layout.rb', line 35

def render_layout
  layout_path = @option['layout'] || File.expand_path('../../erb/layout.erb', __FILE__)
  ERB.new(File.read(layout_path)).result(binding)
end

#taglineObject

副标题



53
54
55
# File 'lib/write_down/model/layout.rb', line 53

def tagline
  '没有银弹'
end

#titleObject

网站 title



48
49
50
# File 'lib/write_down/model/layout.rb', line 48

def title
  '编程青年'
end