Class: Docwu::Folder
- Inherits:
-
Object
- Object
- Docwu::Folder
- Defined in:
- lib/docwu/folder.rb
Instance Attribute Summary collapse
-
#content_data ⇒ Object
readonly
一个文件夹下面可能会有很多文件或文件夹的.
-
#dest ⇒ Object
readonly
一个文件夹下面可能会有很多文件或文件夹的.
-
#folders ⇒ Object
readonly
一个文件夹下面可能会有很多文件或文件夹的.
-
#index_dest ⇒ Object
readonly
一个文件夹下面可能会有很多文件或文件夹的.
-
#index_post ⇒ Object
readonly
一个文件夹下面可能会有很多文件或文件夹的.
-
#name ⇒ Object
readonly
一个文件夹下面可能会有很多文件或文件夹的.
-
#parent ⇒ Object
readonly
一个文件夹下面可能会有很多文件或文件夹的.
-
#parents ⇒ Object
readonly
一个文件夹下面可能会有很多文件或文件夹的.
-
#path ⇒ Object
readonly
一个文件夹下面可能会有很多文件或文件夹的.
-
#posts ⇒ Object
readonly
一个文件夹下面可能会有很多文件或文件夹的.
-
#src ⇒ Object
readonly
一个文件夹下面可能会有很多文件或文件夹的.
-
#url ⇒ Object
readonly
一个文件夹下面可能会有很多文件或文件夹的.
-
#worker ⇒ Object
readonly
一个文件夹下面可能会有很多文件或文件夹的.
Instance Method Summary collapse
- #folder? ⇒ Boolean
- #generate ⇒ Object
-
#has_index? ⇒ Boolean
有首页文件了.
- #index_page_data ⇒ Object
-
#initialize(attrs = {}) ⇒ Folder
constructor
A new instance of Folder.
- #parent_datas ⇒ Object
- #post? ⇒ Boolean
-
#to_data ⇒ Object
转为数据.
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_data ⇒ Object (readonly)
一个文件夹下面可能会有很多文件或文件夹的
5 6 7 |
# File 'lib/docwu/folder.rb', line 5 def content_data @content_data end |
#dest ⇒ Object (readonly)
一个文件夹下面可能会有很多文件或文件夹的
5 6 7 |
# File 'lib/docwu/folder.rb', line 5 def dest @dest end |
#folders ⇒ Object (readonly)
一个文件夹下面可能会有很多文件或文件夹的
5 6 7 |
# File 'lib/docwu/folder.rb', line 5 def folders @folders end |
#index_dest ⇒ Object (readonly)
一个文件夹下面可能会有很多文件或文件夹的
5 6 7 |
# File 'lib/docwu/folder.rb', line 5 def index_dest @index_dest end |
#index_post ⇒ Object (readonly)
一个文件夹下面可能会有很多文件或文件夹的
5 6 7 |
# File 'lib/docwu/folder.rb', line 5 def index_post @index_post end |
#name ⇒ Object (readonly)
一个文件夹下面可能会有很多文件或文件夹的
5 6 7 |
# File 'lib/docwu/folder.rb', line 5 def name @name end |
#parent ⇒ Object (readonly)
一个文件夹下面可能会有很多文件或文件夹的
5 6 7 |
# File 'lib/docwu/folder.rb', line 5 def parent @parent end |
#parents ⇒ Object (readonly)
一个文件夹下面可能会有很多文件或文件夹的
5 6 7 |
# File 'lib/docwu/folder.rb', line 5 def parents @parents end |
#path ⇒ Object (readonly)
一个文件夹下面可能会有很多文件或文件夹的
5 6 7 |
# File 'lib/docwu/folder.rb', line 5 def path @path end |
#posts ⇒ Object (readonly)
一个文件夹下面可能会有很多文件或文件夹的
5 6 7 |
# File 'lib/docwu/folder.rb', line 5 def posts @posts end |
#src ⇒ Object (readonly)
一个文件夹下面可能会有很多文件或文件夹的
5 6 7 |
# File 'lib/docwu/folder.rb', line 5 def src @src end |
#url ⇒ Object (readonly)
一个文件夹下面可能会有很多文件或文件夹的
5 6 7 |
# File 'lib/docwu/folder.rb', line 5 def url @url end |
#worker ⇒ Object (readonly)
一个文件夹下面可能会有很多文件或文件夹的
5 6 7 |
# File 'lib/docwu/folder.rb', line 5 def worker @worker end |
Instance Method Details
#folder? ⇒ Boolean
115 116 117 |
# File 'lib/docwu/folder.rb', line 115 def folder? true end |
#generate ⇒ Object
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
有首页文件了
111 112 113 |
# File 'lib/docwu/folder.rb', line 111 def has_index? !!(self.index_post) end |
#index_page_data ⇒ Object
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_datas ⇒ Object
56 57 58 |
# File 'lib/docwu/folder.rb', line 56 def parent_datas self.parents.map(&:to_data) end |
#post? ⇒ Boolean
119 120 121 |
# File 'lib/docwu/folder.rb', line 119 def post? false end |
#to_data ⇒ Object
转为数据
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 |