Class: Kitabu::SourceList

Inherits:
Object
  • Object
show all
Defined in:
lib/kitabu/source_list.rb

Constant Summary collapse

IGNORE_DIR =

List of directories that should be skipped.

%w[. .. .svn .git].freeze
IGNORE_FILES =

Files that should be skipped.

/^(CHANGELOG|TOC)\..*?$/
EXTENSIONS =

List of recognized extensions.

%w[md erb].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_dir) ⇒ SourceList

Returns a new instance of SourceList.



19
20
21
22
# File 'lib/kitabu/source_list.rb', line 19

def initialize(root_dir)
  @root_dir = root_dir
  @source = root_dir.join("text")
end

Instance Attribute Details

#root_dirObject (readonly)

Returns the value of attribute root_dir.



17
18
19
# File 'lib/kitabu/source_list.rb', line 17

def root_dir
  @root_dir
end

#sourceObject (readonly)

Returns the value of attribute source.



17
18
19
# File 'lib/kitabu/source_list.rb', line 17

def source
  @source
end

Instance Method Details

#chapter_files(entry) ⇒ Object



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

def chapter_files(entry)
  # Chapters can be files outside a directory.
  if File.file?(entry)
    [entry]
  else
    Dir["#{entry}/**/*.{#{EXTENSIONS.join(',')}}"]
  end
end

#each_chapter(&block) ⇒ Object



24
25
26
# File 'lib/kitabu/source_list.rb', line 24

def each_chapter(&block)
  files_grouped_by_chapter.each(&block)
end

#entriesObject

Return a list of all recognized files.



46
47
48
49
50
# File 'lib/kitabu/source_list.rb', line 46

def entries
  Dir.entries(source).sort.each_with_object([]) do |entry, buffer|
    buffer << source.join(entry) if valid_entry?(entry)
  end
end

#files_grouped_by_chapterObject



28
29
30
31
32
33
# File 'lib/kitabu/source_list.rb', line 28

def files_grouped_by_chapter
  entries.each_with_object([]) do |entry, buffer|
    files = chapter_files(entry)
    buffer << files unless files.empty?
  end
end

#valid_directory?(entry) ⇒ Boolean

Check if path is a valid directory.

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/kitabu/source_list.rb', line 61

def valid_directory?(entry)
  File.directory?(source.join(entry)) &&
    !IGNORE_DIR.include?(File.basename(entry))
end

#valid_entry?(entry) ⇒ Boolean

Check if path is a valid entry. Files/directories that start with a dot or underscore will be skipped.

Returns:

  • (Boolean)


55
56
57
# File 'lib/kitabu/source_list.rb', line 55

def valid_entry?(entry)
  entry !~ /^(\.|_)/ && (valid_directory?(entry) || valid_file?(entry))
end

#valid_file?(entry) ⇒ Boolean

Check if path is a valid file.

Returns:

  • (Boolean)


68
69
70
71
72
73
74
# File 'lib/kitabu/source_list.rb', line 68

def valid_file?(entry)
  ext = File.extname(entry).delete(".").downcase

  File.file?(source.join(entry)) &&
    EXTENSIONS.include?(ext) &&
    entry !~ IGNORE_FILES
end