Class: HTOTConv::Parser::DirTree

Inherits:
Base
  • Object
show all
Defined in:
lib/htot_conv/parser/dir_tree.rb

Instance Attribute Summary

Attributes inherited from Base

#option

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from HTOTConv::Parser::Base

Class Method Details

.option_helpObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/htot_conv/parser/dir_tree.rb', line 7

def self.option_help
  {
    :key_header => {
      :default => [],
      :pat => Array,
      :desc => "key header",
    },
    :glob_pattern => {
      :default => "**/*",
      :pat => String,
      :desc => "globbing pattern (default: \"**/*\")",
    },
    :dir_indicator => {
      :default => "",
      :pat => String,
      :desc => "append directory indicator",
    },
  }
end

Instance Method Details

#parse(input = Dir.pwd) ⇒ Object



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
# File 'lib/htot_conv/parser/dir_tree.rb', line 27

def parse(input=Dir.pwd)
  outline = HTOTConv::Outline.new
  outline.key_header = @option[:key_header]
  outline.value_header = []

  outline_item = Set.new
  Dir.chdir(input) do
    Dir.glob(@option[:glob_pattern]).each do |f|
      f.split(File::SEPARATOR).inject(nil) do |parent_path, v|
        file_path = (parent_path)? File.join(parent_path, v) : v
        outline_item << file_path
        file_path
      end
    end

    outline_item.sort.each do |file_path|
      key = File.basename(file_path)
      key << "#{option[:dir_indicator]}" if FileTest.directory?(file_path)
      level = file_path.split(File::SEPARATOR).length
      outline.add_item(key, level, [])
    end
  end
  
  outline
end