Class: Docwu::Folder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Folder

Returns a new instance of Folder.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
# File 'lib/docwu/folder.rb', line 9

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

  @parents = if @parent.nil?
               []
             else
               @parent.parents.dup << @parent
             end

  @src  = attrs[:src]
  @path = attrs[:path]
  @url  = "/#{@path}/index.html"

  @name = ::Docwu::Utils.filename(@src)

  @dest = "#{self.worker.tmp_deploy_path}/#{self.path}"
  @index_dest = "#{self.dest}/index.html"
  # -------------------------------------

  @posts   = []
  @folders = []

  ::Dir.glob("#{self.src}/*").each do |_src|
    _name = "#{_src.sub("#{self.src}/", '')}"

    if File.exists?(_src)
      if File.file?(_src) # 如果一个文件
        _post = ::Docwu::Post.new(:src => _src, :parent => self, :worker => self.worker, :path => "#{self.path}/#{_name}")

        if _post.index?
          @index_post = _post
        else
          @posts << _post
        end
      elsif File.directory?(_src) # 如果是一个文件夹
        @folders << self.class.new(:src => _src, :parent => self, :worker => self.worker, :path => "#{self.path}/#{_name}")
      end
    end
  end

  self.posts.sort! { |a, b| b.ranking <=> a.ranking }
  self.posts.sort! { |a, b| b.datetime.to_s <=> a.datetime.to_s }

  #  TODO: 是否从index文件中读取data呢?
end

Instance Attribute Details

#content_dataObject (readonly)

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



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

def content_data
  @content_data
end

#destObject (readonly)

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



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

def dest
  @dest
end

#foldersObject (readonly)

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



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

def folders
  @folders
end

#index_destObject (readonly)

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



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

def index_dest
  @index_dest
end

#index_postObject (readonly)

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



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

def index_post
  @index_post
end

#nameObject (readonly)

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



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

def name
  @name
end

#parentObject (readonly)

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



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

def parent
  @parent
end

#parentsObject (readonly)

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



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

def parents
  @parents
end

#pathObject (readonly)

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



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

def path
  @path
end

#postsObject (readonly)

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



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

def posts
  @posts
end

#srcObject (readonly)

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



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

def src
  @src
end

#urlObject (readonly)

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



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

def url
  @url
end

#workerObject (readonly)

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



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

def worker
  @worker
end

Instance Method Details

#folder?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/docwu/folder.rb', line 115

def folder?
  true
end

#generateObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/docwu/folder.rb', line 85

def generate
  _prepare_data

  # TODO: index : 需要生成首页!
  _template = if self.has_index?
                self.worker.layouts[self.index_post.layout]
              end

  _template ||= self.worker.layouts['folder']

  ::Docwu::Render.generate(
    :content_data => self.content_data,
    :dest         => self.index_dest,
    :template     => _template
  )

  self.folders.each do |folder|
    folder.generate
  end

  self.posts.each do |post|
    post.generate
  end
end

#has_index?Boolean

有首页文件了

Returns:

  • (Boolean)


111
112
113
# File 'lib/docwu/folder.rb', line 111

def has_index?
  !!(self.index_post)
end

#index_page_dataObject



77
78
79
80
81
82
83
# File 'lib/docwu/folder.rb', line 77

def index_page_data
  if self.has_index?
    self.index_post.page_data
  else
    {}
  end
end

#parent_datasObject



56
57
58
# File 'lib/docwu/folder.rb', line 56

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

#post?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/docwu/folder.rb', line 119

def post?
  false
end

#to_dataObject

转为数据



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/docwu/folder.rb', line 61

def to_data
  posts_count = self.posts.size
  posts_limit = 10
  limit_posts_data = self.posts[0..posts_limit].map(&:to_data)

  {
    'name'              => self.name,
    'url'               => self.url,
    'title'             => self.name,
    'folders'           => self.folders.map(&:to_data),
    'posts'             => self.posts.map(&:to_data),
    'limit_posts'       => limit_posts_data,
    'limit_posts_more?' => (posts_count > posts_limit)
  }
end