Sha256: c012d43ffa947397430e6b7120b7b2e3297f56cfd62a94625363d049a7847080

Contents?: true

Size: 1.28 KB

Versions: 6

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

require "jekyll"

module CloudCannonJekyll
  # Reads data files and creates a collections-style hash representation
  # Aims to replicate the data reading logic in Jekyll 2.5
  class OldDataReader
    attr_reader :site

    def initialize(site)
      @site = site
      @safe = site.safe
      @content = {}
    end

    def read(dir)
      base = Jekyll.sanitized_path(@site.source, dir)
      read_data_to(base, @content)
      @content
    end

    def read_data_to(dir, data)
      return unless File.directory?(dir) && (!@safe || !File.symlink?(dir))

      entries = Dir.chdir(dir) do
        Dir["*.{yaml,yml,json,csv}"] + Dir["*"].select { |fn| File.directory?(fn) }
      end

      entries.each do |entry|
        path = Jekyll.sanitized_path(dir, entry)
        next if File.symlink?(path) && @safe

        key = sanitize_filename(File.basename(entry, ".*"))
        if File.directory?(path)
          read_data_to(path, data[key] = {})
        else
          data[key] = read_data_file(path)
        end
      end
    end

    def read_data_file(path)
      {
        "path" => path,
      }
    end

    def sanitize_filename(name)
      name.gsub!(%r![^\w\s_-]+!, "")
      name.gsub!(%r!(^|\b\s)\s+($|\s?\b)!, '\\1\\2')
      name.gsub(%r!\s+!, "_")
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cloudcannon-jekyll-0.3.2 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-1.5.3 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-1.5.2 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-0.3.1 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-0.3.0 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-1.5.0 lib/cloudcannon-jekyll/readers/old-data-reader.rb