Sha256: fb79d06ee83430f478678fd1a9d5a086040b5e9ba1fc910c8516f9fb83c7e30b

Contents?: true

Size: 1.75 KB

Versions: 5

Compression:

Stored size: 1.75 KB

Contents

module Kitabu
  class SourceList
    # List of directories that should be skipped.
    #
    IGNORE_DIR = %w[. .. .svn .git]

    # Files that should be skipped.
    #
    IGNORE_FILES = /^(CHANGELOG|TOC)\..*?$/

    # List of recognized extensions.
    #
    EXTENSIONS = %w[md erb]

    attr_reader :root_dir
    attr_reader :source

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

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

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

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

    # Return a list of all recognized files.
    #
    def entries
      Dir.entries(source).sort.each_with_object([]) do |entry, buffer|
        buffer << source.join(entry) if valid_entry?(entry)
      end
    end

    # Check if path is a valid entry.
    # Files/directories that start with a dot or underscore will be skipped.
    #
    def valid_entry?(entry)
      entry !~ /^(\.|_)/ && (valid_directory?(entry) || valid_file?(entry))
    end

    # Check if path is a valid directory.
    #
    def valid_directory?(entry)
      File.directory?(source.join(entry)) && !IGNORE_DIR.include?(File.basename(entry))
    end

    # Check if path is a valid file.
    #
    def valid_file?(entry)
      ext = File.extname(entry).gsub(/\./, "").downcase
      File.file?(source.join(entry)) && EXTENSIONS.include?(ext) && entry !~ IGNORE_FILES
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
kitabu-2.1.0 lib/kitabu/source_list.rb
kitabu-2.0.4 lib/kitabu/source_list.rb
kitabu-2.0.3 lib/kitabu/source_list.rb
kitabu-2.0.2 lib/kitabu/source_list.rb
kitabu-2.0.1 lib/kitabu/source_list.rb