Class: CloudCannonJekyll::OldDataReader

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudcannon-jekyll/readers/old-data-reader.rb

Overview

Reads data files and creates a collections-style hash representation Aims to replicate the data reading logic in Jekyll 2.5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ OldDataReader

Returns a new instance of OldDataReader.



12
13
14
15
16
# File 'lib/cloudcannon-jekyll/readers/old-data-reader.rb', line 12

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

Instance Attribute Details

#siteObject (readonly)

Returns the value of attribute site.



10
11
12
# File 'lib/cloudcannon-jekyll/readers/old-data-reader.rb', line 10

def site
  @site
end

Instance Method Details

#read(dir) ⇒ Object



18
19
20
21
22
# File 'lib/cloudcannon-jekyll/readers/old-data-reader.rb', line 18

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

#read_data_file(path) ⇒ Object



44
45
46
47
48
# File 'lib/cloudcannon-jekyll/readers/old-data-reader.rb', line 44

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

#read_data_to(dir, data) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cloudcannon-jekyll/readers/old-data-reader.rb', line 24

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

#sanitize_filename(name) ⇒ Object



50
51
52
53
54
# File 'lib/cloudcannon-jekyll/readers/old-data-reader.rb', line 50

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