Class: Docwu::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/docwu/post.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Post

Returns a new instance of Post.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/docwu/post.rb', line 30

def initialize attrs={}
  @parent = attrs[:parent]

  @worker = attrs[:worker]

  @src = attrs[:src]

  _parse_content = self.parse_content

  # 将合并来自worker的数据
  @content_data = ::Docwu::Utils.hash_deep_merge(self.worker.data, 'page' => _parse_content[:data])  # 来自页面的数据

  @content_type = @content_data['content_type'] || 'html'

  _extend_name = case self.content_type
                 when 'html'
                   'html'
                 else
                   'html'
                 end

  # URL ---------------------------------
  _filename_extless = ::Docwu::Utils.filename_extless(attrs[:path])

  @path = "#{_filename_extless}.#{_extend_name}"
  @url  = "/#{@path}"
  @dest = "#{self.worker.tmp_deploy_path}/#{self.path}"

  @file_name = ::Docwu::Utils.filename(_filename_extless)

  @name = self.file_name
  @datetime = self.page_data['datetime']  # 创建时间
  @ranking  = self.page_data['ranking'].to_i  # 创建时间

  # puts "post to: -----------------> desc: #{self.dest}"
  # puts "                            src:  #{self.src}"
  # puts "                            path: #{self.path}"
  # puts "                            url:  #{self.url}"
  # -------------------------------------
end

Instance Attribute Details

#content_dataObject (readonly)

一个文件夹下面可能会有很多文件或文件夹的



5
6
7
# File 'lib/docwu/post.rb', line 5

def content_data
  @content_data
end

#content_typeObject (readonly)

一个文件夹下面可能会有很多文件或文件夹的



5
6
7
# File 'lib/docwu/post.rb', line 5

def content_type
  @content_type
end

#datetimeObject (readonly)

一个文件夹下面可能会有很多文件或文件夹的



5
6
7
# File 'lib/docwu/post.rb', line 5

def datetime
  @datetime
end

#destObject (readonly)

一个文件夹下面可能会有很多文件或文件夹的



5
6
7
# File 'lib/docwu/post.rb', line 5

def dest
  @dest
end

#file_nameObject (readonly)

一个文件夹下面可能会有很多文件或文件夹的



5
6
7
# File 'lib/docwu/post.rb', line 5

def file_name
  @file_name
end

#nameObject (readonly)

一个文件夹下面可能会有很多文件或文件夹的



5
6
7
# File 'lib/docwu/post.rb', line 5

def name
  @name
end

#parentObject (readonly)

一个文件夹下面可能会有很多文件或文件夹的



5
6
7
# File 'lib/docwu/post.rb', line 5

def parent
  @parent
end

#pathObject (readonly)

一个文件夹下面可能会有很多文件或文件夹的



5
6
7
# File 'lib/docwu/post.rb', line 5

def path
  @path
end

#rankingObject (readonly)

一个文件夹下面可能会有很多文件或文件夹的



5
6
7
# File 'lib/docwu/post.rb', line 5

def ranking
  @ranking
end

#srcObject (readonly)

一个文件夹下面可能会有很多文件或文件夹的



5
6
7
# File 'lib/docwu/post.rb', line 5

def src
  @src
end

#urlObject (readonly)

一个文件夹下面可能会有很多文件或文件夹的



5
6
7
# File 'lib/docwu/post.rb', line 5

def url
  @url
end

#workerObject (readonly)

一个文件夹下面可能会有很多文件或文件夹的



5
6
7
# File 'lib/docwu/post.rb', line 5

def worker
  @worker
end

Instance Method Details

#folder?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/docwu/post.rb', line 130

def folder?
  false
end

#generateObject

渲染



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/docwu/post.rb', line 112

def generate
  _prepare_data

  _parse_content = self.parse_content

  _content_text = _parse_content[:text]

  _path = self.path
  _dest = self.dest

  ::Docwu::Render.generate(
    :content_text => _content_text,
    :content_data => self.content_data,
    :dest         => _dest,
    :template     => self.template
  )
end

#has_folder?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/docwu/post.rb', line 145

def has_folder?
  self.parent.is_a?(::Docwu::Folder)
end

#index?Boolean

是否是首页?

Returns:

  • (Boolean)


76
77
78
# File 'lib/docwu/post.rb', line 76

def index?
  self.file_name == 'index'
end

#layoutObject



80
81
82
# File 'lib/docwu/post.rb', line 80

def layout
  self.page_data['layout']
end

#outlineObject



84
85
86
# File 'lib/docwu/post.rb', line 84

def outline
  @outline ||= self.page_data['outline'].to_s
end

#page_dataObject

页面数据



103
104
105
# File 'lib/docwu/post.rb', line 103

def page_data
  self.content_data['page'] ||= {}
end

#parent_datasObject



26
27
28
# File 'lib/docwu/post.rb', line 26

def parent_datas
  self.parents.map(&:to_data)
end

#parentsObject



18
19
20
21
22
23
24
# File 'lib/docwu/post.rb', line 18

def parents
  if self.parent.nil?
    []
  else
    self.parent.parents.dup << self.parent
  end
end

#parse_contentObject

解析正文



139
140
141
142
143
# File 'lib/docwu/post.rb', line 139

def parse_content
  _content = ::File.read(self.src)

  ::Docwu::Utils.parse_marked_content(_content)
end

#post?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/docwu/post.rb', line 134

def post?
  true
end

#templateObject



71
72
73
# File 'lib/docwu/post.rb', line 71

def template
  self.worker.layouts[self.layout] || self.worker.layouts['post'] || self.worker.layouts['application']
end

#titleObject



107
108
109
# File 'lib/docwu/post.rb', line 107

def title
  self.page_data['title'] || self.name
end

#to_dataObject



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/docwu/post.rb', line 88

def to_data
  introduction_size = 32

  introduction = "#{self.outline[0, introduction_size]}#{' ...' if self.outline.size > introduction_size}"

  {
    'name'  => self.name,
    'url'   => self.url,
    'title' => self.title,
    'outline' => self.outline,
    'introduction' => introduction
  }
end