class PPZ::Folder::FolderModel

Attributes

children[R]

Public Class Methods

new(path, level) click to toggle source
Calls superclass method PPZ::Folder::AbstractModel::new
# 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

Public Instance Methods

_compile(out_dir) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

get_content_html() click to toggle source
# File lib/folder/model/folder.rb, line 100
def get_content_html
  "<article>#{get_content_table_html self}</article>"
end
linearize_children(child, list) click to toggle source
# File lib/folder/model/folder.rb, line 89
def linearize_children child, list
  if child.is_a? PPZFileModel
    list.push child
  elsif child.is_a? FolderModel
    list.push child
    child.children.each do |cc|
      linearize_children cc, list
    end
  end
end
set_prev_and_next_page() click to toggle source

设置页面的“上一篇、下一篇”

# File lib/folder/model/folder.rb, line 79
def set_prev_and_next_page
  list = []
  linearize_children self, list
  list.inject do |pre, nex|
    pre.next_model = nex
    nex.prev_model = pre
    nex
  end
end