Class: PPZ::Folder::FolderModel

Inherits:
AbstractModel show all
Defined in:
lib/folder/model/folder.rb

Instance Attribute Summary collapse

Attributes inherited from AbstractModel

#father_model, #index, #level, #name, #next_model, #prev_model

Instance Method Summary collapse

Methods inherited from AbstractModel

from_path, #relative_link

Constructor Details

#initialize(path, level) ⇒ FolderModel

Returns a new instance of FolderModel.



7
8
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
# File 'lib/folder/model/folder.rb', line 7

def initialize path, level
  super
  /^((\d+)_)?(.+)/.match @basename
  @index = $2?($2.to_i):(Float::INFINITY)
  @name = $3

  @children = []
  (Dir.children @path, encoding: 'utf-8').each do |child_name|
    @children.push AbstractModel.from_path (@path + child_name), level
  end
  @children.sort! do |a, b|
    a.index <=> b.index
  end

  # 设置上级 和 左右
  left = right = nil
  @children.each do |child|
    child.father_model = self # 上级

    next unless child.is_a? PPZFileModel
    if left
      left.right = child
      child.left = left
    end
    left = child
  end
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
end

Instance Method Details

#_compile(out_dir) ⇒ Object

compile 是 _compile 的安全版本



35
36
37
38
39
40
41
42
# File 'lib/folder/model/folder.rb', line 35

def _compile out_dir # compile 是 _compile 的安全版本
  out_file_pathname = out_dir + (@name + '.html')
  PPZ::Func.write_to_file out_file_pathname, to_html

  children_dir = out_dir + @name
  Dir.mkdir children_dir
  @children.each { |child| child._compile children_dir }
end

#compile(out_dir) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/folder/model/folder.rb', line 44

def compile out_dir
  if out_dir.is_a? String
    out_dir = Pathname out_dir
  elsif !(out_dir.is_a? Pathname)
    throw '输出文件夹的名字必须是 String 或 Pathname'
  end

  set_prev_and_next_page

  unless Dir.exist? out_dir
    throw "out_dir #{out_dir} 不存在"
  end
  
  _compile out_dir
end

#get_content_table_html(root) ⇒ Object



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

def get_content_table_html root
  %~<ul>#{
    @children
    .select do |child|
      (child.class == FolderModel) || (child.file_ext == '.ppz')
    end
    .map do |child|
      result = "<li><a href=\"./#{root.relative_link child}\">#{child.name}</a></li>"
      if child.is_a? FolderModel
        result += child.get_content_table_html root
      end
      result
    end
    .join
  }</ul>~
end