Sha256: 89024f2016aeca833580973779ec87baea8d4b58d0d467cb65554570ac1b9536

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

# coding: utf-8
require 'erb'
require 'pathname'

# 布局, 是最大的 wrapper
# home_page 什么的都是它的内嵌
module WriteDown
  module Model
    class Layout

      # @param page 被塞入的页面
      # @param target_file 被写入的目标文件
      def initialize(page, target_file, option = {})
        @page        = page
        @target_file = target_file
        @option = option
      end

      # 实际写入硬盘的是这个地方
      def render
        output = Pathname.new(@target_file)
        output.dirname.mkpath
        final_content = render_all_content(@page)
        output.write(final_content)
      end

      # page 相当于一个接口, 需要实现 render 方法
      # @see http://stackoverflow.com/questions/2460839/yield-in-erb-without-rails
      def render_all_content(page)
        page_html = page.render # 先渲染 page, 这里使用了代理模式把职责分配给当前的 page
        render_layout { page_html } # 再渲染 layout
      end

      # 可以调用到 erb 中的 yield
      def render_layout
        layout_path = @option['layout'] || File.expand_path('../../erb/layout.erb', __FILE__)
        ERB.new(File.read(layout_path)).result(binding)
      end

      # css 的版本号
      def assets_version
        "" #"?v=1"
      end

      # ===================== 一些 config 的 meta data ==============================

      # 网站 title
      def title
        '编程青年'
      end

      # 副标题
      def tagline
        '没有银弹'
      end

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

      # 友情链接集合
      def friend_links
        { "苏州八零年代": 'http://niandai80.com/' }
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
write_down-0.2.2 lib/write_down/model/layout.rb
write_down-0.2.1 lib/write_down/model/layout.rb
write_down-0.2.0 lib/write_down/model/layout.rb