Class: WriteDown::Model::Base

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

Overview

基类, 定义了一些接口

Direct Known Subclasses

Essay, Page, Post, Slide

Instance Attribute Summary collapse

Instance Method Summary collapse

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
  meta_file      = File.join("source", "meta", "#{@base_name}.json")
  @meta          = File.exist?(meta_file) ? JSON.parse(File.open(meta_file).read) : read_from_head
  @draft         = @meta['draft'].to_s == "true" ? true : false
  @created_at    = @meta['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

#contentObject

Returns the value of attribute content.



11
12
13
# File 'lib/write_down/model/base.rb', line 11

def content
  @content
end

#created_atObject

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

#draftObject

Returns the value of attribute draft.



8
9
10
# File 'lib/write_down/model/base.rb', line 8

def draft
  @draft
end

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

#bodyObject



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

def body
  # binding.pry
  converter.new(self).render
end

#buildObject

接口 build 用于把自己序列化到磁盘上



54
55
56
# File 'lib/write_down/model/base.rb', line 54

def build
  raise "Not impl..."
end

#converterObject



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_headObject



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

#renderObject

接口 render 用于找到自己的 erb 然后生成 html 字符串



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

def render
  raise "Not impl..."
end