Sha256: 335f4cd525eb361ad31418a6bfdca66d022fb10a6b70b28bf08d703cd2f016f4

Contents?: true

Size: 1.41 KB

Versions: 20

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

if Jekyll::VERSION.start_with? "2."
  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
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
cloudcannon-jekyll-2.3.4 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-2.3.3 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-2.3.2 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-2.3.1 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-2.3.0 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-2.2.0 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-2.1.0 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-2.0.2 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-2.0.1 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-0.5.0 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-2.0.0 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-1.6.1 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-0.4.0 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-1.6.0 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-1.5.7 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-1.5.6 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-1.5.5 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-0.3.4 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-1.5.4 lib/cloudcannon-jekyll/readers/old-data-reader.rb
cloudcannon-jekyll-0.3.3 lib/cloudcannon-jekyll/readers/old-data-reader.rb