Class: CloudCannonJekyll::OldDataReader
- Inherits:
-
Object
- Object
- CloudCannonJekyll::OldDataReader
- 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
-
#site ⇒ Object
readonly
Returns the value of attribute site.
Instance Method Summary collapse
-
#initialize(site) ⇒ OldDataReader
constructor
A new instance of OldDataReader.
- #read(dir) ⇒ Object
- #read_data_file(path) ⇒ Object
- #read_data_to(dir, data) ⇒ Object
- #sanitize_filename(name) ⇒ Object
Constructor Details
#initialize(site) ⇒ OldDataReader
Returns a new instance of OldDataReader.
13 14 15 16 17 |
# File 'lib/cloudcannon-jekyll/readers/old-data-reader.rb', line 13 def initialize(site) @site = site @safe = site.safe @content = {} end |
Instance Attribute Details
#site ⇒ Object (readonly)
Returns the value of attribute site.
11 12 13 |
# File 'lib/cloudcannon-jekyll/readers/old-data-reader.rb', line 11 def site @site end |
Instance Method Details
#read(dir) ⇒ Object
19 20 21 22 23 |
# File 'lib/cloudcannon-jekyll/readers/old-data-reader.rb', line 19 def read(dir) base = Jekyll.sanitized_path(@site.source, dir) read_data_to(base, @content) @content end |
#read_data_file(path) ⇒ Object
47 48 49 50 51 |
# File 'lib/cloudcannon-jekyll/readers/old-data-reader.rb', line 47 def read_data_file(path) { 'path' => path } end |
#read_data_to(dir, data) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/cloudcannon-jekyll/readers/old-data-reader.rb', line 25 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 do |fn| File.directory?(fn) end 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
53 54 55 56 57 |
# File 'lib/cloudcannon-jekyll/readers/old-data-reader.rb', line 53 def sanitize_filename(name) name.gsub!(/[^\w\s_-]+/, '') name.gsub!(/(^|\b\s)\s+($|\s?\b)/, '\\1\\2') name.gsub(/\s+/, '_') end |