Class: WriteDown::Model::Base
- Inherits:
-
Object
- Object
- WriteDown::Model::Base
- Defined in:
- lib/write_down/model/base.rb
Overview
基类, 定义了一些接口
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#draft ⇒ Object
Returns the value of attribute draft.
-
#link_to ⇒ Object
Returns the value of attribute link_to.
Instance Method Summary collapse
- #body ⇒ Object
-
#build ⇒ Object
接口 build 用于把自己序列化到磁盘上.
- #converter ⇒ Object
-
#initialize(source_file, target_file = nil) ⇒ Base
constructor
A new instance of Base.
- #read_from_head ⇒ Object
-
#render ⇒ Object
接口 render 用于找到自己的 erb 然后生成 html 字符串.
Constructor Details
#initialize(source_file, target_file = nil) ⇒ Base
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/write_down/model/base.rb', line 13 def initialize(source_file, target_file = nil) @source_file = source_file @base_name = source_file.split("/").last.split(".").first @ext_name = source_file.split("/").last.split(".").last = File.join("source", "meta", "#{@base_name}.json") = File.exist?() ? JSON.parse(File.open().read) : read_from_head @draft = ['draft'].to_s == "true" ? true : false @created_at = ['date'] || File.open(@source_file).ctime #.iso8610 @content = File.open(@source_file).read if target_file @target_file = target_file elsif @base_name.end_with?"index" @target_file = "dist/#{@base_name}.html" else @target_file = "dist/#{@base_name}/index.html" end @link_to = @target_file.gsub("index.html", "") end |
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
11 12 13 |
# File 'lib/write_down/model/base.rb', line 11 def content @content end |
#created_at ⇒ Object
Returns the value of attribute created_at.
9 10 11 |
# File 'lib/write_down/model/base.rb', line 9 def created_at @created_at end |
#draft ⇒ Object
Returns the value of attribute draft.
8 9 10 |
# File 'lib/write_down/model/base.rb', line 8 def draft @draft end |
#link_to ⇒ Object
Returns the value of attribute link_to.
10 11 12 |
# File 'lib/write_down/model/base.rb', line 10 def link_to @link_to end |
Instance Method Details
#body ⇒ Object
41 42 43 44 |
# File 'lib/write_down/model/base.rb', line 41 def body # binding.pry converter.new(self).render end |
#build ⇒ Object
接口 build 用于把自己序列化到磁盘上
54 55 56 |
# File 'lib/write_down/model/base.rb', line 54 def build raise "Not impl..." end |
#converter ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/write_down/model/base.rb', line 32 def converter if @ext_name == "md" || @ext_name == "markdown" return WriteDown::Converter::Markdown end if @ext_name == "org" return WriteDown::Converter::Org end end |
#read_from_head ⇒ Object
58 59 60 61 62 |
# File 'lib/write_down/model/base.rb', line 58 def read_from_head FrontMatterParser.parse_file(@source_file).front_matter rescue {} end |
#render ⇒ Object
接口 render 用于找到自己的 erb 然后生成 html 字符串
48 49 50 |
# File 'lib/write_down/model/base.rb', line 48 def render raise "Not impl..." end |