Class: Verku::SourceList

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

Constant Summary collapse

IGNORE_DIR =

List of directories that should be skipped.

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

Files that should be skipped.

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

List of recognized extensions.

%w[markdown mkdown mkdn mkd md text]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_dir) ⇒ SourceList

Returns a new instance of SourceList.



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

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

Instance Attribute Details

#root_dirObject (readonly)

Returns the value of attribute root_dir.



15
16
17
# File 'lib/verku/source_list.rb', line 15

def root_dir
  @root_dir
end

#sourceObject (readonly)

Returns the value of attribute source.



16
17
18
# File 'lib/verku/source_list.rb', line 16

def source
  @source
end

Instance Method Details

#chapter_files(entry) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/verku/source_list.rb', line 37

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

#each_chapter(&block) ⇒ Object



26
27
28
# File 'lib/verku/source_list.rb', line 26

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

#entriesObject

Return a list of all recognized files.



48
49
50
51
52
# File 'lib/verku/source_list.rb', line 48

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



30
31
32
33
34
35
# File 'lib/verku/source_list.rb', line 30

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)


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

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)


56
57
58
# File 'lib/verku/source_list.rb', line 56

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
# File 'lib/verku/source_list.rb', line 68

def valid_file?(entry)
  ext = File.extname(entry).gsub(/\./, "").downcase
  File.file?(source.join(entry)) && EXTENSIONS.include?(ext) && entry !~ IGNORE_FILES
end