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.
11 12 13 14 15 |
# File 'lib/cloudcannon-jekyll/readers/old-data-reader.rb', line 11 def initialize(site) @site = site @safe = site.safe @content = {} end |
Instance Attribute Details
#site ⇒ Object (readonly)
Returns the value of attribute site.
9 10 11 |
# File 'lib/cloudcannon-jekyll/readers/old-data-reader.rb', line 9 def site @site end |
Instance Method Details
#read(dir) ⇒ Object
17 18 19 20 21 |
# File 'lib/cloudcannon-jekyll/readers/old-data-reader.rb', line 17 def read(dir) base = Jekyll.sanitized_path(@site.source, dir) read_data_to(base, @content) @content end |
#read_data_file(path) ⇒ Object
43 44 45 46 47 |
# File 'lib/cloudcannon-jekyll/readers/old-data-reader.rb', line 43 def read_data_file(path) { "path" => path, } end |
#read_data_to(dir, data) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cloudcannon-jekyll/readers/old-data-reader.rb', line 23 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
49 50 51 52 53 |
# File 'lib/cloudcannon-jekyll/readers/old-data-reader.rb', line 49 def sanitize_filename(name) name.gsub!(%r![^\w\s_-]+!, "") name.gsub!(%r!(^|\b\s)\s+($|\s?\b)!, '\\1\\2') name.gsub(%r!\s+!, "_") end |